Skip to content

Commit

Permalink
Merge pull request #125 from nunobrum/param_match
Browse files Browse the repository at this point in the history
Param match
  • Loading branch information
nunobrum authored Jan 19, 2025
2 parents b5f1685 + 22b1f8e commit 7b48f5c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 17 deletions.
31 changes: 15 additions & 16 deletions spicelib/editor/asc_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,27 +322,26 @@ def set_component_position(self, reference: str, position: Point, rotation: ERot
component.position = position
component.rotation = rotation

def _get_directive(self, command, search_expression: re.Pattern):
command_upped = command.upper()
def _find_param_declaration(self, param_name):
param_name_uppercase = param_name.upper()
search_expression = re.compile(PARAM_REGEX(r"\w+"), re.IGNORECASE)
for directive in self.directives:
command_upped_directive = directive.text.upper()
if command_upped_directive.startswith(command_upped):
match = search_expression.search(directive.text)
if match:
return match, directive
if directive.text.upper().startswith(".PARAM"):
matches = search_expression.finditer(directive.text)
for match in matches:
if match.group("name").upper() == param_name_uppercase:
return match, directive
return None, None

def get_parameter(self, param: str) -> str:
param_regex = re.compile(PARAM_REGEX(param), re.IGNORECASE)
match, directive = self._get_directive(".PARAM", param_regex)
match, directive = self._find_param_declaration(param)
if match:
return match.group('value')
else:
raise ParameterNotFoundError(f"Parameter {param} not found in ASC file")

def set_parameter(self, param: str, value: Union[str, int, float]) -> None:
param_regex = re.compile(PARAM_REGEX(param), re.IGNORECASE)
match, directive = self._get_directive(".PARAM", param_regex)
match, directive = self._find_param_declaration(param)
if match:
_logger.debug(f"Parameter {param} found in ASC file, updating it")
if isinstance(value, (int, float)):
Expand Down Expand Up @@ -433,11 +432,11 @@ def get_component_parameters(self, element: str, as_dicts: bool = False) -> dict
# if we have a structured attribute, return the full dict of it
# this is compatible with set_component_parameters
sub_parameters = {}
matches = search_regex.findall(value)
if matches:
# This might contain one or more parameters
for param_name, param_value in matches:
sub_parameters[param_name] = try_convert_value(param_value)
matches = search_regex.finditer(value)
# This might contain one or more parameters
for match in matches:
sub_parameters[match.group("name")] = try_convert_value(match.group("value"))
if sub_parameters:
if as_dicts:
parameters[key] = sub_parameters
else:
Expand Down
2 changes: 1 addition & 1 deletion spicelib/editor/base_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@


def PARAM_REGEX(pname):
return r"(?P<name>" + pname + r")\s*[= ]\s*(?P<value>[\w\*\/\.\+\-\/\*\{\}\(\)%]*)"
return r"(?P<name>" + pname + r")\s*[= ]\s*(?P<value>(?P<cb>\{)?(?(cb)[^\}]*\}|[\d\+\-Ee]+[a-zA-Z%]*))"


def format_eng(value) -> str:
Expand Down
5 changes: 5 additions & 0 deletions unittests/test_asc_editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ def test_parameter_edit(self):
self.equalFiles(temp_dir + 'test_parameter_output.asc', golden_dir + 'test_parameter_output.asc')
self.edt.set_parameter('TEMP', 0) # reset to 0
self.assertEqual(self.edt.get_parameter('TEMP'), '0', "Tested TEMP Parameter") # add assertion here
self.edt.set_parameters(ttotal="{ton + toff}")
self.edt.set_parameters(ton="34n", toff="{10p + 50p}")
self.assertEqual("34n", self.edt.get_parameter("ton"), "ton test 1")
self.edt.set_parameters(ton="{sin(0.22)}", toff="{10p + 50p}")
self.assertEqual("{sin(0.22)}", self.edt.get_parameter("ton"), "ton test 2")

def test_instructions(self):
self.edt.add_instruction('.ac dec 10 1 100k')
Expand Down

0 comments on commit 7b48f5c

Please sign in to comment.