utility

This module contains useful utility functions, decorators, and plotting helpers that are useful throughout the kpfm package.

Should my function go here? If you find yourself wanting it in many IPython notebooks, yes! Just add a docstring and make sure the function is useful on its own (not dependent on any external data, doesn’t make assumptions that limit broader usefulness of the code).

kpfm.util.silent_remove(filename)[source]

If filename exists, delete it. Otherwise, return nothing. See http://stackoverflow.com/q/10840533/2823213.

kpfm.util.align_labels(axes_list, lim, axis='y')[source]

Align matplotlib axis labels to the same horizontal (y-axis) or vertical (x-axis) position.

kpfm.util.color2gray(x)[source]

Convert an RGB or RGBA (Red Green Blue Alpha) color tuple to a grayscale value.

kpfm.util.txt_filename(func)[source]

Decorator to allow seamless use of filenames rather than file handles for functions that operate on a text file.

To use this decorator, write the function to take a file object as the function’s first argument.

kpfm.util.h5filename(func)[source]

Decorator to allow seamless use of filenames rather than file handles for functions that operate on an HDF5 file.

To use this decorator, write the function to take an HDF5 file handle as the function’s first argument.

Example: We create a simple function and HDF5 file.

>>> @h5filename
>>> def h5print(fh):
>>>     print(fh.values())
>>>
>>> fh = h5py.File('test.h5')
>>> fh['x'] = 2

We can call the function on the file handle

>>> h5print(fh)
[<HDF5 dataset "x": shape (), type "<i8">]

or call the function on the filename

>>> fh.close()
>>> h5print('test.h5')
[<HDF5 dataset "x": shape (), type "<i8">]
kpfm.util.h5ls_str(g, offset='', print_types=True)[source]

Prints the input file/group/dataset (g) name and begin iterations on its content.

See goo.gl/2JiUQK.

kpfm.util.h5ls(*args)[source]

List the contents of an HDF5 file object or group. Accepts a file / group handle, or a string interpreted as the hdf5 file path.

kpfm.util.prnDict(aDict, br='\n', html=0, keyAlign='l', sortKey=0, keyPrefix='', keySuffix='', valuePrefix='', valueSuffix='', leftMargin=4, indent=1, braces=True)[source]

Return a string representive of aDict in the following format:

{
 key1: value1,
 key2: value2,
 ...
 }

Spaces will be added to the keys to make them have same width.

Parameters:
  • sortKey (0 or 1) – set to 1 if want keys sorted;
  • keyAlign – either ‘l’ or ‘r’, for left, right align, respectively.
  • keySuffix, valuePrefix, valueSuffix (keyPrefix,) – The prefix and suffix to wrap the keys or values. Good for formatting them for html document (for example, keyPrefix=’<b>’, keySuffix=’</b>’). Note: The keys will be padded with spaces to have them equally-wide. The pre- and suffix will be added OUTSIDE the entire width.
  • html (0 or 1) – If set to 1, all spaces will be replaced with ‘&nbsp;’, and the entire output will be wrapped with ‘<code>’ and ‘</code>’.
  • br (string) – Determine the carriage return. If html, it is suggested to set br to ‘<br>’. If you want the html source code easy to read, set br to ``‘<br>
  • '``.

References

version: 04b52 author : Runsun Pan require: odict() # an ordered dict, if you want the keys sorted.