A collection of Python utilities developed for the
"Python for Scientific Computing" book.
Imported modules
|
|
from FuncDependenceViz import *
import getopt
import math
import os
import re
import shutil
import sys
import threading
import time
|
Functions
|
|
|
|
after
|
after ( str, character )
Return part of str after character.
|
|
before
|
before ( str, character )
Return part of str before character.
|
|
debugregex
|
debugregex ( pattern, string )
debugging of regular expressions: write the match and the groups
|
|
dump
|
dump ( obj )
Inspect an object obj by the dir function.
This function first prints the repr(obj) of the object,
then all variables/attributes and their values are
listed, and finally a line listing all functions/methods
is printed.
|
|
f
|
f (
a,
b,
max=1.2,
min=2.2,
)
-- tests ---
|
|
find
|
find (
func,
rootdir,
arg=None,
)
Traverse the directory tree rootdir and call func for each file.
arg is a user-provided argument transferred to func(filename,arg).
|
|
findprograms
|
findprograms (
programs,
searchlibs=[],
write_message=0,
)
Given a list of programs (programs), find the full path
of each program and return a dictionary with the program
name as key and the full path as value. The value is None
if the program is not found.
The program list can either be a list/tuple or a
dictionary (in the latter case, the keys are the programs
and the values are explanations of the programs).
If write_message is true, the function writes a message
if a program is not found. In that case, None is returned
if not all programs are found. Example on usage:
if findprograms(['plotmtv'], write_message=1):
os.system(plotmtv ... )
programs = [gs , 'convert']
path = findprograms(programs)
if path['gs']:
os.system(gs ... )
if path['convert']:
os.system(convert ... )
programs = { gs : Ghostscript: file format conversions ,
convert : File format conversion from ImageMagick ,
}
if not findprograms(programs, write_message=1):
print the mentioned programs need to be installed
sys.exit(1)
Exceptions
|
|
TypeError, 'platform %s/%s not supported' %( sys.platform, os.name )
|
|
|
fontscheme1
|
fontscheme1 ( root )
Alternative font scheme for Tkinter-based widgets.
|
|
fontscheme2
|
fontscheme2 ( root )
Alternative font scheme for Tkinter-based widgets.
|
|
lines2paragraphs
|
lines2paragraphs ( lines )
Return a list of paragraphs from a list of lines
(normally holding the lines in a file).
|
|
message
|
message ( m )
|
|
movefiles
|
movefiles (
files,
destdir,
confirm=True,
verbose=True,
copy=True,
)
Move a set of files to a a destination directory tree,
but let the original complete path be reflected in the
destination tree. files list of filenames
destdir root of destination directory tree
confirm let the user confirm movement of each file
verbose write out the original and new path of each file
copy True: copy, False: move
The function is useful for backing up or temporarily moving
files; the files are easily restored in their original
locations since the original complete path is maintained in
the destination directory tree.
|
|
pathsearch
|
pathsearch (
programs=[],
modules=[],
where=0,
)
Given a list of programs (programs) and modules (modules),
search for these programs and modules in the directories
in the PATH and PYTHONPATH environment variables, respectively.
Check that each directory has read and write access too.
The function is useful for checking that PATH and PYTHONPATH
are appropriately set in CGI scripts.
|
|
pow_eff
|
pow_eff (
a,
b,
powfunc=math.pow,
)
Returns a^b. Smart function that happened to be slower
than a straight math.pow.
|
|
str2obj
|
str2obj ( s )
Turn string s into the corresponding object.
eval(s) normally does this, but if s is just a string ready
from file, GUI or the command-line, eval will not work when
s really represents a string:
>>> eval(some string )
Traceback (most recent call last):
SyntaxError: unexpected EOF while parsing
It tries to parse some string as Python code. In this function we try to eval(s), and if it works, we
return that object. If it does not work, s probably has
meaning as just a string, and we return just s.
You can safely apply eval on the output of this function.
>>> from py4cs.funcs import str2obj
>>> s = str2obj(0.3 )
>>> s, type(s)
0.3 <type float >
>>> s = str2obj(3 )
>>> s, type(s)
3 <type int >
>>> s = str2obj((1,8) )
>>> s, type(s)
(1, 8) <type tuple >
>>> s = str2obj(some string )
some string <type str >
|
|
timer
|
timer (
func,
args=[],
kwargs={},
repetitions=10,
comment='',
)
Run a function func, with arguments given by the tuple
args and keyword arguments given by the dictionary kwargs,
a specified number of times (repetitions) and
write out the elapsed and CPU times together.
|
|
timer_system
|
timer_system ( command, comment='' )
Run an os.system(command) statement and measure the CPU time.
With os.system, the CPU time is registered as the user and
system time of child processes.
|
Classes
|
|
|
|