From aed397c34f0bcc9eeb12e42256cc47e79e049971 Mon Sep 17 00:00:00 2001 From: Javier Sancho Date: Wed, 5 Feb 2014 16:38:41 +0100 Subject: [PATCH] msgpack instead cPickle (for multiple platforms) and str instead unicode (thinking in python3) --- MySQL.py | 2 +- collection.py | 18 +++++++++--------- connection.py | 4 ++-- cursor.py | 16 ++++++++-------- database.py | 2 +- 5 files changed, 21 insertions(+), 21 deletions(-) diff --git a/MySQL.py b/MySQL.py index 3ee09ae..f5f8888 100644 --- 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)) 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] diff --git a/connection.py b/connection.py index ca67b9e..32cd8ae 100644 --- a/connection.py +++ b/connection.py @@ -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 [] diff --git a/cursor.py b/cursor.py index ca9e2a4..ef63280 100644 --- 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 diff --git a/database.py b/database.py index 1b378bf..ad1e129 100644 --- a/database.py +++ b/database.py @@ -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) -- 2.39.5