Announcing EFL’s Second Open-Source Library

EFL Technology Blog

This week, EFL announced its commitment to participate in Open Source Friday, and I’m excited to share the results with you!

Say hello to ClassRegistry! ClassRegistry is a library that makes it simple to create global registries and factories for common classes and services in your applications.

  • 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!

The entry points integration is particularly interesting, and we will cover it in more detail in a future release of the Filters library.

So what does a ClassRegistry look like in practice? Let’s take a look at a simple example.

from abc import abstractmethod
from class_registry import AutoRegister, ClassRegistry
from six import with_metaclass

# Create a registry.
pokedex = ClassRegistry('element')

# Define a base class and configure it to register
# subclasses automatically.
class Pokemon(with_metaclass(AutoRegister(pokedex))):
  def get_abilities(self):
    raise NotImplementedError()

# Any non-abstract classes that extend ``Pokemon`` will be
# automatically registered in our Pokédex!
class Charizard(Pokemon):
  element = 'fire'

  def get_abilities(self):
    return ['blaze', 'solar_power']

class Blastoise(Pokemon):
  element = 'water'

  def __init__(self, name=None):
    super(Blastoise, self).__init__() = name

  def get_abilities(self):
    return ['torrent', 'rain_dish']

# Create new instances from our registry!
sparky = pokedex['fire']

# Provide arguments to the factory!
chauncy = pokedex.get('water', name='Chauncy')

Once a class is registered, it can be referenced using its corresponding registry key. This makes it ideal for customizing application behavior based on user input, configuration files, database values and more!

There’s a lot more you can do with ClassRegistry than we can show in a single blog post! Head on over to ReadTheDocs to learn about what this library can do for your applications!

Phoenix Zerin is a Senior Software Architect at EFL.