-
Notifications
You must be signed in to change notification settings - Fork 89
ENH: Add Support for dict unpacking in csp.node
#730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,19 @@ | |
| from csp.impl.wiring.base_parser import BaseParser, CspParseError, _pythonic_depr_warning | ||
|
|
||
|
|
||
| def _csp_output_kwargs(outputs_by_name, values): | ||
| try: | ||
| items = values.items() | ||
| except AttributeError: | ||
| raise TypeError(f"csp.output(**{values!r}) requires a dict-like value") from None | ||
| for k, v in items: | ||
| try: | ||
| proxy = outputs_by_name[k] | ||
| except KeyError: | ||
| raise KeyError(f"unrecognized output '{k}'") from None | ||
| proxy + v | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment here noting we override the ast.Add operator to output a value, elsewise this code looks like it's dead. You may also need to do a ruff noqa to tell ruff to ignore it |
||
|
|
||
|
|
||
| class _SingleProxyFuncArgResolver(object): | ||
| class INVALID_VALUE: | ||
| pass | ||
|
|
@@ -73,6 +86,7 @@ class NodeParser(BaseParser): | |
| _CSP_ENGINE_START_TIME_FUNC = "_engine_start_time" | ||
| _CSP_ENGINE_END_TIME_FUNC = "_engine_end_time" | ||
| _CSP_ENGINE_STATS_FUNC = "_csp_engine_stats" | ||
| _CSP_OUTPUT_KWARGS_FUNC = "_csp_output_kwargs" | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: you can remove _CSP_OUTPUT_KWARGS_FUNC from the dicts here and just call |
||
|
|
||
| _CSP_STOP_ENGINE_FUNC = "_csp_stop_engine" | ||
| _CSP_IN_REALTIME_FUNC = "_csp_in_realtime" | ||
|
|
@@ -83,6 +97,7 @@ class NodeParser(BaseParser): | |
| _CSP_STOP_ENGINE_FUNC: _cspimpl._csp_stop_engine, | ||
| _CSP_ENGINE_STATS_FUNC: _cspimpl._csp_engine_stats, | ||
| _CSP_IN_REALTIME_FUNC: _cspimpl._csp_in_realtime, | ||
| _CSP_OUTPUT_KWARGS_FUNC: _csp_output_kwargs, | ||
| } | ||
|
|
||
| _SPECIAL_BLOCKS_METH = {"alarms", "state", "start", "stop", "outputs"} | ||
|
|
@@ -402,6 +417,10 @@ def _parse_output_or_return(self, node, is_return): | |
| node.lineno, | ||
| ) | ||
| nodes = [] | ||
| for node_arg in node.args: | ||
| if isinstance(node_arg, ast.Starred): | ||
| raise CspParseError(f"{func_name} does not support * unpacking", node.lineno) | ||
|
|
||
| if len(node.args) == 1: | ||
| if len(self._signature._outputs) > 1 and self._signature.output(0).name is not None: | ||
| raise CspParseError( | ||
|
|
@@ -464,6 +483,20 @@ def _parse_output_or_return(self, node, is_return): | |
| self._returned_outputs.add(node.args[0].id) | ||
|
|
||
| for arg in node.keywords: | ||
| if arg.arg is None: | ||
| # A **expr unpack, resolved at runtime, can't statically verify | ||
| # which outputs it covers, so assume it may cover all of them. | ||
| self._returned_outputs.update(o.name for o in self._signature._outputs if o.name is not None) | ||
| nodes.append( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can try to avoid the function call overhead and generator the for loop AST directly here instead
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I'm working on it, I saw the
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not sure i follow, where would there be an extra dict creation? ** would be replaced with a loop on the variable being expanded, no new dict being created. we should add an isinstance check to your point above ( #730 (comment) )
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After the inline change it would generate: while True:
yield
if +#inp_0:
values = {'a': 1, 'b': 2}
for k, v in values.items():
{'a': #outp_0, 'b': #outp_1}[k] + vA dict display is a runtime instruction, Python constructs a fresh dict every time it evaluates yield
#csp_outmap = {'a': #outp_0, 'b': #outp_1} # built once with proxies
...
while True:
yield
if +#inp_0:
values = {'a': 1, 'b': 2}
for #k, #v in values.items():
#csp_outmap[#k] + #vHoping I'm not missing something obvious here!
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right I forgot that you need to lookup the out proxy dynamically but yes of course you would build the map once outside of the generator loop |
||
| ast.Call( | ||
| func=ast.Name(id=self._CSP_OUTPUT_KWARGS_FUNC, ctx=ast.Load()), | ||
| args=[self._build_outputs_by_name_dict(node), arg.value], | ||
| keywords=[], | ||
| lineno=node.lineno, | ||
| end_lineno=node.end_lineno, | ||
| ) | ||
| ) | ||
| continue | ||
| if self._signature.output(arg.arg, True) is None: | ||
| raise CspParseError(f"unrecognized output '{arg.arg}'", node.lineno) | ||
| output = self._signature.output(arg.arg) | ||
|
|
@@ -505,6 +538,21 @@ def _parse_output_or_return(self, node, is_return): | |
|
|
||
| return res | ||
|
|
||
| def _build_outputs_by_name_dict(self, node): | ||
| keys = [] | ||
| values = [] | ||
| for output in self._signature._outputs: | ||
| if output.name is None: | ||
| continue | ||
| keys.append(ast.Constant(value=output.name)) | ||
| values.append(self._ts_outproxy_expr(output.name)) | ||
| return ast.Dict( | ||
| keys=keys, | ||
| values=values, | ||
| lineno=node.lineno, | ||
| end_lineno=node.end_lineno, | ||
| ) | ||
|
|
||
| def _parse_output(self, node): | ||
| return self._parse_output_or_return(node=node, is_return=False) | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.