Myokit contains a module
myokit.tools with functions and classes that are
used throughout Myokit, but are not particularly Myokit-specific.
This module is imported automatically when
import myokit is run.
- class myokit.tools.Benchmarker¶
Allows benchmarking using the with statement.
m,p,x = myokit.load('example') s = myokit.Simulation(m, p) b = myokit.tools.Benchmarker() s.run() print(b.time()) b.reset() s.run() print(b.time())
Formats a (non-integer) number of seconds, returns a string like “5 weeks, 3 days, 1 hour, 4 minutes, 9 seconds”, or “0.0019 seconds”.
timeis passed in, the value from
Prints a message to stdout, preceded by the benchmarker time in us.
Resets this timer’s start time.
Returns the time since benchmarking started (as a float, in seconds).
Capturing printed output¶
- class myokit.tools.capture(fd=False, enabled=True)¶
Context manager that temporarily redirects the current standard output and error streams, and captures anything that’s written to them.
with myokit.tools.capture() as a: print('This will be captured')
Within a single thread, captures can be nested, for example:
with myokit.tools.capture() as a: print('This will be captured by a') with myokit.tools.capture() as b: print('This will be captured by b, not a') print('This will be captured by a again')
Capturing is thread-safe: a lock is used to ensure only a single thread is capturing at any time. For example, if we have a function:
def f(i): with myokit.tools.capture() as a: print(i) ... return a.text()
and this is called from several threads, the
captureacts as a lock (a
threading.RLock) so that one thread will need to finish executing the code within the
withstatement before a second thread can start capturing.
In multiprocessing, no locks are used, and no memory or streams are shared so that this should also be safe.
By default, this method works by simply redirecting
sys.stderr. This captures any output written by the Python interpreter, but does not catch output from C/C++ extensions or subrocesses. To also catch that output start the capture with the optional argument
fd=True, which enables a file descriptor duplication method of redirection.
To easily switch capturing on/off, a switch
enabled=Falsecan be passed in to create a context manager that doesn’t do anything.
Returns the text captured from stderr, or an empty string if nothing was captured or capturing is still active.
Returns the text captured from stdout, or an empty string if nothing was captured or capturing is still active.
Returns the combined text captured from output and error text, if any (output first, then error text).
- myokit.tools.format_path(path, root='.')¶
Formats a path for use in user messages. If the given path is a subdirectory of the current directory this part is chopped off.
rootdirectory may be given explicitly: any subdirectory of this path will be formatted relative to
This function differs from os.path.relpath() in the way it handles paths outside the root: In these cases relpath returns a relative path such as ‘../../’ while this function returns an absolute path.
- myokit.tools.rmtree(path, silent=False)¶
shutil.rmtreethat handles Windows “access denied” errors (when the user is lacking write permissions, but is allowed to set them).
silent=Trueany other exceptions will be caught and ignored.
- myokit.tools.lvsd(s, t)¶
Returns the Levenshtein distance between two strings
Function to use as
keyin a sort, to get natural sorting of strings (e.g. “2” before “10”).
names.sort(key=myokit.tools.natural_sort_key) variables.sort(key=lambda v: myokit.tools.natural_sort_key(v.qname()))
Some of the functions in
tools have deprecated aliases, that will be
removed in future Myokit releases.
Takes a dictionary of
string : floatmappings and returns a single multi-line string, where each line is of the form
key = value, where key is padded with spaces to make the equals signs align.
This method is deprecated and will be removed in future versions of Myokit.