Skip to content

reflections

tergite_autocalibration.utils.misc.reflections

Classes:

Name Description
ASTParser

Parser for the abstract syntax tree. This is to allow inferring more information from

Functions:

Name Description
find_inheriting_classes_ast_recursive

Recursively finds all classes in a directory and its subdirectories that inherit

get_class_attributes

Current implementation only supports to return lists, but can easily be extended.

import_class_from_file

Imports a class from a given file path.

ASTParser

Parser for the abstract syntax tree. This is to allow inferring more information from the sourcecode itself.

Methods:

Name Description
get_init_attribute_names

Returns all the attributes from the init function of a class

get_init_attribute_names staticmethod

get_init_attribute_names() -> Set[str]

Returns all the attributes from the init function of a class

Examples:

1
2
3
4
5
6
7
8
9
>>> class MyClass:
>>>
>>>     def __init__(self):
>>>         self.attr1 = 1
>>>         self.attr2: str = "hello"
>>>
>>> init_attributes = ASTParser.get_init_attribute_names(MyClass)
>>> logger.info(init_attributes)
{'attr1', 'attr2'}

Parameters:

Name Type Description Default
cls Type[Any]

Class to be analysed

required

Returns:

Type Description
Set[str]

Set[str]: The names of the attributes in init as set

find_inheriting_classes_ast_recursive

find_inheriting_classes_ast_recursive(directory: Union[str, Path], base_class_name: str = None) -> dict

Recursively finds all classes in a directory and its subdirectories that inherit from the specified base class using AST parsing.

Parameters:

Name Type Description Default
directory str

The root directory to search for classes.

required
base_class_name str

The name of the base class to check inheritance.

None

Returns:

Name Type Description
dict dict

A dictionary where keys are file paths, and values are lists of class names inheriting from base_class_name.

get_class_attributes

get_class_attributes(file_path: Union[str, Path], class_name: str) -> Dict[str, List[str]]

Current implementation only supports to return lists, but can easily be extended.

Parameters:

Name Type Description Default
file_path Union[str, Path]

Path to the file that contains the class.

required
class_name str

Name of the class to parse.

required

Returns:

Type Description
Dict[str, List[str]]

A dictionary that maps the class attribute name to their values.

import_class_from_file

import_class_from_file(class_name: str, file_path: Union[str, Path]) -> Type

Imports a class from a given file path.

Parameters:

Name Type Description Default
class_name str

The name of the class to import.

required
file_path str

The path to the file containing the class.

required

Returns:

Name Type Description
Type Type

The class type if found, otherwise raises an AttributeError or FileNotFoundError.