ll.misc – Utility functions and classes
ll.misc contains various utility functions and classes used by the other
LivingLogic modules and packages.
- ll.misc.item(iterable, index, /, default=None)[source]
Returns the
index’th item from the iterable.indexmay 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. Ifindexis negative the iterator will be completely exhausted, if it’s positive it will be exhausted up to theindex’th item. If the iterator doesn’t produce that many itemsdefaultwill be returned.indexmay also be an iterable of indexes, in which caseitem()will be applied recursively, i.e.item(["foo", "bar"], (1, -1))returns'r'.
- ll.misc.first(iterable, /, default=None)[source]
Return the first item from the iterable. If the iterator doesn’t produce any items
defaultwill be returned.
- ll.misc.last(iterable, /, default=None)[source]
Return the last item from the iterable. If the iterator doesn’t produce any items
defaultwill be returned.
- ll.misc.count(iterable, /)[source]
Count the number of items produced by the iterable. Calling this function will exhaust the iterator.
- ll.misc.isfirst(iterable, /)[source]
Iterate through items of the iterable and give information about whether the item is the first in the iterable:
>>> list(misc.isfirst("foo")) [(True, 'f'), (False, 'o'), (False, 'o')]
- ll.misc.islast(iterable, /)[source]
Iterate through items of the iterable and give information about whether the item is the last in the iterable:
>>> list(misc.islast("foo")) [(False, 'f'), (False, 'o'), (True, 'o')]
- ll.misc.isfirstlast(iterable, /)[source]
Iterate through items of the iterable and give information about whether the item is the first and/or last in the iterable:
>>> list(misc.isfirstlast("foo")) [(True, False, 'f'), (False, False, 'o'), (False, True, 'o')]
- ll.misc.notimplemented(function)[source]
A decorator that raises
NotImplementedErrorwhen the method is called.This saves you the trouble of formatting the error message yourself for each implementation.
- 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.
- class ll.misc.Enum[source]
Bases:
enum.EnumSubclass of
enum.Enumwhere class and instancerepr()output include the module and fully qualified class name.
- class ll.misc.IntEnum[source]
Bases:
enum.IntEnumSubclass of
enum.IntEnumwhere class and instancerepr()output includes the module and fully qualified class name.
- class ll.misc.propclass[source]
Bases:
propertypropclassprovides an alternate way to define properties.Subclassing
propclassand 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.format_class(obj)[source]
Format the name of the class of
obj.Example:
>>> misc.format_class(42) 'int' >>> misc.format_class(open('README.rst', 'rb')) '_io.BufferedReader'
- ll.misc.format_exception(exc)[source]
Format an exception object.
Example:
>>> misc.format_exception(ValueError("bad value")) 'ValueError: bad value'
- ll.misc.exception_chain(exc)[source]
Traverses the chain of exceptions. This is a generator.
- class ll.misc.Pool[source]
Bases:
objectA
Poolobject 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.- register(object)[source]
Register
objectin the pool.objectcan be a module, a dictionary or aPoolobjects (with registers the pool as a base pool). Ifobjectis a module and has an attribute__bases__(being a sequence of other modules) this attribute will be used to initializeselfs base pool.
- clear()[source]
Make
selfempty.
- clone()[source]
Return a copy of
self.
- ll.misc.iterone(item)[source]
Return an iterator that will produce one item:
item.
- class ll.misc.Iterator[source]
Bases:
objectIteratoradds__getitem__()support to an iterator. This is done by callingitem()internally.- get(index, default=None)[source]
Return the
index’th item from the iterator (ordefaultif there’s no such item).
- class ll.misc.Queue[source]
Bases:
objectQueueprovides FIFO queues: The methodwrite()writes to the queue and the methodread()read from the other end of the queue and remove the characters read.- write(chars)[source]
Write the string
charsto the buffer.
- read(size=- 1)[source]
Read up to
sizecharacter from the buffer (or all ifsizeis negative). Those characters will be removed from the buffer.
- class ll.misc.FlagAction[source]
Bases:
argparse.ActionFlagActioncan be use withargparsefor options that represent flags. An options can have a value likeyesornofor the correspending boolean value, or if the value is omitted it is the inverted default value (i.e. specifying the option toggles it).
- ll.misc.tokenizepi(string)[source]
Tokenize the string object
stringaccording 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.itersplitat(string, positions)[source]
Split
stringat the positions specified inpositions.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.module(source, filename='unnamed.py', name=None)[source]
Create a module from the Python source code
source.filenamewill be used as the filename for the module andnameas the module name (defaulting to the filename part offilename).
- 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)
- class ll.misc.SysInfo[source]
Bases:
objectA
SysInfoobject contains information about the host, user, python version and script. Available attributes arehost_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_nameandscript_url.SysInfoobject also support a mimimal dictionary interface (i.e.__getitem__()and__iter__()).One module global instance named
sysinfois created at module import time.
- class ll.misc.monthdelta[source]
Bases:
objectmonthdeltaobjects can be used to add months/years to adatetime.datetimeordatetime.dateobject. 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)
- exception ll.misc.Timeout[source]
Bases:
ExceptionException that is raised when a timeout in
timeout()occurs.
- ll.misc.timeout(seconds)[source]
A context manager that limits the runtime of the wrapped code.
As this uses
signal, this won’t with threads and only on UNIX.
- ll.misc.notifystart()[source]
Notify OS X of the start of a process by removing the previous notification.
- ll.misc.notifyfinish(title, subtitle, message)[source]
Notify OS X of the end of a process.
- ll.misc.prettycsv(rows, padding=' ')[source]
Format table
rows.rowsmust be a list of lists of strings (e.g. as produced by thecsvmodule).paddingis the padding between columns.prettycsv()is a generator.
- ll.misc.jsmin(input)[source]
Minimizes the Javascript source
input.