A library for mapping dictionaries to Python objects
Project description
DictObject
Subclassing this type you can declare your object model. DictObject itself is dict subclass so you can access object properties as attribute or as an item:
class Foo(dicty.DictObject): foo = dicty.Field() obj = Foo(foo='bar') obj.foo # 'bar' obj['foo'] # 'bar'
Object constructor accepts properties as keyword arguments, or you can create instance with fromjson() classmethod that takes object dictionary (e.g. result json.loads()) as an argument:
obj = Foo(foo='bar') obj = Foo.fromjson({'foo': 'bar'})
You can pass DictObject instance directly to json library or call jsonize() method first that will return plain dict version of your object with only declared fields left:
obj = Foo.fromjson({'foo': 123, 'bar': 123}) obj == {'foo': 123, 'bar': 123} obj.jsonize() == {'foo': 123}
Name aliasing
If you don’t like naming scheme in JSON objects, API and so on. Dicty allows to choose whatever python name you like while manually specify dictionary key. For instance map camel-case keys to their underscore counterparts:
class Foo(dicty.DictObject): prop_foo = dicty.Field('propFoo') obj = Foo(prop_foo=123) obj = Foo.fromjson({'propFoo': 123}) obj.prop_foo obj['propFoo']
Subclassing
DictObject supports subclassing:
class Foo(dicty.DictObject): foo = dicty.Field() class Bar(Foo): bar = dicty.Field() obj = Bar.fromjson({'foo': 1, 'bar': 2}) print obj.jsonize() # {'foo': 1, 'bar': 2}
Mixins are supported as well:
class FooMixIn(object): foo = dicty.Field() class Bar(dicty.DictObject, FooMixIn): bar = dicty.Field() obj = Bar.fromjson({'foo': 1, 'bar': 2}) print obj.jsonize() # {'foo': 1, 'bar': 2}
Fields
dicty.Field is baseclass for all dicty fields. You can use itself directly to declare a field with no special type info.
Optional fields and default values
Accessing field that is not set will lead to AttributeError: You can specify default value for your field:
class Foo(dicty.DictObject): foo = dicty.Field() obj = Foo() obj.foo # raises AttributeError
You can mark field as optional, in this case None will be returned if it was not set before:
class Foo(dicty.DictObject): foo = dicty.Field(optional=True) obj = Foo() obj.foo # None
For optional fields you can specify default value other than None with default argument:
class Foo(dicty.DictObject): foo = dicty.Field(optional=True, default=123) obj = Foo() obj.foo # 123 obj == {}
Please note that default value does not affect internal dictionary. But if default value is NOT hashable dict key will be set on getattr access.
There is also an option to suply default_func it’s get default value for object’s field. It takes object instance as an argument. Value returned by default_func is always stored in dict:
class Foo(dicty.DictObject): id = dicty.Field(optional=True, default_func=lambda obj: uuid.uuid4().hex) obj = Foo() obj == {} obj.id # Would be populated with newly generated UUID obj == {'id': '07d0af8affaf46c885cc251e17dbc37a'}
Available Fields
Dicty is shipped with the follwing:
BooleanField
DateField
DatetimeField
DictField
FloatField
IntegerField
ListField
NativeDateField
NativeDatetimeField
NumberField
RegexpStringField
StringField
TypedDictField
TypedListField
TypedObjectField
Sample usage
With dicty you can easily describe your data model and then use it to encode/decode JSON objects. It supports data validataion, optional parameters, default values, nested objects and so on.
import dicty class MyDoc(dicty.DictObject): prop1 = dicty.StringField() prop2 = dicty.IntegerField() # Regular constructor doc = MyDoc(prop1='foo', prop2=123) print doc.prop1 # you can access values as attributes print doc['prop2'] # as well as dictionary items print json.dumps(doc) print json.dumps(doc.jsonify()) # Jsonify will clean and validate output data # Create instance from dictionary doc = MyDoc.fromjson({'prop1': 'foo', 'prop2': 123}) # would raise dicty.FieldError here doc = MyDoc.fromjson({'prop1': 123, 'prop2': 123})
Nested Objects
import dicty class Foo(dicty.DictObject): class Bar(dicty.DictObject): prop = dicty.StringField() bar = dicty.TypedObjectField(Bar) obj = Foo() obj.bar.prop = 123 print obj # {'bar': {'prop': 123}}
Mongo-style key pathes
Dicty allows to build key pathes that can be used to create mongo query:
class Foo(dicty.DictObject): bar = dicty.Field('myBar') print Foo.bar # 'myBar' full path to the item print Foo.bar.key # 'myBar' only leaf key print Foo.bar.attname # 'bar' python attribute name
Nested object:
class Bar(dicty.DictObject): foo = dicty.TypedObjectField(Foo) print Bar.foo # 'foo' print Bar.foo.bar # 'foo.myBar'
List of objects:
class Bar(dicty.DictObject): items = dicty.TypedListField(Foo) print Bar.items.foo # 'items.myBar' without index print Bar.items[0].foo # 'items.0.myBar' indexed path
Dict of objects:
class Bar(dicty.DictObject): items = dicty.TypedDictField(Foo) # With index print Bar.items['maurice'].bar # 'items.maurice.myBar' # Would raise IndexError print Bar.items['x.y'].bar
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file dicty-0.1.1.tar.gz
.
File metadata
- Download URL: dicty-0.1.1.tar.gz
- Upload date:
- Size: 6.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32302f627e9c4990031bc20cb364b139b6f384da3147c77c3165a3cacc71545e |
|
MD5 | cd2f2fa7a0f4c703db01325950bb9bae |
|
BLAKE2b-256 | 7e17f25df974c6fae8cb86f7e57eedf01c9b60ce47b51f094aa25856a0acee60 |