mgqpy 0.1__tar.gz

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-0.1/.gitignore ADDED
@@ -0,0 +1,162 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[cod]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py,cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ #Pipfile.lock
96
+
97
+ # poetry
98
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102
+ #poetry.lock
103
+
104
+ # pdm
105
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
106
+ #pdm.lock
107
+ # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
108
+ # in version control.
109
+ # https://pdm.fming.dev/latest/usage/project/#working-with-version-control
110
+ .pdm.toml
111
+ .pdm-python
112
+ .pdm-build/
113
+
114
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
115
+ __pypackages__/
116
+
117
+ # Celery stuff
118
+ celerybeat-schedule
119
+ celerybeat.pid
120
+
121
+ # SageMath parsed files
122
+ *.sage.py
123
+
124
+ # Environments
125
+ .env
126
+ .venv
127
+ env/
128
+ venv/
129
+ ENV/
130
+ env.bak/
131
+ venv.bak/
132
+
133
+ # Spyder project settings
134
+ .spyderproject
135
+ .spyproject
136
+
137
+ # Rope project settings
138
+ .ropeproject
139
+
140
+ # mkdocs documentation
141
+ /site
142
+
143
+ # mypy
144
+ .mypy_cache/
145
+ .dmypy.json
146
+ dmypy.json
147
+
148
+ # Pyre type checker
149
+ .pyre/
150
+
151
+ # pytype static type analyzer
152
+ .pytype/
153
+
154
+ # Cython debug symbols
155
+ cython_debug/
156
+
157
+ # PyCharm
158
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
159
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
160
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
161
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
162
+ #.idea/
mgqpy-0.1/LICENSE ADDED
@@ -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.
mgqpy-0.1/PKG-INFO ADDED
@@ -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
mgqpy-0.1/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # mgqpy
2
+
3
+ mongo query as a python filter predicate
4
+
5
+ ```python
6
+ from mgqpy import Query
7
+
8
+ predicate = Query({"foo.bar": 1})
9
+
10
+ inputs = [
11
+ {"foo": [{"bar": [1]}]},
12
+ {"foo": {"bar": 1}},
13
+ {"foo": None},
14
+ ]
15
+
16
+ filtered = filter(predicate.match, inputs)
17
+
18
+ assert list(filtered) == [
19
+ {"foo": [{"bar": [1]}]},
20
+ {"foo": {"bar": 1}},
21
+ ]
22
+ ```
23
+
24
+ ## Supported operators
25
+
26
+ Comparison query operators
27
+
28
+ - [x] \$eq
29
+ - [x] \$eq (implicit), e.g. `{"foo": None}`
30
+ - [ ] \$ne
31
+ - [ ] \$gt
32
+ - [ ] \$gte
33
+ - [ ] \$lt
34
+ - [ ] \$lte
35
+ - [ ] \$in
36
+ - [ ] \$nin
37
+
38
+ Logical query operators
39
+
40
+ - [ ] \$and
41
+ - [x] \$and (implicit), e.g. `{"foo": 1, "bar": "baz"}`
42
+ - [ ] \$or
43
+ - [ ] \$not
44
+ - [ ] \$nor
45
+
46
+ Evaluation query operators
47
+
48
+ - [ ] \$regex
49
+ - [ ] \$mod
50
+
51
+ Array query operators
52
+
53
+ - [ ] \$all
54
+ - [ ] \$elemMatch
55
+ - [ ] \$size
@@ -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,17 @@
1
+ [build-system]
2
+ requires = ["flit_core >=3.2,<4"]
3
+ build-backend = "flit_core.buildapi"
4
+
5
+ [project]
6
+ name = "mgqpy"
7
+ authors = [{name = "Chiawei Ong", email = "ongchiawei@gmail.com"}]
8
+ license = {file = "LICENSE"}
9
+ classifiers = ["License :: OSI Approved :: MIT License"]
10
+ dynamic = ["version", "description"]
11
+
12
+ [project.urls]
13
+ Home = "https://github.com/weiliddat/mgqpy"
14
+
15
+ [tool.pytest.ini_options]
16
+ pythonpath = ["."]
17
+ testpaths = ["tests"]
@@ -0,0 +1,4 @@
1
+ iniconfig==2.0.0
2
+ packaging==24.1
3
+ pluggy==1.5.0
4
+ pytest==8.2.2
@@ -0,0 +1,25 @@
1
+ import pytest
2
+ from mgqpy import Query
3
+
4
+
5
+ testcases = [
6
+ (
7
+ "implicit $and",
8
+ {"foo": "bar", "baz": 2},
9
+ [
10
+ {"foo": "bar", "baz": 2},
11
+ {},
12
+ {"foo": "bar"},
13
+ {"baz": 2},
14
+ {"foo": {"foo": "bar"}},
15
+ ],
16
+ [{"foo": "bar", "baz": 2}],
17
+ ),
18
+ ]
19
+
20
+
21
+ @pytest.mark.parametrize("name,query,input,expected", testcases)
22
+ def test_mgqpy_and(name, query, input, expected):
23
+ q = Query(query)
24
+ actual = filter(q.match, input)
25
+ assert list(actual) == expected, name
@@ -0,0 +1,202 @@
1
+ import pytest
2
+ from mgqpy import Query
3
+
4
+
5
+ testcases = [
6
+ (
7
+ "explicit $eq",
8
+ {"foo": {"$eq": "bar"}},
9
+ [{"foo": "bar"}, {}, {"foo": "baz"}, {"foo": {"foo": "bar"}}],
10
+ [{"foo": "bar"}],
11
+ ),
12
+ (
13
+ "implicit $eq",
14
+ {"foo": "bar"},
15
+ [{"foo": "bar"}, {}, {"foo": "baz"}, {"foo": {"foo": "bar"}}],
16
+ [{"foo": "bar"}],
17
+ ),
18
+ (
19
+ "implicit $eq, full object match",
20
+ {"foo": {"bar": 1, " $size": 2}},
21
+ [
22
+ {"foo": "bar"},
23
+ {},
24
+ {"foo": [{"bar": 1}, {"bar": 2}]},
25
+ {"foo": {"bar": 1, " $size": 2}},
26
+ ],
27
+ [{"foo": {"bar": 1, " $size": 2}}],
28
+ ),
29
+ (
30
+ "explicit $eq, full object match",
31
+ {"foo": {"$eq": {"bar": 1, " $size": 2}}},
32
+ [
33
+ {"foo": "bar"},
34
+ {},
35
+ {"foo": [{"bar": 1}, {"bar": 2}]},
36
+ {"foo": {"bar": 1, " $size": 2}},
37
+ ],
38
+ [{"foo": {"bar": 1, " $size": 2}}],
39
+ ),
40
+ (
41
+ "nested object path, explicit $eq",
42
+ {"foo.bar": {"$eq": "baz"}},
43
+ [
44
+ {"foo": {"bar": "baz"}},
45
+ {},
46
+ {"foo": "bar"},
47
+ {"foo": {"bar": "qux"}},
48
+ ],
49
+ [{"foo": {"bar": "baz"}}],
50
+ ),
51
+ (
52
+ "nested object path, implicit $eq",
53
+ {"foo.bar": "baz"},
54
+ [
55
+ {"foo": {"bar": "baz"}},
56
+ {},
57
+ {"foo": "bar"},
58
+ {"foo": {"bar": "qux"}},
59
+ ],
60
+ [{"foo": {"bar": "baz"}}],
61
+ ),
62
+ (
63
+ "nested object path, full object match",
64
+ {"foo.bar": {"baz": "qux", "$eq": "bar"}},
65
+ [
66
+ {"foo": {"bar": {"baz": "qux", "$eq": "bar"}}},
67
+ {"foo": {"bar": {"baz": "qux", "bla": "jaz"}}},
68
+ {},
69
+ {"foo": "bar"},
70
+ {"foo": {"bar": "baz"}},
71
+ ],
72
+ [{"foo": {"bar": {"baz": "qux", "$eq": "bar"}}}],
73
+ ),
74
+ (
75
+ "nested object path, full object match",
76
+ {"foo.bar": {"baz": "qux"}},
77
+ [
78
+ {"foo": {"bar": {"baz": "qux"}}},
79
+ {"foo": {"bar": {"baz": "qux", "bla": "jaz"}}},
80
+ {},
81
+ {"foo": "bar"},
82
+ {"foo": {"bar": "baz"}},
83
+ ],
84
+ [{"foo": {"bar": {"baz": "qux"}}}],
85
+ ),
86
+ (
87
+ "implicit $eq, object against null",
88
+ {"foo.bar": None},
89
+ [
90
+ {"foo": {"bar": None}},
91
+ {"foo": {"bar": "baz"}},
92
+ {"foo": None},
93
+ {"foo": "bar"},
94
+ {},
95
+ ],
96
+ [{"foo": {"bar": None}}, {"foo": None}, {"foo": "bar"}, {}],
97
+ ),
98
+ (
99
+ "explicit $eq, object against null",
100
+ {"foo.bar": {"$eq": None}},
101
+ [
102
+ {"foo": {"bar": None}},
103
+ {"foo": {"bar": "baz"}},
104
+ {"foo": None},
105
+ {"foo": "bar"},
106
+ {},
107
+ ],
108
+ [{"foo": {"bar": None}}, {"foo": None}, {"foo": "bar"}, {}],
109
+ ),
110
+ (
111
+ "match against arrays on ov",
112
+ {"foo.bar": ["baz"]},
113
+ [
114
+ {"foo": {"bar": "baz"}},
115
+ {"foo": {"bar": ["baz"]}},
116
+ {"foo": {"bar": [["baz"]]}},
117
+ {"foo": {"bar": ["baz", ["baz"]]}},
118
+ {"foo": {"bar": ["baz", "bar"]}},
119
+ {},
120
+ {"foo": "bar"},
121
+ {"foo": [{"bar": "qux"}]},
122
+ ],
123
+ [
124
+ {"foo": {"bar": ["baz"]}},
125
+ {"foo": {"bar": [["baz"]]}},
126
+ {"foo": {"bar": ["baz", ["baz"]]}},
127
+ ],
128
+ ),
129
+ (
130
+ "match against arrays on doc",
131
+ {"foo.bar": "baz"},
132
+ [
133
+ {"foo": {"bar": ["bar"]}},
134
+ {"foo": {"bar": ["baz", "bar"]}},
135
+ {},
136
+ {"foo": "bar"},
137
+ {"foo": [{"bar": "qux"}]},
138
+ ],
139
+ [{"foo": {"bar": ["baz", "bar"]}}],
140
+ ),
141
+ (
142
+ "unindexed nested object path with intermediate arrays on doc",
143
+ {"a.b.c.d": 1},
144
+ [
145
+ {"a": {"b": {"c": [{"d": [1]}]}}},
146
+ {"a": [{"b": [{"c": [{"d": 1}]}]}]},
147
+ {"a": [{"b": {"c": [{"d": 1}]}}]},
148
+ {"a": {"b": {"c": [None, {"d": 1}]}}},
149
+ {"a": [{"b": [{"c": [{"d": 2}]}]}]},
150
+ {"a": {}},
151
+ {},
152
+ ],
153
+ [
154
+ {"a": {"b": {"c": [{"d": [1]}]}}},
155
+ {"a": [{"b": [{"c": [{"d": 1}]}]}]},
156
+ {"a": [{"b": {"c": [{"d": 1}]}}]},
157
+ {"a": {"b": {"c": [None, {"d": 1}]}}},
158
+ ],
159
+ ),
160
+ (
161
+ "unindexed nested object path against null",
162
+ {"foo.bar": None},
163
+ [
164
+ {"foo": [{"bar": "baz"}]},
165
+ {},
166
+ {"foo": "bar"},
167
+ {"foo": {"bar": None}},
168
+ {"foo": [{"bar": "qux"}]},
169
+ ],
170
+ [{}, {"foo": "bar"}, {"foo": {"bar": None}}],
171
+ ),
172
+ (
173
+ "indexed nested object path with intermediate arrays on doc",
174
+ {"foo.1.bar": "baz"},
175
+ [
176
+ {"foo": [{}, {"bar": "baz"}]},
177
+ {"foo": [{"bar": "baz"}, {}]},
178
+ {},
179
+ {"foo": "bar"},
180
+ {"foo": [{"bar": "qux"}]},
181
+ ],
182
+ [{"foo": [{}, {"bar": "baz"}]}],
183
+ ),
184
+ (
185
+ "nested arrays on doc",
186
+ {"foo.bar.baz": "qux"},
187
+ [
188
+ {"foo": [{"bar": [{"baz": "qux"}]}]},
189
+ {},
190
+ {"foo": "bar"},
191
+ {"foo": [{"bar": "baz"}]},
192
+ ],
193
+ [{"foo": [{"bar": [{"baz": "qux"}]}]}],
194
+ ),
195
+ ]
196
+
197
+
198
+ @pytest.mark.parametrize("name,query,input,expected", testcases)
199
+ def test_mgqpy_eq(name, query, input, expected):
200
+ q = Query(query)
201
+ actual = filter(q.match, input)
202
+ assert list(actual) == expected, name