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. also flat_map!

bools

currently only provides an xor function.

dates

Mostly cool for the TimePeriod classes:

>>> from datetime import date # will also work with datetimes
>>> time_period = TimePeriod(date(2013, 5, 10), date(2013, 8, 11))
>>> time_period
<TimePeriod: 2013-05-10 00:00:00-2013-08-11 23:59:59>
>>> date(2013, 6, 12) in time_period
True
>>> other_time_period = TimePeriod(date(2013, 6, 1), date(2013, 6, 30))
>>> other_time_period in time_period
True
>>> another_time_period = TimePeriod(date(2013, 8, 1), date(2013, 8, 30))
>>> time_period.overlaps(another_time_period)
True
>>> TimePeriod.get_containing_period(time_period, another_time_period)
<TimePeriod: 2013-05-08 00:00:00-2013-08-30 23:59:59>

and so on and so forth. There’s also a DiscontinousTimePeriod class, which stores a collection of TimePeriods.

There’s also helper functions for common operations like days_ahead and days_ago, which pretty much do what they say on the tin.

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.

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.9.0.tar.gz (14.8 kB view details)

Uploaded Source

Built Distribution

utils-0.9.0-py2.py3-none-any.whl (23.4 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

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

File hashes

Hashes for utils-0.9.0.tar.gz
Algorithm Hash digest
SHA256 9ef5730cf1bc26e0d29db407f3c129b596ede7bc679e08213cde76e079415512
MD5 c705687fe230ff0369e7d404bca06060
BLAKE2b-256 074942c86388fed58455e7e18d3821d7687f4921e47a40cb312e69b82f75c660

See more details on using hashes here.

File details

Details for the file utils-0.9.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for utils-0.9.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 6892efdfdfdf1fd99cef76daa20b9b4667d53e00aa289651fedfb0dbbebf1906
MD5 1113552f4556ba707659fd21823d3d1b
BLAKE2b-256 9bde9ffaf89be661b32d1e0cff05e1af5e4fc2d608c47498975e94aca219aed4

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