File: evaluation.py

package info (click to toggle)
python-beanie 2.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,484 kB
  • sloc: python: 14,427; makefile: 6; sh: 6
file content (199 lines) | stat: -rw-r--r-- 4,399 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
from abc import ABC
from typing import Optional

from beanie.odm.operators.find import BaseFindOperator


class BaseFindEvaluationOperator(BaseFindOperator, ABC): ...


class Expr(BaseFindEvaluationOperator):
    """
    `$type` query operator

    Example:

    ```python
    class Sample(Document):
        one: int
        two: int

    Expr({"$gt": [ "$one" , "$two" ]})
    ```

    Will return query object like

    ```python
    {"$expr": {"$gt": [ "$one" , "$two" ]}}
    ```

    MongoDB doc:
    <https://docs.mongodb.com/manual/reference/operator/query/expr/>
    """

    def __init__(self, expression: dict):
        self.expression = expression

    @property
    def query(self):
        return {"$expr": self.expression}


class JsonSchema(BaseFindEvaluationOperator):
    """
    `$jsonSchema` query operator

    MongoDB doc:
    <https://docs.mongodb.com/manual/reference/operator/query/jsonSchema/>
    """

    def __init__(self, expression: dict):
        self.expression = expression

    @property
    def query(self):
        return {"$jsonSchema": self.expression}


class Mod(BaseFindEvaluationOperator):
    """
    `$mod` query operator

    Example:

    ```python
    class Sample(Document):
        one: int

    Mod(Sample.one, 4, 0)
    ```

    Will return query object like

    ```python
    { "one": { "$mod": [ 4, 0 ] } }
    ```

    MongoDB doc:
    <https://docs.mongodb.com/manual/reference/operator/query/mod/>
    """

    def __init__(self, field, divisor: int, remainder: int):
        self.field = field
        self.divisor = divisor
        self.remainder = remainder

    @property
    def query(self):
        return {self.field: {"$mod": [self.divisor, self.remainder]}}


class RegEx(BaseFindEvaluationOperator):
    """
    `$regex` query operator

    MongoDB doc:
    <https://docs.mongodb.com/manual/reference/operator/query/regex/>
    """

    def __init__(
        self,
        field,
        pattern: str,
        options: Optional[str] = None,
    ):
        self.field = field
        self.pattern = pattern
        self.options = options

    @property
    def query(self):
        expression = {"$regex": self.pattern}
        if self.options:
            expression["$options"] = self.options
        return {self.field: expression}


class Text(BaseFindEvaluationOperator):
    """
    `$text` query operator

    Example:

    ```python
    class Sample(Document):
        description: Indexed(str, pymongo.TEXT)

    Text("coffee")
    ```

    Will return query object like

    ```python
    {
        "$text": {
            "$search": "coffee" ,
            "$caseSensitive": False,
            "$diacriticSensitive": False
        }
    }
    ```

    MongoDB doc:
    <https://docs.mongodb.com/manual/reference/operator/query/text/>

    Note: if you need to run a query against Azure Cosmos DB for MongoDB,
    which does not support diacritic sensitivity yet, you can set
    `diacritic_sensitive` argument to `None` to exclude it from the query.
    """

    def __init__(
        self,
        search: str,
        language: Optional[str] = None,
        case_sensitive: bool = False,
        diacritic_sensitive: Optional[bool] = False,
    ):
        """

        :param search: str
        :param language: Optional[str] = None
        :param case_sensitive: bool = False
        :param diacritic_sensitive: Optional[bool] = False
        """
        self.search = search
        self.language = language
        self.case_sensitive = case_sensitive
        self.diacritic_sensitive = diacritic_sensitive

    @property
    def query(self):
        expression = {
            "$text": {
                "$search": self.search,
                "$caseSensitive": self.case_sensitive,
            }
        }
        if self.language:
            expression["$text"]["$language"] = self.language
        if self.diacritic_sensitive is not None:
            expression["$text"]["$diacriticSensitive"] = (
                self.diacritic_sensitive
            )
        return expression


class Where(BaseFindEvaluationOperator):
    """
    `$where` query operator

    MongoDB doc:
    <https://docs.mongodb.com/manual/reference/operator/query/where/>
    """

    def __init__(self, expression: str):
        self.expression = expression

    @property
    def query(self):
        return {"$where": self.expression}