Skip to main content

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.

Metadata

Release files for hopscotch 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hopscotch 0.3.2
File Size Uploaded Provenance Actions
hopscotch-0.3.2.tar.gz 40.2 kB Without provenance Details for hopscotch-0.3.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hopscotch 0.3.2
File Interpreter ABI Platform Actions
hopscotch-0.3.2-py3-none-any.whl Python 3 none any Details for hopscotch-0.3.2-py3-none-any.whl

Total release size: 57.4 kB

Release files / hopscotch-0.3.2.tar.gz

Download URL hopscotch-0.3.2.tar.gz
Size 40.2 kB
Tags Source
SHA-256 checksum
How to use checksums
8a8acb643817b389b0c68ac712431cc040bcd44bf2545e4ed3d3f6310ae2b515
BLAKE2b-256 checksum
How to use checksums
2426a2a60d59448e400539a55186e35c65c53323e45c6a9e0f08e5b459fe1e20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / hopscotch-0.3.2-py3-none-any.whl

Download URL hopscotch-0.3.2-py3-none-any.whl
Size 17.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dcf37d275a43d81583ea9fc33711609f5dfcd0c2a1cc7d18a650130a2b1f7f84
BLAKE2b-256 checksum
How to use checksums
6d2b17a49e3a73b68583347115ef2d580944e3665d7628ded35366f9e2dbe9d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.7

Security for hopscotch 0.3.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page