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 subleq VM/interpreter created by me for no reason

What is Dumbleq? Dumbleq is a dumb Subleq VM/interpreter implementation created by me for absolutely no reason at all. What is Subleq? If you haven't

Phu Minh 2 Nov 13, 2022
Ballcone is a fast and lightweight server-side Web analytics solution.

Ballcone Ballcone is a fast and lightweight server-side Web analytics solution. It requires no JavaScript on your website. Screenshots Design Goals Si

Dmitry Ustalov 49 Dec 11, 2022
A (hopefully) considerably copious collection of classical cipher crackers

ClassicalCipherCracker A (hopefully) considerably copious collection of classical cipher crackers Written in Python3 (and run with PyPy) TODOs Write a

Stanley Zhong 2 Feb 22, 2022
Semantic Data Management - Property Graphs 📈

SDM - Lab 1 @ UPC 👨🏻‍💻 Table of contents Introduction Property Graph Dataset 1. Introduction This repo is all about what we have done in SDM lab 1

Mohammad Zain Abbas 1 Mar 20, 2022
Fast Base64 encoding/decoding in Python

Fast Base64 implementation This project is a wrapper on libbase64. It aims to provide a fast base64 implementation for base64 encoding/decoding. Insta

Matthieu Darbois 96 Dec 26, 2022
Eros is an expiremental programming language built using simple Python code.

Eros is an expiremental programming language built using simple Python code. Featuring an easy syntax and unique features like type slicing, the language remains an expirement that grows in down time

zxro 2 Nov 21, 2021
A Lego Mindstorm robot for dealing out cards based on a birds-eye view of a poker table and given ArUco fiducial tags.

A Lego Mindstorm robot for dealing out cards based on a birds-eye view of a poker table and given ArUco fiducial tags.

4 Dec 06, 2021
Why write code when you can import it directly from GitHub Copilot?

Copilot Importer Why write code when you can import it directly from GitHub Copilot? What is Copilot Importer? The copilot python module will dynamica

Mythic 41 Jan 04, 2023
The parser of a timetable of tennis matches for Flashscore website

FlashscoreParser The parser of a timetable of tennis matches for Flashscore website. The program collects the schedule of tennis matches for two days

Valendovsky 1 Jul 15, 2022
Organize seu linux - organize your linux

OrganizeLinux Organize seu linux - organize your linux Organize seu linux Uma forma rápida de separar arquivos dispersos em pastas. formatos a serem c

Marcus Vinícius Ribeiro Andrade 1 Nov 30, 2021
SEH-Helper - Binary Ninja plugin for exploring Structured Exception Handlers

SEH Helper Author: EliseZeroTwo A Binary Ninja helper for exploring structured e

Elise 74 Dec 26, 2022
Removes all archived super productivity tasks. Just run the python script.

delete-archived-sp-tasks.py Removes all archived super productivity tasks. Just run the python script. This is helpful to do a cleanup every 3-6 month

Ben Herbst 1 Jan 09, 2022
Cup Noodle Vending Maching Ordering Queue

Noodle-API Cup Noodle Vending Machine Ordering Queue Install dependencies in virtual environment python3 -m venv

Jonas Kazlauskas 1 Dec 09, 2021
Minitel 5 somewhat reverse-engineered

Minitel 5 The Minitel was a french dumb terminal with an embedded modem which had its Golden Age before the rise of Internet. Typically cubic, with an

cLx 10 Dec 28, 2022
Control your gtps with gtps-tools!

Note Please give credit to me! Do not try to sell this app, because this app is 100% open source! Do not try to reupload and rename the creator app! S

Jesen N 6 Feb 16, 2022
Autogenerador tonto de paquetes para ROSCPP

Autogenerador tonto de paquetes para ROSCPP Autogenerador de paquetes que usan C++ en ROS. Por ahora tiene las siguientes capacidades: Permite crear p

1 Nov 26, 2021
Extended functionality for Namebase past their web UI

Namebase Extended Extended functionality for Namebase past their web UI.

RunDavidMC 12 Sep 02, 2022
kurwa deska ADB

kurwa-deska-ADB kurwa-deska Запуск Linux -- python3 kurwa_deska.py Termux -- python3 kurwa_deska.py Встановлення cd kurwa_deska ADB і зразу запуск pyt

1 Jan 21, 2022
Mines all the moneys and stuff and things.

NFT Miner NFT Miner - Version 1.1.0 - Quick Fix Since the whole NFT thing started booming on Twitter it's been hard not to see one of those ugly ass m

8w8 1 Dec 13, 2021
Parser for RISC OS Font control characters in Python

RISC OS Font control parsing in Python This repository contains a class (FontControlParser) for parsing font control codes from a byte squence, in Pyt

Charles Ferguson 1 Nov 02, 2021