API Reference

Other module functions

class l2l.sdict(obj)[source]

Bases: l2l.sdictm

Immutable version of sdictm

update(**kwargs)[source]

Update the dictionary with the values given in the function (only goes one level down) :param kwargs: :return:

apply(fn)[source]

Recursively apply fn on all leaf key, value pairs :param fn: :return:

class l2l.sdictm(obj)[source]

Bases: object

A dictionary which allows accessing it’s values using a dot notation. i.e. d[‘a’] can be accessed as d.a Mutable version

get(key, default_value)[source]
keys()[source]
todict()[source]
copy()[source]

Return a copy of the class. The copy is deep. :return:

update(**kwargs)[source]

Update the dictionary with the values given in the function (only goes one level down) :param kwargs: :return:

apply(fn)[source]

Recursively apply fn on all leaf key, value pairs :param fn: :return:

frozen()[source]
class l2l.DictEntryType(value)[source]

Bases: enum.Enum

An enumeration.

Sequence = 0
Scalar = 1
l2l.list_to_dict(input_list, dict_spec)[source]

This function converts a list back into a dict.

Parameters
  • input_list (list) – This is the list to be converted into a dict.

  • dict_spec (tuple) – This is the dict specification that conveys information regarding the dict to be converted to. See dict_to_list() for information about the dict specification.

Returns

A Dictionary representing the list. Note that for valid behaviour, the list should have been generated by dict_to_list(). Moreover, This operation is not a perfect inverse of list_to_dict(). While the types of individual elements are preserved, the types of the iterables inside which they reside are not. list_to_dict() creates all iterables as numpy arrays.

l2l.dict_to_list(input_dict, get_dict_spec=False)[source]

This function converts the given dictionary into a list.

Parameters
  • input_dict (dict) – The dictionary to be converted into a list. It is assumed that each key in the dictionary is a string and each value is either a scalar numerical value or an iterable.

  • get_dict_spec (bool) –

    This is a flag that indicates whether one wants the dictionary specification tuple to be returned. It is disabled by default

    Dict-Specification:

    The Dict-Specification tuple is required to convert back from the list to dictionary.

    It is a tuple of tuples, one tuple for each key of the dictionary. Each tuple is of the following form:

    (key_name, value_type, value_length)

    key_type may be one of DictEntryType.Sequence or DictEntryType.Scalar. In case the object is a scalar, value_length is 1.

Returns

The list representing the contents of the dict. If get_dict_spec is True, the dict specification is also returned. Note that the keys are always sorted ascendingly by name

l2l.stdout_redirected(filename)[source]

This context manager causes all writes to stdout (whether within python or its subprocesses) to be redirected to the filename specified. For usage, look at example below:

import os

with stdout_redirected(filename):
    print("from Python")
    os.system("echo non-Python applications are also supported")

inspired from the article http://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/

Parameters

filename – The filename (NOT file stream object, this is to ensure that the stream is always a valid file object) to which the stdout is to be redirected

l2l.stdout_discarded()[source]

This context manager causes all writes to stdout (whether within python or its subprocesses) to be redirected to os.devnull, thereby effectively discarding the output. For usage look at example below:

import os

with stdout_discarded():
    print("from Python")
    os.system("echo non-Python applications are also supported")
l2l.convert_dict_to_numpy(input_dict)[source]

This function takes a dictionary as input and converts the values to numpy data types. This is useful when importing parameters from config files.

NOTE: Only the following data types are converted:

  1. Scalars integer or float. They are converted to np.int64 and numpy.float64 respectively

  2. Iterables. All elements of the iterable are converted to a numpy array via np.array(...)

  3. Dictionaries. Dictionaries are recursively converted

Parameters

input_dict – This is the dictionary to convert

Returns

The converted output dictionary