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:
- Preserves ordering of members as initialized (subclassed from OrderedDict).
- Key and attribute access. Attribute access is limited to keys that are valid python variable names.
- Import/export from JSON and YAML formats.
-
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 extrasep
argument.Parameters: 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: 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: Returns: - If stream is a file, returns None.
Otherwise, returns the JSON structure as string
Return type:
-
to_yaml
(stream=None, default_flow_style=False, **kwargs)[source]¶ Convert mapping to YAML format.
Parameters:
-
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., combinesexpanduser
,expandvars
, andabspath
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 byos.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
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.