| |
- __builtin__.object
-
- Abort
- Bar
- Converge
- Monitor
- OpenBar
- StatusLine
- Terminate
- Until
class Abort(__builtin__.object) |
|
Check keyboard buffer if a certain key has been pressed.
Initialize before your loop (e.g., of an iterative algorithm). Check within
the loop body (e.g., break the loop on positive check). Finalize (end)
after the loop. Do not forget to finalize, so that your terminal is put
back into normal mode! Or use it as context manager (use the with
statement), then finalization should be done automatically.
Example:
> import time
> with Abort() as a:
> for i in range(10):
> time.sleep(1) # do stuff
> if a.check():
> break # leave loop early, because "q" has been pressed
> print e.buff |
|
Methods defined here:
- __del__(self)
- __enter__(self)
- __exit__(self, exc_type, exc_value, exc_traceback)
- __init__(self, key='q', timeout=0)
- Initialize. Specify key and timeout. Put terminal into cbreak mode.
Future plans:
--> enable key combinations (e.g., CTRL+q)
--> enable special keys (e.g., ESC)
- check(self)
- Check if the key has been pressed in the meanwhile. All other
contents of the keyboard buffer will be lost and cannot be
recovered.
- end(self)
- Finalize. Put terminal back into normal mode. Return string
buffer.
- report(self)
- If this handler was triggered, display a message.
- reset(self)
- Reset the abort handler.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class Bar(__builtin__.object) |
|
Display progress bar of a certain loop. Instantiate it before loop. Call
the method "step" at the end of each loop (don't forget to do the same
before any occurence of "continue"). If loop is left early (break), or if
you want to make sure the line break is done after the loop, call the
method "end". To reset the counter, call the method "reset".
On initialization, the number of iterations "nstep" must be specified.
Additional keyword arguments: text : User-defined text message (instead
of "Progress") width : Width of the current shell window. Default: 80
verbose : If set to False, the progress bar isn't shown etc : If set to
True, show estimated time to complete
This class is only meant to be used in loops where the number of steps is
known before entering the loop. This excludes all iterative algorithms that
leave the loop only on some convergence criterions after a variable number
of steps (e.g., while-loops). Of course, one could "estimate" the number of
loops that will probably be used, but this is not the way this class is
intended to be used.
Future ideas:
--> multiple progress bars at once, to show status of multiple subprocesses
--> allow nested progress bars, passing and multiplying step numbers to the
inner-next instance
--> report elapsed time instead of printing "ETC 0 sec"
--> save start and end times (and duration), to check them later
Written by Daniel Jung, Jacobs University Bremen, Germany (2011-2012). |
|
Methods defined here:
- __del__(self)
- __enter__(self)
- __exit__(self, exc_type, exc_value, exc_traceback)
- __init__(self, nstep=1, text='progress', width=None, verbose=True, etc=True)
- end(self)
- jump(self, howmany=1)
- Skip one or several steps. Using this instead of step is only
important for the correct calculation of the estimated time to complete
(ETC).
- reset(self)
- step(self, howmany=1)
- Move one or several steps forward.
- write(self)
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
Data and other attributes defined here:
- __created__ = '2011-09-13'
- __modified__ = '2012-09-04'
|
class Converge(__builtin__.object) |
|
Check data for convergence criterion in some sort of iteration step of
an interative algorithm. Specify a certain tolerance (accuracy) that should
be reached. Increase the "smooth value" to average over the last few
Deltas.
Future plans:
--> choose from various convergence criterions (also based on standard
error)
--> could choose from mean, gmean, min, max, max-min (peak-to-peak), ...
(find more under http://en.wikipedia.org/wiki/Average)
--> offer relative and absolute versions of each criterion
--> let the user specify his own criterion (as a function object)
--> use Cython, write version that is callable from C, support OpenMP
--> add feature to remember several values (e.g., 5) and check that all the
deltas are small enough (then, it is not enought that "by chance" the
delta value drops below the tolerance) |
|
Methods defined here:
- __enter__(self)
- __exit__(self, exc_type, exc_value, exc_traceback)
- __init__(self, tol=None, smooth=1)
- check(self, data)
- Check convergence criterion. Return True if the requested accuracy
is reached, False otherwise. If data is None, return False.
- delta(self)
- Return the current mean delta (based on the last call of "check").
Return -1 if the delta list is still empty.
- report(self)
- If this handler was triggered, display a message.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class Monitor(__builtin__.object) |
|
Monitor the values of certain variables within a loop (e.g., for
iterative algorithms). Uses the class StatusLine.
Update the line by using carriage return each time the method "step" is
called. Do this until the method "end" is called (then, a line break is
issued).
Future plans:
--> support complex numbers |
|
Methods defined here:
- __del__(self)
- __enter__(self)
- __exit__(self, exc_type, exc_value, exc_traceback)
- __init__(self, **kwargs)
- end(self)
- Finish monitoring values, make the line break (if not already done).
Update the line one last time.
- remove(self, *names)
- Stop monitoring the specified variables.
- remove_value(self, name)
- Stop monitoring the specified variable.
- reset(self)
- Reset this object. Empty the list of values and begin a new status
line.
- set_delay(self, delay)
- Set the delay of the StatusLine instance.
- update(self, **values)
- Update one or more values, or add new variables to monitor.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class OpenBar(__builtin__.object) |
|
Display progress bar of an unlimited iteration process ("open-end"). Use
this if the total number of iterations is unclear, i.e. the loop may be
exited at any point (e.g., while loop). However, to display a progress bar,
some measure of progress must still be defined (start and target values).
If the start value is None, use the value given by the first call of step.
Intended use: For example, an iterative averaging process will be aborted
as soon as a certain accuracy has been reached. The progress measure will
be this accuracy.
Future plans:
--> use something different than linear extrapolation |
|
Methods defined here:
- __init__(self, target, start=None, text='progress', width=None, verbose=True, etc=False)
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class StatusLine(__builtin__.object) |
|
Show a status line that is updated by the user every once in a while
using "carriage return". |
|
Methods defined here:
- __del__(self)
- __enter__(self)
- __exit__(self, exc_type, exc_value, exc_traceback)
- __init__(self, line='', delay=0.0, cols=None)
- Initialize status line object. Can be given an initial line, a
minimum delay time and a custom number of columns (terminal width, will
be obtained automatically on Unix systems).
- end(self)
- End status line, make line break (only if this status line has
already been used).
- reset(self)
- Reset status line (make a line break if neccessary and begin a new
status line).
- update(self, line)
- Update the line by the given string, replacing the old line.
The line only gets printed if the line has changed, and if the given
delay time has been passed. If now is True, ignore the delay.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class Terminate(__builtin__.object) |
|
Trap the TERM signal (15). For example, you can stop an interative
algorithm in a controlled way, leaving the loop after the next iteration
and still saving the results. This is done by sending the TERM signal to
the process (also remotely), i.e. in the shell you can do "kill 12345", or
you can use "top".
Works on Unix systems (Linux etc.). |
|
Methods defined here:
- __del__(self)
- Remove the trap, set action back to system defaults before the
handler is deleted.
- __enter__(self)
- Enable context manager functionality, enter context.
- __exit__(self, exc_type, exc_value, exc_traceback)
- Leave context.
- __init__(self)
- Initialize the terminate handler.
- check(self)
- Return True if a TERM signal has been received, otherwise False.
- end(self)
- Remove the trap. Set the action for the TERM signal back to system
standard.
- report(self)
- If this handler was triggered, display a message.
- reset(self)
- Reset the terminate handler, setting the flag back to False and
waiting for a new TERM signal.
- terminate(self, signal, frame)
- If the TERM signal is received, this method is executed, setting the
flag to True.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
|
class Until(__builtin__.object) |
|
Check if a certain date/time has been reached. Can be used to
conveniently confine the execution time of an iterative algorithm.
To do:
--> define word "next", excluding the present day, to allow something like
"next tuesday" ==> even if it is tuesday right now, run until next
tuesday |
|
Methods defined here:
- __enter__(self)
- __exit__(self, exc_type, exc_value, exc_traceback)
- __init__(self, until=None)
- Initialize. Specify datetime or duration.
- __iter__(self)
- Return iterator.
- __len__(self)
- __repr__(self)
- check(self)
- Check if the specified time has already been passed.
- goback_first(self)
- Move backward to the beginning of the first day of the month.
- goback_midnight(self)
- Move backward to the beginning of that day (midnight).
- goto_date(self, year, month, mday)
- Move timestamp to the given date.
- goto_day(self, day)
- Move timestamp forward to the next given day of the week.
- goto_mday(self, mday)
- Move timestamp forward to the given day of the month.
- goto_month(self, month)
- Move timestamp forward to the next given month.
- goto_time(self, hours=0, minutes=0, seconds=None)
- Move timestamp forward to the next given time.
- goto_tomorrow(self)
- Move timestamp forward to the next day.
- goto_year(self, year)
- Move timestamp forward to the given year.
- isdatetime(self, string)
- Decide if the given string contains a date-time combination. If so,
return True. Otherwise, it will contain a duration, and False is
returned.
- report(self)
- If this handler was triggered, display a message.
Static methods defined here:
- one_eq(seq, obj)
- Return True if at least one element of the given sequence "seq" is
equal to the object "obj". Otherwise, return False.
- one_in(seq, obj)
- Return True if at least one element of the given sequence "seq" is
contained in the given object "obj". Otherwise, return False.
- one_is(seq, obj)
- Return True if at least one element of the given sequence "seq" is
identical to the object "obj". Otherwise, return False.
- only_digits(string)
- Return True if the given string contains only digits. Otherwise,
return False.
Data descriptors defined here:
- __dict__
- dictionary for instance variables (if defined)
- __weakref__
- list of weak references to the object (if defined)
Data and other attributes defined here:
- DAYNUM = {'fri': 4, 'friday': 4, 'mon': 0, 'monday': 0, 'sat': 5, 'saturday': 5, 'sun': 6, 'sunday': 6, 'thu': 3, 'thursday': 3, ...}
- DAYS = ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday']
- DAYS_SHORT = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun']
- MONTHNUM = {'apr': 4, 'april': 4, 'aug': 8, 'august': 8, 'dec': 12, 'december': 12, 'feb': 2, 'february': 2, 'jan': 1, 'january': 1, ...}
- MONTHS = ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
- MONTHS_SHORT = ['jan', 'feb', 'mar', 'apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec']
- SPECIAL = ['tomorrow']
- day = 'Sun'
- month = 'Dec'
| |