Skip to main content

Type-oriented registry with dependency injection.

Project description

Hopscotch

Coverage Status Documentation Status Code style: black PyPI Python Version PyPI - Downloads License Test Status pre-commit black

Writing a decoupled application -- a "pluggable app" -- in Python is a common practice. Looking for a modern registry that scales from simple use, up to rich dependency injection (DI)? hopscotch is a registry and DI package for Python 3.9+, written to support research into component-driven development for Python's web story.

I expect a lot of skepticism.
In fact, I don't expect a lot of adoption.
Instead, I'm using this to learn and write articles.

Features

  • Simple to complex. The easy stuff for a simple registry is easy, but rich, replaceable systems are in scope also.
  • Better DX. Improve developer experience through deep embrace of static analysis and usage of symbols instead of magic names.
  • Hierarchical. A cascade of parent registries helps model request lifecycles.
  • Tested and documented. High test coverage and quality docs with lots of (tested) examples.- Extensible.
  • Great with components. When used with viewdom, everything is wired up and you can just work in templates.

Hopscotch takes its history from wired, which came from Pyramid, which came from Zope.

Requirements

  • Python 3.9+.
  • venusian (for decorators)

Installation

You can install Hopscotch via pip from PyPI:

$ pip install hopscotch

Quick Examples

Let's look at: a hello world, same but with a decorator, replacement, and multiple choice.

Here's a registry with one "kind of thing" in it:

# One kind of thing
@dataclass
class Greeter:
    """A simple greeter."""

    greeting: str = "Hello!"


registry = Registry()
registry.register(Greeter)
# Later
greeter = registry.get(Greeter)
# greeter.greeting == "Hello!"

That's manual registration -- let's try with a decorator:

@injectable()
@dataclass
class Greeter:
    """A simple greeter."""

    greeting: str = "Hello!"


registry = Registry()
registry.scan()
# Later
greeter = registry.get(Greeter)
# greeter.greeting == "Hello!"

You're building a pluggable app where people can replace builtins:

# Some site might want to change a built-in.
@injectable(kind=Greeter)
@dataclass
class CustomGreeter:
    """Provide a different ``Greeter`` in this site."""

    greeting: str = "Howdy!"

Sometimes you want a Greeter but sometimes you want a FrenchGreeter -- for example, based on the row of data a request is processing:

@injectable(kind=Greeter, context=FrenchCustomer)
@dataclass
class FrenchGreeter:
    """Provide a different ``Greeter`` in this site."""

    greeting: str = "Bonjour!"

# Much later
child_registry = Registry(
    parent=parent_registry,
    context=french_customer
)
greeter2 = child_registry.get(Greeter)
# greeter2.greeting == "Bonjour!"

Finally, have your data constructed for you in rich ways, including custom field "operators":

@injectable()
@dataclass
class SiteConfig:
    punctuation: str = "!"


@injectable()
@dataclass
class Greeter:
    """A simple greeter."""

    punctuation: str = get(SiteConfig, attr="punctuation")
    greeting: str = "Hello"

    def greet(self) -> str:
        """Provide a greeting."""
        return f"{self.greeting}{self.punctuation}"

The full code for these examples are in the docs, with more explanation (and many more examples.)

And don't worry, dataclasses aren't required. Some support is available for plain-old classes, NamedTuple, and even functions.

Contributing

Contributions are very welcome. To learn more, see the contributor's guide.

License

Distributed under the terms of the MIT license, Hopscotch is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This project was generated from @cjolowicz's Hypermodern Python Cookiecutter template.

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

hopscotch-0.2.0.tar.gz (15.6 kB view details)

Uploaded Source

Built Distribution

hopscotch-0.2.0-py3-none-any.whl (16.2 kB view details)

Uploaded Python 3

File details

Details for the file hopscotch-0.2.0.tar.gz.

File metadata

  • Download URL: hopscotch-0.2.0.tar.gz
  • Upload date:
  • Size: 15.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for hopscotch-0.2.0.tar.gz
Algorithm Hash digest
SHA256 6dc933c100dc57a77cc3a6dacc2a5a0fab39c98d817884d210d832d50d556834
MD5 748f5e9ba45146f05cb3d7305d7fae22
BLAKE2b-256 9818eb92a2c05879ad81955ed14ba35ff989f9646ce4e4d9ae46a8fec3b1fc06

See more details on using hashes here.

File details

Details for the file hopscotch-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: hopscotch-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 16.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10

File hashes

Hashes for hopscotch-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e1beb79df65b60d1fba6540a2905e32996ea7c6502a1a318a71bc82b5d2cff08
MD5 0606e0d03d45a62ccc804f2bf9b70248
BLAKE2b-256 7b4d7687c914d35197d269520526713bf4c20a76b1e22a41fa379ce55fc7efb2

See more details on using hashes here.

Supported by

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