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

kdmt-1.0.73-py2.py3-none-any.whl (443.3 kB view hashes)

Uploaded Python 2 Python 3

Supported by

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