Coder Social home page Coder Social logo

pytil's Introduction

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)

pytil's People

Contributors

yumichael avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.