mgqpy 0.1__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
mgqpy/__init__.py ADDED
@@ -0,0 +1,90 @@
1
+ """mongo query as a python filter predicate"""
2
+
3
+ __version__ = "0.1"
4
+
5
+ # Terminology
6
+ #
7
+ # Query refers to the document that is passed to the compiler / mongodb find that holds
8
+ # the query conditions (can be multiple),
9
+ # e.g. { "fruits.type": { "$eq": "berry", "$ne": "aggregate" }, "fruits": { "$size": 3 }}
10
+ #
11
+ # Cond (conditions) refers to a single path-expression pair,
12
+ # e.g. { "fruits.type": { "$eq": "berry", "$ne": "aggregate" }
13
+ #
14
+ # Path refers to dot-separated fields,
15
+ # e.g. "fruits.type"
16
+ #
17
+ # Exp (expression) refers to the object that holds operator and value pairs (can be multiple),
18
+ # e.g. { "$eq": "berry", "$ne": "aggregate" }
19
+ #
20
+ # Op (operator) refers to the logical operator that is matched against the value,
21
+ # e.g. "$eq"
22
+ #
23
+ # Ov (operator value) refers to the value that you are matching against in
24
+ # the context of a single operator e.g. "berry"
25
+ #
26
+ # Doc refers to the object that is passed to the compiled filter function
27
+
28
+ from typing import List
29
+
30
+ cmp_ops = {
31
+ "$eq",
32
+ "$gt",
33
+ "$gte",
34
+ "$in",
35
+ "$lt",
36
+ "$lte",
37
+ "$ne",
38
+ "$nin",
39
+ }
40
+
41
+
42
+ class Query:
43
+ def __init__(self, query):
44
+ self._query = query
45
+ pass
46
+
47
+ def match(self, doc) -> bool:
48
+ results: List[bool] = []
49
+
50
+ for path in self._query:
51
+ exp_or_ov = self._query[path]
52
+ is_all_ops = (
53
+ exp_or_ov
54
+ and isinstance(exp_or_ov, dict)
55
+ and all(key in cmp_ops for key in exp_or_ov.keys())
56
+ )
57
+
58
+ path_parts = path.split(".")
59
+
60
+ if is_all_ops:
61
+ if "$eq" in exp_or_ov:
62
+ results.append(_match_eq(doc, path_parts, exp_or_ov["$eq"]))
63
+ else:
64
+ results.append(_match_eq(doc, path_parts, exp_or_ov))
65
+
66
+ return all(results)
67
+
68
+
69
+ def _match_eq(doc: dict, path: List[str], ov) -> bool:
70
+ if len(path) == 0:
71
+ if isinstance(doc, list) and any([_match_eq(d, path, ov) for d in doc]):
72
+ return True
73
+
74
+ return doc == ov
75
+
76
+ key = path[0]
77
+ rest = path[1:]
78
+
79
+ if isinstance(doc, dict) and key in doc:
80
+ return _match_eq(doc[key], rest, ov)
81
+
82
+ if isinstance(doc, list) and key.isdigit():
83
+ idx = int(key)
84
+ if idx < len(doc):
85
+ return _match_eq(doc[idx], rest, ov)
86
+
87
+ if isinstance(doc, list):
88
+ return any([_match_eq(d, path, ov) for d in doc])
89
+
90
+ return ov is None
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Chiawei Ong
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,7 @@
1
+ Metadata-Version: 2.1
2
+ Name: mgqpy
3
+ Version: 0.1
4
+ Summary: mongo query as a python filter predicate
5
+ Author-email: Chiawei Ong <ongchiawei@gmail.com>
6
+ Classifier: License :: OSI Approved :: MIT License
7
+ Project-URL: Home, https://github.com/weiliddat/mgqpy
@@ -0,0 +1,5 @@
1
+ mgqpy/__init__.py,sha256=cEALfA6yFzVkT6R4oVkH-Yxr77a9spqG9hTxh7blm7g,2401
2
+ mgqpy-0.1.dist-info/LICENSE,sha256=pEMbA1CSJ6STVH6OvDwoswM9--WxyerjT44X0-scvCU,1068
3
+ mgqpy-0.1.dist-info/WHEEL,sha256=Sgu64hAMa6g5FdzHxXv9Xdse9yxpGGMeagVtPMWpJQY,99
4
+ mgqpy-0.1.dist-info/METADATA,sha256=4ghhCFUHmOdT6GRDK-UST79XimkuFYyFYSRyGM0CjWA,251
5
+ mgqpy-0.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: flit 3.9.0
3
+ Root-Is-Purelib: true
4
+ Tag: py2-none-any
5
+ Tag: py3-none-any