-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a function to allow MetaData over the wire (#78)
Provides language constructs for adding MetaData to the query
- Loading branch information
1 parent
0535266
commit b9df64d
Showing
7 changed files
with
142 additions
and
16 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,6 @@ | ||
from .meta_data import extract_metadata # NOQA | ||
from .call_stack import argument_stack, stack_frame # NOQA | ||
from .func_adl_ast_utils import FuncADLNodeVisitor, function_call # NOQA | ||
from .aggregate_shortcuts import aggregate_node_transformer # NOQA | ||
from .func_adl_ast_utils import change_extension_functions_to_calls # NOQA | ||
from .function_simplifier import simplify_chained_calls # NOQA |
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,56 @@ | ||
|
||
import ast | ||
from func_adl.ast.func_adl_ast_utils import FuncADLNodeTransformer | ||
from typing import Dict, List, Tuple | ||
|
||
|
||
class _extract_metadata(FuncADLNodeTransformer): | ||
'''Extract all the metadata from an expression, and remove the | ||
metadata nodes. Assume the metadata can all be bubbled to the top and | ||
has equal precedence. | ||
''' | ||
def __init__(self): | ||
super().__init__() | ||
self._metadata = [] | ||
|
||
@property | ||
def metadata(self) -> List[Dict[str, str]]: | ||
'''Returns the metadata found while scanning expressions | ||
in the order it was encountered. | ||
Returns: | ||
List[Dict[str, str]]: List of all metadata found. | ||
''' | ||
return self._metadata | ||
|
||
def visit_Call(self, node: ast.Call): | ||
'''Detect a MetaData call, and remove it, storing the | ||
information. | ||
Args: | ||
node (ast.Call): The call node to process. | ||
Returns: | ||
ast.AST: The ast without the call node (if need be). | ||
''' | ||
if isinstance(node.func, ast.Name) and node.func.id == 'MetaData': | ||
self._metadata.append(ast.literal_eval(node.args[1])) | ||
return self.visit(node.args[0]) | ||
return super().visit_Call(node) | ||
return super().visit_Call(node) | ||
|
||
|
||
def extract_metadata(a: ast.AST) -> Tuple[ast.AST, List[Dict[str, str]]]: | ||
'''Returns the expresion with extracted metadata and the metadata, in order | ||
from the outter most to the inner most `MetaData` expressions. | ||
Args: | ||
a (ast.AST): The AST potentially containing metadata definitions | ||
Returns: | ||
Tuple[ast.AST, List[Dict[str, str]]]: a new AST without the metadata references | ||
and a list of metadata found. | ||
''' | ||
e = _extract_metadata() | ||
a_new = e.visit(a) | ||
return a_new, e.metadata |
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,43 @@ | ||
|
||
|
||
import ast | ||
from func_adl.ast.meta_data import extract_metadata | ||
from typing import Dict, List | ||
|
||
|
||
def compare_metadata(with_metadata: str, without_metadata: str) -> List[Dict[str, str]]: | ||
''' | ||
Compares two AST expressions after first removing all metadata references from the | ||
first expression. Returns a list of dictionaries of the found metadata | ||
''' | ||
a_with = ast.parse(with_metadata) | ||
a_without = ast.parse(without_metadata) | ||
|
||
a_removed, metadata = extract_metadata(a_with) | ||
|
||
assert ast.dump(a_removed) == ast.dump(a_without) | ||
return metadata | ||
|
||
|
||
def test_no_metadata(): | ||
'Make sure expression with no metadata is not changed' | ||
meta = compare_metadata("Select(jets, lambda j: j*2)", "Select(jets, lambda j: j*2)") | ||
assert len(meta) == 0 | ||
|
||
|
||
def test_simple_metadata(): | ||
'Make sure expression with metadata correctly cleaned up and removed' | ||
meta = compare_metadata("MetaData(Select(jets, lambda j: j*2), {'hi': 'there'})", | ||
"Select(jets, lambda j: j*2)") | ||
assert len(meta) == 1 | ||
assert meta[0] == {'hi': 'there'} | ||
|
||
|
||
def test_two_metadata(): | ||
'Make sure expression with no metadata is not changed' | ||
meta = compare_metadata( | ||
"MetaData(Select(MetaData(jets, {'fork': 'dude'}), lambda j: j*2), {'hi': 'there'})", | ||
"Select(jets, lambda j: j*2)") | ||
assert len(meta) == 2 | ||
assert meta[0] == {'hi': 'there'} | ||
assert meta[1] == {'fork': 'dude'} |
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