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

Overview

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

You might also like...
🍕 A small app with capabilities ordering food and listing them with pub/sub pattern

food-ordering A small app with capabilities ordering food and listing them. Prerequisites Docker Run Tests docker-compose run --rm web ./manage.py tes

ripgrep recursively searches directories for a regex pattern while respecting your gitignore
ripgrep recursively searches directories for a regex pattern while respecting your gitignore

ripgrep (rg) ripgrep is a line-oriented search tool that recursively searches the current directory for a regex pattern. By default, ripgrep will resp

Hacktoberfest2021 🥳- Contribute Any Pattern In Any Language😎 Every PR will be accepted Pls contribute
Hacktoberfest2021 🥳- Contribute Any Pattern In Any Language😎 Every PR will be accepted Pls contribute

✨ Hacktober Fest 2021 ✨ 🙂 All Contributors are requested to star this repo and follow me for a successful merge of pull request. 🙂 👉 Add any patter

Really bad lisp implementation. Fun with pattern matching.

Lisp-py This is a horrible, ugly interpreter for a trivial lisp. Don't use it. It was written as an excuse to mess around with the new pattern matchin

Blender addon that enables exporting of xmodels from blender. Great for custom asset creation for cod games
Blender addon that enables exporting of xmodels from blender. Great for custom asset creation for cod games

Birdman's XModel Tools For Blender Greetings everyone in the custom cod community. This blender addon should finally enable exporting of custom assets

LanguageCreator - Simple library for easy creation transpilator.

LanguageCreator - Simple library for easy creation transpilator. Create transpilators in one hour! Install. Download code, rename folder to "LanguageC

In this project we will implement AirBnB clone using console

AirBnB Clone In this project we will implement AirBnB clone using console. Usage The shell should work like this

A collection of design patterns and idioms in Python (With tests!).

Python Patterns Help the project financially: Donate: https://smartlegion.github.io/donate/ Yandex Money: https://yoomoney.ru/to/4100115206129186 PayP

A test repository to build a python package and publish the package to Artifact Registry using GCB

A test repository to build a python package and publish the package to Artifact Registry using GCB. Then have the package be a dependency in a GCF function.

Comments
  • Support for nested field types

    Support for nested field types

    • allow setting class type for serializable Nested field
    • allow setting class type for nested serializable List
    • will automatically use this class if _type is not specified in dictionary
    • will throw error if conflicting _type is provided
    • bump to 0.0.3
    opened by devinaconley 2
  • 0.1.0

    0.1.0

    Merge updates for release 0.1.0

    • integrate marshmallow serialization framework
    • support typed fields
    • implement nested factory field for marshmallow
    • allow custom marshmallow schema
    • expose static factory methods directly
    • support type enforcement and hinting in factory create method
    • support required fields
    • add from_kwargs constructor to free up __init__
    • create proper abstract base classes
    • refactor and expand test suite
    • setup documentation with sphinx and read the docs
    • update travis build for new platform
    • improve documentation
    • various bug fixes
    opened by devinaconley 1
  • Refactor

    Refactor

    • break out tests in separate modules
    • break out factory and field functionality into separate modules
    • added product_orders.py example
    • added shapes.py example
    • updated README.md
    opened by devinaconley 1
  • Marshmallow integration

    Marshmallow integration

    This PR integrates marshmallow as the underlying serialization framework. This includes:

    • create new abstract base class (ABC) for objects and fields
    • dynamically define marshmallow schema from object definition
    • use schema de/serialization
    • implements enforced fields for primitive types (string, int, float, bool)
    • nested marshmallow field for nested objects and lists
    • support optional custom marshmallow schema
    • new and up[dated examples
    • refactor and expand testing
    opened by devinaconley 0
Releases(0.1.0)
  • 0.1.0(Sep 28, 2021)

    This is the first stable release of objectfactory

    Associated with PR #5

    Changes include:

    • integrate marshmallow serialization framework
    • support typed fields
    • implement nested factory field for marshmallow
    • allow custom marshmallow schema
    • expose static factory methods directly
    • support type enforcement and hinting in factory create method
    • support required fields
    • add from_kwargs constructor to free up __init__
    • create proper abstract base classes
    • refactor and expand test suite
    • setup documentation with sphinx and read the docs
    • update travis build for new platform
    • improve documentation
    • various bug fixes
    Source code(tar.gz)
    Source code(zip)
  • 0.0.3(Dec 18, 2019)

    associated with PR #3

    • allow setting class type for serializable Nested field
    • allow setting class type for nested serializable List
    • will automatically use this class if _type is not specified in dictionary
    • will throw error if conflicting _type is provided
    Source code(tar.gz)
    Source code(zip)
  • 0.0.2(Mar 21, 2019)

  • 0.0.1(Mar 16, 2019)

An awesome list of AI for art and design - resources, and popular datasets and how we may apply computer vision tasks to art and design.

Awesome AI for Art & Design An awesome list of AI for art and design - resources, and popular datasets and how we may apply computer vision tasks to a

Margaret Maynard-Reid 20 Dec 21, 2022
an elegant datasets factory

rawbuilder an elegant datasets factory Free software: MIT license Documentation: https://rawbuilder.readthedocs.io. Features Schema oriented datasets

Mina Farag 7 Nov 12, 2022
System Design Assignments as part of Arpit's System Design Masterclass

System Design Assignments The repository contains a set of problem statements around Software Architecture and System Design as conducted by Arpit's S

Relog 1.1k Jan 9, 2023
Python-Kite: Simple python code to make kite pattern

Python-Kite Simple python code to make kite pattern. Getting Started These instr

Anoint 0 Mar 22, 2022
Pattern Matching for Python 3.7+ in a simple, yet powerful, extensible manner.

Awesome Pattern Matching (apm) for Python pip install awesome-pattern-matching Simple Powerful Extensible Composable Functional Python 3.7+, PyPy3.7+

Julian Fleischer 97 Nov 3, 2022
Pampy: The Pattern Matching for Python you always dreamed of.

Pampy: Pattern Matching for Python Pampy is pretty small (150 lines), reasonably fast, and often makes your code more readable and hence easier to rea

Claudio Santini 3.5k Dec 30, 2022
Minimal, super readable string pattern matching for python.

simplematch Minimal, super readable string pattern matching for python. import simplematch simplematch.match("He* {planet}!", "Hello World!") >>> {"p

Thomas Feldmann 147 Dec 1, 2022
A library for pattern matching on symbolic expressions in Python.

MatchPy is a library for pattern matching on symbolic expressions in Python. Work in progress Installation MatchPy is available via PyPI, and

High-Performance and Automatic Computing 151 Dec 24, 2022
A repo to record how I prepare my Interview, and really hope it can help you as well. Really appreciate Kieran's help in the pattern's part.

Project Overview The purpose of this repo is to help others to find solutions and explaintion I will commit a solution and explanation to every proble

Vincent Zhenhao ZHAO 1 Nov 29, 2021
Sequence clustering and database creation using mmseqs, from local fasta files

Sequence clustering and database creation using mmseqs, from local fasta files

Ana Julia Velez Rueda 3 Oct 27, 2022