Skip to main content

pydefer

pydefer aims to emulate the same experience you get from using Golang's defer keyword. pydefer allows you to execute a function around a given wrapped function after the wrapped function has executed.

Installing

$ pip install pydefer

or

$ pipenv install pydefer

Usage

Here's a basic example of using defer to print the sequence of numbers between [6,10] after print_sequence is called (which calls the sequence of numbers between [1,5]).

>>> from pydefer.defer import defer
>>>
>>> @defer(print,6,7,8,9,10)
... def print_sequence():
...     print(1,2,3,4,5)
...
>>> print_seqeuence()
1 2 3 4 5
6 7 8 9 10

Some more complex use cases of pydefer is to be able to close files and not have to worry about closing them yourself in the actual function. Another use case is possibly session management for a web app after a user visits different routes.

Here's the aforementioned more complex example of deferring of closing a file.

from python.defer import defer

@defer(open('employee_data.txt','r').close)
def update_employee(e_id):
    f = open('employee_data.txt','w')
    # DO STUFF TO FILE

    f.write(e_id.salary)

    # DO MORE STUFF LATER ON

In the above example, you do not need to manage the file anywhere inside the function as the defer function wrapper will close that file for you after the update_employee function has finished.

Release files for pydefer 0.0.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 pydefer 0.0.2
File Size Uploaded
pydefer-0.0.2.tar.gz 2.4 kB Details

Built distribution (wheel)

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

Total release size: 5.9 kB

Release files / pydefer-0.0.2.tar.gz

Download URL pydefer-0.0.2.tar.gz
Size 2.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7fd71e5567c643542d0e92f43a9500ad99f44979d82786436f7f58e150b557ad
BLAKE2b-256 checksum
How to use checksums
089d8a3eb93ed90e1ebc9886aa708af95d2b4b63a1c61b0c7d23ee078710ceba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.5

Release files / pydefer-0.0.2-py3-none-any.whl

Download URL pydefer-0.0.2-py3-none-any.whl
Size 3.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2b4e8c810be4cc80aa2a4f494142d746afd67c5f6fbb2c8d929370b9c709c6be
BLAKE2b-256 checksum
How to use checksums
608e931ca84f3503605860da3e5daaa7db261bc8cacf4e9534385dc452d5357e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.5

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files

0.0.1

2 release files

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