perun.util¶
Util module.
Attributes¶
Classes¶
Metaclass for creating singleton classes. |
Functions¶
|
Return run id based on the configuration object or the current datetime. |
|
Increase id counter based on number of existing entries with the same id. |
|
Filter sensors based on include and exclude lists. |
|
Check if a string matches any of the given patterns. |
Module Contents¶
- class perun.util.Singleton[source]¶
Bases:
typeMetaclass for creating singleton classes.
Singleton classes are classes that can only have one instance. This metaclass ensures that only one instance of a class is created and provides a way to access that instance.
- _instances¶
A dictionary that stores the instances of the singleton classes. The keys are the singleton classes and the values are the corresponding instances.
- Type:
dict
- __allow_reinitialization¶
A flag that indicates whether the singleton class allows reinitialization. If set to True, the __call__ method will reinitialize the instance if the class has already been instantiated.
- Type:
bool
- __call__(*args, \*\*kwargs)[source]¶
Overrides the default behavior of calling the class. It ensures that only one instance of the class is created and returns that instance.
Examples
>>> class MyClass(metaclass=Singleton): ... pass >>> my_instance = MyClass() # Returns the instance of MyClass
- perun.util.getRunId(starttime: datetime.datetime, run_id: str | None = None) str[source]¶
Return run id based on the configuration object or the current datetime.
- Parameters:
starttime (datetime) – The datetime object representing the start time of the run.
run_id (str, optional) – A string with the id given by the user in the configuration or the command line.
- Returns:
The run id.
- Return type:
str
Notes
If the configuration object has a valid run id specified, it will be returned. If the run id is set to “SLURM” and the environment variable “SLURM_JOB_ID” is present, the value of “SLURM_JOB_ID” will be returned. Otherwise, the ISO formatted string representation of the start time will be returned.
- perun.util.increaseIdCounter(existing: List[str], newId: str) str[source]¶
Increase id counter based on number of existing entries with the same id.
- Parameters:
existing (List[str]) – List of existing ids.
newId (str) – New id to compare againts.
- Returns:
newId with an added counter if any matches were found.
- Return type:
str
- perun.util.filter_sensors(sensors: Dict[str, Tuple], include_sensors: List[str] | None = None, exclude_sensors: List[str] | None = None, include_backends: List[str] | None = None, exclude_backends: List[str] | None = None) Dict[str, Tuple][source]¶
Filter sensors based on include and exclude lists.
- Parameters:
sensors (Dict[str, Tuple]) – Dictionary of sensors.
include_sensors (Optional[List[str]], optional) – List of sensors to include, by default None
exclude_sensors (Optional[List[str]], optional) – List of sensors to exclude, by default None
include_backends (Optional[List[str]], optional) – List of backends to include, by default None
exclude_backends (Optional[List[str]], optional) – List of backends to exclude, by default None
- Returns:
Filtered dictionary of sensors.
- Return type:
Dict[str, Tuple]
- perun.util.matchesOneOf(patterns: List[str], string: str) bool[source]¶
Check if a string matches any of the given patterns.
- Parameters:
patterns (List[str]) – List of patterns to match against.
string (str) – String to check.
- Returns:
True if the string matches any of the patterns, False otherwise.
- Return type:
bool