cosmograph.util
Utils to prepare data for cosmos
- class cosmograph.util.JsFiles(rootdir, subpath='', pattern_for_field=None, max_levels=None, *, include_hidden=False, assert_rootdir_existence=False)[source]
A store of js files
- cosmograph.util.annotation_to_str(annotation, *, remove_typing_prefix=True)[source]
Encodes Python type annotations as strings for JSON serialization.
This is part of a specialized Python-to-string codec designed to encode and decode Python type annotations in a way that allows them to be serialized and deserialized in JSON-compatible formats.
- Parameters:
annotation – The type annotation to encode.
- Returns:
A string representation of the annotation.
- Raises:
ValueError – If the annotation type is unknown.
Examples: >>> original = [ … bool, … float, … list[Any], … typing.Union[str, list[float]], … int, … str, … list[float], … list[list[float]], … typing.Union[int, str], … list[str], … object, … list[int], … typing.Callable[[typing.Dict[str, Any]], Any] … ] >>> encoded = list(map(annotation_to_str, original)) >>> encoded # doctest: +NORMALIZE_WHITESPACE [‘bool’, ‘float’, ‘list[Any]’, ‘Union[str, list[float]]’, ‘int’, ‘str’, ‘list[float]’, ‘list[list[float]]’, ‘Union[int, str]’, ‘list[str]’, ‘object’, ‘list[int]’, ‘Callable[[Dict[str, Any]], Any]’]
- cosmograph.util.cosmograph_base_docs(param_names=None, take_name_of_types=True)[source]
Get the params information part of a docstring
- cosmograph.util.default_is_safe(string: str) bool[source]
Checks if a string is safe to evaluate as a type annotation.
The string is considered safe if: - It contains only alphanumericals, spaces, dots, commas, and brackets. - It starts with one of a predefined list of allowed prefixes.
- Parameters:
string – The string to check.
- Returns:
True if the string is safe, False otherwise.
- cosmograph.util.is_parameterized_type(obj)[source]
Determines if an object is a parameterized type (e.g., list[int]) or not.
- Parameters:
obj – The object to check.
- Returns:
True if it’s a parameterized type, False otherwise.
- cosmograph.util.js_files_as_attrs(rootdir)[source]
Will make a JsFiles, but where the keys are available as attributes. To do so, any non alphanumerics of file name are replaced with underscore, and there can be no two files that collide with that key transformation!
- cosmograph.util.move_to_front(df: DataFrame, cols) DataFrame[source]
Move the columns in cols to the front of the DataFrame
- cosmograph.util.str_to_annotation(string, is_safe: ~typing.Callable[[str], bool] = <function default_is_safe>)[source]
Decodes a string representation of a Python type annotation back into the annotation.
- Parameters:
string – The string representation of the annotation.
is_safe – A function that determines if the string is safe to evaluate.
- Returns:
The decoded Python type annotation.
- Raises:
ValueError – If the string is not safe to evaluate.