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

Uploaded Source

Built Distribution

objectfactory-0.2.0-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: objectfactory-0.2.0.tar.gz
  • Upload date:
  • Size: 14.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for objectfactory-0.2.0.tar.gz
Algorithm Hash digest
SHA256 292950c232aca5fba1ba5957fd8b17a84e147a8dc5f5d64bb5e20320ffea38a3
MD5 1c0737b0167da9f504f4cad127296e36
BLAKE2b-256 f54277b6375afa5ca7fa46e5b67c450c6744fbe9bb6da49535c7d6ee5e555023

See more details on using hashes here.

Provenance

The following attestation bundles were made for objectfactory-0.2.0.tar.gz:

Publisher: publish.yml on devinaconley/py-object-factory

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: objectfactory-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for objectfactory-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b54a9bc59b5e7fcf8150c4fc7c66940ecd4b54da13332540afe75ec10c35b9e3
MD5 6df5cbf064e737c6893af752a4973648
BLAKE2b-256 95335657fcafa7584f508b481ad64e697a018284d780a5f2c7ebf35926375633

See more details on using hashes here.

Provenance

The following attestation bundles were made for objectfactory-0.2.0-py3-none-any.whl:

Publisher: publish.yml on devinaconley/py-object-factory

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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