utils


Nameutils JSON
Version 1.0.2 PyPI version JSON
download
home_pagehttp://github.com/haaksmash/pyutils
SummaryA grab-bag of utility functions and objects
upload_time2024-01-06 07:11:25
maintainer
docs_urlNone
authorHaak Saxberg
requires_python>=3.6
license
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            Utils
=====

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

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.

.. code-block:: python

    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:

.. code-block:: python

    >>> 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:

.. code-block:: python

    >>> 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:

.. code-block:: python

    >>> 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``:

.. code-block:: python

    >>> 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 utils


            

Raw data

            {
    "_id": null,
    "home_page": "http://github.com/haaksmash/pyutils",
    "name": "utils",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "",
    "author": "Haak Saxberg",
    "author_email": "haak.erling@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/ad/1f/c196d21c2df061923154aecf24cab049a114394956e90c9bfbfdd398e27a/utils-1.0.2.tar.gz",
    "platform": null,
    "description": "Utils\n=====\n\n.. image:: https://travis-ci.org/haaksmash/pyutils.svg?branch=master\n    :target: https://travis-ci.org/haaksmash/pyutils\n\nSometimes you write a function over and over again; sometimes you look up at\nthe ceiling and ask \"why, Guido, why isn't this included in the standard\nlibrary?\"\n\nWell, we perhaps can't answer that question. But we can collect those functions\ninto a centralized place!\n\nProvided things\n+++++++++++++++\n\nUtils is broken up into broad swathes of functionality, to ease the task of\nremembering where exactly something lives.\n\nenum\n----\n\nPython 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.\n\n.. code-block:: python\n\n    from utils import enum\n\n    class Colors(enum.Enum):\n        RED = 0\n        GREEN = 1\n\n        # Defining an Enum class allows you to specify a few\n        # things about the way it's going to behave.\n        class Options:\n            frozen = True # can't change attributes\n            strict = True # can only compare to itself; i.e., Colors.RED == Animals.COW\n                          # will raise an exception.\n\n    # or use the enum factory (no Options, though)\n    ColorsAlso = enum.enum(\"RED\", \"GREEN\")\n\nOnce defined, use is straightforward:\n\n.. code-block:: python\n\n    >>> Colors\n    <class 'blahblah.Colors'>\n    >>> Colors.RED\n    <EnumItem: RED [0]>\n    >>> Colors.RED == 0\n    True\n    >>> Colors.RED == Colors.RED\n    True\n    >>> Colors.RED = 2\n    Traceback (most recent call last):\n      File \"<stdin>\", line 1, in <module>\n      File \"utils/enum.py\", line 114, in __setattr__\n        raise TypeError(\"can't set attributes on a frozen enum\")\n    TypeError: can't set attributes on a frozen enum\n\nmath\n----\n\nCurrently only has the multiplicative analogue of the built-in ``sum``.\n\ndicts\n-----\n\nintersections, differences, winnowing, a few specialized dicts...\n\nlists\n-----\n\nflatten and unlisting. also ``flat_map``!\n\nbools\n-----\n\ncurrently only provides an ``xor`` function.\n\ndates\n-----\n\nMostly cool for the ``TimePeriod`` classes:\n\n.. code-block:: python\n\n    >>> from datetime import date # will also work with datetimes\n    >>> time_period = TimePeriod(date(2013, 5, 10), date(2013, 8, 11))\n    >>> time_period\n    <TimePeriod: 2013-05-10 00:00:00-2013-08-11 23:59:59>\n    >>> date(2013, 6, 12) in time_period\n    True\n    >>> other_time_period = TimePeriod(date(2013, 6, 1), date(2013, 6, 30))\n    >>> other_time_period in time_period\n    True\n    >>> another_time_period = TimePeriod(date(2013, 8, 1), date(2013, 8, 30))\n    >>> time_period.overlaps(another_time_period)\n    True\n    >>> TimePeriod.get_containing_period(time_period, another_time_period)\n    <TimePeriod: 2013-05-08 00:00:00-2013-08-30 23:59:59>\n\n\nand so on and so forth. There's also a ``DiscontinousTimePeriod`` class, which\nstores a collection of TimePeriods.\n\nThere's also helper functions for common operations like ``days_ahead`` and\n``days_ago``, which pretty much do what they say on the tin.\n\nobjects\n-------\n\nprovides ``get_attr``, which is really just a convenient way to do deep ``getattr`` chaining:\n\n.. code-block:: python\n\n    >>> get_attr(complicated, 'this.is.a.deep.string', default=None)\n    \"the deep string\"  # or None, if anything in the lookup chain didn't exist\n\nThere's also an ``immutable`` utility, which will wrap an object and preven all attribute changes,\nrecursively by default. Any attempt to set attributes on the wrapped object will raise an ``AttributeError``:\n\n.. code-block:: python\n\n    >>> imm = immutable(something)\n    >>> imm\n    <Immutable Something: <Something>>\n    >>> imm.red\n    <Immutable SomethingElse: <SomethingElse: red>>\n    >>> imm.red = SomethingElse('blue')\n    # ...\n    AttributeError: This object has been marked as immutable; you cannot set its attributes.\n    >>> something.red = SomethingElse('blue')\n    >>> imm.red\n    <Immutable SomethingElse: <SomethingElse: blue>>\n\nYou can toggle the recursive immutability by specifying the 'recursive' flag.\n\nInstallation (via pip)\n++++++++++++++++++++++\n\n    pip install utils\n\n",
    "bugtrack_url": null,
    "license": "",
    "summary": "A grab-bag of utility functions and objects",
    "version": "1.0.2",
    "project_urls": {
        "Homepage": "http://github.com/haaksmash/pyutils"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ad1fc196d21c2df061923154aecf24cab049a114394956e90c9bfbfdd398e27a",
                "md5": "265f1089713160b5e20f99883684a614",
                "sha256": "f4d5157e27e9d434006b5b52a1ec951a34e53e7ecaa145d43a153ec452eb5d9e"
            },
            "downloads": -1,
            "filename": "utils-1.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "265f1089713160b5e20f99883684a614",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 13203,
            "upload_time": "2024-01-06T07:11:25",
            "upload_time_iso_8601": "2024-01-06T07:11:25.750081Z",
            "url": "https://files.pythonhosted.org/packages/ad/1f/c196d21c2df061923154aecf24cab049a114394956e90c9bfbfdd398e27a/utils-1.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-06 07:11:25",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "haaksmash",
    "github_project": "pyutils",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "utils"
}
        
Elapsed time: 0.22309s