Various handy functions and objects
Project description
In any project we usually need a set of functions that we use again and again and that don’t typically belong to the project. They go in an util file because they do not belong to the specific application logic. We also spend time in every project searching, adapting and copy/pasting these functions.
In this project, I, decided to collect those functions and put them in one centralized place!
Functionalities
Utilities libprary is broken up into broad domains of functionality in orther to easily remember where thinks are located.
Dictionnaries
DotDict
enum
Python doesn’t have a built-in way to define an enum, so this module provides (what I think) is a pretty clean way to go about them.
from utils import enum
class ModelTypes(enum.Enum):
CLASSIFICATION = 0
REGRESSION = 1
# Defining an Enum class allows you to specify a few
# things about the way it's going to behave.
class Options:
frozen = True # can't change attributes
strict = True # can only compare to itself; i.e., Colors.RED == Animals.COW
# will raise an exception.
Once defined, use is straightforward:
>>> ModelTypes
dicts
intersections, differences, winnowing, a few specialized dicts…
lists
flatten and unlisting. also flat_map!
bools
currently only provides an xor function.
dates
objects
provides get_attr, which is really just a convenient way to do deep getattr chaining:
>>> get_attr(complicated, 'this.is.a.deep.string', default=None)
"the deep string" # or None, if anything in the lookup chain didn't exist
There’s also an immutable utility, which will wrap an object and preven all attribute changes, recursively by default. Any attempt to set attributes on the wrapped object will raise an AttributeError:
>>> imm = immutable(something)
>>> imm
<Immutable Something: <Something>>
>>> imm.red
<Immutable SomethingElse: <SomethingElse: red>>
>>> imm.red = SomethingElse('blue')
# ...
AttributeError: This object has been marked as immutable; you cannot set its attributes.
>>> something.red = SomethingElse('blue')
>>> imm.red
<Immutable SomethingElse: <SomethingElse: blue>>
You can toggle the recursive immutability by specifying the ‘recursive’ flag.
Installation (via pip)
pip install kdmt
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 Distribution
File details
Details for the file kdmt-1.0.73-py2.py3-none-any.whl
.
File metadata
- Download URL: kdmt-1.0.73-py2.py3-none-any.whl
- Upload date:
- Size: 443.3 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.11
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 535117a235b2e8faa4389949a5ddece3277ba3e97c46eb74270972fa3fcb3788 |
|
MD5 | 9e2e0eb6a1f72f547efa3eb136d86760 |
|
BLAKE2b-256 | 945847debd042c2807623880467c6ef6d69d73759edaa8d3871802906e850440 |