Skip to main content

Python Distribution Utilities

Project description

Sometimes you write a function over and over again; sometimes you look up at the ceiling and ask “why, Guido, why isn’t this included in the standard library?”

Well, we perhaps can’t answer that question. But we can collect those functions into a centralized place!

Provided things

Utils is broken up into broad swathes of functionality, to ease the task of remembering where exactly something lives.

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 Colors(enum.Enum):
    RED = 0
    GREEN = 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.

# or use the enum factory (no Options, though)
ColorsAlso = enum.enum("RED", "GREEN")

Once defined, use is straightforward:

>>> Colors
<class 'blahblah.Colors'>
>>> Colors.RED
<EnumItem: RED [0]>
>>> Colors.RED == 0
True
>>> Colors.RED == Colors.RED
True
>>> Colors.RED = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "utils/enum.py", line 114, in __setattr__
    raise TypeError("can't set attributes on a frozen enum")
TypeError: can't set attributes on a frozen enum

math

Currently only has the multiplicative analogue of the built-in sum.

dicts

intersections, differences, winnowing, a few specialized dicts…

lists

flatten and unlisting

bools

currently only provides an xor function.

dates

TimePeriod, from string, to_datetime, and days_ago and _ahead

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

utils-0.4.tar.gz (10.6 kB view details)

Uploaded Source

File details

Details for the file utils-0.4.tar.gz.

File metadata

  • Download URL: utils-0.4.tar.gz
  • Upload date:
  • Size: 10.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for utils-0.4.tar.gz
Algorithm Hash digest
SHA256 bee2133a3c964b4d66dd845a2cc682c046ed6be5ea1e64d187d5d3931be9e174
MD5 fd7725586b8a6a8141c80bed8a2bf9c2
BLAKE2b-256 c81cd4948569840a99390abd6cf522982918a75e9dbbcbaeb7fba9d2cc834746

See more details on using hashes here.

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