Python idoms and recipes
Project description
Recipie
Python idoms and recipes
Submodule
Some recipie modules having the same name with standard libraries are meant as a drop in replacement for their counter parts. For example:
# Replace contextlib by recipie.contextlib
#from contextlib import *
from recipie.contextlib import *
# All standard contextlib and recipie.contextlib extensions are available
contextlib
cleanup(func, *args, **kwargs) [0.0.3]
Executes a clean up task on exiting context. The clean up task can be canceled.
Example:
with cleanup(undo, arg) as cleanup_task:
result = action()
if result:
cleanup_task.cancel()
commit(func, *args, **kwargs) [0.0.1]
Executes func if the code ran to completion without any error:
Example:
def save_data(data):
...
with commit(save_data, data):
# other actions
...
# save_data is called if there is no error
- Also see rollback
rollback(func, *args, **kwargs) [0.0.1]
Executes func if when encouter error
Example:
def undo(data):
...
with rollback(undo, data):
# Other actions
...
- Also see ***commit
Buffer [0.0.2]
Accumulates data into a buffer to be processed when the buffer is full.
Example:
def save_data(data: list[int]):
...
with Buffer(1000, save_data) as buffer:
for i in range(1500):
buffer.add(i)
# On 1001th item, called save_data to save previous 1000 items
# When out of context, call save_data on the last 500 items
functools
@scoped [0.0.2]
"Namespaces are one honking great idea -- let's do more of those!"
Decorates a function to make it part of another namespace
Examples:
def retry():
...
# Put no_delay under "retry" so it can only be refered to as retry.no_delay
@scoped(retry)
def no_delay():
...
@default_on_error(value, errors) [0.0.1]
Decorates a function to return the specified value if errors is raised.
value: Default value to return on error
errors: Error or tuple of errors to check for
Example:
@default_on_error(None, KeyError)
def get_from_dict(dict_, key):
return dict_[key] # Possible KeyError
value = get_from_dict(d, "Test") # Value is None when "Test" is not in d
@skip_on_error(errors) [0.0.1]
Decorates a function to return None if errors is raised (equivalent to @default_on_error(None, errors)
@retry(tries, errors, error_filter, delay_gen, log_error) [0.0.1]
Retries function tries times if encounter an error that matches errors and error_filter
tries: Number of attempts on the function. Must be at least 2
errors: exception or tuple of exception to retry
error_filter: function that takes in an error and returns True if it is a retriable error
delay_gen: Delay generator that produce an iterator of delay values for use between attempts. Example:
@retry(3, NetworkError) # Retry 3 times on NetworkError)
def get_db_connection():
...
conn = get_db_connection() # Automatically retry on network error
- Either errors or error_filter must be specified
Predefined delay policies:
-
retry.no_delay: No delay between attempts. This is picked up when no delay policy specified. It is equivalent to retry.const_delay(0).
-
retry.const_delay(delay): Delay in seconds as specified by delay
-
retry.expo_backoff(base, cap, jitter): exponential backoff delay. With no jitter, values are max(base * 2 ^ n, cap).
- retry.no_jitter: (default) actual delay is fixed at the calculated value
- retry.half_jitter: actual delay is randomly picked from [value/2, value)
- retry.full_jitter: actual delay is randomly picked from [0, value)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file recipie-0.0.3.tar.gz.
File metadata
- Download URL: recipie-0.0.3.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9b3efb72d379aee9ddd81a44754d9f7b52c5be183c6caf52ea900457d7d3532d
|
|
| MD5 |
e064c77ee766f8d5c1546f2e447ed58f
|
|
| BLAKE2b-256 |
aab848021139b28cef5de7b0323de7d32d68fc1993051be62ed0357388f2a710
|
File details
Details for the file recipie-0.0.3-py3-none-any.whl.
File metadata
- Download URL: recipie-0.0.3-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e012f6fcc3a1fe3d4661b403aa50def4321e26622466b2da86f1e5f3357ddcf6
|
|
| MD5 |
0aa9c416ce748f915d620d00bd1283a2
|
|
| BLAKE2b-256 |
16d7b76a08309d352cade4ec7b9d2386aa062d716777d65003810f2671a005db
|