Reconcile the two previous commits (json/to_dict)
Also fix some to_dict move things that were incomplete. And some PEP8 fixes. closes #1060 closes #1061
This commit is contained in:
parent
1b47455225
commit
a6fbcbaec7
3 changed files with 40 additions and 16 deletions
|
|
@ -62,7 +62,8 @@ class FieldNotFound(Exception):
|
|||
"""
|
||||
def __init__(self, obj, field):
|
||||
Exception.__init__(self,
|
||||
u'Field "%s" not found for object %s' % (field, obj))
|
||||
u'Field "%s" not found for object %s' % (field, obj))
|
||||
|
||||
|
||||
class ConversionWarning(UserWarning):
|
||||
"""
|
||||
|
|
@ -71,12 +72,14 @@ class ConversionWarning(UserWarning):
|
|||
"""
|
||||
pass
|
||||
|
||||
|
||||
class AttributeCreationWarning(UserWarning):
|
||||
"""
|
||||
A non-field attribute has been created with a name not
|
||||
prefixed with a _.
|
||||
"""
|
||||
|
||||
|
||||
class NotAvailableMeta(type):
|
||||
def __str__(self):
|
||||
return unicode(self).decode('utf-8')
|
||||
|
|
@ -158,6 +161,7 @@ class Field(object):
|
|||
"""
|
||||
return value
|
||||
|
||||
|
||||
class IntField(Field):
|
||||
"""
|
||||
A field which accepts only :class:`int` and :class:`long` types.
|
||||
|
|
@ -168,6 +172,7 @@ class IntField(Field):
|
|||
def convert(self, value):
|
||||
return int(value)
|
||||
|
||||
|
||||
class DecimalField(Field):
|
||||
"""
|
||||
A field which accepts only :class:`decimal` type.
|
||||
|
|
@ -180,6 +185,7 @@ class DecimalField(Field):
|
|||
return value
|
||||
return Decimal(value)
|
||||
|
||||
|
||||
class FloatField(Field):
|
||||
"""
|
||||
A field which accepts only :class:`float` type.
|
||||
|
|
@ -190,6 +196,7 @@ class FloatField(Field):
|
|||
def convert(self, value):
|
||||
return float(value)
|
||||
|
||||
|
||||
class StringField(Field):
|
||||
"""
|
||||
A field which accepts only :class:`unicode` strings.
|
||||
|
|
@ -200,6 +207,7 @@ class StringField(Field):
|
|||
def convert(self, value):
|
||||
return to_unicode(value)
|
||||
|
||||
|
||||
class BytesField(Field):
|
||||
"""
|
||||
A field which accepts only :class:`str` strings.
|
||||
|
|
@ -212,6 +220,7 @@ class BytesField(Field):
|
|||
value = value.encode('utf-8')
|
||||
return str(value)
|
||||
|
||||
|
||||
class DateField(Field):
|
||||
"""
|
||||
A field which accepts only :class:`datetime.date` and :class:`datetime.datetime` types.
|
||||
|
|
@ -219,6 +228,7 @@ class DateField(Field):
|
|||
def __init__(self, doc, **kwargs):
|
||||
Field.__init__(self, doc, datetime.date, datetime.datetime, **kwargs)
|
||||
|
||||
|
||||
class TimeField(Field):
|
||||
"""
|
||||
A field which accepts only :class:`datetime.time` and :class:`datetime.time` types.
|
||||
|
|
@ -226,6 +236,7 @@ class TimeField(Field):
|
|||
def __init__(self, doc, **kwargs):
|
||||
Field.__init__(self, doc, datetime.time, datetime.datetime, **kwargs)
|
||||
|
||||
|
||||
class DeltaField(Field):
|
||||
"""
|
||||
A field which accepts only :class:`datetime.timedelta` type.
|
||||
|
|
@ -233,6 +244,7 @@ class DeltaField(Field):
|
|||
def __init__(self, doc, **kwargs):
|
||||
Field.__init__(self, doc, datetime.timedelta, **kwargs)
|
||||
|
||||
|
||||
class _CapBaseObjectMeta(type):
|
||||
def __new__(cls, name, bases, attrs):
|
||||
fields = [(field_name, attrs.pop(field_name)) for field_name, obj in attrs.items() if isinstance(obj, Field)]
|
||||
|
|
@ -254,6 +266,7 @@ class _CapBaseObjectMeta(type):
|
|||
new_class.__doc__ += '\n:var %s: %s' % (name, doc)
|
||||
return new_class
|
||||
|
||||
|
||||
class CapBaseObject(object):
|
||||
"""
|
||||
This is the base class for a capability object.
|
||||
|
|
@ -370,7 +383,8 @@ class CapBaseObject(object):
|
|||
# If the value was converted
|
||||
if nvalue is not value:
|
||||
warnings.warn('Value %s was converted from %s to %s' %
|
||||
(name, type(value), type(nvalue)), ConversionWarning, stacklevel=2)
|
||||
(name, type(value), type(nvalue)),
|
||||
ConversionWarning, stacklevel=2)
|
||||
value = nvalue
|
||||
except Exception:
|
||||
# error during conversion, it will probably not
|
||||
|
|
@ -399,5 +413,3 @@ class CapBaseObject(object):
|
|||
|
||||
fields_iterator = self.iter_fields()
|
||||
return OrderedDict(iter_decorate(fields_iterator))
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue