-
Notifications
You must be signed in to change notification settings - Fork 1.2k
infra: refactor matching logic in v2 migration tool #1654
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
103 changes: 103 additions & 0 deletions
103
src/sagemaker/cli/compatibility/v2/modifiers/matching.py
This file contains hidden or 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,103 @@ | ||
# Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"). You | ||
# may not use this file except in compliance with the License. A copy of | ||
# the License is located at | ||
# | ||
# http://aws.amazon.com/apache2.0/ | ||
# | ||
# or in the "license" file accompanying this file. This file is | ||
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF | ||
# ANY KIND, either express or implied. See the License for the specific | ||
# language governing permissions and limitations under the License. | ||
"""Functions for checking AST nodes for matches.""" | ||
from __future__ import absolute_import | ||
|
||
import ast | ||
|
||
|
||
def matches_any(node, name_to_namespaces_dict): | ||
"""Determines if the ``ast.Call`` node matches any of the provided names and namespaces. | ||
|
||
Args: | ||
node (ast.Call): a node that represents a function call. For more, | ||
see https://docs.python.org/3/library/ast.html#abstract-grammar. | ||
name_to_namespaces_dict (dict[str, tuple]): a mapping of names to appropriate namespaces. | ||
|
||
Returns: | ||
bool: if the node matches any of the names and namespaces. | ||
""" | ||
return any( | ||
matches_name_or_namespaces(node, name, namespaces) | ||
for name, namespaces in name_to_namespaces_dict.items() | ||
) | ||
|
||
|
||
def matches_name_or_namespaces(node, name, namespaces): | ||
"""Determines if the ``ast.Call`` node matches the function name in the right namespace. | ||
|
||
Args: | ||
node (ast.Call): a node that represents a function call. For more, | ||
see https://docs.python.org/3/library/ast.html#abstract-grammar. | ||
name (str): the function name. | ||
namespaces (tuple): the possible namespaces to match to. | ||
|
||
Returns: | ||
bool: if the node matches the name and any of the namespaces. | ||
""" | ||
if matches_name(node, name): | ||
return True | ||
|
||
if not matches_attr(node, name): | ||
return False | ||
|
||
return any(matches_namespace(node, namespace) for namespace in namespaces) | ||
|
||
|
||
def matches_name(node, name): | ||
"""Determines if the ``ast.Call`` node points to an ``ast.Name`` node with a matching name. | ||
|
||
Args: | ||
node (ast.Call): a node that represents a function call. For more, | ||
see https://docs.python.org/3/library/ast.html#abstract-grammar. | ||
name (str): the function name. | ||
|
||
Returns: | ||
bool: if ``node.func`` is an ``ast.Name`` node with a matching name. | ||
""" | ||
return isinstance(node.func, ast.Name) and node.func.id == name | ||
|
||
|
||
def matches_attr(node, name): | ||
"""Determines if the ``ast.Call`` node points to an ``ast.Attribute`` node with a matching name. | ||
|
||
Args: | ||
node (ast.Call): a node that represents a function call. For more, | ||
see https://docs.python.org/3/library/ast.html#abstract-grammar. | ||
name (str): the function name. | ||
|
||
Returns: | ||
bool: if ``node.func`` is an ``ast.Attribute`` node with a matching name. | ||
""" | ||
return isinstance(node.func, ast.Attribute) and node.func.attr == name | ||
|
||
|
||
def matches_namespace(node, namespace): | ||
"""Determines if the ``ast.Call`` node corresponds to a matching namespace. | ||
|
||
Args: | ||
node (ast.Call): a node that represents a function call. For more, | ||
see https://docs.python.org/3/library/ast.html#abstract-grammar. | ||
namespace (str): the namespace. | ||
|
||
Returns: | ||
bool: if the node's namespaces matches the given namespace. | ||
""" | ||
names = namespace.split(".") | ||
name, value = names.pop(), node.func.value | ||
while isinstance(value, ast.Attribute) and len(names) > 0: | ||
if value.attr != name: | ||
return False | ||
name, value = names.pop(), value.value | ||
|
||
return isinstance(value, ast.Name) and value.id == name |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this change is because the TFS constructor change for
Predictor
-->TensorFlowPredictor
is covered, so we want to prevent false positives fromRealTimePredictor
-->Predictor
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 great call!