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.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. Ifindex
is 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 itemsdefault
will be returned.index
may 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
default
will be returned.
-
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.
count
(iterable)[source]¶ Count the number of items produced by the iterable. Calling this function will exhaust the iterator.
-
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.
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.
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.
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'
-
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.
-
class
ll.misc.
Iterator
(iterator)[source]¶ Bases:
object
Iterator
adds__getitem__()
support to an iterator. This is done by callingitem()
internally.
-
class
ll.misc.
Queue
[source]¶ Bases:
object
Queue
provides FIFO queues: The methodwrite()
writes to the queue and the methodread()
read from the other end of the queue and remove the characters read.
-
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 withargparse
for options that represent flags. An options can have a value likeyes
orno
for 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
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.
itersplitat
(string, positions)[source]¶ Split
string
at 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
.filename
will be used as the filename for the module andname
as 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:
object
A
SysInfo
object 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_name
andscript_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.
-
class
ll.misc.
monthdelta
(months=0)[source]¶ Bases:
object
monthdelta
objects can be used to add months/years to adatetime.datetime
ordatetime.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)
-
exception
ll.misc.
Timeout
(seconds)[source]¶ Bases:
Exception
Exception 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.
This doesn’t work 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.
prettycsv
(rows, padding=' ')[source]¶ Format table
rows
.rows
must be a list of lists of strings (e.g. as produced by thecsv
module).padding
is the padding between columns.prettycsv()
is a generator.