Skip to main content

Various handy functions and objects

Project description

https://travis-ci.org/haaksmash/pyutils.svg?branch=master

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

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

kdmt-1.0.21-py2.py3-none-any.whl (164.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file kdmt-1.0.21-py2.py3-none-any.whl.

File metadata

  • Download URL: kdmt-1.0.21-py2.py3-none-any.whl
  • Upload date:
  • Size: 164.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.9.7

File hashes

Hashes for kdmt-1.0.21-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 afd533b7104b62d51c1c1626e52132b1ec048c9bc04fe157cf881a9c0be5e0ed
MD5 915539614f9f605ef22f681e4921a101
BLAKE2b-256 cd91160e1faa8c6c80b4fab9c8f5b76662f17bd1404b957596925bf343184e79

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