Skip to main content

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

simpleregistry-0.2.2.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simpleregistry-0.2.2-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file simpleregistry-0.2.2.tar.gz.

File metadata

  • Download URL: simpleregistry-0.2.2.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for simpleregistry-0.2.2.tar.gz
Algorithm Hash digest
SHA256 8897e3a2e93e400f98d1d97e3e28cb40985afac956d14814b84c3b042f107179
MD5 eada089b3ffa8ea3e66802ff58fa73cc
BLAKE2b-256 15bbf571e6a52f555423cc62d0d61342ecae257feb9b26a1842aae4b488d9cb3

See more details on using hashes here.

Provenance

The following attestation bundles were made for simpleregistry-0.2.2.tar.gz:

Publisher: pypi_upload.yml on piotrkilczuk/simpleregistry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file simpleregistry-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: simpleregistry-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for simpleregistry-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 12c11ca46acccb5ae234591aa345b0cc8428d404a6d41d2be2d1838013ec1644
MD5 d7c0cd85989caa86d81cfc46b957f208
BLAKE2b-256 7a0696149b1bcfc03838ce27863997860e2a76c6dcb5875641cd597e9992c7cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for simpleregistry-0.2.2-py3-none-any.whl:

Publisher: pypi_upload.yml on piotrkilczuk/simpleregistry

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page