A collection of utility functions
Project description
utils-anviks
Useful decorators and functions for everyday Python programming.
Features:
Decorators:
@stopwatch
measures execution time of a function (upon being called) and prints the time taken in seconds to the console@read_file
reads file content and passes it to the decorated function@catch
catches exceptions from a function@memoize
caches function results (only works with one positional hashable argument)@enforce_types
checks types of function arguments and return value (raises TypeError if types don't match)
Functions:
b64encode
encodes a string to a base64 string a specified number of timesb64decode
decodes a base64 string a specified number of timestm_snapshot_to_string
builds a readable string from the giventracemalloc
snapshot
Installation
pip install utils-anviks
Usage
import tracemalloc
from utils_anviks import stopwatch, read_file, catch, memoize, enforce_types, b64encode, b64decode, \
tm_snapshot_to_string
@stopwatch
def some_function():
pass
@read_file('file.txt')
def some_function(file_content):
pass
@catch
def some_function():
pass
@memoize
def some_function(n):
pass
@enforce_types
def some_function(n: int) -> int:
pass
b64encode('string', 3) # 'WXpOU2VXRlhOVzQ9'
b64decode('WXpOU2VXRlhOVzQ9', 3) # 'string'
tracemalloc.start()
arr = [i for i in range(1000000)] # random memory allocation
snapshot = tracemalloc.take_snapshot()
tracemalloc.stop()
print(tm_snapshot_to_string(snapshot))
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
utils_anviks-1.0.2.tar.gz
(6.3 kB
view hashes)
Built Distribution
Close
Hashes for utils_anviks-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 943aca019399b3b5c6fb70b02ad0e76c00c772b39c0ce7936d732c2d685c665b |
|
MD5 | e28be8cf191721e1b4ce718f0cfcd57f |
|
BLAKE2b-256 | 0246ff3bde5e89411d989f9a67a4b6dce369632f867db590152b4030fa75b371 |