util.helper module

Define misc helper functions.

Todo

Clean this, check what is still used.

chunks(lst: list[T], n_size: int) Generator[list[T], int, None]

Yield successive n_size-ed chunks from lst.

https://stackoverflow.com/questions/312443/how-do-i-split-a-list-into-equally-sized-chunks

flatten(nest: Iterable) Iterator

Flatten nested list of lists of…

get_constructor(name: str, constructors: dict[str, type]) type

Get the proper class from a string and dict of classes.

get_constructors(names: Iterable[str], constructors: dict[str, type]) Generator[type, None, None]

Get several class constructors from their names.

pascal_case(message: str) str

Convert a string to Pascal case (as class names).

Todo

Second example does not work

Examples

>>> pascal_case("bonjoure sa_vA")
"BonjoureSaVa"
>>> pascal_case("BonjoureSaVa")
"BonjoureSaVa"
pd_output(message: DataFrame, header: str = '') str

Give a string describing a pandas dataframe.

printc(*args: str, color: str = 'cyan') None

Print colored messages.

range_vals(name: str, data: ndarray | None) str

Return formatted first and last value of the data array.

range_vals_object(obj: object, name: str) str

Return first and last value of the name attr from obj.

recursive_getter(wanted_key: str, dictionary: dict, **kwargs: bool | str | int) Any

Get first key in a possibly nested dictionary.

recursive_items(dictionary: dict[Any, Any]) Iterator[str]

Recursively list all keys of a possibly nested dictionary.

remove_duplicates(iterable: Iterable) Iterator

Create an iterator without duplicates.

Taken from: https://stackoverflow.com/questions/32012878/iterator-object-for-removing-duplicates-in-python

resample(x_1: ndarray, y_1: ndarray, x_2: ndarray, y_2: ndarray) tuple[ndarray, ndarray, ndarray, ndarray]

Downsample y_highres(olution) to x_1 or x_2 (the one with low res).

save_energy_phase_tm(lin: object) None

Save energy, phase, transfer matrix as a function of s.

s [m] E[MeV] phi[rad] M_11 M_12 M_21 M_22

Parameters:

lin (Accelerator object) – Object of corresponding to desired output.

save_vcav_and_phis(lin: object) None

Output the Vcav and phi_s as a function of z.

s [m] V_cav [MV] phi_s [deg]

Parameters:

accelerator (Accelerator object) – Object of corresponding to desired output.