Utils

This module contains some low-level functions. Some functions are extracted from the Futile Utils of PyBigDFT.

mppi.Utilities.Utils.convertTonumber(x)[source]

Check if the input string can be converted to an integer or to a float variable

Parameters:

x (string) – string to be converted

mppi.Utilities.Utils.dict_get(inp, *subfields)[source]

Find the value of the provided sequence of keys in the dictionary, if available.

Retrieve the value of the dictionary in a sequence of keys if it is available. Otherwise it provides as default value the last item of the sequence subfields.

Parameters:
  • inp (dict) – the top-level dictionary. Unchanged on exit.

  • subfields (str,object) – keys, ordered by level, that have to be retrieved from topmost level of inp. The last item correspond to the value to be set.

Returns:

The value provided by the sequence of subfields if available, otherwise the default value given as the last item of the subfields sequence.

mppi.Utilities.Utils.dict_merge(dest, src)[source]

Recursive dict merge. Inspired by dict.update(), instead of updating only top-level keys, dict_merge recurses down into dicts nested to an arbitrary depth, updating keys. The src is merged into dest. From angstwad/dict-merge.py

Parameters:
  • dest (dict) – dict onto which the merge is executed

  • src (dict) – dict merged into dest

mppi.Utilities.Utils.dict_set(inp, *subfields)[source]

Ensure the provided fields and set the value

Provide a entry point to the dictionary. Useful to define a key in a dictionary that may not have the previous keys already defined.

Parameters:
  • inp (dict) – the top-level dictionary

  • subfields (str,object) – keys, ordered by level, that have to be retrieved from topmost level of inp. The last item correspond to the value to be set .

Example

>>> inp={}
>>> dict_set(inp,'dft','nspin','mpol',2)
>>> print (inp)
{'dft': {'nspin': {'mpol': 2}}}
mppi.Utilities.Utils.ensure_dir(file_path)[source]

Guarantees the existance on the directory given by the (relative) file_path

Parameters:

file_path (str) – path of the directory to be created

Returns:

True if the directory needed to be created, False if it existed already

Return type:

bool

mppi.Utilities.Utils.file_parser(filename, skip='#', sep=None)[source]

Parse a file. All the lines the start with the skip string are skipped. The lines of the file are converted in floats (elements are separated using the sep parameter). Elements that cannot be converted to float are ignored.

Parameters:
  • filename (string) – name of the file

  • skip (string) – first elements of the skipped lines

  • sep (string) – Delimiter at which splits occur. If None the string is splitted at whitespaces

Returns:

py:class:`array’array with the floats extracted from the file sorted in columns. So columns[0] contains

the first column of the file and so on

mppi.Utilities.Utils.file_to_list(filename, skip='#')[source]

Read the filename and append all the lines that do not start with the skip string, to a list. Empty lines are skipped

Parameters:
  • filename (str) – name of the file

  • skip (str) – first elements of the skipped lines

mppi.Utilities.Utils.floats_from_string(line, sep=None)[source]

Split a string using blank spaces and convert the elements to float. If an element cannot be converted it is skipped.

Parameters:
  • line (string) – string that contains a line of the file

  • sep (string) – Delimiter at which splits occur. If None the string is splitted at whitespaces

Returns:

list with the floats extracted from the line

Return type:

list

mppi.Utilities.Utils.sort_lists(sort_by, ascending, *lists)[source]

Sort lists altogether following the lists indicated by the sort_by index.

Parameters:
  • sort_by (int) – the index of the list which has to be taken as reference for sorting

  • ascending (bool) – Sort is performed in ascending order if True

  • *lists – sequence of lists to be mutually sorted. They have to be of the same length.

Returns:

tuple of sorted lists

Example

>>> l1=[5,3,4]
>>> l2=['c','t','q']
>>> l3=[6,3,7]
>>> print (sort_lists(0,True,l1,l2,l3))
>>> print (sort_lists(2,True,l1,l2,l3))
[(3, 4, 5), ('t', 'q', 'c'), (3, 7, 6)]
[(3, 5, 4), ('t', 'c', 'q'), (3, 6, 7)]