diff --git a/docs/code/tumblelog.py b/docs/code/tumblelog.py index c10160ea..796336e6 100644 --- a/docs/code/tumblelog.py +++ b/docs/code/tumblelog.py @@ -45,27 +45,27 @@ post2.link_url = 'http://tractiondigital.com/labs/mongoengine/docs' post2.tags = ['mongoengine'] post2.save() -print 'ALL POSTS' -print +print('ALL POSTS') +print() for post in Post.objects: - print post.title + print(post.title) #print '=' * post.title.count() - print "=" * 20 + print("=" * 20) if isinstance(post, TextPost): - print post.content + print(post.content) if isinstance(post, LinkPost): - print 'Link:', post.link_url + print('Link:', post.link_url) - print -print + print() +print() -print 'POSTS TAGGED \'MONGODB\'' -print +print('POSTS TAGGED \'MONGODB\'') +print() for post in Post.objects(tags='mongodb'): - print post.title -print + print(post.title) +print() num_posts = Post.objects(tags='mongodb').count() -print 'Found %d posts with tag "mongodb"' % num_posts +print('Found %d posts with tag "mongodb"' % num_posts) diff --git a/mongoengine/base/common.py b/mongoengine/base/common.py index f80471ef..dd177920 100644 --- a/mongoengine/base/common.py +++ b/mongoengine/base/common.py @@ -3,10 +3,10 @@ from mongoengine.errors import NotRegistered __all__ = ('UPDATE_OPERATORS', 'get_document', '_document_registry') -UPDATE_OPERATORS = set(['set', 'unset', 'inc', 'dec', 'mul', - 'pop', 'push', 'push_all', 'pull', - 'pull_all', 'add_to_set', 'set_on_insert', - 'min', 'max', 'rename']) +UPDATE_OPERATORS = {'set', 'unset', 'inc', 'dec', 'mul', + 'pop', 'push', 'push_all', 'pull', + 'pull_all', 'add_to_set', 'set_on_insert', + 'min', 'max', 'rename'} _document_registry = {} diff --git a/mongoengine/base/datastructures.py b/mongoengine/base/datastructures.py index db292f14..0197ad10 100644 --- a/mongoengine/base/datastructures.py +++ b/mongoengine/base/datastructures.py @@ -377,7 +377,7 @@ class EmbeddedDocumentList(BaseList): class StrictDict(object): __slots__ = () - _special_fields = set(['get', 'pop', 'iteritems', 'items', 'keys', 'create']) + _special_fields = {'get', 'pop', 'iteritems', 'items', 'keys', 'create'} _classes = {} def __init__(self, **kwargs): diff --git a/mongoengine/base/document.py b/mongoengine/base/document.py index 85906a3e..fdebab23 100644 --- a/mongoengine/base/document.py +++ b/mongoengine/base/document.py @@ -302,7 +302,7 @@ class BaseDocument(object): data['_cls'] = self._class_name # only root fields ['test1.a', 'test2'] => ['test1', 'test2'] - root_fields = set([f.split('.')[0] for f in fields]) + root_fields = {f.split('.')[0] for f in fields} for field_name in self: if root_fields and field_name not in root_fields: @@ -567,7 +567,7 @@ class BaseDocument(object): continue elif isinstance(field, SortedListField) and field._ordering: # if ordering is affected whole list is changed - if any(map(lambda d: field._ordering in d._changed_fields, data)): + if any(field._ordering in d._changed_fields for d in data): changed_fields.append(db_field_name) continue diff --git a/mongoengine/base/fields.py b/mongoengine/base/fields.py index d25d4305..a0726aa6 100644 --- a/mongoengine/base/fields.py +++ b/mongoengine/base/fields.py @@ -501,7 +501,7 @@ class GeoJsonBaseField(BaseField): def validate(self, value): """Validate the GeoJson object based on its type.""" if isinstance(value, dict): - if set(value.keys()) == set(['type', 'coordinates']): + if set(value.keys()) == {'type', 'coordinates'}: if value['type'] != self._type: self.error('%s type must be "%s"' % (self._name, self._type)) diff --git a/mongoengine/dereference.py b/mongoengine/dereference.py index 40bc72b2..6c993223 100644 --- a/mongoengine/dereference.py +++ b/mongoengine/dereference.py @@ -146,7 +146,7 @@ class DeReference(object): for key, doc in references.iteritems(): object_map[(col_name, key)] = doc else: # Generic reference: use the refs data to convert to document - if isinstance(doc_type, (ListField, DictField, MapField,)): + if isinstance(doc_type, (ListField, DictField, MapField)): continue refs = [dbref for dbref in dbrefs diff --git a/mongoengine/document.py b/mongoengine/document.py index 25af273d..cdeed4c6 100644 --- a/mongoengine/document.py +++ b/mongoengine/document.py @@ -39,7 +39,7 @@ class InvalidCollectionError(Exception): pass -class EmbeddedDocument(BaseDocument): +class EmbeddedDocument(six.with_metaclass(DocumentMetaclass, BaseDocument)): """A :class:`~mongoengine.Document` that isn't stored in its own collection. :class:`~mongoengine.EmbeddedDocument`\ s should be used as fields on :class:`~mongoengine.Document`\ s through the @@ -58,7 +58,6 @@ class EmbeddedDocument(BaseDocument): # The __metaclass__ attribute is removed by 2to3 when running with Python3 # my_metaclass is defined so that metaclass can be queried in Python 2 & 3 my_metaclass = DocumentMetaclass - __metaclass__ = DocumentMetaclass # A generic embedded document doesn't have any immutable properties # that describe it uniquely, hence it shouldn't be hashable. You can @@ -95,7 +94,7 @@ class EmbeddedDocument(BaseDocument): self._instance.reload(*args, **kwargs) -class Document(BaseDocument): +class Document(six.with_metaclass(TopLevelDocumentMetaclass, BaseDocument)): """The base class used for defining the structure and properties of collections of documents stored in MongoDB. Inherit from this class, and add fields as class attributes to define a document's structure. @@ -150,7 +149,6 @@ class Document(BaseDocument): # The __metaclass__ attribute is removed by 2to3 when running with Python3 # my_metaclass is defined so that metaclass can be queried in Python 2 & 3 my_metaclass = TopLevelDocumentMetaclass - __metaclass__ = TopLevelDocumentMetaclass __slots__ = ('__objects',) @@ -996,7 +994,7 @@ class Document(BaseDocument): return {'missing': missing, 'extra': extra} -class DynamicDocument(Document): +class DynamicDocument(six.with_metaclass(TopLevelDocumentMetaclass, Document)): """A Dynamic Document class allowing flexible, expandable and uncontrolled schemas. As a :class:`~mongoengine.Document` subclass, acts in the same way as an ordinary document but has expanded style properties. Any data @@ -1013,7 +1011,6 @@ class DynamicDocument(Document): # The __metaclass__ attribute is removed by 2to3 when running with Python3 # my_metaclass is defined so that metaclass can be queried in Python 2 & 3 my_metaclass = TopLevelDocumentMetaclass - __metaclass__ = TopLevelDocumentMetaclass _dynamic = True @@ -1029,7 +1026,7 @@ class DynamicDocument(Document): super(DynamicDocument, self).__delattr__(*args, **kwargs) -class DynamicEmbeddedDocument(EmbeddedDocument): +class DynamicEmbeddedDocument(six.with_metaclass(DocumentMetaclass, EmbeddedDocument)): """A Dynamic Embedded Document class allowing flexible, expandable and uncontrolled schemas. See :class:`~mongoengine.DynamicDocument` for more information about dynamic documents. @@ -1038,7 +1035,6 @@ class DynamicEmbeddedDocument(EmbeddedDocument): # The __metaclass__ attribute is removed by 2to3 when running with Python3 # my_metaclass is defined so that metaclass can be queried in Python 2 & 3 my_metaclass = DocumentMetaclass - __metaclass__ = DocumentMetaclass _dynamic = True diff --git a/mongoengine/fields.py b/mongoengine/fields.py index 244551d2..f794edff 100644 --- a/mongoengine/fields.py +++ b/mongoengine/fields.py @@ -24,6 +24,7 @@ try: except ImportError: Int64 = long + from mongoengine.base import (BaseDocument, BaseField, ComplexBaseField, GeoJsonBaseField, LazyReference, ObjectIdField, get_document) @@ -41,6 +42,12 @@ except ImportError: Image = None ImageOps = None +if six.PY3: + # Useless as long as 2to3 gets executed + # as it turns `long` into `int` blindly + long = int + + __all__ = ( 'StringField', 'URLField', 'EmailField', 'IntField', 'LongField', 'FloatField', 'DecimalField', 'BooleanField', 'DateTimeField', 'DateField', @@ -597,7 +604,7 @@ class ComplexDateTimeField(StringField): >>> ComplexDateTimeField()._convert_from_string(a) datetime.datetime(2011, 6, 8, 20, 26, 24, 92284) """ - values = map(int, data.split(self.separator)) + values = [int(d) for d in data.split(self.separator)] return datetime.datetime(*values) def __get__(self, instance, owner): @@ -1525,9 +1532,11 @@ class GridFSProxy(object): def __get__(self, instance, value): return self - def __nonzero__(self): + def __bool__(self): return bool(self.grid_id) + __nonzero__ = __bool__ # For Py2 support + def __getstate__(self): self_dict = self.__dict__ self_dict['_fs'] = None diff --git a/mongoengine/queryset/base.py b/mongoengine/queryset/base.py index 6fddc381..0be48654 100644 --- a/mongoengine/queryset/base.py +++ b/mongoengine/queryset/base.py @@ -2,7 +2,6 @@ from __future__ import absolute_import import copy import itertools -import operator import pprint import re import warnings @@ -209,14 +208,12 @@ class BaseQuerySet(object): queryset = self.order_by() return False if queryset.first() is None else True - def __nonzero__(self): - """Avoid to open all records in an if stmt in Py2.""" - return self._has_data() - def __bool__(self): """Avoid to open all records in an if stmt in Py3.""" return self._has_data() + __nonzero__ = __bool__ # For Py2 support + # Core functions def all(self): @@ -269,13 +266,13 @@ class BaseQuerySet(object): queryset = queryset.filter(*q_objs, **query) try: - result = queryset.next() + result = six.next(queryset) except StopIteration: msg = ('%s matching query does not exist.' % queryset._document._class_name) raise queryset._document.DoesNotExist(msg) try: - queryset.next() + six.next(queryset) except StopIteration: return result @@ -1478,13 +1475,13 @@ class BaseQuerySet(object): # Iterator helpers - def next(self): + def __next__(self): """Wrap the result in a :class:`~mongoengine.Document` object. """ if self._limit == 0 or self._none: raise StopIteration - raw_doc = self._cursor.next() + raw_doc = six.next(self._cursor) if self._as_pymongo: return self._get_as_pymongo(raw_doc) @@ -1498,6 +1495,8 @@ class BaseQuerySet(object): return doc + next = __next__ # For Python2 support + def rewind(self): """Rewind the cursor to its unevaluated state. diff --git a/mongoengine/queryset/field_list.py b/mongoengine/queryset/field_list.py index 0524c3bb..dba724af 100644 --- a/mongoengine/queryset/field_list.py +++ b/mongoengine/queryset/field_list.py @@ -63,9 +63,11 @@ class QueryFieldList(object): self._only_called = True return self - def __nonzero__(self): + def __bool__(self): return bool(self.fields) + __nonzero__ = __bool__ # For Py2 support + def as_dict(self): field_list = {field: self.value for field in self.fields} if self.slice: diff --git a/mongoengine/queryset/manager.py b/mongoengine/queryset/manager.py index 199205e9..f93dbb43 100644 --- a/mongoengine/queryset/manager.py +++ b/mongoengine/queryset/manager.py @@ -36,7 +36,7 @@ class QuerySetManager(object): queryset_class = owner._meta.get('queryset_class', self.default) queryset = queryset_class(owner, owner._get_collection()) if self.get_queryset: - arg_count = self.get_queryset.func_code.co_argcount + arg_count = self.get_queryset.__code__.co_argcount if arg_count == 1: queryset = self.get_queryset(queryset) elif arg_count == 2: diff --git a/mongoengine/queryset/queryset.py b/mongoengine/queryset/queryset.py index 1aadfb76..f9fed7b7 100644 --- a/mongoengine/queryset/queryset.py +++ b/mongoengine/queryset/queryset.py @@ -115,7 +115,7 @@ class QuerySet(BaseQuerySet): # the result cache. try: for _ in six.moves.range(ITER_CHUNK_SIZE): - self._result_cache.append(self.next()) + self._result_cache.append(six.next(self)) except StopIteration: # Getting this exception means there are no more docs in the # db cursor. Set _has_more to False so that we can use that @@ -170,7 +170,7 @@ class QuerySetNoCache(BaseQuerySet): data = [] for _ in six.moves.range(REPR_OUTPUT_SIZE + 1): try: - data.append(self.next()) + data.append(six.next(self)) except StopIteration: break diff --git a/setup.py b/setup.py index 98964d19..c7632ce3 100644 --- a/setup.py +++ b/setup.py @@ -44,9 +44,8 @@ CLASSIFIERS = [ "Programming Language :: Python :: 2", "Programming Language :: Python :: 2.7", "Programming Language :: Python :: 3", - "Programming Language :: Python :: 3.3", - "Programming Language :: Python :: 3.4", "Programming Language :: Python :: 3.5", + "Programming Language :: Python :: 3.6", "Programming Language :: Python :: Implementation :: CPython", "Programming Language :: Python :: Implementation :: PyPy", 'Topic :: Database', diff --git a/tests/__init__.py b/tests/__init__.py index eab0ddc7..08db7186 100644 --- a/tests/__init__.py +++ b/tests/__init__.py @@ -1,4 +1,4 @@ -from all_warnings import AllWarnings -from document import * -from queryset import * -from fields import * +from .all_warnings import AllWarnings +from .document import * +from .queryset import * +from .fields import * diff --git a/tests/document/__init__.py b/tests/document/__init__.py index f71376ea..dc35c969 100644 --- a/tests/document/__init__.py +++ b/tests/document/__init__.py @@ -1,13 +1,13 @@ import unittest -from class_methods import * -from delta import * -from dynamic import * -from indexes import * -from inheritance import * -from instance import * -from json_serialisation import * -from validation import * +from .class_methods import * +from .delta import * +from .dynamic import * +from .indexes import * +from .inheritance import * +from .instance import * +from .json_serialisation import * +from .validation import * if __name__ == '__main__': unittest.main() diff --git a/tests/fields/__init__.py b/tests/fields/__init__.py index 8e0640db..4994d0c6 100644 --- a/tests/fields/__init__.py +++ b/tests/fields/__init__.py @@ -1,3 +1,3 @@ -from fields import * -from file_tests import * -from geo import * +from .fields import * +from .file_tests import * +from .geo import * diff --git a/tests/queryset/__init__.py b/tests/queryset/__init__.py index c36b2684..31016966 100644 --- a/tests/queryset/__init__.py +++ b/tests/queryset/__init__.py @@ -1,6 +1,6 @@ -from transform import * -from field_list import * -from queryset import * -from visitor import * -from geo import * -from modify import * \ No newline at end of file +from .transform import * +from .field_list import * +from .queryset import * +from .visitor import * +from .geo import * +from .modify import *