a function decorator for surfacing dead code
Project description
sanka
a function decorator for surfacing dead code
Setup
Ensure nox
is installed.
pip install nox
Run nox
to install sanka
and run tests.
Usage
the basics
Decorate any funciton with @sanka
.
@sanka
def function():
pass
The decorator instance tracks function calls so the call count can be gotten as follows:
function(YaDead) #=> returns number of times `function` was called
callbacks
One can also pass a callback argument to the @sanka
decorator:
from string import Template
report = Template('Called $f called $this_many times')
callback: Callable[[int], None] = lambda tally: print(
"Ya, mon."
if tally == 0
else report.substitute(f=str(function), this_many=tally)
)
@sanka(callback=callback)
def function():
pass
The callback is expected to be a single-parameter function that accepts an int
.
This int
is the latest tally for the number of times the decorated function has been called.
To access the call count for the docorated function, just ask @sanka
if it's "dead":
function(YaDead)
and as per the example you'd get this response if the function had never been called:
Ya, mon.
callback control
By default, callbacks are only executed when YaDead
is passed.
The callback can be executed on every function call if desired.
@sanka(callback=callback, only_callback_when_dead=False)
def function():
pass
Now, every time function
is called, the callback
will also be called.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file sanka-0.1.2.tar.gz
.
File metadata
- Download URL: sanka-0.1.2.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.0 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 541a3364973c5c8ea4e287d50d2e62fdd6eb90f5aa51d9d24f60edd5124c714d |
|
MD5 | 5bec60b8bd5383a99dfc41e5e7368c8e |
|
BLAKE2b-256 | 8e8415661f463ed845dd6c6bf03aa09719563ae52ad8c69ff2117c474c3e26a2 |