Skip to content
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

2to3 #66

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

2to3 #66

Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions djangotoolbox/db/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def _value_for_db_collection(self, value, field, field_kind, db_type,
# Do convert filter parameters.
if lookup:
# Special case where we are looking for an empty list
if lookup == 'exact' and db_type == 'list' and value == u'[]':
if lookup == 'exact' and db_type == 'list' and value == '[]':
return []
value = self._value_for_db(value, subfield,
subkind, db_subtype, lookup)
Expand All @@ -459,7 +459,7 @@ def _value_for_db_collection(self, value, field, field_kind, db_type,
value = (
(key, self._value_for_db(subvalue, subfield,
subkind, db_subtype, lookup))
for key, subvalue in value.iteritems())
for key, subvalue in value.items())

# Return just a dict, a once-flattened list;
if db_type == 'dict':
Expand Down Expand Up @@ -514,9 +514,9 @@ def _value_from_db_collection(self, value, field, field_kind, db_type):
# Generator yielding pairs with deconverted values, the
# "list" db_type stores keys and values interleaved.
if db_type == 'list':
value = zip(value[::2], value[1::2])
value = list(zip(value[::2], value[1::2]))
else:
value = value.iteritems()
value = iter(value.items())

# DictField needs to hold a dict.
return dict(
Expand Down Expand Up @@ -575,7 +575,7 @@ def _value_for_db_model(self, value, field, field_kind, db_type, lookup):
value = (
(subfield.column, self._value_for_db(
subvalue, lookup=lookup, *self._convert_as(subfield, lookup)))
for subfield, subvalue in value.iteritems())
for subfield, subvalue in value.items())

# Cast to a dict, interleave columns with values on a list,
# serialize, or return a generator.
Expand Down Expand Up @@ -603,7 +603,7 @@ def _value_from_db_model(self, value, field, field_kind, db_type):

# Separate keys from values and create a dict or unpickle one.
if db_type == 'list':
value = dict(zip(value[::2], value[1::2]))
value = dict(list(zip(value[::2], value[1::2])))
elif db_type == 'bytes' or db_type == 'string':
value = pickle.loads(value)

Expand Down
8 changes: 4 additions & 4 deletions djangotoolbox/db/basecompiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def _decode_child(self, child):
raise DatabaseError("This database doesn't support filtering "
"on non-primary key ForeignKey fields.")

field = (f for f in opts.fields if f.column == column).next()
field = next((f for f in opts.fields if f.column == column))
assert field.rel is not None

value = self._normalize_lookup_value(
Expand Down Expand Up @@ -421,7 +421,7 @@ def execute_sql(self, result_type=MULTI):
"""
self.pre_sql_setup()

aggregates = self.query.aggregate_select.values()
aggregates = list(self.query.aggregate_select.values())

# Simulate a count().
if aggregates:
Expand Down Expand Up @@ -547,9 +547,9 @@ def get_fields(self):
only_load = self.deferred_to_columns()
if only_load:
db_table = self.query.model._meta.db_table
only_load = dict((k, v) for k, v in only_load.items()
only_load = dict((k, v) for k, v in list(only_load.items())
if v or k == db_table)
if len(only_load.keys()) > 1:
if len(list(only_load.keys())) > 1:
raise DatabaseError("Multi-table inheritance is not "
"supported by non-relational DBs %s." %
repr(only_load))
Expand Down
6 changes: 3 additions & 3 deletions djangotoolbox/db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ def decimal_to_string(value, max_digits=16, decimal_places=0):

# Handle sign separately.
if value.is_signed():
sign = u'-'
sign = '-'
value = abs(value)
else:
sign = u''
sign = ''

# Let Django quantize and cast to a string.
value = format_number(value, max_digits, decimal_places)
Expand All @@ -29,5 +29,5 @@ def decimal_to_string(value, max_digits=16, decimal_places=0):
if n < 0:
n = len(value)
if n < max_digits - decimal_places:
value = u'0' * (max_digits - decimal_places - n) + value
value = '0' * (max_digits - decimal_places - n) + value
return sign + value
16 changes: 8 additions & 8 deletions djangotoolbox/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.db.models.fields.subclassing import Creator
from django.db.utils import IntegrityError
from django.db.models.fields.related import add_lazy_relation
import collections


__all__ = ('RawField', 'ListField', 'SetField', 'DictField',
Expand Down Expand Up @@ -58,15 +59,15 @@ def __init__(self, item_field=None, *args, **kwargs):

# Ensure a new object is created every time the default is
# accessed.
if default is not None and not callable(default):
if default is not None and not isinstance(default, collections.Callable):
kwargs['default'] = lambda: self._type(default)

super(AbstractIterableField, self).__init__(*args, **kwargs)

# Either use the provided item_field or a RawField.
if item_field is None:
item_field = RawField()
elif callable(item_field):
elif isinstance(item_field, collections.Callable):
item_field = item_field()
self.item_field = item_field

Expand All @@ -85,7 +86,7 @@ def contribute_to_class(self, cls, name):
if item_metaclass and issubclass(item_metaclass, models.SubfieldBase):
setattr(cls, self.name, Creator(self))

if isinstance(self.item_field, models.ForeignKey) and isinstance(self.item_field.rel.to, basestring):
if isinstance(self.item_field, models.ForeignKey) and isinstance(self.item_field.rel.to, str):
"""
If rel.to is a string because the actual class is not yet defined, look up the
actual class later. Refer to django.models.fields.related.RelatedField.contribute_to_class.
Expand Down Expand Up @@ -176,7 +177,7 @@ class ListField(AbstractIterableField):

def __init__(self, *args, **kwargs):
self.ordering = kwargs.pop('ordering', None)
if self.ordering is not None and not callable(self.ordering):
if self.ordering is not None and not isinstance(self.ordering, collections.Callable):
raise TypeError("'ordering' has to be a callable or None, "
"not of type %r." % type(self.ordering))
super(ListField, self).__init__(*args, **kwargs)
Expand Down Expand Up @@ -225,15 +226,15 @@ def get_internal_type(self):

def _map(self, function, iterable, *args, **kwargs):
return self._type((key, function(value, *args, **kwargs))
for key, value in iterable.iteritems())
for key, value in iterable.items())

def validate(self, values, model_instance):
if not isinstance(values, dict):
raise ValidationError("Value is of type %r. Should be a dict." %
type(values))


class EmbeddedModelField(models.Field):
class EmbeddedModelField(models.Field, metaclass=models.SubfieldBase):
"""
Field that allows you to embed a model instance.

Expand All @@ -245,7 +246,6 @@ class EmbeddedModelField(models.Field):
the embedded instance (not just pre_save, get_db_prep_* and
to_python).
"""
__metaclass__ = models.SubfieldBase

def __init__(self, embedded_model=None, *args, **kwargs):
self.embedded_model = embedded_model
Expand All @@ -271,7 +271,7 @@ def _set_model(self, model):
our "model" attribute in its contribute_to_class method).
"""
self._model = model
if model is not None and isinstance(self.embedded_model, basestring):
if model is not None and isinstance(self.embedded_model, str):

def _resolve_lookup(self_, resolved_model, model):
self.embedded_model = resolved_model
Expand Down
Loading