ya
Make filters with the mongo language
To install: pip install ya
Overview
The ya package provides a Python implementation for creating and applying filters based on a MongoDB-like query language. This allows users to filter dictionaries in Python similarly to how one might filter documents in a MongoDB collection using query operators like $gt, $lt, $eq, and more.
The package primarily revolves around constructing filter functions that can be applied to dictionaries to check if they match certain conditions. These conditions are specified in a way that closely resembles MongoDB's query syntax, making it intuitive for users familiar with MongoDB.
Main Features
- MongoDB-like Query Language: Use familiar MongoDB operators to specify conditions.
- Flexible Key Paths: Supports nested dictionaries using dot notation in key paths.
- Logical Operators: Includes support for
$and,$or, and$norlogical operations to combine multiple conditions.
Installation
To install the ya package, run the following command:
pip install ya
Usage Examples
Basic Filtering
from ya import dict_filt_from_mg_filt
# Define a filter
mg_filt = {'age': {'$gte': 18}}
filt = dict_filt_from_mg_filt(mg_filt)
# Apply the filter
print(filt({'name': 'Alice', 'age': 20})) # Output: True
print(filt({'name': 'Bob', 'age': 17})) # Output: False
Complex Filtering with Logical Operators
from ya import dict_filt_from_mg_filt
# Define a complex filter
mg_filt = {
'$and': [
{'age': {'$gte': 18}},
{'age': {'$lte': 30}}
]
}
filt = dict_filt_from_mg_filt(mg_filt)
# Apply the filter
print(filt({'name': 'Charlie', 'age': 25})) # Output: True
print(filt({'name': 'Dave', 'age': 31})) # Output: False
Nested Fields and Lists
from ya import dict_filt_from_mg_filt
# Define a filter that uses nested fields and lists
mg_filt = {
'details.education.degree': 'MSc',
'hobbies': {'$in': ['hiking', 'reading']}
}
filt = dict_filt_from_mg_filt(mg_filt)
# Apply the filter
person = {
'name': 'Eve',
'details': {
'education': {
'degree': 'MSc'
}
},
'hobbies': ['reading', 'painting']
}
print(filt(person)) # Output: True
Function Documentation
dict_filt_from_mg_filt(mg_filt)
Creates a filter function based on the specified MongoDB-like filter mg_filt. This function can be used to filter a list of dictionaries, returning True if a dictionary matches the filter conditions.
- Parameters:
mg_filt: A dictionary specifying the MongoDB-like query conditions.
- Returns:
- A function that takes a dictionary and returns
Trueif it matches the conditions specified inmg_filt.
- A function that takes a dictionary and returns
mg_filt_kv_to_func(key_path, val_condition)
Converts a key-value pair into a filter function. This function is used internally to handle individual conditions within a larger query.
- Parameters:
key_path: The path to the value in the dictionary (supports nested paths).val_condition: The condition that the value atkey_pathshould meet.
- Returns:
- A function that takes a dictionary and returns
Trueif the value atkey_pathmeetsval_condition.
- A function that takes a dictionary and returns
This package is ideal for applications requiring filtering of complex data structures in a way that is both expressive and familiar to those who use MongoDB.
Metadata
Release files for ya 0.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| ya-0.0.6.tar.gz | 8.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| ya-0.0.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 17.5 kB
Release files / ya-0.0.6.tar.gz
| Download URL | ya-0.0.6.tar.gz |
|---|---|
| Size | 8.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1e37cef0fbab00d676418cf0649e3e6e9804393fda82490126e1c3d672450240
|
|
BLAKE2b-256 checksum How to use checksums |
bf03a0ffcf2877df2c4fad148e7f1262cf3768a4a5725942f6eaf05f3c076f7f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / ya-0.0.6-py3-none-any.whl
| Download URL | ya-0.0.6-py3-none-any.whl |
|---|---|
| Size | 8.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
de6961211f5fe6f21d75671b38e271370405a03d76d82e2a3e6c2e846cb6a3d0
|
|
BLAKE2b-256 checksum How to use checksums |
2b2a1bbd8d64d73213fda4559d04756b6ef698ccc7a86a1a2ffc4a2d04155c91
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|