Skip to content

Commit

Permalink
Merge pull request #1889 from bagerard/compatible_pyton_2_3_improvement
Browse files Browse the repository at this point in the history
improve 2-3 codebase compatibility
  • Loading branch information
erdenezul committed Sep 14, 2018
2 parents 5c805be + 4314fa8 commit dd0fdcf
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 68 deletions.
26 changes: 13 additions & 13 deletions docs/code/tumblelog.py
Expand Up @@ -45,27 +45,27 @@ class LinkPost(Post):
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)
8 changes: 4 additions & 4 deletions mongoengine/base/common.py
Expand Up @@ -3,10 +3,10 @@
__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 = {}
Expand Down
2 changes: 1 addition & 1 deletion mongoengine/base/datastructures.py
Expand Up @@ -377,7 +377,7 @@ def update(self, **update):

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):
Expand Down
4 changes: 2 additions & 2 deletions mongoengine/base/document.py
Expand Up @@ -302,7 +302,7 @@ def to_mongo(self, use_db_field=True, fields=None):
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:
Expand Down Expand Up @@ -567,7 +567,7 @@ def _get_changed_fields(self, inspected=None):
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

Expand Down
2 changes: 1 addition & 1 deletion mongoengine/base/fields.py
Expand Up @@ -501,7 +501,7 @@ def __init__(self, auto_index=True, *args, **kwargs):
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))
Expand Down
2 changes: 1 addition & 1 deletion mongoengine/dereference.py
Expand Up @@ -146,7 +146,7 @@ def _fetch_objects(self, doc_type=None):
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
Expand Down
12 changes: 4 additions & 8 deletions mongoengine/document.py
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -95,7 +94,7 @@ def reload(self, *args, **kwargs):
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.
Expand Down Expand Up @@ -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',)

Expand Down Expand Up @@ -996,7 +994,7 @@ def compare_indexes(cls):
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
Expand All @@ -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

Expand All @@ -1029,7 +1026,7 @@ def __delattr__(self, *args, **kwargs):
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.
Expand All @@ -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

Expand Down
13 changes: 11 additions & 2 deletions mongoengine/fields.py
Expand Up @@ -24,6 +24,7 @@
except ImportError:
Int64 = long


from mongoengine.base import (BaseDocument, BaseField, ComplexBaseField,
GeoJsonBaseField, LazyReference, ObjectIdField,
get_document)
Expand All @@ -41,6 +42,12 @@
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',
Expand Down Expand Up @@ -597,7 +604,7 @@ def _convert_from_string(self, data):
>>> 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):
Expand Down Expand Up @@ -1525,9 +1532,11 @@ def __getattr__(self, name):
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
Expand Down
17 changes: 8 additions & 9 deletions mongoengine/queryset/base.py
Expand Up @@ -2,7 +2,6 @@

import copy
import itertools
import operator
import pprint
import re
import warnings
Expand Down Expand Up @@ -209,14 +208,12 @@ def _has_data(self):
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):
Expand Down Expand Up @@ -269,13 +266,13 @@ def get(self, *q_objs, **query):
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

Expand Down Expand Up @@ -1478,13 +1475,13 @@ def item_frequencies(self, field, normalize=False, map_reduce=True):

# 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)
Expand All @@ -1498,6 +1495,8 @@ def next(self):

return doc

next = __next__ # For Python2 support

def rewind(self):
"""Rewind the cursor to its unevaluated state.
Expand Down
4 changes: 3 additions & 1 deletion mongoengine/queryset/field_list.py
Expand Up @@ -63,9 +63,11 @@ def __add__(self, f):
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:
Expand Down
2 changes: 1 addition & 1 deletion mongoengine/queryset/manager.py
Expand Up @@ -36,7 +36,7 @@ def __get__(self, instance, owner):
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:
Expand Down
4 changes: 2 additions & 2 deletions mongoengine/queryset/queryset.py
Expand Up @@ -115,7 +115,7 @@ def _populate_cache(self):
# 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
Expand Down Expand Up @@ -170,7 +170,7 @@ def __repr__(self):
data = []
for _ in six.moves.range(REPR_OUTPUT_SIZE + 1):
try:
data.append(self.next())
data.append(six.next(self))
except StopIteration:
break

Expand Down
3 changes: 1 addition & 2 deletions setup.py
Expand Up @@ -44,9 +44,8 @@ def get_version(version_tuple):
"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',
Expand Down
8 changes: 4 additions & 4 deletions 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 *
16 changes: 8 additions & 8 deletions 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()
6 changes: 3 additions & 3 deletions 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 *
12 changes: 6 additions & 6 deletions 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 *
from .transform import *
from .field_list import *
from .queryset import *
from .visitor import *
from .geo import *
from .modify import *

0 comments on commit dd0fdcf

Please sign in to comment.