tardis.io.util module

class tardis.io.util.MockRegexPattern(target_type: type)[source]

Bases: object

A mock class to be used in place of a compiled regular expression when a type check is needed instead of a regex match.

Parameters:
target_typetype

The target type for conversion testing.

Notes

This is usually a lot slower than regex matching.

Initialize the MockRegexPattern.

Parameters:
target_typetype

The target type for conversion testing.

match(text: str) bool[source]

Test if text can be converted to the target type.

Parameters:
textstr

A string to be passed to target_type for conversion.

Returns:
bool

Returns True if text can be converted to target_type, otherwise returns False.

class tardis.io.util.YAMLLoader(stream)[source]

Bases: Loader

A custom YAML loader containing all the constructors required to properly parse the tardis configuration.

Initialize the scanner.

construct_quantity(node: ScalarNode) Quantity[source]

A constructor for converting quantity-like YAML nodes to astropy.units.Quantity objects.

Parameters:
nodeyaml.Node

The YAML node to be constructed.

Returns:
astropy.units.Quantity

The constructed quantity object.

mapping_constructor(node: MappingNode) OrderedDict[source]

Construct an OrderedDict from a YAML mapping node.

Parameters:
nodeyaml.Node

The YAML mapping node to construct.

Returns:
OrderedDict

The constructed ordered dictionary.

tardis.io.util.assert_equality(item1: Any, item2: Any) None[source]

Assert that two items are equal, handling special cases for units and arrays.

Parameters:
item1Any

First item to compare.

item2Any

Second item to compare.

Raises:
AssertionError

If the items are not equal.

tardis.io.util.check_equality(item1: Any, item2: Any) bool[source]

Check if two items are equal using traverse_configs and assert_equality.

Parameters:
item1Any

First item to compare.

item2Any

Second item to compare.

Returns:
bool

True if items are equal, False otherwise.

tardis.io.util.download_from_url(url: str, dst: str, checksum: str, src: tuple[str, ...] | None = None, retries: int = 3) None[source]

Download files from a given URL.

Parameters:
urlstr

URL to download from.

dststr

Destination folder for the downloaded file.

checksumstr

Expected MD5 checksum of the file.

srctuple of str, optional

List of URLs to use as mirrors.

retriesint, optional

Number of retry attempts, by default 3.

Raises:
RuntimeError

If maximum number of retries is reached and checksum still doesn’t match.

tardis.io.util.get_internal_data_path(fname: str) str[source]

Get internal data path of TARDIS.

Parameters:
fnamestr

The filename to join with the internal data path.

Returns:
str

Internal data path of TARDIS joined with the filename.

tardis.io.util.quantity_from_str(text: str) Quantity[source]

Convert a string to astropy.units.Quantity.

Parameters:
textstr

The string to convert to astropy.units.Quantity. Expected format is “value unit”, e.g., “1.0 cm” or “5 log_lsun”.

Returns:
astropy.units.Quantity

The converted quantity with appropriate units.

Notes

Special handling for “log_lsun” unit which is converted to solar luminosity in CGS units.

tardis.io.util.traverse_configs(base: Any, other: Any, func: Any, *args: Any) None[source]

Recursively traverse a base dict or list along with another one calling func for leafs of both objects.

Parameters:
baseAny

The object on which the traversing is done.

otherAny

The object which is traversed along with base.

funcAny

A function called for each leaf of base and the corresponding leaf of other. Signature: func(item1, item2, *args).

*argsAny

Arguments passed into func.

tardis.io.util.yaml_load_file(filename: str, loader: type = <class 'yaml.loader.Loader'>) Any[source]

Load a YAML file using the specified loader.

Parameters:
filenamestr

Path to the YAML file to load.

loadertype, optional

YAML loader class to use, by default yaml.Loader.

Returns:
Any

The loaded YAML content.