Dynamic fields inside JSON object for C3 - spray

I am trying to create JSON output in this format for C3 charts =>
{
"data1": [220, 240, 270, 250, 280],
"data2": [180, 150, 300, 70, 120],
"data3": [200, 310, 150, 100, 180]
}
I can't use normal Scala classes and jsonFormat2 as the names of the fields are dynamic. I have tried creating a custom RootJsonFormat parser on a simple case class like this =>
case class NamedList[Int](name: String, items: Seq[Int])
But it did not work since the output required by D3 is a JSON object with values inside it, not a JsArray of name, item pairs.
What is the best way of doing this?

I have been told that this is, of course, a:
Map[String, Seq[Int]]
Doh!

Related

Categorical aggregation in Histogrammar

This is a followup question of SO Two-dimensional aggregation in Histogrammar (Jim Pivarski created this entry from a private email question):
From:
data = [{"item": 'ball', "qty": 3.0},
{"item": 'whistle', "qty": 2.0},
{"item": 'ball', "qty": 5.0}]
I want to obtain a sum aggregation using Histogrammar, i.e.:
ball: 8.0
whistle: 20
Following http://histogrammar.org/docs/tutorials/python-numpy/#histogrammar-in-numpy, and Jim's advice in mentioned SO , I try:
import histogrammar as hg
data = [{"item": 'ball', "qty": 3.0}, {"item": 'whistle', "qty": 2.0}, {"item": 'ball', "qty": 5.0}]
h = hg.Categorize(quantity=lambda d: d.item, value=hg.Sum(lambda d: d.qty))
for datum in data:
h.fill(datum)
print h.toJson()
I get:
AttributeError: 'dict' object has no attribute 'item'
This is just a Python issue: since each datum in your example has the form
{"item": X, "qty": Y}
the way to access it is with d["item"] and d["qty"], rather than d.item and d.qty.
So
h = hg.Categorize(quantity=lambda d: d["item"], value=hg.Sum(lambda d: d["qty"]))
for datum in data:
h.fill(datum)
print h.toJsonString()
results in
{"data": {"bins:type": "Sum", "bins": {"whistle": {"sum": 2.0, "entries": 1.0},
"ball": {"sum": 8.0, "entries": 2.0}}, "entries": 3.0}, "version": "1.0",
"type": "Categorize"}
If you change the way your data are represented, you'd have to change the way they're extracted from each datum.
Incidentally, Histogrammar-Python has a string-based shortcut that extracts fields as attributes (as you were trying to do) or as items (as I did above). The following would work with either kind of data:
h = hg.Categorize("item", hg.Sum("qty"))
This string-based method will also work if data is a dictionary of 1D Numpy arrays (or equivalently, a Numpy Record Array; I don't remember if there's a Pandas hook in there, too). In that case, you'd declare the histogram exactly as above but fill it like this:
h.fill.numpy(data)
It's the different fill method that interprets the strings differently.

Can I do this when I define an enum?

Can I do this when I define an enum? I mean have a specific entry at the end with a value that is not the expected one?
typedef NS_ENUM(NSInteger, countries) {
kAustria = 101,
kBelgium,
kEngland,
kFrance,
kNONE = 200,
};
This enum will assign 101, 102, 103 and 104 to the four countries respectively and 200 to kNONE, that is what I want, right?
Will this work correctly?
and what about this? (just checking to see the possibilities)
typedef NS_ENUM(NSInteger, countries) {
kAustria = 101,
kBelgium,
kNONE = 200,
kEngland = 103,
kFrance,
};
That's not a problem at all -
you can define specific values for individual constants in the enum definition.
Mixing specific and auto-generated values (i.e. the continuation the compiler will generate for you if you don't explicitly specify a value) will work just fine as well.
Here's a nice blog article explaining enums:
Objective C Enum: How to Declare and Use Enumerated Types in Objective C

MagicalRecord keypath

I am using magicalrecord and have my properties mapped to the json details.slots.
My JSON looks like this
"details": {
"startTimestamp": "2014-01-13",
"endTimestamp": "2014-01-16",
"employeeId" : 176,
"slots": [
{
"numberOfAppointments": 0,
"numberOfSpots": 1,
"isReserved": 0,
"startTimestamp": "2014-01-13 08:00:00",
"endTimestamp": "2014-01-13 08:05:00"
},
{
"numberOfAppointments": 0,
"numberOfSpots": 1,
"isReserved": 0,
"startTimestamp": "2014-01-13 08:05:00",
"endTimestamp": "2014-01-13 08:10:00"
},
{
"numberOfAppointments": 0,
"numberOfSpots": 1,
"isReserved": 0,
"startTimestamp": "2014-01-13 08:10:00",
"endTimestamp": "2014-01-13 08:15:00"
},
....
I was wondering is there an easy way to map a field to a field up a level? IE I can use my.object.property to drill down but is there anyway if my import is importing the slots array to easily add the employeeId to each object? Slots can be as little as 600 records to up to a couple thousand.
The only solution I can think of is looping through every slot and manually adding it before I run the magical record import. Is anyone familiar with another, easier way to get this done?
One way you could achieve this with the current framework is to implement the method
- (BOOL) importDetails:(id)data;
In your entity. MagicalRecord importing will look for this method and call it based on your core data property name if it's implemented. This would mean that you're basically rewriting the import code for the entire array, but you'd have access to the data you're after. Not ideal, but still possible.

Lua pass function argument as a table key

I'm working on exporting the contents of a Lua table to a HTML File so I can display the contents in the browser. Right now I'm having problems with passing a function argument as a table key.
I have a sparse table as such:
map = {}
for x = 1, 20 do
map[x] = {}
for y = 1, 20 do
map[x][y] = {}
map[x][y].node = math.random(1,20)
map[x][y].image = "path/to/image.png"
end
end
I pass the table to my function as such:
htmParser:_dumpSparseToHTML(map, 20, 20) where map = table I want to pass, 20,20 = width and height of the array. Somewhere in _dumpSparseToHTML I write the values of v.node and v.image to the file. How do I handle the exact same thing without knowing the name of the keys in the table? For example map could contain map[x][y].value, map[x][y].gfx, map[x][y].nodeType, and I would like to pass them as htmParser:_dumpSparseToHTML(map, 20, 20, value, gfx, nodeType, etc).
I know that Lua can handle a variable number of arguments, by defining the function as: _dumpSparseToHTML(map, 20, 20, ...). I tried to do the following:
--_table = map
for i,v in ipairs(arg) do
file:write("<td>".._table[x][y].v.."</td>)
end
The error I get is: "attempt to concatenate field 'v' (a nil value).
So, my question is: how do I pass a variable number of arguments as table keys?
You need to use _table[x][y][v] for that. _table[x][y].v is _table[x][y]["v"].

Easiest way to merge these arrays vertically

I have these arras
names = ["Will","Bob","John","Ben"]
ages = [45,49,32,49]
postcodes = [9320,3991,1234,2993]
Whats the most efficient way to stack them so they appear as
people = [["Will",45,9320],["Bob",49,3991],["John",32,1234],["Ben",49,2993]]
Does ruby have a function to merge these vertically? (by index) nice and simple without all those nasty loops?
people = names.zip(ages, postcodes)
=> [["Will", 45, 9320], ["Bob", 49, 3991], ["John", 32, 1234], ["Ben", 49, 2993]]
You can use Array#zip:
people = names.zip ages, postcodes

Resources