Skip to main content

Utilities for wrapping and dispatching functions

Project description

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")

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

wraptools-1.1.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

wraptools-1.1-py2.py3-none-any.whl (10.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file wraptools-1.1.tar.gz.

File metadata

  • Download URL: wraptools-1.1.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for wraptools-1.1.tar.gz
Algorithm Hash digest
SHA256 d938334821fa1d3804a5393362db4fcdcb76b4de21790ebc7afc2734f3b9366e
MD5 8ffcf036c8720ea6a20614a0e25e5365
BLAKE2b-256 c541a1945de825433b13615a37e3d16deabd891acd06534a8c904a7e7cd5e89f

See more details on using hashes here.

File details

Details for the file wraptools-1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for wraptools-1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 b9d8f8c01383863949557c756df513810b786771d51486bf64c0392e29436d26
MD5 5a3b542f0b8b05fcc97a8bba9cff29e9
BLAKE2b-256 0bb4f9193b98722bbea885863179d42a598fadcf97abc7568d41a40a9fda835c

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