]> git.jsancho.org Git - mojodb.git/commitdiff
msgpack instead cPickle (for multiple platforms) and str instead unicode (thinking...
authorJavier Sancho <jsf@jsancho.org>
Wed, 5 Feb 2014 15:38:41 +0000 (16:38 +0100)
committerJavier Sancho <jsf@jsancho.org>
Wed, 5 Feb 2014 15:38:41 +0000 (16:38 +0100)
MySQL.py
collection.py
connection.py
cursor.py
database.py

index 3ee09aef346bea3cb0e46a9bc9a296c5029a4e67..f5f888828b83109b43b41d7b8d109ee066b5742b 100644 (file)
--- a/MySQL.py
+++ b/MySQL.py
@@ -124,7 +124,7 @@ class Connection(connection.Connection):
         vals = []
         for k, v in values.iteritems():
             keys.append(k)
-            if type(v) in (str, unicode):
+            if type(v) is str:
                 vals.append("'%s'" % v.replace("'", "''"))
             else:
                 vals.append(str(v))
index 7af5caaa7817e872bb856741945436f617162506..bbc56cacfbc7fcf7aeb5619dc9932eea58a252ea 100644 (file)
 #
 ##############################################################################
 
-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]
index ca67b9e297cf4e1422bee2a8885a05a04d64379e..32cd8ae1316a3aa8b16927ff002e4d036ed20663 100644 (file)
@@ -39,7 +39,7 @@ class Connection(object):
 
     def database_names(self):
         try:
-            return [unicode(x) for x in self._get_databases()]
+            return [str(x) for x in self._get_databases()]
         except:
             return []
 
@@ -48,7 +48,7 @@ class Connection(object):
 
     def collection_names(self, db_name):
         try:
-            return list(set([unicode(x.split('$')[0]) for x in filter(lambda x: '$' in x, self._get_tables(db_name))]))
+            return list(set([str(x.split('$')[0]) for x in filter(lambda x: '$' in x, self._get_tables(db_name))]))
         except:
             return []
 
index ca9e2a4cc0f902dd8a3261b854ba35d64207b97e..ef6328028141994ca9e775fb13f6a4be67f5604a 100644 (file)
--- a/cursor.py
+++ b/cursor.py
@@ -19,7 +19,7 @@
 #
 ##############################################################################
 
-import cPickle
+import msgpack
 
 class Cursor(object):
     def __init__(self, collection, spec=None, fields=None, **kwargs):
@@ -89,14 +89,14 @@ class Cursor(object):
             query['where'] = []
             for k, v in self.spec.iteritems():
                 table_f = '%s$%s' % (self.collection.table_name, k)
-                if k == u'_id':
-                    query['where'].append(((table_id, 'id'), '=', cPickle.dumps(v)))
+                if k == '_id':
+                    query['where'].append(((table_id, 'id'), '=', msgpack.dumps(v)))
                 elif type(v) in (int, float):
                     field_q = self._get_cursor_field(table_id, table_f, field_name='number')
                     query['where'].append((field_q, '=', v))
                 else:
                     field_q = self._get_cursor_field(table_id, table_f)
-                    query['where'].append((field_q, '=', cPickle.dumps(v)))
+                    query['where'].append((field_q, '=', msgpack.dumps(v)))
 
         return self.collection.database.connection._get_cursor(self.collection.database.db_name, query)
 
@@ -117,12 +117,12 @@ class Cursor(object):
                 raise StopIteration
             else:
                 document = {}
-                if u'_id' in self.fields:
-                    document[u'_id'] = cPickle.loads(res[0])
-                fields_without_id = filter(lambda x: x != u'_id', self.fields)
+                if '_id' in self.fields:
+                    document['_id'] = msgpack.loads(res[0])
+                fields_without_id = filter(lambda x: x != '_id', self.fields)
                 for i in xrange(len(fields_without_id)):
                     if not res[i + 1] is None:
-                        document[fields_without_id[i]] = cPickle.loads(res[i + 1])
+                        document[fields_without_id[i]] = msgpack.loads(res[i + 1])
                 return document
         else:
             return None
index 1b378bf468c60cb0d7645261e57ce2c2ee467a22..ad1e1297050326cd6131313032d1874d08940dbe 100644 (file)
@@ -24,7 +24,7 @@ from collection import Collection
 class Database(object):
     def __init__(self, connection, db_name):
         self.connection = connection
-        self.db_name = unicode(db_name)
+        self.db_name = str(db_name)
 
     def __getattr__(self, table_name):
         return Collection(self, table_name)