No project description provided
Project description
simpleregistry
Simple registries in Python.
Right now supports only object registries, but class registries are coming in the future releases:
import dataclasses
import simpleregistry
book_registry = simpleregistry.Registry('books')
@book_registry
@dataclasses.dataclass
class Book:
isbn: int
title: str
author: str
def __hash__(self) -> int:
return hash(self.isbn)
lord_of_the_rings = Book(123, 'The Lord of the Rings', 'J. R. R. Tolkien')
assert lord_of_the_rings in book_registry
assert len(book_registry) == 1
assert book_registry.all() == {lord_of_the_rings}
assert book_registry.get(isbn=123) == lord_of_the_rings
assert book_registry.filter(author='J. R. R. Tolkien') == {lord_of_the_rings}
Works with custom types, standard-library dataclasses and Pydantic. See tests for examples.
This project is currently in Alpha status. You are welcome to use it. Code should be stable, but the API may change.
Registries and Type Constraints
By default, registries allow for limited polymorphism. This means that if you register one type with a registry, any instances of its subclasses will also be registered:
import simpleregistry
publication_registry = simpleregistry.Registry('publications')
@publication_registry
class Book:
pass
class Magazine(Book):
pass
book = Book()
magazine = Magazine()
assert book in publication_registry
assert magazine in publication_registry
If this is not desired, you can use the allow_subclasses
argument to the Registry
constructor:
import simpleregistry
publication_registry = simpleregistry.Registry('publications', allow_subclasses=False)
@publication_registry
class Book:
pass
class Magazine(Book):
pass
book = Book()
magazine = Magazine()
assert book in publication_registry
assert magazine not in publication_registry
If you want to be able to register multiple related or unrelated types,
you can use the allow_polymorphism
argument to the Registry
constructor:
import simpleregistry
publication_registry = simpleregistry.Registry('publications', allow_polymorphism=True)
@publication_registry
class Book:
pass
@publication_registry
class Magazine:
pass
book = Book()
magazine = Magazine()
assert book in publication_registry
assert magazine in publication_registry
If you want to further relax the type constraints, you can use the check_type
argument. This will allow you to force
any types of objects into the registry:
import simpleregistry
publication_registry = simpleregistry.Registry('publications', check_type=False)
@publication_registry
class Book:
pass
class Magazine:
pass
# Book will be registered automatically
book = Book()
assert book in publication_registry
# Magazine will not be registered automatically
magazine = Magazine()
assert magazine not in publication_registry
# You can, however, register it manually
publication_registry.register(magazine)
assert magazine in publication_registry
Project details
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 simpleregistry-0.2.0.tar.gz
.
File metadata
- Download URL: simpleregistry-0.2.0.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4ff9f93916b5bb2c0014aa9f1c46cc0f3c69dc1baa2fb2b44a541b7421e5411e |
|
MD5 | 71c0a079901c9eda0624d04565d46ce1 |
|
BLAKE2b-256 | 107cf7f3103367bd635a95e60355fc6ba707d42b5059c05a4802707268366ef6 |
File details
Details for the file simpleregistry-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: simpleregistry-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0d2ae5b3ab999a0a3082fa1a8b90811663930af60405ab3bfee8646b85e65be4 |
|
MD5 | d3112e5f1ab0eb1ce957204491cb6679 |
|
BLAKE2b-256 | 666d44e367a4ff151396d258b9e0a8ed34fd9ba2d5b3b4a3515002e76aee89f0 |