API

bounds module

The Bounds module implements the Bounds class, which can be used to represent bounds for certain arguments.

class json_configparser.bounds.Bounds(arg_name, lower_bound=None, lower_inclusive=True, upper_bound=None, upper_inclusive=True)[source]

Bases: object

Represents the Bounds of an argument. A Bounds instance is defined by lower and upper bounds, and flags indicating if the bounds are inclusive or exclusive. The supported argument types are integer, float, or lists/dictionaries of those types.

__init__(arg_name, lower_bound=None, lower_inclusive=True, upper_bound=None, upper_inclusive=True)[source]

At least one bound must be provided.

Parameters:
  • arg_name (str) – The name of the argument.
  • lower_bound (Union[int, float, None]) – The lower bound value.
  • lower_inclusive (bool) – Flag indicating if the lower bound is inclusive.
  • upper_bound (Union[int, float, None]) – The upper bound value.
  • upper_inclusive (bool) – Flag indicating if the lower bound is inclusive.
validate_value(arg_value)[source]

Validates a value against the provided bounds.

Parameters:arg_value (Union[int, float]) – The value of the argument to validate.
Raises:ValueError – If the value is out of bounds.

config_args module

Implements the ConfigArgs class which is the main class for parsing options classes, validating arguments, and parsing a JSON file.

class json_configparser.config_args.ConfigArgs(options_class, bounds_lst=None, extra_validations=None)[source]

Bases: object

Parses the Arguments NamedTuple class to extract information about argument names, types, and defaults. Also holds information about Bounds and extra validations.

The parse_json method can be used to parse a JSON file and validate it against the known information.

__init__(options_class, bounds_lst=None, extra_validations=None)[source]
Parameters:
  • options_class (type) – The NamedTuple class which defines all arguments, types, and defaults.
  • bounds_lst (Optional[List[Bounds]]) – A list of Bounds objects, which defines bounds for arguments.
  • extra_validations (Optional[Callable]) – A function which contains extra validations. Should receive a dictionary mapping from argument name to value and should return a dictionary of the same type.
parse_json(path_to_json, encoding='utf-8')[source]

Parses a JSON file, reads the arguments, validates them, and returns a dictionary with them.

Parameters:
  • path_to_json (str) – Path to JSON configuration file.
  • encoding (str) – The encoding to use when loading the JSON file.
Return type:

Dict[str, Any]

Returns:

A Dictionary mapping argument name to value.

Raises:
  • ValueError – If an argument is an empty string, if an argument with no default is missing from the JSON, or if the JSON contains an unknown argument.
  • TypeError – If an argument is of the wrong type.

type_defaults module

Implements the TypeDefaultBounds NamedTuple, which holds information about an argument.

class json_configparser.type_defaults.TypeDefaultBounds[source]

Bases: tuple

NamedTuple representing the name, type, default value, and bounds of an argument.

arg_name

the name of the argument

bound_obj

an instance of the Bound class, representing the bounds of the argument

default_value

the value of the default argument

has_default

flag to indicate if there is a default value for the argument

type_

the type of the argument

validations module

This module implements the type and bound validation of all supported types.

json_configparser.validations.validate_argument(arg_value, arg_type_defaults)[source]

Given a value and type/bounds, this function checks if the type is supported. If so, then check if the value is of the correct type and if it is within the defined bounds. For Lists and Dictionaries these checks are applied to all elements.

Parameters:
  • arg_value (Any) – The value of the argument to check.
  • arg_type_defaults (TypeDefaultBounds) – An instance of TypeDefaultBounds, specifying the type and bounds of the argument to check.
Raises:
  • ValueError – If the argument is an empty string.
  • TypeError – If the argument value is not of the expected type.
Return type:

Any