This modules are provided with the library to ease the interoperability between I/O of the Fortran library with python scripting
This file contains some low-level useful functions
futile.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: |
---|
futile.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: |
---|
Example
>>> inp={}
>>> dict_set(inp,'dft','nspin','mpol',2)
>>> print (inp)
{'dft': {'nspin': {'mpol': 2}}}
futile.Utils.
ensure_copy
(src, dest)[source]¶Copy src into dest.
Guarantees that the file indicated by dest
is a copy of the file src
Parameters: |
|
---|---|
Returns: |
|
Return type: |
futile.Utils.
ensure_dir
(file_path)[source]¶Guarantees the existance on the directory given by the (relative) file_path
Parameters: | file_path (str) – path of thh directory to be created |
---|---|
Returns: | True if the directory needed to be created, False if it existed already |
Return type: | bool |
futile.Utils.
file_time
(filename)[source]¶Gives the date of the creation of the file, if exists.
Parameters: | filename (str) – name of the file |
---|---|
Returns: | if the file exists, the date of the filename as per os.path.getmtime. Otherwise it returns 0 |
futile.Utils.
find_files
(regexp, archive=None)[source]¶Returns a list of the paths to the files that follow the regular expression regexp. They are searched from the current working directory or from an archive given as optional argument.
Parameters: |
|
---|---|
Returns: | a list of all the paths that agree with the regexp |
Return type: | list of strings |
Raises: | ValueError if the regexp does not find a single path. |
Example:
#Find all python files in the current working directory
find_files('*py')
#An exmple outside of the current working directory
find_files('*/log-*.yaml')
#Example using a tarfile
import tarfile
my_archive = tarfile.open('archive.tar.gz')
find_files('*/*/log-*.yaml', archive=my_archive)
futile.Utils.
function_signature_regenerator
(target_kwargs_function, fun_name='', fun_docstring='', **kwargs)[source]¶Generate the function of the name provided by fun_name, with signature provided by the kwargs dictionary.
Parameters: |
|
---|
Example
>>> def write_kwargs(**kwargs):
>>> """
>>> Convert keyword arguments into a string
>>> """
>>> return str(kwargs)
>>> write_opts=function_signature_regenerator(write_kwargs,fun_name='write_opts',opt1='default1',opt2='default2')
>>> help(write_opts)
>>> print (write_opts())
Help on function write_opts:
{‘opt1’: ‘default1’, ‘opt2’: ‘default2’}
futile.Utils.
kw_pop
(*args, **kwargs)[source]¶Treatment of kwargs. Eliminate from kwargs the tuple in args.
futile.Utils.
make_dict
(inp)[source]¶Transform the instance inp
into a python dictionary. If inp is already a dictionary, it perfroms a copy.
Parameters: | inp (dict) – a instance of a Class which inherits from dict |
---|---|
Returns: | the copy of the class, converted as a dictionary |
Return type: | dict |
futile.Utils.
option_line_generator
(separator='--', **kwargs)[source]¶Associate to each of the keyword arguments a command line argument.
Parameters: |
|
---|
Warning
The separator comes before the first argument therefore pay attention to lstrip it in case you want to use it as a function signature string.
Example
>>> option_line_generator(arg1='val1',arg2='val2')
'--arg1=val1 --arg2=val2'
futile.YamlIO.
load
(file=None, stream=None, doc_lists=True, safe_mode=False)[source]¶Encapsulate the loading of yaml documents.
Provides a dictionary, or a list of dictionaries, which represents the structure of the stream to be loaded. It also wraps the yaml loader to perform a optimized parsing when the minloader of PyYaml 3.13 is available. This wrapper ensures to extract from the stream the maximum possible information by choosing the best loader available.
Parameters: |
|
---|---|
Returns: |
|
futile.YamlIO.
dump
(data, filename=None, raw=False, tar=False)[source]¶Encapsulate the dumping of dictionaries.
This function is useful to dump a dictionary in yaml or json-compliant form.
This may be used as an alternative to the usual yaml.dump
method,
especially when the dictionary to be dump’ed is heavy.
No particular attention is paid in human readability of the output.
The dumped information can then be parsed either from json or yaml interpreter.
Parameters: |
|
---|
futile.YamlIO.
clean_logfile
(logfile_lines, to_remove)[source]¶Remove yaml fields from a list of lines.
Removes from a set of lines the yaml_fields contained in the to_remove list.
Parameters: |
|
---|---|
Returns: | list of lines where the removed keys have as values the “<folded>” string |
Handle the input variable specifications.
This module is the python complement to the specification of the input variables of a
file as provided by the f_input_file
module. It uses the same syntax as defined there
and make possible the interplay between a python-based serialization of the input dictionary
and the construction of
Handle and automatize the parsing of input arguments
This module uses the same convention of the yaml_parse
fortran module to define the command line arguments.
Such module can be used to define command line arguments of python scripts that follows the same conventions
or to generate python functions that have the same signature than the provided command arguments.
futile.YamlArgparse.
get_python_function
(target_kwargs_function, func_name, func_spec)[source]¶Convert a argparse spec into a python function
This function provides a python function with a signature indicated by the fun_spec
dictionary
With the conventions of the yaml_argparse
modules.
The futile.Utils.function_signature_regenerator()
function is used for the conversion
Parameters: |
|
---|---|
Returns: |
|
Return type: | func |
Todo
Create the docstring of the generated function by also including the docstring of the arguments
futile.Figures.
AxisSet
(fig, rect, facecolor=None, frameon=True, sharex=None, sharey=None, label=u'', xscale=None, yscale=None, **kwargs)[source]¶Bases: matplotlib.axes._axes.Axes
futile.Figures.
FigureSet
(**kwargs)[source]¶Container for multiple figures.
Define a container for a plot with the possiblity to switch between simple and gnuplot plotting
Arguments: title: The title of the master figure **kwargs: arguments for the axis instance
futile.Figures.
VertSlider
(ax, label, valmin, valmax, valinit=0.5, valfmt='%1.2f', closedmin=True, closedmax=True, slidermin=None, slidermax=None, dragging=True, **kwargs)[source]¶Bases: matplotlib.widgets.AxesWidget
A slider representing a floating point range.
For the slider to remain responsive you must maintain a reference to it.
*ax*
the slider matplotlib.axes.Axes
instance
*val*
the current slider value
*hline*
a matplotlib.lines.Line2D
instance
representing the initial value of the slider
*poly*
A matplotlib.patches.Polygon
instance
which is the slider knob
*valfmt*
the format string for formatting the slider text
*label*
a matplotlib.text.Text
instance
for the slider label
*closedmin*
whether the slider is closed on the minimum
*closedmax*
whether the slider is closed on the maximum
*slidermin*
another slider - if not None, this slider must be greater than slidermin
*slidermax*
another slider - if not None, this slider must be less than slidermax
*dragging*
allow for mouse dragging on slider
Call on_changed()
to connect to the slider event
futile.Time.
TimeData
(*filenames, **kwargs)[source]¶barwidth
= 0.9¶collect_categories
(dict_list, vals)[source]¶Collect all the categories which belong to all the dictionaries
ignored_counters
= ['CPU parallelism', 'Routines timing and number of calls', 'SUMMARY', 'Report timestamp', 'Hostnames']¶load_unbalancing
(ax, dict, hosts)[source]¶Extract the data for plotting the hostname balancings between different categories in bar chart