Classes defined in module utils
A class for autogenerating sequences of names.
The name is a string including a numeric part, which is incremented at each call of the ‘next()’ method.
The constructor takes name template and a possible extension as arguments. If the name starts with a non-numeric part, it is taken as a constant part. If the name ends with a numeric part, the next generated names will be obtained by incrementing this part. If not, a string ‘-000’ will be appended and names will be generated by incrementing this part.
If an extension is given, it will be appended as is to the names. This makes it possible to put the numeric part anywhere inside the names.
Example:
>>> N = NameSequence('abc.98')
>>> [ N.next() for i in range(3) ]
['abc.98', 'abc.99', 'abc.100']
>>> N = NameSequence('abc-8x.png')
>>> [ N.next() for i in range(3) ]
['abc-8x.png', 'abc-9x.png', 'abc-10x.png']
>>> NameSequence('abc','.png').next()
'abc-000.png'
>>> N = NameSequence('/home/user/abc23','5.png')
>>> [ N.next() for i in range(2) ]
['/home/user/abc235.png', '/home/user/abc245.png']
Return the next name in the sequence
Return the next name in the sequence without incrementing.
Return a UNIX glob pattern for the generated names.
A NameSequence is often used as a generator for file names. The glob() method returns a pattern that can be used in a UNIX-like shell command to select all the generated file names.
Return a (sorted) list of files matching the name pattern.
A function may be specified to sort/filter the list of file names. The function should take a list of filenames as input. The output of the function is returned. The default sort function will sort the filenames in a human order.
A class to compute the difference between two dictionaries
Parameters:
The differences are reported as sets of keys: - items added - items removed - keys same in both but changed values - keys same in both and unchanged values
Return the keys in current_dict but not in past_dict
Return the keys in past_dict but not in current_dict
Return the keys for which the value has changed
Return the keys with same value in both dicts
Return True if both dicts are equivalent
Functions defined in module utils
Split a file name in dir,base,ext tuple.
Parameters:
Returns a tuple dir,base,ext:
Examples:
>>> splitFilename("cc/dd/aa.bb")
('cc/dd', 'aa', '.bb')
>>> splitFilename("cc/dd/aa.")
('cc/dd', 'aa', '.')
>>> splitFilename("..//aa.bb")
('..', 'aa', '.bb')
>>> splitFilename("aa.bb")
('', 'aa', '.bb')
>>> splitFilename("aa/bb")
('aa', 'bb', '')
>>> splitFilename("aa/bb/")
('aa/bb', '', '')
>>> splitFilename("/aa/bb")
('/aa', 'bb', '')
>>> splitFilename(".bb")
('', '.bb', '')
>>> splitFilename("/")
('/', '', '')
>>> splitFilename(".")
('', '.', '')
>>> splitFilename("")
('', '', '')
>>> splitFilename("cc/dd/aa.bb",accept_ext=['.aa','.cc'])
('cc/dd', 'aa.bb', '')
>>> splitFilename("cc/dd/aa.bb",reject_ext=['.bb'])
('cc/dd', 'aa.bb', '')
Build a filename from a directory path, filename and optional extension.
The dirname and basename are joined using the system path separator, and the extension is added at the end. Note that no ‘.’ is added between the basename and the extension. While the extension will normally start with a ‘.’, this function can also be used to add another tail to the filename.
This is a convenience function equivalent with:
os.path.join(dirname,basename) + ext
Change the extension of a file name.
This function splits the specified file name in a base name and an extension, replaces the extension with the specified one, and returns the reassembled file name. If the filename has no extension part, the specified extension is just appended.
Parameters:
Returns a file name with the specified extension.
Example:
>>> changeExt('image.png','.jpg')
'image.jpg'
>>> changeExt('image','.jpg')
'image.jpg'
>>> changeExt('image','jpg') # Deprecated
'image.jpg'
>>> changeExt('image.1','.jpg')
'image.jpg'
>>> changeExt('image.1','.jpg',reject_ext=['.1'])
'image.1.jpg'
Derive a project name from a file name. ` The project name is the basename of the file without the extension. It is equivalent with splitFilename(fn)[1]
Perform tilde expansion on a filename.
Bash, the most used command shell in Linux, expands a ‘~’ in arguments to the users home direction. This function can be used to do the same for strings that did not receive the bash tilde expansion, such as strings in the configuration file.
Return a list with all known image extensions.
Return a description of the specified file type.
The description of known types are listed in a dict file_description. If the type is unknown, the returned string has the form TYPE files (*.type)
Normalize a filetype string.
The string is converted to lower case and a leading dot is removed. This makes it fit for use with a filename extension.
Example:
>>> fileType('pdf')
'pdf'
>>> fileType('.pdf')
'pdf'
>>> fileType('PDF')
'pdf'
>>> fileType('.PDF')
'pdf'
Derive the file type from the file name.
The derived file type is the file extension part in lower case and without the leading dot.
Example:
>>> fileTypeFromExt('pyformex.pdf')
'pdf'
>>> fileTypeFromExt('pyformex')
''
>>> fileTypeFromExt('pyformex.pgf')
'pgf'
>>> fileTypeFromExt('pyformex.pgf.gz')
'pgf.gz'
>>> fileTypeFromExt('pyformex.gz')
'gz'
Return the size in bytes of the file fn
Return the file name for an icon with given name.
If no icon file is found, returns the question mark icon.
Prepend a prefix to a list of filenames.
Return multiple regular expression matches of the same target string.
Return the number of matches of target to regexps.
Check whether target matches any of the regular expressions.
Check whether targes matches none of the regular expressions.
Check whether targets matches all of the regular expressions.
List all files in path.
If listdirs==False, directories are not listed. By default the tree is listed top down and entries in the same directory are unsorted.
exludedirs and excludefiles are lists of regular expressions with dirnames, resp. filenames to exclude from the result.
includedirs and includefiles can be given to include only the directories, resp. files matching any of those patterns.
Note that ‘excludedirs’ and ‘includedirs’ force top down handling.
If symlinks is set False, symbolic links are removed from the list.
Remove a file, ignoring error when it does not exist.
Remove all files below path. If top==True, also path is removed.
Return a list of the pyFormex source .py files.
Finds pattern in the pyFormex source .py files.
Uses the grep program to find all occurrences of some specified pattern text in the pyFormex source .py files (including the examples). Extra options can be passed to the grep command. See man grep for more info.
Returns the output of the grep command.
Set a sane local configuration for LC_NUMERIC.
localestring is the locale string to be set, e.g. ‘en_US.UTF-8’
This will change the LC_ALL setting to the specified string, and set the LC_NUMBERIC to ‘C’.
Changing the LC_NUMERIC setting is a very bad idea! It makes floating point values to be read or written with a comma instead of a the decimal point. Of course this makes input and output files completely incompatible. You will often not be able to process these files any further and create a lot of troubles for yourself and other people if you use an LC_NUMERIC setting different from the standard.
Because we do not want to help you shoot yourself in the foot, this function always sets LC_NUMERIC back to a sane value and we call this function when pyFormex is starting up.
Normalize a string.
Text normalization removes all ‘&’ characters and converts it to lower case.
Convert a text string to have it recognized as reStructuredText.
Returns the text with two lines prepended: a line with ‘..’ and a blank line. The text display functions will then recognize the string as being reStructuredText. Since the ‘..’ starts a comment in reStructuredText, it will not be displayed.
Furthermore, if underline is set True, the first line of the text will be underlined to make it appear as a header.
Underline the first line of a text.
Adds a new line of text below the first line of s. The new line has the same length as the first, but all characters are equal to the specified char.
Compress a file in gzip format.
Parameters:
Returns the name of the compressed file.
Uncompress a file in gzip format.
Parameters:
Returns the name of the decompressed file.
Return the (UNIX) time of last change of file fn.
Return the time needed for evaluating a string.
s is a string with a valid Python instructions. The string is evaluated using Python’s eval() and the difference in seconds between the current time before and after the evaluation is printed. The result of the evaluation is returned.
This is a simple method to measure the time spent in some operation. It should not be used for microlevel instructions though, because the overhead of the time calls. Use Python’s timeit module to measure microlevel execution time.
Return the number of lines in a text file.
Execute an external command.
Execute an external command.
Parameters:
Returns:
Run an external command in a user friendly way.
This uses the system() function to run an external command, adding some extra user notifications of what is happening. If no error occurs, the (sta,out) obtained form the system() function are returned. The value sta will be zero, unless a timeout condition has occurred, in which case sta will be -15 or -9. If the system() call returns with an error that is not a timeout,
Parameters:
If no error occurs in the execution of the command by the system() function, returns a tuple
Example: cmd = ‘sleep 2’ sta,out=runCommand3(cmd,quiet=False, timeout=5.) print (sta,out)
Spawn a child process.
Send the specified signal to the processes in list
Find the name of the user.
Checks whether an application name is rather a script name
Checks whether an application name is rather a script name
Return the docstring from a script file.
This actually returns the first multiline string (delimited by triple double quote characters) from the file. It does relies on the script file being structured properly and indeed including a doctring at the beginning of the file.
Split a string in numerical and non-numerical parts.
Returns a series of substrings of s. The odd items do not contain any digits. Joined together, the substrings restore the original. The even items only contain digits. The number of items is always odd: if the string ends or starts with a digit, the first or last item is an empty string.
Example:
>>> print(numsplit("aa11.22bb"))
['aa', '11', '.', '22', 'bb']
>>> print(numsplit("11.22bb"))
['', '11', '.', '22', 'bb']
>>> print(numsplit("aa11.22"))
['aa', '11', '.', '22', '']
Sort a list of strings in human order.
When human sort a list of strings, they tend to interprete the numerical fields like numbers and sort these parts numerically, instead of the lexicographic sorting by the computer.
Returns the list of strings sorted in human order.
Example: >>> hsorted([‘a1b’,’a11b’,’a1.1b’,’a2b’,’a1’]) [‘a1’, ‘a1.1b’, ‘a1b’, ‘a2b’, ‘a11b’]
Split a string at a sequence of digits.
The input string is split in three parts, where the second part is a contiguous series of digits. The second argument specifies at which numerical substring the splitting is done. By default (pos=-1) this is the last one.
Returns a tuple of three strings, any of which can be empty. The second string, if non-empty is a series of digits. The first and last items are the parts of the string before and after that series. Any of the three return values can be an empty string. If the string does not contain any digits, or if the specified splitting position exceeds the number of numerical substrings, the second and third items are empty strings.
Example:
>>> splitDigits('abc123')
('abc', '123', '')
>>> splitDigits('123')
('', '123', '')
>>> splitDigits('abc')
('abc', '', '')
>>> splitDigits('abc123def456fghi')
('abc123def', '456', 'fghi')
>>> splitDigits('abc123def456fghi',0)
('abc', '123', 'def456fghi')
>>> splitDigits('123-456')
('123-', '456', '')
>>> splitDigits('123-456',2)
('123-456', '', '')
>>> splitDigits('')
('', '', '')
Prefix all the keys of a dict with the given prefix.
The return value is a dict with all the items of d, but where the keys have been prefixed with the given string.
Return a dict with the items whose key starts with prefix.
The return value is a dict with all the items from d whose key starts with prefix. The keys in the returned dict will have the prefix stripped off, unless strip=False is specified.
Return a dict with the items whose key is in keys.
The return value is a dict with all the items from d whose key is in keys. See removeDict() for the complementary operation.
Example:
>>> d = dict([(c,c*c) for c in range(6)])
>>> selectDict(d,[4,0,1])
{0: 0, 1: 1, 4: 16}
Remove a set of keys from a dict.
The return value is a dict with all the items from d whose key is not in keys. This is the complementary operation of selectDict.
Example:
>>> d = dict([(c,c*c) for c in range(6)])
>>> removeDict(d,[4,0])
{1: 1, 2: 4, 3: 9, 5: 25}
Refresh a dict with values from another dict.
The values in the dict d are update with those in src. Unlike the dict.update method, this will only update existing keys but not add new keys.
Return the keys in a dict which have a specified value
The return value is a list with all the keys from d whose value is in keys.
Example:
>>> d = dict([(c,c*c) for c in range(6)])
>>> selectDictValues(d,range(10))
[0, 1, 2, 3]
Returns the sorted keys of a dict.
It is required that the keys of the dict be sortable, e.g. all strings or integers.
Returns a (non)linear response on the input x.
xval and yval should be lists of 3 values: [xmin,x0,xmax], [ymin,y0,ymax]. Together with the exponent exp, they define the response curve as function of x. With an exponent > 0, the variation will be slow in the neighbourhood of (x0,y0). For values x < xmin or x > xmax, the limit value ymin or ymax is returned.
List all fonts known to the system.
Returns a list of path names to all the font files found on the system.
Print useful information about item.
Return info about memory usage
Return the approximate total memory footprint of an object.
This function returns the approximate total memory footprint of an object and all of its contents.
Automatically finds the contents of the following builtin containers and their subclasses: tuple, list, deque, dict, set and frozenset. To search other containers, add handlers to iterate over their contents:
- handlers = {SomeContainerClass: iter,
- OtherContainerClass: OtherContainerClass.get_elements}
Adapted from http://code.activestate.com/recipes/577504/