A python package for the serializable model / factory pattern
Project description
py-object-factory
objectfactory is a python package to easily implement the factory design pattern for object creation and serialization
- serialization schema inherent in class definition
- consistent interface to load many objects of arbitrary type
- serialize object to human-readable JSON format
Example
Simple shapes example:
import objectfactory
@objectfactory.Factory.register_class
class Square( objectfactory.Serializable ):
side = objectfactory.Field()
def get_area( self ):
return self.side * self.side
@objectfactory.Factory.register_class
class Triangle( objectfactory.Serializable ):
base = objectfactory.Field()
height = objectfactory.Field()
def get_area( self ):
return 0.5 * self.base * self.height
serialized_data = [
{"_type": "Square", "side": 2.0},
{"_type": "Triangle", "base": 1.75, "height": 2.50},
{"_type": "Square", "side": 1.5},
]
for data in serialized_data:
shape = objectfactory.Factory.create_object( data )
print( 'class type: {}, shape area: {}'.format( type( shape ), shape.get_area() ) )
Output:
class type: <class '__main__.Square'>, shape area: 4.0
class type: <class '__main__.Triangle'>, shape area: 2.1875
class type: <class '__main__.Square'>, shape area: 2.25
See more examples here
Install
Use pip for installation
pip install objectfactory
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
objectfactory-0.0.2.tar.gz
(5.8 kB
view hashes)
Built Distribution
Close
Hashes for objectfactory-0.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b4137d2ca5cce9f433f4289afb460e622f86625a73e9410daa3e844ee2161ee |
|
MD5 | a4186747405700c00a73112567d49bbf |
|
BLAKE2b-256 | 9aab46485580e4d39da3dd7dec2ba9a3bd75a157861423883b0a3d4709dfeb20 |