Skip to main content

objectfactory is a python package to easily implement the factory design pattern for object creation, serialization, and polymorphism

Project description

py-object-factory

Build Status codecov Documentation Status

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


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.1.1.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

objectfactory-0.1.1-py3-none-any.whl (19.3 kB view details)

Uploaded Python 3

File details

Details for the file objectfactory-0.1.1.tar.gz.

File metadata

  • Download URL: objectfactory-0.1.1.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.12

File hashes

Hashes for objectfactory-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9fc351142b84bf0af493d56139d43986294f5aa516327db0864485cf6cd18bd4
MD5 c37220f24f9e9ee8693b50fa5baa0e98
BLAKE2b-256 c588b96743e8c98d556a558a065571c937c3ec3e991204932f3efa8a1bb0f1be

See more details on using hashes here.

File details

Details for the file objectfactory-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for objectfactory-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 aa4282db332c53c64db72919ecb46f161051025151275d66ebe0cf12ff364006
MD5 45b2d8c4983fca3ee4933a33c65fe34d
BLAKE2b-256 cc841b216ecf5f5673f91e062ecb12a5e6844dde2afa838066e4df76b9d6c499

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page