A collection of utilities (mostly for Advent of Code)
Project description
utils-anviks
Useful decorators and functions for everyday Python programming.
Features:
Decorators:
@stopwatchmeasures execution time of a function (upon being called) and prints the time taken in seconds to the console.@catchcatches exceptions from a function.@enforce_typeschecks types of function arguments and return value (raises TypeError if types don't match).
Functions:
parse_stringsplits a string by separators and converts to the given type.parse_file_contentsame asparse_string, but parses file content instead of a string.b64encodeencodes a string to a base64 string a specified number of times.b64decodedecodes a base64 string a specified number of times.dict_to_objectconverts a dictionary to an object, based on given type argument.tm_snapshot_to_stringbuilds a readable string from the giventracemallocsnapshot.
Classes:
CaptureMalloccaptures memory allocations within a block of code (context manager).Cellrepresents a location in a 2-dimensional grid, with attributes forrowandcolumn.Gridrepresents a 2-dimensional grid, provides various operations to manipulate and query the grid.
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, 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'
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)] # 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.gradient_by_step(5, 5, 3, 2, 'diagonal'))
# Grid(
# [3, 5, 7, 9, 11],
# [5, 7, 9, 11, 13],
# [7, 9, 11, 13, 15],
# [9, 11, 13, 15, 17],
# [11, 13, 15, 17, 19],
# )
print(grid := grid.map(lambda cell, value: 'X' if cell.is_neighbour(Cell(2, 3), 'all') else ' ').join_to_str())
#
# XXX
# X X
# XXX
#
grid[list(grid.find('X'))] = 'O'
print(grid.join_to_str())
#
# OOO
# O O
# OOO
#
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 Distributions
Built Distributions
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 utils_anviks-2.1.1-cp313-cp313-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: utils_anviks-2.1.1-cp313-cp313-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 38.5 kB
- Tags: CPython 3.13, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
457380fab9a7e98829a966b93f962651a908b8e40e55858149e0e8bd42737f40
|
|
| MD5 |
ff7f133cb07d57ae2d3241b512a6bb0c
|
|
| BLAKE2b-256 |
4e4c66cd87f3105d6c5f428633b50eea14eff99affdbff2fa6aaefd07e543be7
|
File details
Details for the file utils_anviks-2.1.1-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.
File metadata
- Download URL: utils_anviks-2.1.1-cp313-cp313-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
- Upload date:
- Size: 38.5 kB
- Tags: CPython 3.13, manylinux: glibc 2.28+ x86-64, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c477eb93aae1d039972b4388b5aea28b0dfeba1b74131fd0ed055e8d7c32466
|
|
| MD5 |
2f8f5af6a68f3765dc0eb373b950d8b5
|
|
| BLAKE2b-256 |
a5afdda08b34677d7d7f224dd9a947b6088042c7c811315ba572e4661de7cc49
|
File details
Details for the file utils_anviks-2.1.1-cp312-cp312-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: utils_anviks-2.1.1-cp312-cp312-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 38.4 kB
- Tags: CPython 3.12, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20b083f541d4921e0eb607d9bb1fab14923d41870a8908906d3db9eed065a17e
|
|
| MD5 |
b89e2d229c9f5e8f0053064bbdde86aa
|
|
| BLAKE2b-256 |
162d9a9547d4ad583dc8f5dd3f4dff5bcc99bdf5b2e264e1751a92795ac5f7c8
|
File details
Details for the file utils_anviks-2.1.1-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.
File metadata
- Download URL: utils_anviks-2.1.1-cp312-cp312-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
- Upload date:
- Size: 38.5 kB
- Tags: CPython 3.12, manylinux: glibc 2.28+ x86-64, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
17fe819e0511b9b4debe1187f42704064c3c71f103b7be0a3771052b99e9fbac
|
|
| MD5 |
e1b1a71235c3596d732159507cef85ce
|
|
| BLAKE2b-256 |
ed2bb128e203cd518d512d5bb6296f8c085fb5f457f483d76511c74df6bbf43c
|
File details
Details for the file utils_anviks-2.1.1-cp311-cp311-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: utils_anviks-2.1.1-cp311-cp311-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 38.0 kB
- Tags: CPython 3.11, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3b97919e9e61196f44bcc4cf3b2b4bab5570705e96a69f52e0d2b154e3e93db5
|
|
| MD5 |
f5dff5833e7aab874d1814af1d695202
|
|
| BLAKE2b-256 |
fb71d78f9afdf6eff1fa9e9e37c539b23007ccebd3a7479afcc385463394e51e
|
File details
Details for the file utils_anviks-2.1.1-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.
File metadata
- Download URL: utils_anviks-2.1.1-cp311-cp311-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
- Upload date:
- Size: 38.0 kB
- Tags: CPython 3.11, manylinux: glibc 2.28+ x86-64, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a102be65bc6cb2f80701e2b653077303d424371bfee98e53bcf3b67c6ee815ab
|
|
| MD5 |
bd7f52db30ef8a122c7caf531cd25263
|
|
| BLAKE2b-256 |
bf65f669e1fb31f42669a4354273ca5be601fce6ed879cbd8bc94ee5ef977d35
|
File details
Details for the file utils_anviks-2.1.1-cp310-cp310-musllinux_1_2_x86_64.whl.
File metadata
- Download URL: utils_anviks-2.1.1-cp310-cp310-musllinux_1_2_x86_64.whl
- Upload date:
- Size: 37.9 kB
- Tags: CPython 3.10, musllinux: musl 1.2+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6613e52c12eef4697e76f996a5f60b6e53daea1a1e024acbb4edb46448184391
|
|
| MD5 |
ed0f067a237ce6a32abdf60baac2ed58
|
|
| BLAKE2b-256 |
1d04a0c48c40b0490d35c44131ade4643d858c71996350960da0d6a9d6d58473
|
File details
Details for the file utils_anviks-2.1.1-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl.
File metadata
- Download URL: utils_anviks-2.1.1-cp310-cp310-manylinux1_x86_64.manylinux_2_28_x86_64.manylinux_2_5_x86_64.whl
- Upload date:
- Size: 37.9 kB
- Tags: CPython 3.10, manylinux: glibc 2.28+ x86-64, manylinux: glibc 2.5+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9244832647a6d05af0777db2bc2c4ee4cf6c89286eda601fb533dc69ca31652
|
|
| MD5 |
c8cd2c701ceca6c037c1d58baa5dd768
|
|
| BLAKE2b-256 |
8af4bf1ded42fd74b691fffb27e3a3ceda222fd7893db0b4a64e7def17da6be3
|