Registration management and lookup of objects, for extensible Python applications.
Project description
Registries for Python
Registries is a module for managing collections of objects, allowing for easy registration, lookups, and access.
This is ideal for applications that need to manage abstract interfaces with backend implementations, such as plugins, extensions, and other cases where you'd employ a Registry Pattern.
Registries was built to help develop Review Board, our premier code review product from Beanbag, and we're making it available for use in other projects.
What is the Registry Pattern?
The Registry Pattern is a design pattern that provides a central repository for storing and accessing instances of various classes or objects. Objects can be registered dynamically or as part of the registry definition, each with a unique identifier, and then retrieved later by iterating through or querying the registry.
This can help provide a more decoupled design, reducing the need for dependencies between parts of your codebase, and helping make your codebase more extensible, maintainable, and testable.
Registries for Python makes this simple.
Features
Registries offers:
- Dynamic registration and unregistration of items.
- Flexible lookups based on registered lookup attributes.
- Thread-safety to avoid conflicts with modifying a registry.
- Extensibility through pre/post hooks for registration, unregistration, initial population, and resetting of registries.
- Python type hints to ease development.
- Built-in registries for order-dependent items and wrapping around Python Entry Points.
Installation
To install Registries, run:
$ pip install registries
Registries follows semantic versioning, meaning no surprises when you upgrade.
Example Usage
Here's a quick example to get you started with Registries:
from typing import Type
from registries import Registry
from registries.errors import RegistrationConflictError
# Let's create a base class for our objects and some default items.
class BaseItem:
my_item_id: str
class FooItem(BaseItem):
my_item_id = 'fooitem'
class BarItem(BaseItem):
my_item_id = 'baritem'
# Now we'll create our registry with those items as defaults.
class MyRegistry(Registry[Type[BaseItem]]):
lookup_attrs = ('my_item_id',)
def get_defaults(self):
return [FooItem, BarItem]
# Set up our registry.
registry = MyRegistry()
# This will print FooItem and BarItem.
for item in registry:
print(item)
# Let's register a new item.
class BazItem(BaseItem):
my_item_id = 'bazitem'
registry.register(BazItem)
# BazItem will now be in the registry.
assert BazItem in registry
for item in registry:
print(item)
# Let's look up some objects.
assert registry.get('fooitem') is FooItem
assert registry.get('bazitem') is BazItem
# We can also look up by explicit attributes, in case the registry supports
# multiple lookup attributes.
assert registry.get(my_item_id='baritem') is BarItem
# Registering another item with the same ID will raise a
# RegistrationConflictError.
class ConflictItem(BaseItem):
my_item_id = 'fooitem'
try:
registry.register(ConflictItem)
except RegistrationConflictError:
print('ConflictItem conflicts with FooItem!')
# Let's unregister FooItem.
registry.unregister(FooItem)
assert FooItem not in registry
# And now we can register ConflictItem!
registry.register(ConflictItem)
assert registry.get(my_item_id='fooitem') is ConflictItem
This is just a quick overview of how you might work with a registry. There's a lot more that can be done, including:
- Custom registry-defined error messages and exception classes.
- Multi-attribute registration and lookup.
- Hooks for customization the registration and lookup process, to tie into other pieces of your architecture.
All thread-safe, all in your control.
License
Registries is available under the MIT license.
Contributing
Contributions to Registries can be made on our Review Board server at https://reviews.reviewboard.org/.
To post a change for review:
-
Download RBTools:
$ pip install rbtools
-
Create a branch in your Git clone and make your changes.
-
Post the change for review:
$ rbt post
To update your change:
$ rbt post -u
Our Other Projects
-
Review Board - Our open source, extensible code review, document review, and image review tool.
-
Djblets - Our pack of Django utilities for datagrids, API, extensions, and more. Used by Review Board.
-
Housekeeping - Deprecation management for Python modules, classes, functions, and attributes.
-
kgb - A powerful function spy implementation to help write Python unit tests.
-
Typelets - Type hints and utility objects for Python and Django projects.
You can see more on github.com/beanbaginc and github.com/reviewboard.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file registries-1.0.tar.gz
.
File metadata
- Download URL: registries-1.0.tar.gz
- Upload date:
- Size: 18.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84568a75a8f1162c9452ea1dee53c750eb6eacfa7a617d8646767c486c0d912e |
|
MD5 | 76789a8d80114dac1c5e13d92aae2d6a |
|
BLAKE2b-256 | e96fa603833c345d76be18aaf1b1726d364f83d3caac4d12678ecda5fd851318 |
File details
Details for the file registries-1.0-py3-none-any.whl
.
File metadata
- Download URL: registries-1.0-py3-none-any.whl
- Upload date:
- Size: 18.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fb2136d4af1788a725ebfd1e889abd36428bd2947bc9a77b06ae47e88c5a14b |
|
MD5 | c0ed8be2e8714bc9ee0968c190bbaf59 |
|
BLAKE2b-256 | fb21dbda6a03950ae5f5918153ef61f5e4751ed0324f9d371be57aa059cf16ea |