Coder Social home page Coder Social logo

class-registry's Introduction

image

image

ClassRegistry

At the intersection of the Registry and Factory patterns lies the ClassRegistry:

  • Define global factories that generate new class instances based on configurable keys.
  • Seamlessly create powerful service registries.
  • Integrate with setuptools's entry_points system to make your registries infinitely extensible by 3rd-party libraries!
  • And more!

Getting Started

Create a registry using the class_registry.ClassRegistry class, then decorate any classes that you wish to register with its register method:

from class_registry import ClassRegistry

pokedex = ClassRegistry()

@pokedex.register('fire')
class Charizard(Pokemon):
  ...

@pokedex.register('grass')
class Bulbasaur(Pokemon):
  ...

@pokedex.register('water')
class Squirtle(Pokemon):
  ...

To create a class instance from a registry, use the subscript operator:

# Charizard, I choose you!
fighter1 = pokedex['fire']

# CHARIZARD fainted!
# How come my rival always picks the type that my pokémon is weak against??
fighter2 = pokedex['grass']

Advanced Usage

There's a whole lot more you can do with ClassRegistry, including:

  • Provide args and kwargs to new class instances.
  • Automatically register non-abstract classes.
  • Integrate with setuptools's entry_points system so that 3rd-party libraries can add their own classes to your registries.
  • Wrap your registry in an instance cache to create a service registry.
  • And more!

For more advanced usage, check out the documentation on RTD!

Requirements

ClassRegistry is compatible with Python versions 3.6, 3.5 and 2.7.

Installation

Install the latest stable version via pip:

pip install class-registry

class-registry's People

Contributors

todofixthis avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

class-registry's Issues

code break with Python 3.7

Trying to test my code with multiple versions of python. Python 3.5 and 3.6 works fine, but on 3.7 it breaks

=================================== ERRORS ====================================
__________________ ERROR collecting tests/test_config_load.py __________________
tests/test_config_load.py:1: in
from undercommit.config.config_loader import ConfigurationLoader
undercommit/config/config_loader.py:5: in
from undercommit.config.config import Configuration
undercommit/config/config.py:4: in
from class_registry.entry_points import EntryPointClassRegistry
.eggs/class_registry-2.1.2-py3.7.egg/class_registry/init.py:5: in
from .registry import *
.eggs/class_registry-2.1.2-py3.7.egg/class_registry/registry.py:33: in
class BaseRegistry(with_metaclass(ABCMeta, Mapping)):
.eggs/six-1.12.0-py3.7.egg/six.py:827: in new
return meta(name, bases, d)
/usr/local/lib/python3.7/abc.py:126: in new
cls = super().new(mcls, name, bases, namespace, **kwargs)
E TypeError: type() doesn't support MRO entry resolution; use types.new_class()
________________ ERROR collecting tests/test_config_matching.py ________________
tests/test_config_matching.py:1: in
from undercommit.config.config import Configuration
undercommit/config/config.py:4: in
from class_registry.entry_points import EntryPointClassRegistry
.eggs/class_registry-2.1.2-py3.7.egg/class_registry/init.py:5: in
from .registry import *
.eggs/class_registry-2.1.2-py3.7.egg/class_registry/registry.py:33: in
class BaseRegistry(with_metaclass(ABCMeta, Mapping)):
.eggs/six-1.12.0-py3.7.egg/six.py:827: in new
return meta(name, bases, d)
/usr/local/lib/python3.7/abc.py:126: in new
cls = super().new(mcls, name, bases, namespace, **kwargs)
E TypeError: type() doesn't support MRO entry resolution; use types.new_class()
_________________ ERROR collecting tests/test_config_merger.py _________________
tests/test_config_merger.py:1: in
from undercommit.config.config import Configuration
undercommit/config/config.py:4: in
from class_registry.entry_points import EntryPointClassRegistry
.eggs/class_registry-2.1.2-py3.7.egg/class_registry/init.py:5: in
from .registry import *
.eggs/class_registry-2.1.2-py3.7.egg/class_registry/registry.py:33: in
class BaseRegistry(with_metaclass(ABCMeta, Mapping)):
.eggs/six-1.12.0-py3.7.egg/six.py:827: in new
return meta(name, bases, d)
/usr/local/lib/python3.7/abc.py:126: in new
cls = super().new(mcls, name, bases, namespace, **kwargs)
E TypeError: type() doesn't support MRO entry resolution; use types.new_class()
!!!!!!!!!!!!!!!!!!! Interrupted: 3 errors during collection !!!!!!!!!!!!!!!!!!!!
=========================== 3 error in 0.41 seconds ============================

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.