Skip to main content

Implements a Go-like defer function.

Project description

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.

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

pydefer-0.0.2.tar.gz (2.4 kB view details)

Uploaded Source

Built Distribution

pydefer-0.0.2-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file pydefer-0.0.2.tar.gz.

File metadata

  • Download URL: pydefer-0.0.2.tar.gz
  • Upload date:
  • Size: 2.4 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pydefer-0.0.2.tar.gz
Algorithm Hash digest
SHA256 7fd71e5567c643542d0e92f43a9500ad99f44979d82786436f7f58e150b557ad
MD5 0cb856be15a4973a85cc41faa81775f4
BLAKE2b-256 089d8a3eb93ed90e1ebc9886aa708af95d2b4b63a1c61b0c7d23ee078710ceba

See more details on using hashes here.

File details

Details for the file pydefer-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pydefer-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for pydefer-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2b4e8c810be4cc80aa2a4f494142d746afd67c5f6fbb2c8d929370b9c709c6be
MD5 42d86ec3a365e3578b0f6bea46dee13a
BLAKE2b-256 608e931ca84f3503605860da3e5daaa7db261bc8cacf4e9534385dc452d5357e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page