Skip to main content

Decorator utility that operates on black magic

Project description

Collection of metaprogramming modules that operate on black magic!

Currently the only available module is:

  • black_magic.decorator

License

To the extent possible under law, Thomas Gläßle has waived all copyright and related or neighboring rights to black-magic. This work is published from: Germany.

To the extent possible under law, the person who associated CC0 with black-magic has waived all copyright and related or neighboring rights to black-magic.

You should have received a copy of the CC0 legalcode along with this work. If not, see http://creativecommons.org/publicdomain/zero/1.0/.

black_magic.decorator

This is intended to become a more modern and flexible replacement for the the well known decorator module. This module benefits an API for more flexible usage. The behaviour of the decorator module can easily be duplicated.

Usage

You can use it just like the standard functools.wraps function:

>>> from black_magic.decorator import wraps

>>> x = []
>>> def real(a:int, b=x) -> "Returns b":
...     return b

>>> @wraps(real)
... def fake(*args, **kwargs):
...     print("Fake!")
...     return real(*args, **kwargs)

This will not only update the docstring of the fake function to look like the original but generate a wrapper function that has exactly the same signature:

>>> assert fake(1) is x   # check object-identity of default-parameter!
Fake!

>>> from inspect import getfullargspec
>>> assert getfullargspec(fake) == getfullargspec(real)

If you want to get real crazy you can even use ast exprs:

>>> import ast
>>> fake = wraps(real)(ast.Num(n=1))
>>> fake(0)
1

Under the hood

Q: This uses ugly eval code, right?

A: No, it uses abstract syntax trees to do its dynamic code generation.

Tests

This module has been tested to work on python{2.6, 2.7, 3.2, 3.3} and PyPy1.9 using Travis.

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

black-magic-0.0.3.tar.gz (10.3 kB view details)

Uploaded Source

File details

Details for the file black-magic-0.0.3.tar.gz.

File metadata

  • Download URL: black-magic-0.0.3.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for black-magic-0.0.3.tar.gz
Algorithm Hash digest
SHA256 fe2683ad3dc1e97ad340210560164b01c9a8cfe16f6fd4c20d3e61e31818993f
MD5 21ad5d94d2117c4142d010ce2677c468
BLAKE2b-256 d0bdf5d24681cf671a7b6759dd228927976961a681afa165f50330d4bd3d7513

See more details on using hashes here.

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