Previous topic

mmf.sphinx.ext.mmfext

This Page

mmf.utils

debug Debugging tools
decorators Decorators for labeling test objects
examples
init Tools for writing automatic __init__.py files.
input Input processing functions for simplifying input from the user.
logging Logging Facilities
mdb Some debugging utilities
mmf_plot Plotting tools.
mmf_profile Profiling Module based on hotshot using line-event data.
mmf_sympy Tools for using sympy to generate code.
mmf_test Test tools:
mmf_theano Tools for working with theano.
mmf_warnings Enhanced warning facilities.
myvectorize New version of numpy.vectorize to support kwargs.
pymacs Some tools for using Pymacs in Emacs.
run Running Programs
script Utilities for writing scripts.
text Provides a subclass of textwrap.TextWrapper that allows for multiple
Timer([timeout]) An object that is True for a specified period of time, then
StatusBar([sys]) Simple static class providing status bars at the bottom of the window where status messages can be displayed.
memory() Return the current memory usage by the process in bytes.
print_mem([since, unit]) Print the memory usage minus since in specified units and
get_args(kwargs, args) Return a dictionary with only the specified arguments.
all_bases(cls) Return a list of all the parent classes.
memoize(f[, hash]) Return a memoizing function.
unique_list(l[, preserve_order]) Make list contain only unique elements but preserve order.
isequal(a, b) Performs a somewhat robust comparison of a and b.
inline(code[, arg_names, support_code, ...]) My version of scipy.weave.inline() which adds the hash
vectorize(pyfunc[, otypes, doc, argspec, ...]) Wrapper for numpy.vectorize() which provides support for keyword arguments.

This file contains a set of utility functions and classes useful for various miscellaneous tasks.

class mmf.utils.Timer(timeout=None)

Bases: object

An object that is True for a specified period of time, then false. If timeout is None, then this is always True.

>>> import time
>>> have_time = Timer(1)    # 1 second timer...
>>> start = time.time()
>>> have_time.reset()
>>> while have_time:
...     pass
>>> elapsed = time.time() - start
>>> elapsed                 
1.00...
>>> def tst():
...     have_time.reset()
...     time.sleep(0.5)
...     print round(have_time.remaining(),2)
...     time.sleep(1.0)
...     print round(have_time.remaining(),2)
>>> tst()
0.5
-0.5

Methods

remaining() Return the amount of time remaining.
reset() Restart the timer.
__init__(timeout=None)
remaining()

Return the amount of time remaining. Returns None if there is no specified timeout, and a negative time if there is no time remaining.

reset()

Restart the timer.

class mmf.utils.StatusBar(sys=<module 'sys' (built-in)>)

Bases: object

Simple static class providing status bars at the bottom of the

window where status messages can be displayed. If the curses module is available, then the messages will overwrite each other.

Note

Presently, in order for this to function properly, all output must be through the methods here. Using print() for example would mess up the display somewhat. In order to prevent this, we hijack sys.stdout and sys.stderr to reroute all regular output through this mechanism.

If these are subverted at a later stage, then all status bar behaviour reverts to standard printing.

Todo

If IO occurs that does not use print_(), then the current message will be left on the screen and some of the IO will get clobbered. The curses module could probably be used to control the display so that the messages only appear in a special part of the screen, but I have not figured out how to do this in a way that leaves the rest of the screen unchanged and still visible.

Notes

This class should be instantiated to provide status bar functionality to a sys module. Use mmf.utils.status_bar to instantiate new status bars for sys.stdout and sys.stderr.

1

2


 1

2 
0

1


2

>>> sb.reset()


 

sys.stdout has been hijacked so that even regular printing has control characters:

>>> sb._bars
{<module 'sys' (built-in)>: []}


Regular messages can be interspersed with the status bars, but you must use the print_() method:

>>> bar = sb.new_bar()
>>> sb.print_('Hi')
Hi

This is almost the same as regular printing, except that print sends two write statements – one for the message, and one for the newline:

>>> print('Hi')
Hi

>>> for n in range(7):
...     bar.msg('%i of 6'%n)
...     if n%3 == 0:
...         bar.print_('Regular message %i'%n)
...     time.sleep(0.1)
0 of 6

Regular message 0
<BLANKLINE> 0 of 6 
1 of 6

2 of 6

3 of 6

Regular message 3
<BLANKLINE> 3 of 6 
4 of 6

5 of 6

6 of 6

Regular message 6

<BLANKLINE> 6 of 6

>>> bar.print_('Hi')

Hi

<BLANKLINE> 6 of 6

Always close the status bar when you are done: >>> sb.close()

Methods

Bar

Resister sys.stdout and sys.stderr for status bar behaviour. Returns an instance that will restore these when deleted.

Methods

Bar
__init__(sys=<module 'sys' (built-in)>)

Resister sys.stdout and sys.stderr for status bar behaviour. Returns an instance that will restore these when deleted.

class Bar(sys, keep_last=False)

Bases: object

Represents a single bar. The screen can have several stacked bars. These are returned by StatusBar.new_bar().

Methods

clear
delete
msg
print_
clear()

Try to clear all printed lines so far.

delete()

Delete the bar.

msg(msg)

Display a message, first clearing the previous message.

print_(msg)

Regular print, not to disrupt messages, nor to be erased.

classmethod StatusBar.clear(msg, stream=None, bar=None, sys=<module 'sys' (built-in)>)

Clear out all printed lines in status bar bar and change the message to msg.

If bar is None, then clear all bars and display msg as a printed message above.

classmethod StatusBar.close(sys=<module 'sys' (built-in)>)

Close the status bar, restoring sys.stdout and sys.stderr. The present messages will be left in place.

Note

close() is a static method so it may be called from the class too. This will close the only active status bar without having to have a handle to it.

Examples

Initialize a status bar by creating an instance.

>>> print('hi')
hi
>>> sb = StatusBar()
>>> print('hi')
hi


You can create another instance

>>> sb2 = StatusBar()
>>> print('hi')
hi


Once both of these are deleted, then the status bar system will close:

>>> del sb
>>> print('hi')
hi


>>> del sb2
>>> print('hi')
hi
classmethod StatusBar.new_bar(keep_last=False, sys=<module 'sys' (built-in)>)

Return a new status bar below all the rest.

Parameters :

keep_last : bool, optional

If True, then the last message is kept after the bar is deleted (unless explicitly cleared).

Hi 2
>>> bar.delete()


>>> bar = sb.new_bar(keep_last=True)
>>> bar.msg('Hi 1')
Hi 1
>>> bar.msg('Hi 2')

Hi 2
>>> bar.delete()

Hi 2
<BLANKLINE>
classmethod StatusBar.print_(msg, stream=None, sys=<module 'sys' (built-in)>)

Regular print, not to disrupt messages, nor to be erased. Adds a newline like the print statement.

classmethod StatusBar.reset(sys=<module 'sys' (built-in)>)

Clear out all bars. The bars become inactive

classmethod StatusBar.write_(msg, stream, sys=<module 'sys' (built-in)>)

Regular print, not to disrupt messages, nor to be erased. Does not add a new line.

mmf.utils.memory()

Return the current memory usage by the process in bytes.

mmf.utils.print_mem(since=0.0, unit='MB')

Print the memory usage minus since in specified units and return new memory usage.

mmf.utils.get_args(kwargs, args)

Return a dictionary with only the specified arguments.

Example: >>> def f(x,y,z=1): return x+y*z >>> def g(x,t): return x*t >>> def h(x,**kwargs): ... return (f(x=x,**get_args(kwargs,[‘y’,’z’])) + ... g(x=x,**get_args(kwargs,[‘t’]))) >>> h(1,y=2,t=3) 6 >>> def hh(x,**kwargs): ... return (f(x=x,**get_args(kwargs,f)) + ... g(x=x,**get_args(kwargs,g))) >>> hh(1,y=2,t=3) 6

mmf.utils.all_bases(cls)

Return a list of all the parent classes.

>>> class A(object): pass
>>> class B(object): pass
>>> class C(A,B): pass
>>> all_bases(C)            
[<class '...A'>, <class '...B'>, <type 'object'>]
mmf.utils.memoize(f, hash=None)

Return a memoizing function.

>>> def f(x):
...    print "Computing f(%s)"%`x`
...    return x*x
>>> g = memoize(f)
>>> f(3)
Computing f(3)
9
>>> f(3)
Computing f(3)
9
>>> g(3)
Computing f(3)
9
>>> g(3)
9

Note that the memoization is based on equality testing, and thus may not be suitable for floating-point applications.

mmf.utils.unique_list(l, preserve_order=True)

Make list contain only unique elements but preserve order.

>>> l = [1,2,4,3,2,3,1,0]
>>> unique_list(l)
[1, 2, 4, 3, 0]
>>> l
[1, 2, 4, 3, 2, 3, 1, 0]
>>> unique_list([[1],[2],[2],[1],[3]])
[[1], [2], [3]]

See also

http
//www.peterbe.com/plog/uniqifiers-benchmark
mmf.utils.isequal(a, b)

Performs a somewhat robust comparison of a and b.

>>> import numpy as np
>>> isequal(1,2)
False
>>> isequal([1,2], [1.0,2.0])
True
>>> isequal(np.array([1,2]), [1,3])
False
>>> isequal([np.array([1,2])], [np.array([1,2])])
True
mmf.utils.inline(code, arg_names=[], support_code=None, include_dirs=[], library_dirs=[], extra_link_args=[], libraries=[], define_macros=[], verbose=2, release_GIL=True, **kw)

My version of scipy.weave.inline() which adds the hash of support_code to the code to ensure that changes in support_code trigger a recompile.

mmf.utils.vectorize(pyfunc, otypes='', doc=None, argspec=None, exclude=None)

Wrapper for numpy.vectorize() which provides support for keyword arguments.

This function takes the same parameters as numpy.vectorize() in addition to the following:

Parameters :

argspec : (argnames, vargs, kwargs, defaults)

Tuple returned by inspect.getargspec() defining the argument names argnames in order and the default values defaults for the trailing arguments. Use this, for example, if inspect.getargspec(pyfunc) will fail for some reason.

exclude : set

Set of arguments to exclude from vectorization.