vaayu.prelude – General Vaayu Utilities

Attribute Dictionary

class vaayu.prelude.attrdict.AttrDict(*args, **kwds)[source]

Bases: collections.OrderedDict, _abcoll.MutableMapping

Attribute Dictionary

A dictionary mapping data structure that allows both key and attribute access. The mapping has the following properties:

  1. Preserves ordering of members as initialized (subclassed from OrderedDict).
  2. Key and attribute access. Attribute access is limited to keys that are valid python variable names.
  3. Import/export from JSON and YAML formats.
classmethod from_json(stream)[source]

Initialize mapping from a JSON string/stream

classmethod from_yaml(stream)[source]

Initialize mapping from a YAML string.

Parameters:stream – A string or valid file handle
Returns:YAML data as a python object
Return type:AttrDict
classmethod load_json(filename)[source]

Initialize dictionary from JSON input file

Parameters:filename (path) – Absolute path to the JSON file
classmethod load_yaml(filename)[source]

Load a YAML file

Parameters:filename (str) – Absolute path to YAML file
Returns:YAML data as python object
Return type:AttrDict
merge(*args)[source]

Recursively update dictionary

Merge entries from maps provided such that new entries are added and existing entries are updated.

pget(path, sep='.')[source]

Get value from a nested dictionary entry.

A convenience method that serves various purposes:

  • Access values from a deeply nested dictionary if any of the keys are not valid python variable names.
  • Return None if any of the intermediate keys are missing. Does not raise AttributeError.

By default, the method uses the . character to split keys similar to attribute access. However, this can be overridden by providing and extra sep argument.

Parameters:
  • path (str) – The keys in individual dictionarys separated by sep
  • sep (str) – Separator for splitting keys (default: ”.”)
Returns:

Value corresponding to the key, or None if any of the keys

don’t exist.

pset(path, value, sep='.')[source]

Set value for a nested dictionary entry.

A convenience method to set values in a nested mapping hierarchy without individually creating the intermediate dictionaries. Missing intermediate dictionaries will automatically be created with the same mapping class as the class of self.

Parameters:
  • path (str) – The keys in individual dictionarys separated by sep
  • value (object) – Object assigned to innermost key
  • sep (str) – Separator for splitting keys (default: ”.”)
Raises:

AttributeError – If the object assigned to is a non-mapping type and not the final key.

to_json(stream=None, indent=2, **kwargs)[source]

Convert mapping to JSON format

Parameters:
  • stream (file) – A file handle for output
  • indent (int) – Default indentation (use None for compressed)
Returns:

If stream is a file, returns None.

Otherwise, returns the JSON structure as string

Return type:

str or None

to_yaml(stream=None, default_flow_style=False, **kwargs)[source]

Convert mapping to YAML format.

Parameters:
  • stream (file) – A file handle where YAML is output
  • default_flow_style (bool) –
    • False - pretty printing
    • True - No pretty printing
walk(_node=('root', ))[source]

Yields (key, value) pairs by recursively iterating the mapping.

The keys yielded are tuples containing the list of the keys necessary to access this particular entry in the dictionary hierarcy.

Parameters:node (tuple) – A tuple indicating the root mapping

Examples

>>> mydict = AttrDict(a=1, b=2, c=AttrDict(x=[10, 20, 100]))
>>> for k, v in mydict.walk():
...     print (k, v)
yaml_dumper

alias of AttrDictYAMLDumper

yaml_loader

alias of AttrDictYAMLLoader

Utilities

Miscellaneous utilities that do not fit anywhere else in the library.

Currently available utilities

timestamp Timestamp in ISO format
abspath Get the absolute path
ensure_dir Ensure that the directory exists.
exec_dir A context manager to execute code in a given directory.
find Unix find command like utility.
grep Unix grep-like utility.
echo A simple output sink.
vaayu.prelude.utils.abspath(fpath)[source]

Get the absolute path

Differs from os.path.abspath() in that this function will expand tilde and shell variables, i.e., combines expanduser, expandvars, and abspath in one call.

Returns:Absolute path to the file/directory.
Return type:path
vaayu.prelude.utils.coroutine(func)[source]

Prime a coroutine for send commands.

Parameters:func (coroutine) – A function that takes values via yield
vaayu.prelude.utils.echo(*args, **kwargs)[source]

A simple output sink.

Parameters:fh (file) – A valid file handle to print to
vaayu.prelude.utils.ensure_dir(dpath)[source]

Ensure that the directory exists.

Checks if the path provided exists on the system. If not, creates it. Also creates intermediate directories if they don’t exist.

Returns:Absolute path to the directory
Return type:path
vaayu.prelude.utils.exec_dir(*args, **kwds)[source]

A context manager to execute code in a given directory.

When used within a with-block, the subsequent code is executed within the directory dpath. The original working directory (as given by os.getcwd()) is restored at the end of the with-block.

vaayu.prelude.utils.find(pat, root=None)[source]

Unix find command like utility.

Parameters:
  • pat (str) – A pattern with Unix shell-style wildcards.
  • root (path) – Base directory for starting search (must exist)
Returns:

An iterator that yields all files matching the pattern

vaayu.prelude.utils.grep(*args, **kwargs)[source]

Unix grep-like utility.

Parameters:
  • pattern (str) – A regular expression
  • targets (list) – A list of consumers that act on matching lines
  • send_close (bool) – Send close signal to targets when grep exits
  • matchersearch, match, findall methods
  • flags – Regexp flags used when compiling pattern
vaayu.prelude.utils.timestamp(local=False)[source]

Timestamp in ISO format

If local is set to True, timestamp is returned in local timezone. By default, it returns the timestamp in UTC timezone.

Returns:Timestamp string in ISO format
Return type:str
vaayu.prelude.utils.user_home_dir()[source]

Absolute path to user’s home directory

vaayu.prelude.utils.username()[source]

User’s login name on the system

Multiprocessing module wrappers

Allows class methods to be used in multiprocessing calls by registering pickle option for class methods. The calling site just needs to import this module to trigger the pickle setup. No additional action is necessary.