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)

A patch and keygen tools for typora.

A patch and keygen tools for typora.

Mason Shi 1.4k Apr 12, 2022
Awesome Casino is simple offline casino made on python.

Awesome-Casino Awesome Casino is simple offline casino made on python. I found bug, what can i do? If you find any bug or want to suggest any idea, al

Herman 1 Feb 04, 2022
Python template for Advent of Code event

Advent of Code Python Starter A tamplate for Advent of Code write in Python. Usage The project use poetry for project manager. Clone this repository a

Leonardo Gago 6 Dec 31, 2022
SymbLang are my programming language! Insired by the brainf**k.

SymbLang . - output as Unicode. , - input. ; - clear data. & - character that the main line start with. @value: 0 - 9 - character that the function

1 Apr 04, 2022
Dapp / Forge traces enhancer

traces-explorer Dapp / Forge traces enhancer Usage traces.py and pattern_* files should be in the same directory make test traces.txt py traces.

1 Feb 02, 2022
ticguide: quick + painless TESS observing information

ticguide: quick + painless TESS observing information Complementary to the TESS observing tool tvguide (see also WTV), which tells you if your target

Ashley Chontos 5 Nov 05, 2022
Replite - An embeddable REPL powered by JupyterLite

replite An embeddable REPL, powered by JupyterLite. Usage To embed the code cons

Jeremy Tuloup 47 Nov 09, 2022
That is a example of a Book app on Python, made with support of all JS libraries on React framework

React+Python Books App You can use this repository whenever you want Used for a video Create the database: python -m dbutils Start the web server: pyt

Koma Human 1 Apr 20, 2022
Simple python bot, that notifies about new manga chapters through Telegram.

Simple python bot, that notifies about new manga chapters through Telegram.

Dmitry Kopturov 1 Dec 05, 2021
Use Fofa、shodan、zoomeye、360quake to collect information(e.g:domain,IP,CMS,OS)同时调用Fofa、shodan、zoomeye、360quake四个网络空间测绘API完成红队信息收集

Cyberspace Map API English/中文 Development fofaAPI Completed zoomeyeAPI shodanAPI regular 360 quakeAPI Completed Difficulty APIs uses different inputs

Xc1Ym 61 Oct 08, 2022
A faster Python generator that get function results from multi-process workers

multiyield This package implements a Python generator that get function results from multi-process workers. The faster_fifo Queue (instead of the stan

Xin Du 1 Nov 18, 2021
Python implementation of the Learning Time-Series Shapelets method, that learns a shapelet-based time-series classifier with gradient descent.

shaplets Python implementation of the Learning Time-Series Shapelets method by Josif Grabocka et al., that learns a shapelet-based time-series classif

Mohamed Haseeb 187 Dec 14, 2022
Bitflip Fault Simulation Platform by Daniele Rizzieri (2021)

SEE Injection Framework 2021 This repository contains two Single Event Effect (SEE) injection platforms. The first one is called BFSP - "Bitflip Fault

Daniele Rizzieri 2 Nov 05, 2022
An app to help people apply for admissions on schools/hostels

Admission-helper About An app to help people apply for admissions on schools/hostels This app is a rewrite of Admission-helper-beta-v5.8.9 and I impor

Advik 3 Apr 24, 2022
PyPI package for scaffolding out code for decision tree models that can learn to find relationships between the attributes of an object.

Decision Tree Writer This package allows you to train a binary classification decision tree on a list of labeled dictionaries or class instances, and

2 Apr 23, 2022
TMTC Commander Core

This commander application was first developed by KSat for the SOURCE project to test the on-board software but has evolved into a more generic tool for satellite developers to perform TMTC (Telemetr

robamu 8 Dec 14, 2022
This script can be used to get unlimited Gb for WARP.

Warp-Unlimited-GB This script can be used to get unlimited Gb for WARP. How to use Change the value of the 'referrer' to warp id of yours You can down

Anix Sam Saji 1 Feb 14, 2022
Uma moeda simples e segura!

SecCoin - Documentação A SecCoin foi criada com intuito de ser uma moeda segura, de fácil investimento e mineração. A Criptomoeda está na sua primeira

Sec-Coin Team 5 Dec 09, 2022
dotfiles - Cristian Valero Abundio

In this repository you can find various configurations to configure your Linux operating system, preferably ArchLinux and its derivatives.

Cristian Valero Abundio 1 Jan 09, 2022
SWS Filters App - SWS Filters App With Python

SWS Filters App Fun 😅 ... Fun 😅 Click On photo and see 😂 😂 😂 Your Video rec

Sagar Jangid 3 Jul 07, 2022