objectfactory is a python package to easily implement the factory design pattern for object creation, serialization, and polymorphism
Project description
py-object-factory
objectfactory is a python package to easily implement the factory design pattern for object creation, serialization, and polymorphism
- designed to support polymorphism
- integrates seamlessly with marshmallow and other serialization frameworks
- schema inherent in class definition
- load any object with a generic interface
- serialize objects to JSON
Example
Simple shapes example:
import objectfactory
@objectfactory.register
class Square(objectfactory.Serializable):
side = objectfactory.Field()
def get_area(self):
return self.side * self.side
@objectfactory.register
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.create(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
More examples
See more advanced examples here
Install
Use pip for installation
pip install objectfactory
Documentation
Read the full documentation at objectfactory.readthedocs.io
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.2.0b0.tar.gz
(13.5 kB
view hashes)
Built Distribution
Close
Hashes for objectfactory-0.2.0b0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 896a2465a5a4330a3eb49229845e7904c51727f98d7dcacaed74a68e03d8224d |
|
MD5 | bc2e6ce469ec5f1ccc166c4981b3e0ef |
|
BLAKE2b-256 | 3bc1bf734a9cbdbffba742df1a56ab67a62a26a50f6472e03ad6c2db55abbc7b |