py4sci

Previous topic

Langmuir Python

Next topic

regex

This Page

common

Note

Utility functions.

Module author: Adam Gagorik <adam.gagorik@gmail.com>

common.zhandle(handle, mode='rb')[source]

Open a file using gzip if necessary.

Parameters:handle (str) – filename
Returns:file handle
Return type:file
>>> handle = zhandle('out.dat.gz', 'rb')
common.tail(handle, n=1, mode='python')[source]

Return last n lines of a file. The python version is much faster than a system call. The python version works on gzipped files.

Parameters:
  • handle – filename
  • n (int) – lines
  • mode (str) – python or subprocess
Returns:

list of lines

Return type:

list of str

>>> lines = tail('out.dat', 1)
common.splitext(handle)[source]

Extract stub and extension from a file object or string.

Parameters:handle – filename
Returns:stub and ext
Return type:str, str
>>> stub, ext = splitext('out.dat')
>>> print stub, ext
('out', '.dat')
common.load_pkl(handle, max_objs=256)[source]

Load max objs from a pkl file.

Parameters:
  • handle (str) – filename
  • max_objs (int) – max number of objects to load
Returns:

list of data

Return type:

list

>>> data = load_pkl('combined.pkl')
common.load_pkls(pkls)[source]

Load a set of pkls into a list.

Parameters:pkls (list of str) – list of filenames
Returns:nested list of loaded data
Return type:list
>>> pkls = load_pkls(['a.pkl', 'b.pkl'])
common.save_pkl(obj, handle)[source]

Save obj to a pkl file.

Parameters:obj (object) – python object
Returns:the file handle
Return type:file
>>> handle = save_pkl(1, 'test.pkl')
>>> handle = save_pkl(2, handle)
common.format_string(s, **kwargs)[source]

Format a string using str.format() and process with regex. kwargs are passed to the format function.

The following regex are used:

match replace
‘^_’ ‘’
‘_$’ ‘’
‘_.’ ‘.’
Parameters:s (str) – string to process
Returns:the formatted string
Return type:str
>>> print format_string('_{name}.{ext}', ext='dat', name='adam')
adam.dat
>>> print format_string('adam_.{ext}', ext='dat')
adam.dat
common.format_output(s='{stub}_{name}.{ext}', stub='', name='out', ext='dat', **kwargs)[source]

Format an output file name and then process with regex. If stub is empty, the leading underscore is striped.

Parameters:
  • stub (str) – a unique string to identify the file
  • name (str) – a common string to identify the file
  • ext (str) – the filename extention without a ‘.’
Returns:

formatted filename

Return type:

str

>>> print format_output(stub='systemA', name='fft', ext='txt')
systemA_fft.txt

See also

format_string()

common.compare_dicts(dict1, dict2)[source]

Compare two dictionaries.

Parameters:
  • dict1 (dict) – dict like object
  • dict2 (dict) – dict like object
Returns:

a results dictionary with the output of set operations

Return type:

collections.OrderedDict

>>> results = compare_dicts({'A' : 1}, {'A' : 2, 'B' : 3})
>>> for key, value in results.iteritems():
...     print key, value
size1 1      # size of dict1
size2 2      # size of dict2
union 2      # union of keys        (A, B)
inter 1      # intersection of keys (A)
valid False  # are they the same?
A (1, 2)     # keys that are different and and their values
common.compare_lists(list1, list2)[source]

Compare two lists.

Parameters:
  • list1 (list) – list like object
  • list2 (list) – list like object
Returns:

a results dictionary with the output of set operations

Return type:

collections.OrderedDict

>>> results = compare_lists([1], [1, 2])
>>> for key, value in results.iteritems():
...     print key, value
size1 1      # size of list1
size2 2      # size of list2
union 2      # union of items        (1, 2)
inter 1      # intersection of items (1)
valid False  # are they the same?
common.command_script(paths, name=None, cwd=None, batch='run.batch', batch_dir='~/templates', inp='sim.inp', comment=True, stub='{relpath}', stub_func=None, **kwargs)[source]

Create a handy script.

Parameters:
  • paths (list) – list of paths
  • name (str) – output file name
  • cwd (str) – working directory
  • batch (str) – batch file name
  • batch_dir (str) – batch file directory
  • inp (str) – input file name
  • comment (str) – comment out custom command?
  • stub (str) – job stub string
  • stub_func (func) – post-process stub
class common.ColorCodes[source]

Provides ANSI terminal color codes which are gathered via the tput utility. That way, they are portable. If there occurs any error with tput, all codes are initialized as an empty string.