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}
assert book_registry.exclude(author='J. R. R. Tolkien') == set()
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
Integrations
Pydantic
Pydantic models are supported out of the box, provided you make them hashable.
One way of doing that is by using the frozen
option:
import pydantic
import simpleregistry
publication_registry = simpleregistry.Registry('publications', check_type=False)
@publication_registry
class Book(pydantic.BaseModel):
isbn: int
title: str
class Config:
frozen = True
book = Book(isbn=123, title='The Lord of the Rings')
assert book in publication_registry
Another option is to write a custom hash function, which can for example return the int value of the primary key:
import pydantic
import simpleregistry
publication_registry = simpleregistry.Registry('publications', check_type=False)
@publication_registry
class Book(pydantic.BaseModel):
isbn: int
title: str
def __hash__(self) -> int:
return self.isbn
book = Book(isbn=123, title='The Lord of the Rings')
assert book 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.1.tar.gz
.
File metadata
- Download URL: simpleregistry-0.2.1.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98256797524117da2cd87dcaf2bced64aef1749223a8c9987868c79fa72acbd0 |
|
MD5 | 5f1c675ccaa4f41efb28a6f9325e49cd |
|
BLAKE2b-256 | d28adf57fe8e7e3e35c77d64960145459718180398604110a0137735ab16f295 |
File details
Details for the file simpleregistry-0.2.1-py3-none-any.whl
.
File metadata
- Download URL: simpleregistry-0.2.1-py3-none-any.whl
- Upload date:
- Size: 5.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a139b53cbb00af357f48ba04565f14f441ebc170e25f23dfc833e9057fba3bc6 |
|
MD5 | dbdbfdc68aa52df6505c8e7ebd4855f9 |
|
BLAKE2b-256 | 39a9636f63543c2bd8b2082c632e27def0ca52c0c1770cfcd490c2febdeb65ec |