Related
I have a dataframe with many rows and columns of the form (this is an oversimplified dataframe):
id dur proto service state attack_cat label
0 1 0.121478 tcp dns FIN Normal 0
1 2 4.287901 udp ftp INT Exploits 1
I would like to write all the rows of this dataframe as json items, like for example, for the first row:
{"type": "event",
"subtype": "",
"datatype": "Instance",
"domain": "Cyber",
"created": str(datetime.datetime.now()),
"details": {id: 1,
dur: 0.121478,
proto: tcp,
service: dns,
state: FIN,
attack_cat: Normal,
label:0}
}
I tried to do something like:
{"type": "event",
"subtype": "",
"datatype": "Instance",
"domain": "Cyber",
"created": str(datetime.datetime.now()),
"details": dataframe.loc[i].to_dict()
}
and do a for loop through all the rows, but it gives me the error
TypeError: unhashable type: 'dict'
There are actually two problems, as far as I can see.
Generally, a python dictionary can be the value of a dictionary. For example,
dict = {
"first_name": "robert",
"last_name": "ren",
"hist": {"today": 1, "yesterday": 2}
}
The value of hist is a dict. This means, that from this perspective your code seems to be ok. However, a dictionary can not be the key, nor can you nest one dict in another one. For example,
{{}}
gives you the following error
TypeError: unhashable type: 'dict'
If you want to solve this problem, I recommend that you print out each line as you create the dicts to see where it breaks. That said, I guess that "details": dataframe.loc[i].to_dict() causes you the trouble.
Second, your first dict is invalid as the keys are not strings. This should be
{
"type": "event",
"subtype": "",
"datatype": "Instance",
"domain": "Cyber",
"created": str(datetime.datetime.now()),
"details": {
"id": 1,
"dur": 0.121478,
"proto": tcp,
"service": dns,
"state": FIN,
"attack_cat": Normal,
"label": 0
}
}
Assuming that the variables are defined variables.
EDIT
The following code works on my machine.
import pandas as pd
df = pd.DataFrame({
"id": [1],
"dur": [0.12],
"proto": ["tcp"],
"service": ["dns"],
"state": ["FIN"],
"attack_cat": ["Normal"],
"label": [0]
})
row_list_as_dict = []
for idx, row in df.iterrows():
row_list_as_dict.append({
"type": "event",
"subtype": "",
"datatype": "Instance",
"domain": "Cyber",
"details": {
'id': row["id"],
'dur': row["dur"],
'proto': row["proto"],
'service': row["service"],
'state': row["state"],
'attack_cat': row["attack_cat"],
'label': row["label"]
}
})
row_list_as_dict
I am new to Vega and I would like to load this topojson with Vega :
https://github.com/deldersveld/topojson/blob/master/countries/france/fr-departments.json
I have already followed the airport tutorial but I dont know what to write in the "projections" and "format" field to insert my topojson.
Thanks,
This is a late response but posting as it could be useful for others.
Here are the key attributes/changes for the map to work:
"feature": "FRA_adm2" in data[0].format. I picked this from the topojson's objects key
"field": "properties.ID_1". Important for scale, legend, map fill. I picked this from the topojson's objects.FRA_adm2.geometries[0].properties, assuming that's the unique sub-region key.
define a projection and use it the mark's transform attribute
...
"data": [{
"name": "table",
"url": "https://raw.githubusercontent.com/deldersveld/topojson/master/countries/france/fr-departments.json",
"format": {"type": "topojson", "feature": "FRA_adm2"}
}],
...
"projections": [{
"name": "projection",
"size": {"signal": "[width, height]"},
"fit": {"signal": "data('table')"},
"type": "mercator"
}],
...
"marks": [{
...
"transform": [{"type": "geoshape", "projection": "projection"}]
}]
...
specification
Vega specification: link
How could I remove blocks section from shopify schema code? I simply tried to delete it but was not able to save due to a strange error. Please see the code below:
{% schema %}
{
"name": "Featured Collection",
"settings": [
{
"type": "collection",
"id": "featured_collection",
"label": "Collection"
},
{
"type": "text",
"id": "collection_button_label",
"label": "Button Label",
"default": "Learn More"
}
],
"blocks": [
{
"type": "section",
"name": "Section",
"settings": [
]
}
],
"presets": [
{
"name": "Featured Collection",
"category": "Product"
}
]
}
{% endschema %}
I can save the above code without any error. But When I remove the code section of "blocks", I get the following error:
Error: New schema is incompatible with the current setting value. Invalid type value for block '1577470637989'. Type must be defined in schema.New schema is incompatible with the current setting value. Invalid type value for block '1577470668608'. Type must be defined in schema
The error is kind of self explanatory. The error message
Error: New schema is incompatible with the current setting value.
Invalid type value for block '1577470637989'. Type must be defined in
schema.New schema is incompatible with the current setting value.
Invalid type value for block '1577470668608'. Type must be defined in
schema
It states that new schema that you are trying to save is not compatible with existing data you have. Shopify does not know what to do with existing blocks of those types which you want to remove.
So just remove those blocks from Shopify Customizer first and then edit the schema.
When I try to change https://vega.github.io/editor/#/examples/vega/scatter-plot like,
- "url": "data/cars.json"
+ "url": "http://dummy.restapiexample.com/api/v1/employees",
I get an error,
loader.js:166 Mixed Content: The page at 'https://vega.github.io/editor/#/edited' was loaded over HTTPS, but requested an insecure resource 'http://dummy.restapiexample.com/api/v1/employees'. This request has been blocked; the content must be served over HTTPS.
How do I fetch data from an external API?
Pretty simple. I just needed to use the same protocol HTTPS --> HTTPS
{
"$schema": "https://vega.github.io/schema/vega-lite/v3.json",
"description": "A scatterplot showing horsepower and miles per gallons for various cars.",
"data": {"url": "https://jsonplaceholder.typicode.com/todos"},
"mark": "point",
"encoding": {
"x": {"field": "userId","type": "quantitative"},
"y": {"field": "id","type": "quantitative"}
}
}
My application
Im trying to build a fairly simple application using Laravel as a RESTfull API server and Ember as my fontend framework
My backend server lives on http://api.example.com/1.0/
My frontend lives on http://www.example.com/
Ive just started this project so I'm only a few hours into the devlopment, so there might be some configuration issues that Im missing here, but anyway.
Im trying to get a list of products from my server and display them in my ember application using ember-data
Im running ember 2.0.2 and ember-data 2.0.0
Im getting the following error in chrome.
Error
Error while processing route: product Cannot read property 'replace'
of undefined TypeError: Cannot read property 'replace' of undefined
at Object.func (http://localhost:4200/assets/vendor.js:45832:15)
at Object.Cache.get (http://localhost:4200/assets/vendor.js:23421:36)
at decamelize (http://localhost:4200/assets/vendor.js:45874:29)
at Object.func (http://localhost:4200/assets/vendor.js:45789:12)
at Object.Cache.get (http://localhost:4200/assets/vendor.js:23421:36)
at Object.dasherize (http://localhost:4200/assets/vendor.js:45878:35)
at ember$data$lib$system$normalize$model$name$$normalizeModelName (http://localhost:4200/assets/vendor.js:66295:27)
at ember$data$lib$serializers$json$serializer$$default.extend.modelNameFromPayloadKey
(http://localhost:4200/assets/vendor.js:75184:67)
at ember$data$lib$serializers$json$serializer$$default.extend._normalizeResourceHelper
(http://localhost:4200/assets/vendor.js:75064:30)
at Array.map (native)
Files
In ember I have generated a product resource giving my the following files.
// app/routes/product.js
import Ember from 'ember';
export default Ember.Route.extend({
model() {
return this.store.findAll('product');
}
});
// app/model/product.js
import DS from 'ember-data';
export default DS.Model.extend({
name: DS.attr(),
price: DS.attr()
});
JSON response
The Json returned from my api http://api.example.com/1.0/products
{
"data": [
{
"id": "1",
"name": "dolores",
"price": "59015",
"created_at": "2015-09-06 16:18:13",
"updated_at": "2015-09-06 16:18:13"
},
{
"id": "2",
"name": "debitis",
"price": "57449",
"created_at": "2015-04-07 14:45:16",
"updated_at": "2015-04-07 14:45:16"
},
...
]
}
This is adapter/serializer error, it's not descriptive though. Payload is wrong for the JSONAPIAdapter (the default adapter)
You should modify payload as:
{
"data": [
{
"id": "1",
"type": "products",
"attributes": {
"name": "dolores",
"price": "59015",
"created-at": "2015-09-06 16:18:13",
"updated-at": "2015-09-06 16:18:13"
}
}, {
"id": "2",
"type": "products",
"attributes": {
"name": "debitis",
"price": "57449",
"created-at": "2015-04-07 14:45:16",
"updated-at": "2015-04-07 14:45:16"
}
}]
}
or use RESTAdapter/Serializer with a such payload:
{
"products": [{
"id": "1",
"name": "dolores",
"price": "59015",
"created_at": "2015-09-06 16:18:13",
"updated_at": "2015-09-06 16:18:13"
}, {
"id": "2",
"name": "debitis",
"price": "57449",
"created_at": "2015-04-07 14:45:16",
"updated_at": "2015-04-07 14:45:16"
}]
}
If you can't change response payload, you have to customize Adapter/Serializer pair. Check related questions on SO.
Links for details:
Guides
JSONAPISerializer payload example
RESTSerializer payload example
The same issue happened to me.
Version details:
DEBUG: -------------------------------
ember.debug.js:DEBUG: Ember : 1.11.0
ember.debug.js:DEBUG: Ember Data : 1.0.0-beta.14.1
ember.debug.js:DEBUG: jQuery : 1.11.1
DEBUG: -------------------------------
Cause of the issue:
It seems that adding attributes to Handlebars elements using inline if helpers also causes the issue (whether the property that you are using on your condition is true, false, null or undefined).
my-component.hbs
<button class="btn btn-solve-my-problems" {{action 'solveIt}} {{if isNotSolvable 'disabled'}}>
Solve my problems!
</button>
my-component.coffee
isNotSolveble: (->
if #get('somethingMeaningful') then true else null
).property('somethingMeaningful')
The solution:
Instead of using the inline if helper, I am just attributing the value of isNotSolvable to the disabled attribute (nothing was changed on the my-component.coffee file).
my-component.hbs
<button class="btn btn-solve-my-problems" {{action 'solveIt}} disabled="{{isNotSolvable}}">
Solve my problems!
</button>
PS.: the null being returned from the method has to do with the manipulation of the disabled attribute. It has nothing to do with the issue reported on this thread. It only reflects my scenario and how I've solved the issue. More details here.