ll.misc – Utility functions and classes

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

class ll.misc.Const(name, module=None)[source]

Bases: object

This class can be used for singleton constants.

class ll.misc.FlagAction(option_strings, dest, default=False, help=None)[source]

Bases: 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).

class ll.misc.Iterator(iterator)[source]

Bases: object

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

get(index, default=None)[source]

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

class ll.misc.Pool(*objects)[source]

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

clear()[source]

Make self empty.

clone()[source]

Return a copy of self.

register(object)[source]

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 selfs base pool.

class ll.misc.Queue[source]

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

read(size=-1)[source]

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

write(chars)[source]

Write the string chars to the buffer.

class ll.misc.SysInfo[source]

Bases: 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, script_name, short_script_name and script_url.

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

One module global instance named sysinfo is created at module import time.

exception ll.misc.Timeout(seconds)[source]

Bases: Exception

Exception that is raised when a timeout in timeout() occurs.

ll.misc.count(iterable)[source]

Count the number of items produced by the iterable. Calling this function will exhaust the iterator.

ll.misc.exception_chain(exc)[source]

Traverses the chain of exceptions. This is a generator.

ll.misc.first(iterable, default=None)[source]

Return the first item from the iterable. If the iterator doesn’t produce any items default will be returned.

ll.misc.format_class(obj)[source]

Format the name of the class of obj:

>>> misc.format_class(42)
'int'
>>> misc.format_class(open('README.rst', 'rb'))
'_io.BufferedReader'
ll.misc.format_exception(exc)[source]

Format an exception object:

>>> misc.format_exception(ValueError("bad value"))
'ValueError: bad value'
ll.misc.item(iterable, index, default=None)[source]

Returns the index’th item from the iterable. index may be negative to count from the end. E.g. 0 returns the first item produced by the iterator, 1 the second, -1 the last one etc. If index is negative the iterator will be completely exhausted, if it’s positive it will be exhausted up to the index’th item. If the iterator doesn’t produce that many items default will be returned.

index may also be an iterable of indexes, in which case item() will be applied recursively, i.e. item(["foo", "bar"], (1, -1)) returns 'r'.

ll.misc.iterone(item)[source]

Return an iterator that will produce one item: item.

ll.misc.itersplitat(string, positions)[source]

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.

ll.misc.javaexpr(obj)[source]

Return a Java expression for the object obj.

Example:

>>> print(misc.javaexpr([1, 2, 3]))
java.util.Arrays.asList(1, 2, 3)
ll.misc.jsmin(input)[source]

Minimizes the Javascript source input.

ll.misc.last(iterable, default=None)[source]

Return the last item from the iterable. If the iterator doesn’t produce any items default will be returned.

ll.misc.module(source, filename='unnamed.py', name=None)[source]

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

class ll.misc.monthdelta(months=0)[source]

Bases: object

monthdelta objects can be used to add months/years to a datetime.datetime or datetime.date object. If the resulting day falls out of the range of valid days for the target month, the last day for the target month will be used instead:

>>> import datetime
>>> from ll import misc
>>> datetime.date(2000, 1, 31) + misc.monthdelta(1)
datetime.date(2000, 2, 29)
ll.misc.notifyfinish(title, subtitle, message)[source]

Notify OS X of the end of a process.

ll.misc.notifystart()[source]

Notify OS X of the start of a process by removing the previous notification.

ll.misc.notimplemented(function)[source]

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

ll.misc.prettycsv(rows, padding=' ')[source]

Format table rows.

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

prettycsv() is a generator.

class ll.misc.propclass[source]

Bases: 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
ll.misc.timeout(seconds)[source]

A context manager that limits the runtime of the wrapped code.

This doesn’t work with threads and only on UNIX.

ll.misc.tokenizepi(string)[source]

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).

ll.misc.withdoc(doc)[source]

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.