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.
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. |
Return the amount of time remaining. Returns None if there is no specified timeout, and a negative time if there is no time remaining.
Restart the timer.
Bases: object
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.
[K[A [K1
2 [A
[K2
>>> sb.reset()
[A
[K[A [K
sys.stdout has been hijacked so that even regular printing has control characters:
>>> sb._bars {<module 'sys' (built-in)>: []} [A[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[C[CRegular messages can be interspersed with the status bars, but you must use the print_() method:
>>> bar = sb.new_bar() >>> sb.print_('Hi') [AHiThis 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') [AHi [A[C[C>>> 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 [A
<BLANKLINE> 6 of 6
>>> bar.print_('Hi')
[A
<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 |
Resister sys.stdout and sys.stderr for status bar behaviour. Returns an instance that will restore these when deleted.
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_ |
Try to clear all printed lines so far.
Delete the bar.
Display a message, first clearing the previous message.
Regular print, not to disrupt messages, nor to be erased.
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.
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
[A[C[C
You can create another instance
>>> sb2 = StatusBar()
>>> print('hi')
hi
[A[C[C
Once both of these are deleted, then the status bar system will close:
>>> del sb
>>> print('hi')
[Ahi
[A[C[C
>>> del sb2
>>> print('hi')
hi
Return a new status bar below all the rest.
Parameters : | keep_last : bool, optional
|
---|
>>> bar.delete()
[A
>>> bar = sb.new_bar(keep_last=True)
>>> bar.msg('Hi 1')
Hi 1
>>> bar.msg('Hi 2')
[A
>>> bar.delete()
[A
Regular print, not to disrupt messages, nor to be erased. Adds a newline like the print statement.
Clear out all bars. The bars become inactive
Regular print, not to disrupt messages, nor to be erased. Does not add a new line.
Return the current memory usage by the process in bytes.
Print the memory usage minus since in specified units and return new memory usage.
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
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'>]
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.
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
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
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.
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)
exclude : set
|
---|