|
| 1 | +# Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +"""Simplify Search Expression by provide a simplified DSL""" |
| 14 | +from smexperiments._base_types import ApiObject |
| 15 | +from enum import Enum, unique |
| 16 | + |
| 17 | + |
| 18 | +@unique |
| 19 | +class Operator(Enum): |
| 20 | + EQUALS = "Equals" |
| 21 | + NOT_EQUALS = "NotEquals" |
| 22 | + GREATER_THAN = "GreaterThan" |
| 23 | + GREATER_THAN_OR_EQUAL = "GreaterThanOrEqualTo" |
| 24 | + LESS_THAN = "LessThan" |
| 25 | + LESS_THAN_OR_EQUAL = "LessThanOrEqualTo" |
| 26 | + CONTAINS = "Contains" |
| 27 | + EXISTS = "Exists" |
| 28 | + NOT_EXISTS = "NotExists" |
| 29 | + |
| 30 | + |
| 31 | +@unique |
| 32 | +class BooleanOperator(Enum): |
| 33 | + AND = "And" |
| 34 | + OR = "Or" |
| 35 | + |
| 36 | + |
| 37 | +class SearchObject(ApiObject): |
| 38 | + def to_boto(self): |
| 39 | + return ApiObject.to_boto(self) |
| 40 | + |
| 41 | + |
| 42 | +class Filter(SearchObject): |
| 43 | + """ |
| 44 | + A Python class represent a Search Filter object. |
| 45 | + """ |
| 46 | + |
| 47 | + name = None |
| 48 | + operator = None |
| 49 | + value = None |
| 50 | + |
| 51 | + def __init__(self, name, operator=None, value=None): |
| 52 | + """Construct a Filter object |
| 53 | +
|
| 54 | + Args: |
| 55 | + name (str): filter field name |
| 56 | + operator (dict): one of Operator enum |
| 57 | + value (str): value of the field |
| 58 | + """ |
| 59 | + self.name = name |
| 60 | + self.operator = None if operator is None else operator.value |
| 61 | + self.value = value |
| 62 | + |
| 63 | + |
| 64 | +class NestedFilter(SearchObject): |
| 65 | + """ |
| 66 | + A Python class represent a Nested Filter object. |
| 67 | + """ |
| 68 | + |
| 69 | + nested_property_name = None |
| 70 | + filters = None |
| 71 | + |
| 72 | + def __init__(self, property_name, filters): |
| 73 | + """Construct a Nested Filter object |
| 74 | +
|
| 75 | + Args: |
| 76 | + property_name (str): nested property name |
| 77 | + filters (list): list of Filter objects |
| 78 | + """ |
| 79 | + self.nested_property_name = property_name |
| 80 | + self.filters = list(map(lambda x: x.to_boto(), filters)) |
| 81 | + |
| 82 | + |
| 83 | +class SearchExpression(SearchObject): |
| 84 | + """ |
| 85 | + A Python class representation of a Search Expression object. A sample search expression defined in here: |
| 86 | + https://boto3.amazonaws.com/v1/documentation/api/1.12.8/reference/services/sagemaker.html#SageMaker.Client.search |
| 87 | + """ |
| 88 | + |
| 89 | + filters = None |
| 90 | + nested_filters = None |
| 91 | + operator = None |
| 92 | + sub_expressions = None |
| 93 | + |
| 94 | + def __init__(self, filters=None, nested_filters=None, sub_expressions=None, boolean_operator=BooleanOperator.AND): |
| 95 | + """Construct a Search Expression object |
| 96 | +
|
| 97 | + Args: |
| 98 | + filters (list): list of Filter objects |
| 99 | + nested_filters (list): list of Nested Filters objects |
| 100 | + sub_expressions (list): list of Search Expresssion objects |
| 101 | + operator (dict): one of the boolean operator enums |
| 102 | + """ |
| 103 | + if filters is None and nested_filters is None and sub_expressions is None: |
| 104 | + raise ValueError("You must specify at least one subexpression, filter, or nested filter") |
| 105 | + self.filters = None if filters is None else list(map(lambda x: x.to_boto(), filters)) |
| 106 | + self.nested_filters = None if nested_filters is None else list(map(lambda x: x.to_boto(), nested_filters)) |
| 107 | + self.sub_expressions = None if sub_expressions is None else list(map(lambda x: x.to_boto(), sub_expressions)) |
| 108 | + self.operator = boolean_operator.value |
0 commit comments