https://github.com/anviks/utils-anviks
Useful decorators and functions for everyday Python programming.
https://github.com/anviks/utils-anviks
advent-of-code aoc decorators pip pypi pypi-package python
Last synced: 3 months ago
JSON representation
Useful decorators and functions for everyday Python programming.
- Host: GitHub
- URL: https://github.com/anviks/utils-anviks
- Owner: anviks
- License: mit
- Created: 2024-02-15T21:42:49.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-10-12T22:36:56.000Z (8 months ago)
- Last Synced: 2025-12-15T05:29:57.698Z (6 months ago)
- Topics: advent-of-code, aoc, decorators, pip, pypi, pypi-package, python
- Language: Python
- Homepage:
- Size: 112 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
Awesome Lists containing this project
README
# 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 applies the provided converter to each substring.
- `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.
- `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).
- `Cell` represents a location in a 2-dimensional grid, with attributes for `row` and `column`.
- `Grid` represents a 2-dimensional grid, provides various operations to manipulate and query the grid.
## Installation
```bash
pip install utils-anviks
```
## Usage
```python
import time
import tracemalloc
from utils_anviks import stopwatch, catch, enforce_types, parse_string, parse_file_content, b64encode, b64decode, \
tm_snapshot_to_string, CaptureMalloc, Grid, Cell
@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'
tracemalloc.start()
arr1 = [i for i in range(100_000)] # Arbitrarily chosen 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)] # Arbitrarily chosen memory allocation
print(cm.snapshot_string)
# Top 3 lines
# #1: AOC\dsjdfskld.py:41: 3.8 MiB
# arr2 = [i for i in range(100_000)] # Arbitrarily chosen memory allocation
# Total allocated size: 3.8 MiB
print(Cell(5, 5).neighbours('cardinal'))
# (Cell(row=4, column=5), Cell(row=5, column=6), Cell(row=6, column=5), Cell(row=5, column=4))
print(Cell(3, 2).up.up.up.left.left)
# Cell(row=0, column=0)
print(grid := grid.map(lambda cell, value: 'X' if cell.is_neighbour(Cell(2, 3), 'all') else ' ').join_to_str())
#
# XXX
# X X
# XXX
#
```