How can I preserve the order of the values fetched with memcache's get_multi() function? By default, the order returned is random. Thanks.
Python's Memcache library returns a dictionary, and dictionaries in python are unordered, so you have to get the values from the dictionary in the right order manually:
result = cache.get_multi(keys)
values = [result.get(key) for key in keys]
as I remember memcache has flag GET PRESERVE ORDER, try adding this to function flags
Related
I'm using a dictionary in Python 2, and am wondering if the dictionary.values() returns a list of values in a particular order
i.e. the order they were given into the dictionary in?
Example
dict = {}
dict[1] = "potato"
dict[2] = "tomato"
dict[3] = "orange"
dict[4] = "carrot"
list_val = dict.values()
Is list_val in the order potato->tomato->orange->carrot?
or is it in some other order?
This is a simple example, I mean will it return in the same order for even more complex structures?
For example, replace the strings with dictionaries
making it a dictionary of dictionaries
NOTE: This was answered in the other thread, which is linked
Another good answer to refer to for conceptual answer is :
This
UPDATE:
After reading some documentation on hash tables (what dict in python use to store key information) I found that the order in which you assign keys/values is not what defines its location in the has table. There is a really good video HERE that does a great job of explaining how a hash table works and what is going on during the key assignment to the hash table.
Basically a key is assigned a hash number that is then placed in the hash table according to its hash number rather than its key value. This hash number is placed first in the desired location on the hash table that corresponds with its hash number unless there is already something assigned to that location. If something else is already assigned to that location on the Hash Table then python does some calculations to chose the next best location. This can be before or after the desired location in the has table. HERE Is some documentation but I highly recommend watching the video I liked as it does a great job explaining this concept.
I have a view in couchdb that returns results with two keys:
[<string_key>, <date>]
I need to ignore second key "date" and return all docs that matches key "string". But when I'm trying ignore the "date" key, view returns all values. Any advice?
You need to use startkey and endkey for doing this. You can ignore the second parameter by using {} in the end key. It returns all the possible results.
For reference you can look at the summary of this article: http://ryankirkman.com/2011/03/30/advanced-filtering-with-couchdb-views.html
I create a Berkeley database, and operate with it using bsddb module. And I need to store there information in a style, like this:
username = '....'
notes = {'name_of_note1':{
'password':'...',
'comments':'...',
'title':'...'
}
'name_of_note2':{
#keys same as previous, but another values
}
}
This is how I open database
db = bsddb.btopen['data.db','c']
How do I do that ?
So, first, I guess you should open your database using parentheses:
db = bsddb.btopen('data.db','c')
Keep in mind that Berkeley's pattern is key -> value, where both key and value are string objects (not unicode). The best way in your case would be to use:
db[str(username)] = json.dumps(notes)
since your notes are compatible with the json syntax.
However, this is not a very good choice, say, if you want to query only usernames' comments. You should use a relational database, such as sqlite, which is also built-in in Python.
A simple solution was described by #Falvian.
For a start there is a column pattern in ordered key/value store. So the key/value pattern is not the only one.
I think that bsddb is viable solution when you don't want to rely on sqlite. The first approach is to create a documents = bsddb.btopen['documents.db','c'] and store inside json values. Regarding the keys you have several options:
Name the keys yourself, like you do "name_of_note_1", "name_of_note_2"
Generate random identifiers using uuid.uuid4 (don't forget to check it's not already used ;)
Or use a row inside this documents with key=0 to store a counter that you will use to create uids (unique identifiers).
If you use integers don't forget to pack them with lambda x: struct.pack('>q', uid) before storing them.
If you need to create index. I recommend you to have a look at my other answer introducting composite keys to build index in bsddb.
Am I correct to assume that nested dictionaries are not supported in aws simpledb? Should I just serialize everything into json and push to the database?
For example,
test = dict(company='test company', users={'username':'joe', 'password': 'test'})
This returns test with keys of 'company' and 'users', however 'users' just represents a string..
Simply, YES, SimpleDB provides only first level of keys.
So if you want to store data with higher level of key nesting, you will have to serialize the data to a string and you will not have simple select commands to make queries, using deeper nested data (you will be given to test it as a string, but will not have simple access to subkey values).
Note, that one key (in one record) handles storing multiple values, but this is sort of list (often used to store multiple tags), but not a dictionary.
I have a list of two element tuples, where the first element is a string (name of some parameter) and the second element is a float (the value of that parameter). For example,
thelist = [('costperunit', 200), ('profit', 10000), ('fixedcost', 5000),
('numpeople':300)]
There are many more such tuples and the names are different in the real case. I want to add these to a mongoDB database as key: value pairs. Here is how I want to add it.
db.collection.insert( {paramvalues: {'costperunit':200, 'profit':10000,
'fixedcost': 5000, 'numpeople': 300} } )
One way to do this is:
dictform = dict(thelist)
db.collection.insert( {paramvalues: dictform} )
This, however, does not ensure the order of the parameter names and values as dict changes the order.
I tried
from collections import OrderedDict
dictform = OrderedDict(thelist)
db.collection.insert( {paramvalues: dictform} )
This maintains the original order of parameter names and values, however, inserts the parameter names and values as list of lists.
I am very new to mongoDB and trying to learn it. Is there a trick either in Python or in mongoDB that would achieve what I want? The reason I want the value of the key paramvalues in the Mongodb database as a dictionary (or Javascript object) is that I can then filter results using the value of some parameter. For example, I can do:
db.collection.find( {'paramvalues.costperunit': 200} )
If you are sure there is no way to do this, I would appreciate if you let me know.
Thanks.
Pymongo offers a subclass of dict, bson.son.SON: http://api.mongodb.org/python/current/api/bson/son.html which is ordered for cases where you need that such as sending commands.
Dicts in Python and arrays in Javascript/BSON (MongoDB) are not ordered. Either you store some explicit sort-index number as part of the dict/array and perform app-level sorting on this level or you insert your data into a list which is of course sorted.