yumichael / pytil

Python utility add-ons I frequently use.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Python ease-of-use add-on utility modules

Typical boilerplate code I would add to projects is

from pytil.utility import *
from pytil.object import Namespace as O                # optional include
from pytil.attributes import attribute_accessor as AA  # optional include

pytil.utility

Importing this using import * imports some things hidden away in Python's standard library package into the global namespace and adds some of my own common metaclasses, functional programming helpers, and various other things.

pytil.object

This module contains the Namespace class, which has the internal name <> and goes by the idiomatic reference O. Namespace is a subclass of dict that routes all attributes to dictionary lookup. So it is like a Javascript object, except its methods cannot be accessed by conventional obj.method notation as that would instead mean obj['method']. It has most of the expected magic methods implemented, e.g. obj.foo = 58 translates to obj['foo'] = 58.

pytil.attributes

This module contains a class Attribute that subclasses str whose instances are meant to represent attribute access with a fixed name. An instance of Attribute is callable, and its action on x is to return a specific attribute of x. This module also includes attribute_accessor, idiomatically imported as AA, which lets you get easy access to Attribute instances by the way of AA.some_name is Attribute('some_name') being true.

Here are examples.

>>> from pytil.object import Namespace as O
>>> thing = O(hello=35)
>>> thing
<>(hello=35)
>>> thing.what = 72
>>> thing
<>(hello=35, what=72)
>>> from pytil.attributes import attribute_accessor as AA
>>> AA.what(thing)
72
>>> # you can also create an O using class definition syntax
>>> class another_thing(metaclass=O):
...     hello = 88
...
>>> another_thing
<>(hello=88)

About

Python utility add-ons I frequently use.


Languages

Language:Python 100.0%