-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
first pass at parsing and using parameters in python protocols
- Loading branch information
Showing
5 changed files
with
115 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from typing import Dict, Optional | ||
|
||
from opentrons.protocol_reader.parameter_definition import AllowedTypes | ||
|
||
|
||
class Parameters: | ||
def __init__(self, parameters: Optional[Dict[str, AllowedTypes]] = None) -> None: | ||
self._values: Dict[str, AllowedTypes] = {} | ||
if parameters is not None: | ||
for name, value in parameters.items(): | ||
self._set_parameter(name, value) | ||
|
||
def _set_parameter(self, variable_name: str, value: AllowedTypes) -> None: | ||
# TODO raise an error if the variable name already exists to prevent overwriting anything important | ||
if not hasattr(self, variable_name): | ||
setattr(self, variable_name, value) | ||
self._values[variable_name] = value | ||
|
||
def get_all(self) -> Dict[str, AllowedTypes]: | ||
return self._values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Parameter parser for python protocols.""" | ||
|
||
from typing import List, Dict | ||
|
||
from .parameter_definition import ParameterDefinition, AllowedTypes | ||
|
||
|
||
class ParameterParser: | ||
"""Parser for parameters defined in a python protocol.""" | ||
|
||
def __init__(self) -> None: | ||
"""Initializes a parser for user-set parameters.""" | ||
self._parameters: List[ParameterDefinition[AllowedTypes]] = [] | ||
|
||
def add_int( | ||
self, | ||
display_name: str, | ||
variable_name: str, | ||
default: int, | ||
minimum: int, | ||
maximum: int, | ||
) -> None: | ||
"""Creates an integer parameter, settable within a given range. | ||
Arguments: | ||
display_name: The display name of the int parameter as it would show up on the frontend. | ||
variable_name: The variable name the int parameter will be referred to in the run context. | ||
default: The default value the int parameter will be set to. This will be used in initial analysis. | ||
minimum: The minimum value the int parameter can be set to (inclusive). | ||
maximum: The maximum value the int parameter can be set to (inclusive). | ||
""" | ||
self._parameters.append( | ||
ParameterDefinition( | ||
parameter_type=int, | ||
display_name=display_name, | ||
variable_name=variable_name, | ||
default=default, | ||
minimum=minimum, | ||
maximum=maximum, | ||
) | ||
) | ||
|
||
def get_variable_names_and_values(self) -> Dict[str, AllowedTypes]: | ||
"""Returns all parameters in a dictionary with the variable name as the key and current value as the value.""" | ||
return { | ||
parameter.variable_name: parameter.value for parameter in self._parameters | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters