Scratchpad
The otter.scratchpad
package contains the scratchpad models and utilities. The
Scratchpad stores variables to be replaced in the Otter configuration files.
scratchpad.model module
Scratchpad module.
This module defines the Scratchpad class, which is a centralized place to store key-value pairs in the configuration of the application. It provides utilities to perform template substition.
- class otter.scratchpad.model.ScratchpadTemplate(template)[source]
Bases:
Template
Scratchpad Template class.
A subclass of string.Template that allows dots and spaces in placeholders, and will keep sentinels there when the key is not found in the sentinel dict.
- idpattern = '(?a:[_a-z][._a-z0-9\\s]*[_a-z0-9])'
- class otter.scratchpad.model.Scratchpad(sentinel_dict: dict[str, Any] | None = None)[source]
Bases:
object
A class to store and replace placeholders in strings.
This class is used to store key-value pairs and replace placeholders in strings with the corresponding values. The placeholders are defined in the strings using the dollar sign followed by the placeholder name enclosed in curly braces, e.g.,
${person.name}
. The placeholders can include dots to represent nested dictionaries or objects.- Example:
>>> scratchpad = Scratchpad() >>> scratchpad.store('person.name', 'Alice') >>> scratchpad.replace('Hello, ${person.name}!') 'Hello, Alice!'
The Scratchpad class is used to perform template substitution in a Spec, right before the task intantiation.
Important
You can use internal scratchpads in tasks. For example, to have private variables that are only replaced in one of the specs.
To be able to do this, however, you must set the
scratchpad_ignore_missing
attribute toTrue
in the task model. This will allow the task to be instantiated even if keys are missing from the global scratchpad.You are responsible, however, of ensuring that the keys are present in the internal scratchpad. It is good practice to leave the internal scratchpad set to not ignore, so that you can catch missing keys.
To set the scratchpad_ignore_missing attribute, you can add this to the spec definition:
def model_post_init(self, __context: Any) -> None: self.scratchpad_ignore_missing = True
- sentinel_dict: dict[str, Any]
A dictionary to store the key-value pairs.
- store(key: str, value: str | list[str]) None [source]
Store a key-value pair in the scratchpad.
Both strings and lists of strings are accepted as values. It might be useful to extend it to accept dicts as well.
- Parameters:
key (str) – The key to store.
value (str | list[str]) – The value to store.
- merge(other: Scratchpad) None [source]
Merge the sentinels from another scratchpad.
- Parameters:
other (Scratchpad) – The scratchpad to merge.
- replace_dict(d: dict[str, Any], *, ignore_missing: bool = False) dict[str, Any] [source]
Replace placeholders in a dictionary with the corresponding values.
- Parameters:
d (dict[str, Any]) – The dictionary with placeholders to replace.
ignore_missing (bool) – Whether to ignore missing keys in the scratchpad. Defaults to
False
.
- Returns:
The dictionary with the placeholders replaced by their values.
- Return type:
dict[str, Any]
Module contents
Scratchpad package.
- otter.scratchpad.load_scratchpad(config_path: Path) Scratchpad [source]
Load the scratchpad.
See
otter.scratchpad.model.Scratchpad
for the scratchpad object.- Parameters:
config_path (Path) – The path to the config file.
- Returns:
The scratchpad object.
- Return type: