Table Of Contents

Previous topic

pyviblib.util.exceptions

Next topic

pyviblib.util.pse

This Page

pyviblib.util.misc

Miscellaneous

Inheritance diagram of pyviblib.util.misc

Classes

SmartDict
container for storage of overridable options
Command
emulator of lambda functions
PropertiesContainer
class for exposing properties via attributes

Functions

color_html_to_RGB()
convert a color in the HTML format to RGB
color_RGB_to_html()
convert a color in the RGB format to HTML
color_complementary()
generate the complementary of a color
str_to_float()
convenient conversion from string to float
remove_indices_from_list()
remove indices from a list
random_color()
generate a random color
save_matrix()
save a matrix
unique()
make a unique array
is_command_on_path()
check whether a command is on the path
ps2pdf()
convert from PS/EPS to PDF
ppm2gif()
convert from PPM to GIF with Netpbm
rests()
retrieve atomno, i
readconfig_option()
read an option from a section
gen_molecule_name()
generate the molecule name
gen_group_labels()
generate group labels A, B etc.
import_processing()
import and return a processing module
memoize_deterministic()
memoization decorator

Module variables

CACHE_MEMOIZE
a dictionary that holds the memoize cache
LOCK_MEMOIZE
a lock used during memoization
Author:Maxim Fedorovsky
class pyviblib.util.misc.SmartDict(mapping_or_sequence=None, kw=None, default_value=None)

A container for a convenient storage of overridable options.

The class is based on the Python builtin dictionary. It is made up of two containers:

  • internal dictionary for the default values
  • second dictionary, where the options are first looked up.

The former dictionary is referred to as the default dictionary and the latter one as the reference dictionary.

The following readable and writable property is exposed:
kw
reference dictionary
The following public methods are exported:
update_()
update the reference dictionary
merge()
overwrite the default dictionary with the reference one

Initializer of the class.

Parameters:
  • mapping_or_sequence – initial contents of the default dictionary
  • kw – reference dictionary
  • default_value – returned if a key is found in neither in the default nor in the reference dictionary
merge()
Update the default dictionary with the reference one.
update_(kw)

Update the reference dictionary.

If it is not defined, update the default dictionary.

Parameter:kw – dictionary to update with
class pyviblib.util.misc.Command(func, *args, **kwargs)

Emulator of lambda functions.

The idea is taken from H.P. Langtangen, Python Scripting for Computational Science. Springer Verlag Berlin Heidelberg, 2004. p. 519.

Initializer of the class.

Parameters:
  • func – function to be called, must be callable, otherwise an exception is raised
  • args – positional arguments to the function
  • kwargs – keyword arguments to the function
static fget_attr(obj, name)

Emulate a fget function used by definition of a property (via attribute).

This is a static method of the class.

Parameters:
  • obj – supposed to be the first argument of the fget function
  • name – name of the attribute
static fset_attr(obj, value, name)

Emulate a fset function used by definition of a property.

This is a static method of the class.

Parameters:
  • obj – object
  • value – value of the attribute to be set
  • name – attribute of the object to be set
class pyviblib.util.misc.PropertiesContainer

Class for exposing properties via attributes.

The following protected methods are called in the constructor:
_check_consistency()
check the consistency of the data passed
_declare_properties()
declare properties
The following protected method should be used for adding properties:
_add_property()
expose a given class attribute as a property

Initializer of the class.

pyviblib.util.misc.color_html_to_RGB(colorstr)

Convert a color in the HTML format to RGB.

Parameter:colorstr – color in the HTML format e.g. ‘#FF0000’
Returns:the tuple of float RGB values
pyviblib.util.misc.color_RGB_to_html(rgb)

Convert a color in the RGB format to HTML.

Parameter:rgb – tuple of float RGB values e.g. (1., 0., 0.)
Returns:the color in the HTML format
pyviblib.util.misc.color_complementary(colorstr)

Generate the complementary of a color.

Parameter:colorstr – color in the HTML format e.g. ‘#FF0000’
Returns:the complementary color in the HTML format
pyviblib.util.misc.str_to_float(val, default=0.0)

Convenient conversion from string to float.

If the convertion is failed, return the default value.

Parameters:
  • val – string value to converted to float
  • default – float value to be returned on error (default 0.)
pyviblib.util.misc.remove_indices_from_list(list_, indices)

Remove indices from a list.

Parameters:
  • list – list to be processed
  • indices – indices to be removed from the list

No exception is raised if invalid indices are found.

pyviblib.util.misc.random_color()
Generate a random color in the HTML format.
pyviblib.util.misc.save_matrix(mat, filename, value_format='%13.6f', base=1)

Save a matrix.

Parameters:
  • mat – matrix to be saved (ndarray)
  • filename – file name or file object to save to
  • value_format – format for writing elements of the matrix
  • base – start index of the matrix
pyviblib.util.misc.unique(arr)

Make a unique array.

Parameter:arr – input ndarray
pyviblib.util.misc.is_command_on_path(cmd)

Check whether a command is on the path.

The function searches in directories specified by the PATH environmental variable.

Parameter:cmd – command name
pyviblib.util.misc.ps2pdf(filesrc, removesrc=True, **options)

Convert an PS/EPS file to PDF with the ps2pdf command.

Parameters:
  • filesrc – PS/EPS file
  • removesrc – remove the source file after the conversion (default True)
  • options – any options recognized by ps2pdf
Returns:

whether the conversion has been successful

pyviblib.util.misc.ppm2gif(ppmname, gifname, tmpdir=None, remove_tmpdir=False, background=None)

Convert a ppm file to gif with Netpbm.

Parameters:
  • ppmfile – ppm file
  • gifname – gif file
  • tmpdir – temp directory (default None), if None, create one and delete anyway
  • remove_tmpdir – whether to remove tmpdir when done
  • background – if given, it is a color in the HTML format to be made transparent
Raise:

OSError, IOError

pyviblib.util.misc.rests(num, divisor=3)
Retrieve atomno, i.
pyviblib.util.misc.readconfig_option(cfp, section, option, default=None)

Read an option from a section from a ConfigParser.

If the option cannot be read or on error the function returns the default.

Parameters:
  • section – name of the section
  • option – name of the option
  • default – default value
pyviblib.util.misc.gen_molecule_name(filename, maxchar=None)

Generate the molecule name from the full path of a file.

Parameter:maxchar – maximal number of characters in the molecule name
pyviblib.util.misc.gen_group_labels(ngroups)

Generate group labels A, B etc.

Parameter:ngroups – number of groups
pyviblib.util.misc.import_processing()
Import and return a processing module.
pyviblib.util.misc.memoize_deterministic(gen_key)

Memoization decorator.

Parameter:gen_key – a callable that generates a string to identify a function to be memoized

SourceForge.net Logo