Skip to main content

Utilities for wrapping and dispatching functions.

Supported Python versions:

  • 2.7

  • 3.4

  • 3.5

pluggable

pluggable will take some decorators and simply apply it. But applied decorators can be purged by purge_pluggable_decorators.

from functools import lru_cache
from wraptools.pluggable import pluggable

@pluggable(
    lru_cache,
)
def get_data():
    """ Some function return data from RDB or so. """

purge_pluggable_decorators is useful on tests.

from wraptools.pluggable import purge_pluggable_decorators

    @purge_pluggable_decorators
    def test__get_data(self):
         always_flesh_data = get_data()

Or it can be also used as a context manager.

with purge_pluggable_decorators():
    always_flesh_data = get_data()

pluggable decorator can be useful for view functions of Web Frameworks, like Django.

@pluggable(
    login_required,
)
def profile_view(request):
    return TemplateResponse(request, "profile.html")

dispatch

dispatch will create function by some condition and functions. Following example means

  • When request is authenticated, dashboard function will be called.

  • Other cases, landing_page function will be called.

from wraptools.dispatch import dispatch
from wraptools.contrib.django.dispatchers import is_authenticated


def landing_page(request):
    return TemplateResponse(request, "landing.html")


def dashboard(request):
    return TemplateResponse(request, "dashboard.html")


top_view = dispatch(
    (is_authenticated, dashboard),
    default=landing_page,
)

Combine conditions

In this case dispatch is used for Django’s view, but it can be used generic Python functions.

dispatcher functions can be combined and inverted.

  • To create and condition, just combine dispatchers by &

  • or condition, by |

  • not condition, by ~

from wraptools.contrib.django.dispatchers import is_authenticated, method_get

top = dispatch(
    (is_authenticated & method_get, dashboard_get),
    (is_authenticated & (method_post | method_put), dashboard_post),
    (~is_authenticated, landing),
)

Create own dispatcher

Basically dispatcher is just a function to get same arguments with dispatched functions and return bool values.

def is_even(num):
    return num % 2 == 0

def echo(num):
    return num

dispatch(
    (is_even, echo),
    ...
)

But by using dispatcher decorator, your dispatcher functions will be able to be combined and inverted by &, |, or ~

from wraptools.dispatch import dispatcher

dispatcher
def is_even(num):
    return num % 2 == 0

dispatch(
    (~is_even, ...),  # It will be called when the value is odd (not even).
    ...
)

context

context is a decorator which injects additional arguments to wrapped functions. Following example is to separate logic to get user object into get_user context function, and applying it for profile_page view by context decorator.

from wraptools.context import context


def get_user(user_id):
    return get_object_or_404(User, id=user_id)


@context(
    get_user,
)
def profile_page(request, user_id, user):
    return TemplateResponse(request, "profile.html")

Metadata

Release files for wraptools 1.0

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

Source distribution (sdist)

Source distribution for wraptools 1.0
File Size Uploaded Provenance Actions
wraptools-1.0.tar.gz 5.3 kB Without provenance Details for wraptools-1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for wraptools 1.0
File Interpreter ABI Platform Actions
wraptools-1.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for wraptools-1.0-py2.py3-none-any.whl

Total release size: 15.5 kB

Release files / wraptools-1.0.tar.gz

Download URL wraptools-1.0.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d6e0a0a8d96ba6814b242b4aee792526bb98c683ee31ae623caea1476c7550e5
BLAKE2b-256 checksum
How to use checksums
f65edb04e19d8ebd9863087a460495f4a201bd0f56a20569b0d8e3619f487d0d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / wraptools-1.0-py2.py3-none-any.whl

Download URL wraptools-1.0-py2.py3-none-any.whl
Size 10.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
646f7c339a4cd97c14e5f7f9c6ac87adb17911b6652b716525949649c59f7342
BLAKE2b-256 checksum
How to use checksums
1b661608257162fe965b33c0d8eebbefc402d23c0b42fa048c63ab94b00f64e7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for wraptools 1.0

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.

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files No provenance

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