-
Notifications
You must be signed in to change notification settings - Fork 1
Make compatible with python 2 and python 3 #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 10 commits
6f48603
fd1db8a
9a82719
981ab9a
64e16d2
2a44caa
d972b79
538b938
9af04ec
92aecb3
b6bd4cd
3a1f6ba
b6d4ffe
86e9cc6
b735f5b
2db729f
c6c0d77
94029b3
b6f3dbe
3886461
92f3f81
b9ad574
666a0aa
1eb0c34
1db4387
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| from builtins import object | ||
| class DocumentRegistry(object): | ||
| documentTypes = {} | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| from builtins import object | ||
| class BaseField(object): | ||
| _resyncAtSave = False | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,18 +1,18 @@ | ||
|
|
||
| from builtins import object | ||
| class Q(object): | ||
| def __init__( self, _query=None, **search ): | ||
| if _query is None: | ||
| if 'pk' in search: | ||
| search['id'] = search['pk'] | ||
| del search['pk'] | ||
|
|
||
| self.query = search | ||
| else: | ||
| self.query = _query | ||
|
|
||
| def toMongo( self, document, forUpdate=False, modifier=None ): | ||
| newSearch = {} | ||
| for (name, value) in self.query.iteritems( ): | ||
| for (name, value) in self.query.items( ): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will incur a performance hit on Python 2.x. You might want to consider using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the performance impact will be small - most of the larger arrays that were in our mongo documents have been factored out into their own collections (for reasons of mongo/memcached performance).
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm, I'm surprised it makes that one of the default changes as part of the auto upgrade tool actually... I would have expected it to have gone with option 3 from their site: https://python-future.org/compatible_idioms.html#iterating-through-dict-keys-values-items I guess they try default into making it as python3 compatible as possible from the get go, to hint that you should really be moving to py3 sooner rather than later :P There's a few places that have this change (aka, the Convert iterX methods to use py3 syntax commit). Do you think it's worth updating them to use that import? |
||
| if name in ['$or', '$and']: | ||
| # mongodb logic operator - value is a list of Qs | ||
| newSearch[name] = [ value.toMongo( document ) for value in value ] | ||
|
|
@@ -21,9 +21,9 @@ def toMongo( self, document, forUpdate=False, modifier=None ): | |
| if name.startswith('$') and isinstance(value, basestring): | ||
| newSearch[name] = value | ||
| continue | ||
|
|
||
| fieldName = name | ||
|
|
||
| MONGO_COMPARISONS = ['gt', 'lt', 'lte', 'gte', 'exists', 'ne', 'all', 'in', 'nin', 'elemMatch'] | ||
| REGEX_COMPARISONS = { | ||
| 'contains': ( '%s', '' ), | ||
|
|
@@ -33,14 +33,14 @@ def toMongo( self, document, forUpdate=False, modifier=None ): | |
|
|
||
| 'startswith': ( '^%s', '' ), | ||
| 'istartswith': ( '^%s', 'i' ), | ||
|
|
||
| 'endswith': ( '%s$', '' ), | ||
| 'iendswith': ( '%s$', 'i' ), | ||
|
|
||
| 'matches': ( None, '' ), | ||
| 'imatches': ( None, 'i' ), | ||
| } | ||
| ALL_COMPARISONS = MONGO_COMPARISONS + REGEX_COMPARISONS.keys() | ||
| ALL_COMPARISONS = MONGO_COMPARISONS + list(REGEX_COMPARISONS.keys()) | ||
| ARRAY_VALUE_COMPARISONS = ['all', 'in', 'nin'] | ||
|
|
||
| comparison = None | ||
|
|
@@ -57,10 +57,10 @@ def toMongo( self, document, forUpdate=False, modifier=None ): | |
| # not a comparison operator | ||
| dereferences = chunks[1:] | ||
| comparison = None | ||
|
|
||
| if fieldName not in document._fields: | ||
| raise AttributeError, "%s does not contain the field '%s'" % (document.__name__, fieldName) | ||
| raise AttributeError("%s does not contain the field '%s'" % (document.__name__, fieldName)) | ||
|
|
||
| field = document._fields[fieldName] | ||
| if not forUpdate: | ||
| if comparison in ARRAY_VALUE_COMPARISONS: | ||
|
|
@@ -90,10 +90,10 @@ def toMongo( self, document, forUpdate=False, modifier=None ): | |
| else: | ||
| searchValue = field.fromPython( value, dereferences=dereferences, modifier=modifier ) | ||
| targetSearchKey = '.'.join( [field.dbField] + dereferences) | ||
|
|
||
| valueMapper = lambda value: value | ||
|
|
||
|
|
||
| if comparison is not None: | ||
| if comparison in REGEX_COMPARISONS: | ||
| regex,options = REGEX_COMPARISONS[comparison] | ||
|
|
@@ -115,7 +115,7 @@ def toMongo( self, document, forUpdate=False, modifier=None ): | |
|
|
||
| if isinstance(searchValue, dict): | ||
| if not forUpdate: | ||
| for name,value in searchValue.iteritems( ): | ||
| for name,value in searchValue.items( ): | ||
| if name: | ||
| key = targetSearchKey + '.' + name | ||
| else: | ||
|
|
@@ -125,14 +125,14 @@ def toMongo( self, document, forUpdate=False, modifier=None ): | |
| newSearch[targetSearchKey] = valueMapper(searchValue) | ||
| else: | ||
| newSearch[targetSearchKey] = valueMapper(searchValue) | ||
|
|
||
| return newSearch | ||
|
|
||
| def __or__( self, other ): | ||
| return self.do_merge( other, '$or' ) | ||
|
|
||
| def __and__( self, other ): | ||
| if len( set( self.query.keys() ).intersection( other.query.keys() ) ) > 0: | ||
| if len( set( self.query.keys() ).intersection( list(other.query.keys()) ) ) > 0: | ||
Juko marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| # if the 2 queries have overlapping keys, we need to use a $and to join them. | ||
| return self.do_merge( other, '$and' ) | ||
| else: | ||
|
|
@@ -141,17 +141,17 @@ def __and__( self, other ): | |
| newQuery.update( self.query ) | ||
| newQuery.update( other.query ) | ||
| return Q( _query=newQuery ) | ||
|
|
||
| def do_merge( self, other, op ): | ||
| if len(self.query) == 0: return other | ||
| if len(other.query) == 0: return self | ||
|
|
||
| if op in self.query and len(self.query) == 1: | ||
| items = self.query[op] + [other] | ||
| elif op in other.query and len(self.query) == 1: | ||
| items = other.query[op] + [self] | ||
| else: | ||
| items = [ self, other ] | ||
|
|
||
| newQuery = { op: items } | ||
| return Q( _query=newQuery ) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,5 +11,6 @@ | |
| description='Mongorm', | ||
| long_description='Mongorm', | ||
| platforms=['any'], | ||
| install_requires=['pymongo >= 2.4.2', 'pysignals'], | ||
| install_requires=['pymongo >= 2.4.2', 'pysignals', 'iso8601'], | ||
|
||
| test_requires=['pytest'], | ||
| ) | ||
Uh oh!
There was an error while loading. Please reload this page.