moldesign.utils package

Submodules

moldesign.utils.callsigs module

class moldesign.utils.callsigs.DocInherit(mthd)[source]

Bases: object

Allows methods to inherit docstrings from their superclasses FROM http://code.activestate.com/recipes/576862/

get_no_inst(cls)[source]
get_with_inst(obj, cls)[source]
use_parent_doc(func, source)[source]
moldesign.utils.callsigs.args_from(original_function, only=None, allexcept=None, inject_kwargs=None, inject_docs=None, wraps=None, update_docstring_args=False)[source]

Decorator to transfer call signatures - helps to hide ugly *args and **kwargs in delegated calls

Parameters:
  • original_function (callable) – the function to take the call signature from
  • only (List[str]) – only transfer these arguments (incompatible with allexcept)
  • wraps (bool) – Transfer documentation and attributes from original_function to decorated_function, using functools.wraps (default: True if call signature is unchanged, False otherwise)
  • allexcept (List[str]) – transfer all except these arguments (incompatible with only)
  • inject_kwargs (dict) – Inject new kwargs into the call signature (of the form {argname: defaultvalue})
  • inject_docs (dict) – Add or modifies argument documentation (requires google-style docstrings) with a dict of the form {argname: “(type): description”}
  • update_docstring_args (bool) – Update “arguments” section of the docstring using the original function’s documentation (requires google-style docstrings and wraps=False)

Note

To use arguments from a classes’ __init__ method, pass the class itself as original_function - this will also allow us to inject the documentation

Returns:Decorator function
moldesign.utils.callsigs.doc_inherit

alias of DocInherit

moldesign.utils.callsigs.get_qualified_name(original_function)[source]
moldesign.utils.callsigs.kwargs_from(reference_function, mod_docs=True)[source]

Replaces **kwargs in a call signature with keyword arguments from another function.

Parameters:
  • reference_function (function) – function to get kwargs from
  • mod_docs (bool) – whether to modify the decorated function’s docstring

Note

mod_docs works ONLY for google-style docstrings

moldesign.utils.classes module

class moldesign.utils.classes.Alias(objmethod)[source]

Bases: object

Descriptor that calls a child object’s method. e.g. >>> class A(object): >>> childkeys = Alias(‘child.keys’) >>> child = dict() >>> >>> a = A() >>> a.child[‘key’] = ‘value’ >>> a.childkeys() #calls a.child.keys(), returns [‘key’] [‘key’]

class moldesign.utils.classes.Attribute(name)[source]

Bases: object

For overriding a property in a superclass - turns the attribute back into a normal instance attribute

class moldesign.utils.classes.Categorizer(keyfn, iterable)[source]

Bases: dict

Create a dict of lists from an iterable, with dict keys given by keyfn

add(item)[source]
class moldesign.utils.classes.DictLike(**kwargs)[source]

Bases: object

This just wraps normal dicts so that other classes don’t have to inherit from a built-in class, which apparently breaks pickle quite frequently.

class moldesign.utils.classes.DotDict[source]

Bases: dict

Dict with items accessible as attributes

class moldesign.utils.classes.ExclusiveList(iterable=None, key=None)[source]

Bases: object

Behaves like a list, but won’t allow duplicate items with duplicate keys to be added.

append(obj)[source]
clear()[source]
extend(iterable)[source]
pop(index=None)[source]
remove(obj)[source]
class moldesign.utils.classes.OrderedDotDict(*args, **kwds)[source]

Bases: collections.OrderedDict

Dict with items accessible as attributes

class moldesign.utils.classes.SortedCollection(iterable=(), key=None)[source]

Bases: object

Sequence sorted by a key function.

TAKEN WITHOUT MODIFICATION FROM: https://code.activestate.com/recipes/577197-sortedcollection/ (EDIT: removed __reduce__ - better behavior with __dict__ states)

SortedCollection() is much easier to work with than using bisect() directly. It supports key functions like those use in sorted(), min(), and max(). The result of the key function call is saved so that keys can be searched efficiently.

Instead of returning an insertion-point which can be hard to interpret, the five find-methods return a specific item in the sequence. They can scan for exact matches, the last item less-than-or-equal to a key, or the first item greater-than-or-equal to a key.

Once found, an item’s ordinal position can be located with the index() method. New items can be added with the insert() and insert_right() methods. Old items can be deleted with the remove() method.

The usual sequence methods are provided to support indexing, slicing, length lookup, clearing, copying, forward and reverse iteration, contains checking, item counts, item removal, and a nice looking repr.

Finding and indexing are O(log n) operations while iteration and insertion are O(n). The initial sort is O(n log n).

The key function is stored in the ‘key’ attibute for easy introspection or so that you can assign a new key function (triggering an automatic re-sort).

In short, the class was designed to handle all of the common use cases for bisect but with a simpler API and support for key functions.

>>> from pprint import pprint
>>> from operator import itemgetter
>>> s = SortedCollection(key=itemgetter(2))
>>> for record in [
...         ('roger', 'young', 30),
...         ('angela', 'jones', 28),
...         ('bill', 'smith', 22),
...         ('david', 'thomas', 32)]:
...     s.insert(record)
>>> pprint(list(s))         # show records sorted by age
[('bill', 'smith', 22),
 ('angela', 'jones', 28),
 ('roger', 'young', 30),
 ('david', 'thomas', 32)]
>>> s.find_le(29)           # find oldest person aged 29 or younger
('angela', 'jones', 28)
>>> s.find_lt(28)           # find oldest person under 28
('bill', 'smith', 22)
>>> s.find_gt(28)           # find youngest person over 28
('roger', 'young', 30)
>>> r = s.find_ge(32)       # find youngest person aged 32 or older
>>> s.index(r)              # get the index of their record
3
>>> s[3]                    # fetch the record at that index
('david', 'thomas', 32)
>>> s.key = itemgetter(0)   # now sort by first name
>>> pprint(list(s))
[('angela', 'jones', 28),
 ('bill', 'smith', 22),
 ('david', 'thomas', 32),
 ('roger', 'young', 30)]
clear()[source]
copy()[source]
count(item)[source]

Return number of occurrences of item

find(k)[source]

Return first item with a key == k. Raise ValueError if not found.

find_ge(k)[source]

Return first item with a key >= equal to k. Raise ValueError if not found

find_gt(k)[source]

Return first item with a key > k. Raise ValueError if not found

find_le(k)[source]

Return last item with a key <= k. Raise ValueError if not found.

find_lt(k)[source]

Return last item with a key < k. Raise ValueError if not found.

index(item)[source]

Find the position of an item. Raise ValueError if not found.

insert(item)[source]

Insert a new item. If equal keys are found, add to the left

insert_right(item)[source]

Insert a new item. If equal keys are found, add to the right

key

key function

remove(item)[source]

Remove first occurence of item. Raise ValueError if not found

class moldesign.utils.classes.Synonym(name)[source]

Bases: object

An attribute (class or intance) that is just a synonym for another.

moldesign.utils.databases module

class moldesign.utils.databases.CompressedJsonDbm(filename, flag='r', dbm=<module 'dumbdbm' from '/usr/local/Cellar/python/2.7.12/Frameworks/Python.framework/Versions/2.7/lib/python2.7/dumbdbm.pyc'>)[source]

Bases: object

Quick-and-dirty interface to a DBM file

class moldesign.utils.databases.Hdf5Dbm(filename, mode='r', compression=10)[source]

Bases: object

A simplified DBM interface backed by HDF5.

Like DBMs, it only stores strings. Unlike DBMs, the strings must be completely ascii

Note

if something other than a string is passed, it will be converted using json.dumps

This was written only because dumbdbm has a tendency to get corrupted (because it doesn’t support read-only mode), none of the other standard library DBMs are portable, and other alternatives (like semidbm) don’t play nice with git.

close()[source]
classmethod open(*args, **kwargs)[source]

Synonym for __init__ for compatibility with stdlib DBM modules

class moldesign.utils.databases.ReadOnlyDumb(filebasename, mode)[source]

Bases: dumbdbm._Database

A read-only subclass of dumbdbm

All possible operations that could result in a disk write have been turned into no-ops or raise exceptions

moldesign.utils.utils module

My standard utilities. Intended to be included in all projects Obviously everything included here needs to be in the standard library (or numpy)

class moldesign.utils.utils.BaseTable(categories, fileobj=None)[source]

Bases: object

add_line(obj)[source]
getstring()[source]
writeline(newline)[source]
class moldesign.utils.utils.MarkdownTable(*categories)[source]

Bases: moldesign.utils.utils.BaseTable

getstring()[source]
markdown(replace=None)[source]
writeline(newline)[source]
class moldesign.utils.utils.PipedFile(fileobj, filename='pipe')[source]

Bases: object

Allows us to pass data by filesystem path without ever writing it to disk To prevent deadlock, we spawn a thread to write to the pipe Call it as a context manager: >>> with PipedFile(‘file contents’,filename=’contents.txt’) as pipepath: >>> print open(pipepath,’r’).read()

class moldesign.utils.utils.PrintTable(formatstr, fileobj=<open file '<stdout>', mode 'w'>)[source]

Bases: moldesign.utils.utils.BaseTable

getstring()[source]
writeline(line)[source]
moldesign.utils.utils.binomial_coefficient(n, k)[source]
moldesign.utils.utils.from_filepath(func, filelike)[source]

Run func on a temporary path assigned to filelike

moldesign.utils.utils.if_not_none(item, default)[source]

Equivalent to item if item is not None else default

moldesign.utils.utils.is_color(s)[source]

Do our best to determine if “s” is a color spec that can be converted to hex :param s: :return:

moldesign.utils.utils.is_printable(s)[source]
moldesign.utils.utils.make_local_temp_dir()[source]
moldesign.utils.utils.make_none()[source]
class moldesign.utils.utils.methodcaller(*args, **kwargs)[source]

The pickleable implementation of the standard library operator.methodcaller.

This was copied without modification from: https://github.com/python/cpython/blob/065990fa5bd30fb3ca61b90adebc7d8cb3f16b5a/Lib/operator.py

The c-extension version is not pickleable, so we keep a copy of the pure-python standard library code here. See https://bugs.python.org/issue22955

Original documentation: Return a callable object that calls the given method on its operand. After f = methodcaller(‘name’), the call f(r) returns r.name(). After g = methodcaller(‘name’, ‘date’, foo=1), the call g(r) returns r.name(‘date’, foo=1).

moldesign.utils.utils.pairwise_displacements(a)[source]

from http://stackoverflow.com/questions/22390418/pairwise-displacement-vectors-among-set-of-points

moldesign.utils.utils.printflush(s, newline=True)[source]
class moldesign.utils.utils.progressbar(description)[source]

Bases: object

Create a progress bar for a calculation

The context manager provides a callback which needs to be called as set_progress(percent), where percent is a number between 0 and 100

Examples

>>> import time
>>> with progressbar('count to 100') as set_progress:
>>>     for i in xrange(100):
>>>         time.sleep(0.5)
>>>         set_progress(i+1)
set_progress(percent)[source]
moldesign.utils.utils.recursionlimit_atleast(*args, **kwds)[source]

Context manager for temporarily raising the context manager’s the interpreter’s maximum call stack size (misleading called the recursion limit)

Notes

This will explicitly reset the the recursion limit when we exit the context;
any intermediate recursion limit changes will be lost

This will not lower the limit n is less than the current recursion limit.

class moldesign.utils.utils.redirect_stderr(new_target)[source]

Bases: moldesign.utils.utils._RedirectStream

From python3.4 stdlib

class moldesign.utils.utils.redirect_stdout(new_target)[source]

Bases: moldesign.utils.utils._RedirectStream

From python3.4 stdlib

moldesign.utils.utils.remove_directories(list_of_paths)[source]

Removes non-leafs from a list of directory paths

class moldesign.utils.utils.textnotify(startmsg)[source]

Bases: object

Print a single, immediately flushed line to log the execution of a block.

Prints ‘done’ at the end of the line (or ‘ERROR’ if an uncaught exception)

Examples

>>> import time
>>> with textnotify('starting to sleep'):
>>>     time.sleep(3)
starting to sleep...done
>>> with textnotify('raising an exception...'):
>>>     raise ValueError()
raising an exception...error
ValueError [...]