Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Convert iterX methods to use py3 syntax
  • Loading branch information
Paul Jukic committed Oct 29, 2019
commit 9af04ec0e61e6dc7d68e3ea3d2aa32cbd2f8fc4a
2 changes: 1 addition & 1 deletion mongorm/DocumentMetaclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def indexConverter( fieldName ):
return fields[fieldName].optimalIndex( )
return fieldName

for field,value in fields.iteritems( ):
for field,value in fields.items( ):
if value.primaryKey:
assert primaryKey is None, "Can only have one primary key per document"
primaryKey = field
Expand Down
4 changes: 2 additions & 2 deletions mongorm/queryset/Q.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__( self, _query=None, **search ):

def toMongo( self, document, forUpdate=False, modifier=None ):
newSearch = {}
for (name, value) in self.query.iteritems( ):
for (name, value) in self.query.items( ):
Copy link

Choose a reason for hiding this comment

The 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 six.iteritems instead if you want to avoid it.

Choose a reason for hiding this comment

The 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).

Copy link
Author

Choose a reason for hiding this comment

The 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

# Python 2 and 3: option 3
from future.utils import iteritems
# or
from six import iteritems

for (key, value) in iteritems(heights):
    ...

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 ]
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions mongorm/queryset/QuerySet.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def delete( self ):
def _prepareActions( self, **actions ):
updates = {}

for action, value in actions.iteritems( ):
for action, value in actions.items( ):
assert '__' in action, 'Action "%s" not legal for update' % (action,)
modifier, fieldName = action.split( '__', 1 )
assert modifier in ['set', 'unset', 'setOnInsert', 'inc', 'dec', 'push', 'pull', 'pullAll'], 'Unknown modifier "%s"' % modifier
Expand Down Expand Up @@ -215,7 +215,7 @@ def ignore( self, *fields ):
def fields( self, **projections ):
kwargs = self._get_kwargs( )
kwargs['fields'] = dict(self._fields or {})
for field, value in projections.iteritems( ):
for field, value in projections.items( ):
if '__' in field:
fieldName, sep, projection = field.rpartition( '__' )
if projection in PROJECTIONS:
Expand Down Expand Up @@ -246,7 +246,7 @@ def _do_find( self, **kwargs ):

search = self._get_query( )

if '_types' in search and 'projection' in kwargs and not kwargs['projection'].get( '_types' ) and all(kwargs['projection'].itervalues( )):
if '_types' in search and 'projection' in kwargs and not kwargs['projection'].get( '_types' ) and all(kwargs['projection'].values( )):
kwargs['projection']['_types'] = True

# read_preference not supported in pymongo 3.0+, use with_options() instead
Expand Down Expand Up @@ -342,7 +342,7 @@ def ensure_indexed( self ):
return self

def _queryToIndex( self, query ):
for key, value in query.iteritems( ):
for key, value in query.items( ):
if key in ('$and', '$or'):
for subq in value:
for index in self._queryToIndex( subq ):
Expand Down