String' is not convertible to 'StringLiteralConvertible' in Xcode 7 Beta 5 - xcode

I'm trying to initialize a dictionary constant with
["updateType" : "moveRow", "data" : rows].
rows is an array. I have is as:
let update = ["updateType" : "moveRow", "data" : rows]
I have also tried:
let update: Dictionary< String, AnyObject> = ["updateType" : MoveRow", "data" : rows]
and
let update: [String : AnyObject] = ["updateType" : "moveRow", "data" : rows]
in each case, I get and error on the key "data" that says:
String' is not convertible to 'StringLiteralConvertible'.
Can anybody explain what's going on?

The problem seems to be in your creation of rows as optional:
var rows: Array<Dictionary<String, AnyObject>>?
If you can get rid of the optional, it should start working. E.g.
let d = ["updateType" : "moveRow", "data" : rows!]
...or by creating the rows like this:
var rows = Array<Dictionary<String, AnyObject>>()

Related

Elasticsearch-py Bulk Percolate Functionality

Trying to get the bulk percolate functionality to work for Elasticsearch-py (i.e. mpercolate), but haven't been able to find an example online. I'm able to use the percolate function, so I can get this to work:
doc = {'doc' : {'field1' : 'this is a value', 'field2' : 'this is another value'}}
res = es.percolate(index = 'my_index', doc_type = 'my_doc_type', body = doc)
The documentation I've read so far seems to imply that if I want to do a bulk submission, I need to send header and body as strings, separated by a newline. Thus I've tried:
head = {'percolate' : {'index' : 'my_index', 'type' : 'my_doc_type'}}
doc = {'doc' : {'field1' : 'this is a value', 'field2' : 'this is another value'}}
doc2 = {'doc' : {'field1' : 'values and values', 'field2' : 'billions of values'}}
query_list = [head, doc, head, doc2]
my_body = '\n'.join([str(qry) for qry in query_list])
res = es.mpercolate(body = my_body)
which gives me a generic "elasticsearch.exceptions.TransportError". Anyone have a working example I can adapt?
You don't have to serialize the data yourself, just pass in the query_list as body and it should do the right thing

Rethinkdb execute multiple avg in one query

I have a review table with multiple number columns. I would like to count he avg of all columns in one query.
So if the table looks like:
{
foo : 2,
bar : 5,
foobar : 10
},
{
foo : 4,
bar : 3,
foobar : 12
}
then i would like to get the avg for each column in one query. I know I can do:
r.table('stats' ).avg( 'foo' )
on each column but I would like to do this in just one query and map into into just one object.
Any ideas on how to do this?
You can use map with reduce (if every record in table has all 3 fields):
r.table("stats").map(function(row){
return {foo : row("foo"), bar : row("bar") , foobar : row("foobar"), count : 1};
}).reduce(function(left, right){
return {foo : left("foo").add(right("foo")), bar : left("bar").add(right("bar")), foobar : left("foobar").add(right("foobar")), count : left("count").add(right("count"))};
}).do(function (res) {
return {
foo: res('foo').div(res("count")),
bar: res('bar').div(res("count")),
foobar: res('foobar').div(res("count"))
};
})
If record can have not all fields, you can separate count in map operation for each field, and then in do use it depending on field.

Lua : add a table in a table

I recently started learning Lua and I have a question about tables :
local mytable = {10,11,12}
I would add another table in mytable[3] , if I do this :
table.insert(mytable[3], {[15] = {}})
Will it works ?
And if it works, what will be the "final" result ?
mytable[3][15] = {}
or
mytable[3][1][15] = {}
?
It's probably a basic question but it will help me :p
The first argument of table.insert should be the table being inserted, so the correct syntax is:
table.insert(mytable, {[15] = {}})
After this, the value of mytable[4] (note that Lua table index starts from 1) is the table {[15] = {}}, and the value of mytable[4][15] is therefore an empty table.
To make mytable[3] a table {[15] = {}}, use assignment instead:
mytable[3] = {[15] = {}}

LINQ to Entities does not recognize the method 'Int32 Min(Int32, Int32)'?

Im getting this error when I execute the following code, any Ideas how to fix it?
LINQ to Entities does not recognize the method 'Int32 Min(Int32, Int32)' method, and this method cannot be translated into a store expression.
result = items.ToList()
.Select(b => new BatchToWorkOnModel()
{
BatchID = b.Batch.ID,
SummaryNotes = b.Batch.Notes,
RowVersion = b.Batch.RowVersion,
Items = items
.Select(i => new ItemToWorkOnModel()
{
SupplierTitle = i.Title,
ItemID = i.ID,
BatchID = i.BatchID ?? 0,
ItemDate = i.PubDate,
// KB - Issue 276 - Return the correct Outlet name for each item
Outlet = i.Items_SupplierFields != null ? i.Items_SupplierFields.SupplierMediaChannel != null ? i.Items_SupplierFields.SupplierMediaChannel.Name : null : null,
Status = ((short)ItemStatus.Complete == i.StatusID ? "Done" : "Not done"),
NumberInBatch = i.NumInBatch,
Text = string.IsNullOrEmpty(i.Body) ? "" : i.Body.Substring(0, Math.Min(i.Body.Length, 50)) + (i.Body.Length < 50 ? "" : "..."),
IsRelevant = i.IsRelevant == 1,
PreviouslyCompleted = i.PreviouslyCompleted > 0 ? true : false
}).ToList()
})
.FirstOrDefault();
It seems Math.Min is not implemented by the EF query provider. You should be able to fix it by simply applying AsEnumerable on your items collection to do the expression using Linq to Objects instead;
Items = items.AsEnumerable().Select(i => new ItemToWorkOnModel()...
If you add a where condition to the item selection (seems a little strange to take all items in the whole table), you'll want to add it before AsEnumerable() to allow EF to do the filtering in the database.
Also, you only want the first result from the query, but you're fetching all of them using ToList() before cutting the list down to a single item. You may want to remove the ToList() so that EF/the underlying database can return only a single result;
result = items.Select(b => new BatchToWorkOnModel()...
You do not need Math.Min.
The line in question is:
Text = string.IsNullOrEmpty(i.Body)
? "" : i.Body.Substring(0, Math.Min(i.Body.Length, 50)) + (i.Body.Length < 50 ? "" : "...")
So what does this line return?
If i.Body is null or empty it returns an empty string. If it is 50 or more characters long it returns a substring of 50 characters and appends "...".
If the length is less than 50 it takes a substring with the length of the string and appends an empty string. But that's just the original string.
Text = string.IsNullOrEmpty(i.Body)
? "" : (i.Body.Length < 50 ? i.Body : i.Body.Substring(0, 50) + "...")

Combine 2 JSON objects in JQUERY

Could you please help me on how to combine the below Result1 and Result2 JSON objects into single JSON such that i have Name,No,Avg,Subject1,Subject2 into single JSON object. I am using it in JQUERY AJAX.
{"Result1":"[{"NAME" : "Mark","No" : "23544","Avg" : "49"}]"}
{"Result2":"[{"Subject1" : "Maths","Subject2" : "Computers"}]"}
Please help.
Thanks
See jQuery.extend()
var x = {"Result1":"[{"NAME" : "Mark","No" : "23544","Avg" : "49"}]"}
var y = {"Result2":"[{"Subject1" : "Maths","Subject2" : "Computers"}]"}
var z = jQuery.extend({}, x.Result1[0], y.Result2[0]);
// z.NAME, z.No, z.Avg, z.subject1...
I'm not sure whether you've parsed the JSON string into a JavaScript object yet; but see jQuery.parseJSON() to see how you do this (be aware; parseJSON() will throw an error if you pass it invalid JSON).

Resources