mlonmcu.context package
Submodules
mlonmcu.context.context module
Definition if the contextmanager for mlonmcu environments.
- class mlonmcu.context.context.MlonMcuContext(name: str | None = None, path: str | None = None, deps_lock: str = 'write')[source]
Bases:
object
Contextmanager for mlonmcu environments.
- Attributes:
- environmentEnvironment
The MLonMCU Environment where paths, repos, features,… are configured.
- deps_lockstr (“read” or “write” default “write”)
Read means that the program does not write to the ./deps folder in the env folder.
- sessionslist
List of sessions for the current environment.
- session_idxlist
A counter for determining the next session index.
- cacheTaskCache
The cache where paths of installed dependencies can be looked up.
- get_session(label='', resume=False, config=None) Session [source]
Get an active session if available, else create a new one.
- Returns:
- Session:
An active session
- property is_clean
Return true if all sessions in the context are inactive
- mlonmcu.context.context.get_environment_by_name(name: str) Environment [source]
Utility to find an environment file using a supplied name.
- Parameters:
- namestr
The name/alias if the environment.
- Returns:
- Environment
The environment (if the lookup was successful).
- mlonmcu.context.context.get_environment_by_path(path: str | Path) Environment [source]
Utility to find an environment file using a supplied path.
- Parameters:
- pathstr/Path
The path of the environment (or its YAML file).
- Returns:
- Environment:
The environment (if the lookup was successful).
- mlonmcu.context.context.get_ids(directory: Path) List[int] [source]
Get a sorted list of ids for sessions/runs found in the given directory.
- Parameters:
- directoryPath
Directory where the sessions/runs are stored.
- Returns:
- list
List of integers representing the session numbers. Empty list if directory does not exist.
- mlonmcu.context.context.load_recent_sessions(env: Environment, count: int | None = None) List[Session] [source]
Get a list of recent sessions for the environment.
- Parameters:
- envEnvironment
MLonMCU environment which should be used.
- countint
Maximum number of sessions to return. Collect all if None.
- Returns:
- list:
The resulting list of session objects.
- mlonmcu.context.context.lookup_environment() Environment [source]
Helper function to automatically find a suitable environment.
This function is used if neither a name nor a path of the environment was specified by the user. The lookup follows a predefined order: - Check current working directory - Check MLONMCU_HOME environment variable - Default environment for current user
- Returns:
- environmentPath
The environment (if the lookup was successful).
- mlonmcu.context.context.resolve_environment_file(name: str | None = None, path: str | None = None) Path [source]
Utility to find the environment file by a optionally given name or path.
The lookup is performed in a predefined order: - If specified: name/path - Else: see lookup_environment()
- Parameters:
- namestr
Hint for the environment name provided by the user.
- pathstr
Hint for the environment path provided by the user.
- Returns:
- Path
Path to the found environment.yml (if sucessful)
mlonmcu.context.read_write_filelock module
This file contains read lock and write lock classes based on filelock The locks are non-blocking.
- exception mlonmcu.context.read_write_filelock.RWLockTimeout(lock)[source]
Bases:
TimeoutError
Raised when the lock could not be acquired.
- lock
The Read or Write lock instance.
- class mlonmcu.context.read_write_filelock.ReadFileLock(filepath)[source]
Bases:
object
- acquire(raise_exception=True)[source]
This function tried to acquire a ReadFileLock. The process is the following: 1. acquire filelock 2. read the lock occupation situation 3. check if the lock is already occupied by another write process 4.1. release filelock and raise exception(or return 0) if the lock is already occupied by another write process 4.2. otherwise write the updated lock occupation situation back, release filelock and return
- Parameters:
raise_exception (bool): whether an exception should be raised when failed (default: True)
- Returns:
- success (bool): whether succeeded or not.
True means succeeded, False means failed (if the param raiseException is set to False). A RWLockTimeout exception will be raised if failed (if the param raiseException is set to True).
- property is_locked
This property returns if a lock is occupied(locked) by other processes. the process is the following: 1. acquire filelock 2. read the lock occupation situation 3. check if the lock is already occupied(locked) by another write process
- Returns:
is_locked (bool): whether the lock is already occupied(locked) by another write process
- release()[source]
This function releases a ReadFileLock. the process is the following: 1. acquire filelock 2. read the lock occupation situation 3. delete the record of self.id (no exception will be raised if self.id is not found in the record) 4. write the updated lock occupation situation back, release filelock and return
- class mlonmcu.context.read_write_filelock.WriteFileLock(filepath)[source]
Bases:
object
- acquire(raise_exception=True)[source]
This function tried to acquire a WriteFileLock. The process is the following: 1. acquire filelock 2. read the lock occupation situation 3. check if the lock is already occupied by another write process 4.1. release filelock and raise exception(or return 0) if the lock is already occupied by another write process 4.2. otherwise write the updated lock occupation situation back, release filelock and return
- Parameters:
raise_exception (bool): whether raises an exception when failed (default: True)
- Returns:
- success (bool): whether succeeded or not.
True means succeeded, False means failed (if the param raiseException is set to False). A RWLockTimeout exception will be raised if failed (if the param raiseException is set to True).
- property is_locked
This property returns if a lock is occupied(locked) by other processes. the process is the following: 1. acquire filelock 2. read the lock occupation situation 3. check if the lock is already occupied(locked) by another write process
- Returns:
is_locked (bool): whether the lock is already occupied(locked) by another write process
- release()[source]
This function releases a WriteFileLock. the process is the following: 1. acquire filelock 2. read the lock occupation situation 3. delete the record of self.id (no exception will be raised if self.id is not found in the record) 4. write the updated lock occupation situation back, release filelock and return
Module contents
- class mlonmcu.context.MlonMcuContext(name: str | None = None, path: str | None = None, deps_lock: str = 'write')[source]
Bases:
object
Contextmanager for mlonmcu environments.
- Attributes:
- environmentEnvironment
The MLonMCU Environment where paths, repos, features,… are configured.
- deps_lockstr (“read” or “write” default “write”)
Read means that the program does not write to the ./deps folder in the env folder.
- sessionslist
List of sessions for the current environment.
- session_idxlist
A counter for determining the next session index.
- cacheTaskCache
The cache where paths of installed dependencies can be looked up.
- get_session(label='', resume=False, config=None) Session [source]
Get an active session if available, else create a new one.
- Returns:
- Session:
An active session
- property is_clean
Return true if all sessions in the context are inactive