perun.util

Util module.

Attributes

log

Classes

Singleton

Metaclass for creating singleton classes.

Functions

getRunId(→ str)

Return run id based on the configuration object or the current datetime.

increaseIdCounter(→ str)

Increase id counter based on number of existing entries with the same id.

filter_sensors(→ Dict[str, Tuple])

Filter sensors based on include and exclude lists.

matchesOneOf(→ bool)

Check if a string matches any of the given patterns.

Module Contents

perun.util.log
class perun.util.Singleton

Bases: type

Metaclass 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.

Attributes

_instancesdict

A dictionary that stores the instances of the singleton classes. The keys are the singleton classes and the values are the corresponding instances.

__allow_reinitializationbool

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.

Methods

__call__(*args, **kwargs)

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
_instances: Dict
__allow_reinitialization: bool = False
__call__(*args, **kwargs)

Call method for the singleton class.

This method is called when the singleton class is invoked as a function. It ensures that only one instance of the class is created and returned.

Parameters

clsclass object

The class object.

*argstuple

Variable length argument list.

**kwargsdict

Arbitrary keyword arguments.

Returns

instanceobject

The instance of the singleton class.

Examples

>>> instance = MyClass()
>>> instance()
<MyClass object at 0x7f9a8a3d6a90>
perun.util.getRunId(starttime: datetime.datetime) str

Return run id based on the configuration object or the current datetime.

Parameters

starttimedatetime

The datetime object representing the start time of the run.

Returns

str

The run id.

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

Increase id counter based on number of existing entries with the same id.

Parameters

existingList[str]

List of existing ids.

newIdstr

New id to compare againts.

Returns

str

newId with an added counter if any matches were found.

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]

Filter sensors based on include and exclude lists.

Parameters

sensorsDict[str, Tuple]

Dictionary of sensors.

include_sensorsOptional[List[str]], optional

List of sensors to include, by default None

exclude_sensorsOptional[List[str]], optional

List of sensors to exclude, by default None

include_backendsOptional[List[str]], optional

List of backends to include, by default None

exclude_backendsOptional[List[str]], optional

List of backends to exclude, by default None

Returns

Dict[str, Tuple]

Filtered dictionary of sensors.

perun.util.matchesOneOf(patterns: List[str], string: str) bool

Check if a string matches any of the given patterns.

Parameters

patternsList[str]

List of patterns to match against.

stringstr

String to check.

Returns

bool

True if the string matches any of the patterns, False otherwise.