utils.py
Various utilities.
- utils._extract_field_names(fields_d: dict, depth: int = 0) list
Extract field names (keys of dict) into a list and return it. Ignores fields which are part of the _permitted_duplicate_field_names list. Do this recursively if you find a union or a struct.
- Parameters:
fields_d (dict) – dict containing field names (keys) and their properties (values)
- Returns:
field_names – list of field names in this dict
- Return type:
list
- utils.add_auxiliary_fields(contents_d: dict, id_checks: bool = True, part_struct_accessors: bool = False) dict
Run through the read in contents (passed as contents_d) and add auxiliary fields, if necessary: - If a struct part does not exist, create one and put it in first position. - If we’re doing debug_id_checks for each struct, add that field to each substruct
- Parameters:
contents_d (dict) – dict containing contents of read-in yml file to generate structs and fields from
id_checks (bool) – if True, add a field for IDs for each struct for debugging checks
part_struct_accessors (bool) – if True, add auxiliary fields needed for part struct accessors (offset/index in cell’s particle arrays for this particle, and pointer to cell_part_arrays)
Returns:
- updated_contents_d: dict
Updated dict of particle fields
- utils.check_output_directory(outdir: str | list, swift_header: bool = False, full_output: bool = False, testing: bool = False)
Extract the output directory from the parsed args. If it doesn’t exist, make it.
- Parameters:
outdir (str or list) – (top level) output directory to write output into.
swift_header (bool) – If True, assume we’re writing files for SWIFT, not SWIFTGPUPACKSIM.
full_output (bool) – If True, we’re writing all files, not just (hydro) particles
testing (bool) – If True, we’re writing outputs for unit tests
- Returns:
outdir – cleaned and verified output dir.
- Return type:
string
- utils.check_part_struct_first_in_list(contents_d: dict) bool
Ensure that the particle struct is first in line.
- contents_d: dict
dict containing yml read-in data of particle structs and fields to generate headers from
- Returns:
has_part_struct – True if contents_d has a struct ‘part’ to be generated
- Return type:
bool
- utils.get_full_file_path_relative_to_script(relpath: str) str
get the full (absolute) file path of a file, where the file is specified by relpath, its relative path w.r.t. the location of this script
- utils.get_git_hash() str
Get the git hash of the latest commit. If git is unavailable or command fails for any reason, return current time instead.
- utils.print_separator(msg: str = '')
- utils.process_yml_metadata(metadata_d: dict, is_particle: bool = True, swift_header: bool = False) None
Run through the read-in metadata from the yml file, passed as the dict metadata_d, validate that there are no issues, and set up defaults if necessary.
- Parameters:
metadata_d (dict) – dict containing read-in metadata items from the yml file
is_particle (bool) – if True, assume we’re working with a (hydro) particle.
swift_header (bool) – if True, we’re creating header files for SWIFT. Otherwise, for SWIFTGPUPACKSIM.
- utils.read_default_includes(swift_header: bool = False)
Read default headers to be included.
Returns: List of headers as strings
- utils.read_input_file(filename: str)
Read the yaml input file.
- Returns:
data_d (Dict containing all particle fields specified in yaml file)
metadata_d (Dict containing (SPH flavour) metadata specified in yaml file)
- utils.sanitize_C_macro(macro: str) str
Sanitize a string to be used as a C macro
- utils.sanitize_include_headers(headerfiles: list)
Sanitize header file names, passed as a list of strings
Returns: list of sanitized file names
- utils.truncate_repo_file_path(path: str) str
cut out everything preceeding this repository’s top level dir from the given file path.
Assumes that path is within the repository.
- utils.validate_yml_contents(contents_d: dict, filename: str, is_particle: bool = True, testing: bool = False) None
Run through the read-in data from the yml file, passed as the dict contents_d, and validate that there is no issues:
- Parameters:
contents_d (dict containing the contents of the read-in file)
filename (file name of the read in yml file)
is_particle (bool) – if True, assume this is the data for a hydro particle
testing (bool) – if True, assume the output is intended for unit tests and skip some checks.
twice) ((- no top level struct defined)
twice (- no fields with same name defined)
- utils.verify_dir_exists(dname: str, message: str = '')
Check if file exists, and if not, raise an error. Optionally, pass an additional message to be displayed upon error.
- utils.verify_file_exists(fname: str, message: str = '')
Check if file exists, and if not, raise an error. Optionally, pass an additional message to be displayed upon error.