aiida_crystal17.common package

Submodules

aiida_crystal17.common.atoms module

aiida_crystal17.common.dict_funcs module

class aiida_crystal17.common.dict_funcs.BuilderEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]

Bases: json.encoder.JSONEncoder

Constructor for JSONEncoder, with sensible defaults.

If skipkeys is false, then it is a TypeError to attempt encoding of keys that are not str, int, float or None. If skipkeys is True, such items are simply skipped.

If ensure_ascii is true, the output is guaranteed to be str objects with all incoming non-ASCII characters escaped. If ensure_ascii is false, the output can contain non-ASCII characters.

If check_circular is true, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to prevent an infinite recursion (which would cause an OverflowError). Otherwise, no such check takes place.

If allow_nan is true, then NaN, Infinity, and -Infinity will be encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats.

If sort_keys is true, then the output of dictionaries will be sorted by key; this is useful for regression tests to ensure that JSON serializations can be compared on a day-to-day basis.

If indent is a non-negative integer, then JSON array elements and object members will be pretty-printed with that indent level. An indent level of 0 will only insert newlines. None is the most compact representation.

If specified, separators should be an (item_separator, key_separator) tuple. The default is (‘, ‘, ‘: ‘) if indent is None and (‘,’, ‘: ‘) otherwise. To get the most compact JSON representation, you should specify (‘,’, ‘:’) to eliminate whitespace.

If specified, default is a function that gets called for objects that can’t otherwise be serialized. It should return a JSON encodable version of the object or raise a TypeError.

default(obj)[source]

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return JSONEncoder.default(self, o)
aiida_crystal17.common.dict_funcs.display_json(builder, indent=2)[source]

Pretty print a dictionary object in a Jupyter Notebook.

aiida_crystal17.common.dict_funcs.flatten_dict(indict, delimiter='.')[source]
aiida_crystal17.common.dict_funcs.get_keys(dct, keys, default=None, raise_error=False)[source]

Retrieve the leaf of a key path from a dictionary.

Parameters
  • dct – the dict to search

  • keys – key path

  • default – default value to return

  • raise_error – whether to raise an error if the path isn’t found

Returns

aiida_crystal17.common.dict_funcs.map_nested_dicts(ob, func, apply_lists=False)[source]

Map a function on to all values of a nested dictionary.

aiida_crystal17.common.dict_funcs.recursive_round(dct, dp)[source]

Apply (recursively) the round function to all floats in a dict.

aiida_crystal17.common.dict_funcs.unflatten_dict(indict, delimiter='.')[source]

aiida_crystal17.common.kpoints module

aiida_crystal17.common.kpoints.create_kpoints_from_distance(structure, distance, force_parity=True)[source]

Generate a uniformly spaced kpoint mesh for a given structure where the spacing between kpoints in reciprocal space is guaranteed to be at least the defined distance.

Parameters
  • structure – the StructureData to which the mesh should apply

  • distance – a float with the desired distance between kpoints in reciprocal space

  • force_parity – a bool to specify whether the generated mesh should maintain parity

Returns

a KpointsData with the generated mesh

aiida_crystal17.common.mapping module

aiida_crystal17.common.mapping.get_logging_container()[source]

Return an AttributeDict that can be used to map logging messages to certain log levels.

This datastructure is useful to add log messages in a function that does not have access to the right logger. Once returned, the caller who does have access to the logger can then easily loop over the contents and pipe the messages through the actual logger.

Returns

AttributeDict

aiida_crystal17.common.mapping.prepare_process_inputs(process, inputs)[source]

Prepare the inputs for submission for the given process, according to its spec.

That is to say that when an input is found in the inputs that corresponds to an input port in the spec of the process that expects a Dict, yet the value in the inputs is a plain dictionary, the value will be wrapped in by the Dict class to create a valid input.

Parameters
  • process – sub class of Process for which to prepare the inputs dictionary

  • inputs – a dictionary of inputs intended for submission of the process

Returns

a dictionary with all bare dictionaries wrapped in Dict if dictated by the process spec

aiida_crystal17.common.mapping.update_mapping(original, source)[source]

Update a nested dictionary with another optionally nested dictionary.

The dictionaries may be plain Mapping objects or Dict nodes. If the original dictionary is an instance of Dict the returned dictionary will also be wrapped in Dict.

Parameters
  • original – Mapping object or Dict instance

  • source – Mapping object or Dict instance

Returns

the original dictionary updated with the source dictionary

aiida_crystal17.common.mapping.wrap_bare_dict_inputs(port_namespace, inputs)[source]

Wrap bare dictionaries in inputs in a Dict node if dictated by the corresponding port in given namespace.

Parameters
  • port_namespace – a PortNamespace

  • inputs – a dictionary of inputs intended for submission of the process

Returns

a dictionary with all bare dictionaries wrapped in Dict if dictated by the port namespace

aiida_crystal17.common.parsing module

aiida_crystal17.common.parsing.convert_units(value, in_units, out_units, standard='codata2014')[source]

Convert a value, from one unit to another.

aiida_crystal17.common.parsing.split_numbers(string, as_decimal=False)[source]

get a list of numbers from a string (even with no spacing)

Parameters

as_decimal (bool) – if True return floats as decimal.Decimal objects

Return type

list

Example

>>> split_numbers("1")
[1.0]
>>> split_numbers("1 2")
[1.0, 2.0]
>>> split_numbers("1.1 2.3")
[1.1, 2.3]
>>> split_numbers("1e-3")
[0.001]
>>> split_numbers("-1-2")
[-1.0, -2.0]
>>> split_numbers("1e-3-2")
[0.001, -2.0]

Module contents

common functionality