ll.misc

Miscellaneous utility functions and classes

Home › Python software › ll.miscText · XIST · Python

ll.misc contains various utility functions and classes used by the other LivingLogic modules and packages.

def notimplemented​(function):

A decorator that raises NotImplementedError when the method is called. This saves you the trouble of formatting the error message yourself for each implementation.

def withdoc​(doc):

A decorator that adds a docstring to the function it decorates. This can be useful if the docstring is not static, and adding it afterwards is not possible.

class _propclass_Meta​(type):

class propclass​(property):

propclass provides an alternate way to define properties.

Subclassing propclass and defining methods __get__, __set__ and __delete__ will automatically generate the appropriate property:

class name(misc.propclass):
        """
        The name property
        """
        def __get__(self):
                return self._name
        def __set__(self, name):
                self._name = name.lower()
        def __delete__(self):
                self._name = None

class __metaclass__​(type):

class Pool​(object):

A Pool object can be used as an inheritable alternative to modules. The attributes of a module can be put into a pool and each pool can have base pools where lookup continues if an attribute can't be found.

def __init__​(self, *objects):

def register​(self, object):

Register object in the pool. object can be a module, a dictionary or a Pool objects (with registers the pool as a base pool). If object is a module and has an attribute __bases__ (being a sequence of other modules) this attribute will be used to initialize self base pool.

def __getitem__​(self, key):

def __getattr__​(self, key):

def clear​(self):

Make self empty.

def clone​(self):

Return a copy of self.

def __repr__​(self):

def iterone​(item):

Return an iterator that will produce one item: item.

class Iterator​(object):

Iterator adds __getitem__ support to an iterator. This is done by calling item internally.

def __init__​(self, iterator):

def __getitem__​(self, index):

def __iter__​(self):

def next​(self):

def __nonzero__​(self):

def get​(self, index, default=None):

Return the index h item from the iterator (or default if there's no such item).

class Queue​(object):

Queue provides FIFO queues: The method write writes to the queue and the method read read from the other end of the queue and remove the characters read.

def __init__​(self):

def write​(self, chars):

Write the string chars to the buffer.

def read​(self, size=-1):

Read up to size character from the buffer (or all if size is negative). Those characters will be removed from the buffer.

class Const​(object):

This class can be used for singleton constants.

def __init__​(self, name):

def __repr__​(self):

class FlagAction​(argparse.Action):

FlagAction can be use with argparse for options that represent flags. An options can have a value like yes or no for the correspending boolean value, or if the value is omitted it is the inverted default value (i.e. specifying the option toggles it).

def __init__​(self, option_strings, dest, default=False, help=None):

def _get_kwargs​(self):

def str2bool​(self, value):

def __call__​(self, parser, namespace, values, option_string=None):

def tokenizepi​(string):

Tokenize the string object string according to the processing instructions in the string. tokenize will generate tuples with the first item being the processing instruction target and the second being the PI data. "Text" content (i.e. anything other than PIs) will be returned as (None, data).

def gzip​(data, compresslevel=9):

Compresses the byte string data with gzip using the compression level compresslevel.

def gunzip​(data):

Uncompresses the byte string data with gzip.

def itersplitat​(string, positions):

Split string at the positions specified in positions.

For example:

>>> from ll import misc
>>> import datetime
>>> datetime.datetime(*map(int, misc.itersplitat("20090609172345", (4, 6, 8, 10, 12))))
datetime.datetime(2009, 6, 9, 17, 23, 45)

This is a generator.

def module​(code, filename='unnamed.py', name=None):

Create a module from the Python source code code. filename will be used as the filename for the module and name as the module name (defaulting to the filename part of filename).

def javaexpr​(obj):

Return a Java expression for the object obj.

class SysInfo​(object):

A SysInfo object contains information about the host, user, python version and script. Available attributes are host_name, host_fqdn, host_ip, host_sysname, host_nodename, host_release, host_version, host_machine, user_name, user_uid, user_gid, user_gecos, user_dir, user_shell, python_executable, python_version, pid and scriptname.

SysInfo object also support a mimimal dictionary interface (i.e. __getitem__ and __iter__).

def __init__​(self, encoding='utf-8', errors='replace'):

def __getitem__​(self, key):

def __iter__​(self):

def prettycsv​(rows, padding=' '):

Format table rows.

rows must be a list of lists of strings (e.g. as produced by the cvs module). padding is the padding between columns.

prettycsv is a generator.

class JSMinUnterminatedComment​(Exception):

class JSMinUnterminatedStringLiteral​(Exception):

class JSMinUnterminatedRegularExpression​(Exception):

def jsmin​(input):

Minimizes the Javascript source input.