How do I get unique values of a column in AWS Dynamo? - amazon-web-services

Say, in AWS Dynamo, I have a table like this:
ID (HKey) Date (RKey) BoxName
0 1/1/2014 Box-1
1 2/1/2014 Box-1
2 3/1/2014 Box-2
3 4/1/2014 Box-3
4 5/1/2014 Box-3
5 5/1/2014 Box-1
I want to, in a single query, get the first row for each unique Box. There could be hundreds of boxes I need the first entry for at once, making individual requests inefficient.
I can't seem to find anything in the API that would allow me to do this. Is it possible? How would I do this?

You might want to consider creating a Global secondary index (GSI) on Boxname (hash key) and date as your range key. This will enable you to use Query API on the secondary index where you can query "Find all IDs with Boxname = $box".
See the documentation for GSI.
Hope this helps,
Swami

There's no way to query just for the first appearance of each box without creating an index for the boxes as suggested above. However, if you don't mind reading the whole table and then picking the right lines, then read the whole table into an array, and then make it unique by some simple piece of code. For example, suppose you've read the table into an array (note that you might have to make several calls to scan or query until you get them all), and the array is something like this:
l = [
{"ID": "0", "Date": "1/1/2014", "BoxName": "Box-1"},
{"ID": "1", "Date": "2/1/2014", "BoxName": "Box-1"},
{"ID": "2", "Date": "3/1/2014", "BoxName": "Box-2"},
{"ID": "3", "Date": "4/1/2014", "BoxName": "Box-3"},
{"ID": "4", "Date": "5/1/2014", "BoxName": "Box-3"},
{"ID": "5", "Date": "5/1/2014", "BoxName": "Box-1"}
]
Then, a simple code like this in python will give you the list in the variable "out":
out = []
seen = []
for line in l:
if line["BoxName"] not in seen:
seen.append(line["BoxName"])
out.append(line)

Related

Converting nested list into dictionary

Learning a new thing here - I've been trying to tackle a problem all day and haven't had much success. The idea is to loop through a nested list and return a dictionary. However, the first element of the list contains the column headers for the dictionary values. So here is the nested list, or table_data:
table_data = [
["first_name", "last_name", "city", "state"],
["Elisabeth", "Gardenar", "Toledo", "OH"],
["Jamaal", "Du", "Sylvania", "OH"],
["Kathlyn", "Lavoie", "Maumee", "OH"]
]
convert_table(table_data)
I want to convert the nested list into a dictionary as seen below. Basically, I'd like a function to take in the nested list then spit out the output as shown below.
[
{"first_name": "Elisabeth", "last_name": "Gardenar", "city": "Toledo", "state": "OH"},
{"first_name": "Jamaal", "last_name": "Du", "city": "Sylvania", "state": "OH"},
{"first_name": "Kathlyn", "last_name": "Lavoie", "city": "Maumee", "state": "OH"}
]
Here is some of the code I've been fiddling with so far, but am kind of stuck on how to get the elements of the first index of the list to repeat and become key's for the values in the rest of the dictionary.
Thank you!
for i in range(len(table_data)):
for j in table_data[0]:
print(j)
for i in table_data:
for j in i:
print(j)
Being aware that Python dictionaries are inherently unordered you can do it this way:
lst = []
for row in table_data[1:]:
lst.append(dict(zip(table_data[0],row)))
If you need to preserve order use an OrderedDict like so:
import collections as co
lst_ordered = []
for row in table_data[1:]:
lst_ordered.append(co.OrderedDict(zip(table_data[0],row)))
Bernie got this right. Now I'm going to study it. Thank you.
lst = []
for row in table_data[1:]:
lst.append(dict(zip(table_data[0],row)))

Print key of list w/o knowing its position

I have a list that contains many keys:
mylist = {"a", "b", "c", "1", "2", "3", ...}
and I want to print the key for example that has value "x", without knowing it's exact position in the list. That mean I have to run the whole list and till "x" is found and print it. How could I do this? Seems easy question but it confuses me a bit... Thanks a lot
for key, value in pairs(mylist) do
if value == "x" then print(key) end
You can also create another mapping, eg.
mapping_list = {}
for key, value im pairs(mylist) do
mapping_list[value] = key
(assuming that list elements are unique) then, you'd be able to
print(mapping_list["x"])

How to use OR operator in Freebase?

Case:
So, I'm using the OR operator or ONE OF as to get people from any of 2 countries.
The query looks like:
[{
"id": null,
"type": "/people/person",
"/people/person/nationality": {
"name|=": [
"Jordan",
"Ottoman Empire"
]
},
"name": null,
"limit": 30
}]
The query works fine, but it won't work if you increase the limit to be 40 for example. The error returned is "Unique query may have at most one result. Got 2". This means that there exist a person for both nationalities "Jordan" and "Ottoman Empire".
Question:
It makes sense for a "ONE OF" operator, but not for "OR" operator. Is there any operator in Freebase that can query "ANY OF" or true "OR" to cover these cases?
You're getting the error because you used object notation ({}) which expects a single result in a place where you're returning two results and would those need an array ([]).
Having said that, I think what you really need to do is hoist your |= operator up a level to /people/person/nationality. Note also that you need array notation even if just asking for nationality results for a person, because it's multi-valued (e.g. Sirhan Sirhan has both Jordan and Mandatory Palestine as his nationality).
Here's a query that will do what you want (although you should really use IDs for the countries rather than their English labels):
[{
"id": null,
"name": null,
"nationality": [],
"type": "/people/person",
"nationality|=": [
"Jordan",
"Ottoman Empire"
]
}]

How to display a list in django tables 2

I have a list containing some objects that i want to display in django tables 2 but as a result i got the - in all the columns.
My list is like this format [[<Person>],[<Person>]]
Reading the documentation I've found that this format works :
data = [
{"name": "Bradley"},
{"name": "Stevie"},
]
How can I get a format like this knowing that my data is dynamic?
Update :
I tried this :
for x in person_list:
for y in x:
data=[
{"firstname": y.firstname},
{"surname":y.surname},
]
The problem is now it displays every field in a row, I mean first name in row and surname in another one. How to append to the same data ?
Try to produce your data table manually
I suppose person has first_name field
data=[{"name": x.first_name} for x in persons_list]
Ok so I found the solution to this inspired by this :
d={}
dlist=[]
for x in person_list:
for y in x:
d['firstname']=y.firstname
d['lastname']=y.lastname
dlist.append(d)
And it works like a charm !

How can i get the number of members in a event on facebook?

I need to get the number of members who set the option "GOING", "MAYBE" and "INVITED".
There is a way to do this?
You can use
/v1.0/{event_id}/invited?summary=true&limit=0
// use "limit=0" if you're not interested in data, just the summary
which returns info, such as:
{
"data": [
],
"summary": {
"maybe_count": 0,
"declined_count": 0,
"attending_count": 3,
"count": 3
}
}
Facebook doesn't expose a count method on these tables. The only way to do this is to make a query that returns all the members and count them with your script.
Depending on how you want to process the information you have several ways to attack it
graph.facebook.com/{event_id}/invited - Returns all invited users and each one has an rsvp_status value that does not quite match up to the other options below (attending, unsure, declined, not_replied)
graph.facebook.com/{event_id}/attending - exactly what you would expect
graph.facebook.com/{event_id}/maybe - exactly what you would expect
graph.facebook.com/{event_id}/noreply - exactly what you would expect
graph.facebook.com/{event_id}/declined - exactly what you would expect