Skip to main content

A collection of utilities

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.
  • @catch catches exceptions from a function.
  • @enforce_types checks types of function arguments and return value (raises TypeError if types don't match).

Functions:

  • parse_string splits a string by separators and converts to the given type.
  • parse_file_content same as parse_string, but parses file content instead of a string.
  • b64encode encodes a string to a base64 string a specified number of times.
  • b64decode decodes a base64 string a specified number of times.
  • dict_to_object converts a dictionary to an object, based on given type argument.
  • tm_snapshot_to_string builds a readable string from the given tracemalloc snapshot.

Classes:

  • CaptureMalloc captures memory allocations within a block of code (context manager).

Installation

pip install utils-anviks

Usage

import time
import tracemalloc
from utils_anviks import stopwatch, catch, enforce_types, parse_string, parse_file_content, b64encode, b64decode, \
    dict_to_object, tm_snapshot_to_string, CaptureMalloc


@stopwatch
def foo():
    time.sleep(1.23)


@catch(TypeError, ZeroDivisionError)
def bar(n: int):
    return 1 / n


@enforce_types
def baz(n: int) -> int:
    pass


foo()  # Time taken by the function to execute is printed to the console
print(bar(0))  # Catches ZeroDivisionError and returns (1, [error object])
baz('string')  # Raises TypeError

print(parse_string('111,222,333\n64,59,13', ('\n', ','), int))  # [[111, 222, 333], [64, 59, 13]]
print(parse_file_content('file.txt', ('\n', ','), int))  # Same as above, but reads from a file

print(b64encode('string', 3))  # 'WXpOU2VXRlhOVzQ9'
print(b64decode('WXpOU2VXRlhOVzQ9', 3))  # 'string'

class Foo:
    a: int
    b: str
    
print(dict_to_object({'a': 1, 'b': 'string'}, Foo))  # Foo(a=1, b='string')

tracemalloc.start()
arr1 = [i for i in range(100_000)]  # random memory allocation
snapshot = tracemalloc.take_snapshot()
tracemalloc.stop()
print(tm_snapshot_to_string(snapshot))

with CaptureMalloc() as cm:
    arr2 = [i for i in range(100_000)]  # random memory allocation
print(cm.snapshot_string)

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

utils_anviks-2.0.1.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

utils_anviks-2.0.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file utils_anviks-2.0.1.tar.gz.

File metadata

  • Download URL: utils_anviks-2.0.1.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for utils_anviks-2.0.1.tar.gz
Algorithm Hash digest
SHA256 87255b9ed3f5efaaf104e9413e286c9164c345356714c257a079462c967c0904
MD5 cbddfd65f65ad7293bfb321ee385b666
BLAKE2b-256 b427f8c4293c09e39d1307cca19cf3c22e49f91941a120deb469e4ef9a1f8bbb

See more details on using hashes here.

File details

Details for the file utils_anviks-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: utils_anviks-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.2

File hashes

Hashes for utils_anviks-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 30d66790b8db97304d6eb05cf0fdfd5d3d1561cea758d0a75c844fc63232b69a
MD5 68d469e8065077e034d58ebb0d85fc18
BLAKE2b-256 490d7560c022352e765ac2ec3a2c1af0ec3a262081ed2edcdf1144a1af4fa9ec

See more details on using hashes here.

Supported by

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