X-Git-Url: https://git.jsancho.org/?p=mojodb.git;a=blobdiff_plain;f=collection.py;fp=collection.py;h=bbc56cacfbc7fcf7aeb5619dc9932eea58a252ea;hp=7af5caaa7817e872bb856741945436f617162506;hb=aed397c34f0bcc9eeb12e42256cc47e79e049971;hpb=4a34db8a057d135225e70ede89b767e89f827c8f diff --git a/collection.py b/collection.py index 7af5caa..bbc56ca 100644 --- a/collection.py +++ b/collection.py @@ -19,14 +19,14 @@ # ############################################################################## -import cPickle +import msgpack from cursor import Cursor import uuid class Collection(object): def __init__(self, database, table_name): self.database = database - self.table_name = unicode(table_name) + self.table_name = str(table_name) def __repr__(self): return "Collection(%r, %r)" % (self.database, self.table_name) @@ -50,7 +50,7 @@ class Collection(object): def _get_fields(self): tables = self.database.connection._get_tables(self.database.db_name) - return [unicode(x[x.find('$')+1:]) for x in filter(lambda x: x.startswith('%s$' % self.table_name), tables)] + return [str(x[x.find('$')+1:]) for x in filter(lambda x: x.startswith('%s$' % self.table_name), tables)] def count(self): return self.database.connection._count(self.database.db_name, self.table_name) @@ -69,19 +69,19 @@ class Collection(object): else: docs = doc_or_docs for doc in docs: - if not u'_id' in doc: - doc[u'_id'] = uuid.uuid4().hex + if not '_id' in doc: + doc['_id'] = uuid.uuid4().hex self._insert_document(doc) if type(doc_or_docs) in (list, tuple): - return [d[u'_id'] for d in docs] + return [d['_id'] for d in docs] else: - return docs[0][u'_id'] + return docs[0]['_id'] def _insert_document(self, doc): table_id = '%s$_id' % self.table_name fields = self._get_fields() - coded_id = cPickle.dumps(doc['_id']) + coded_id = msgpack.dumps(doc['_id']) self.database.connection._insert(self.database.db_name, table_id, {'id': coded_id}) for f in doc: if f == '_id': @@ -91,7 +91,7 @@ class Collection(object): table_f = '%s$%s' % (self.table_name, f) values = { 'id': coded_id, - 'value': cPickle.dumps(doc[f]), + 'value': msgpack.dumps(doc[f]), } if type(doc[f]) in (int, float): values['number'] = doc[f]