pypeit.par.util module

Utility functions for PypeIt parameter sets

pypeit.par.util._eval_ignore()[source]

Provides a list of strings that should not be evaluated.

pypeit.par.util._eval_iter(inp, left, right, otype)[source]

Convenience function used to abstract the core functionality of eval_tuple() and eval_list().

Parameters:
  • inp (list[str]) – Input list of strings

  • left (str) – Left bracket delimeter

  • right (str) – Right bracket delimeter

  • otype (Type) – Return type for the components of the list

Return type:

list

Returns:

A list of objects with type otype as converted from the provided strings.

pypeit.par.util.ast_literal_eval(inp)[source]

A wrapper for ast.literal_eval() that returns the input if it raises a ValueError.

pypeit.par.util.eval_list(inp)[source]

Evaluate the input to one or more lists.

This allows conversion of one or more lists provided to a configuration parameters.

Warning

  • Currently can only handle lists with integers, floats, or strings!

Parameters:

inp (list[str]) – A list of strings that are converted into a list of lists. The square brackets must be within the list of elements.

Return type:

list[list]

Returns:

A list of lists with the converted elements.

pypeit.par.util.eval_tuple(inp)[source]

Evaluate the input to one or more tuples.

This allows conversion of one or more tuples provided to a configuration parameters.

Warning

  • Currently can only handle tuples with integers, floats, or strings!

Parameters:

inp (list[str]) – A list of strings that are converted into a list of tuples. The parentheses must be within the list of elements.

Return type:

list[tuple]

Returns:

A list of tuples with the converted elements.

pypeit.par.util.parset_to_dict(par)[source]

Convert the provided parset into a dictionary.

Parameters:

par (ParSet)

Returns:

Converted ParSet

Return type:

dict

pypeit.par.util.recursive_dict_evaluate(d)[source]

Recursively run eval() on each element of the provided dictionary.

A raw read of a configuration file with configobj results in a dictionary that contains strings or lists of strings. However, when assigning the values for the various ParSets, the from_dict methods expect the dictionary values to have the appropriate type. E.g., the configobj will have something like d[‘foo’] = ‘1’, when the from_dict method expects the value to be an integer (d[‘foo’] = 1).

This function tries to evaluate all dictionary values, except for those listed above in the _eval_ignore() function. Any value in this list or where:

ast_literal_eval(d[k]) for k in d.keys()

raises an exception is returned as the original string.

This is currently only used in from_cfg_file(); see further comments there.

Parameters:

d (dict) – Dictionary of values to evaluate

Returns:

Identical to input dictionary, but with all string values replaced with the result of eval(d[k]) for all k in d.keys().

Return type:

dict