Make filters with the mongo language
Project description
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.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ya-0.0.5.tar.gz.
File metadata
- Download URL: ya-0.0.5.tar.gz
- Upload date:
- Size: 8.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c42bd4c587fce5fdc76145c211f653ebf2d3e7cdb9c18df44441fa5b4edda021
|
|
| MD5 |
fdd19da1a60db0ed99e378317c4b7288
|
|
| BLAKE2b-256 |
9e474b2e1fc026232aefa997ad9186e5fb2eb9fd8a2297ee4391458ab34c2a1f
|
File details
Details for the file ya-0.0.5-py3-none-any.whl.
File metadata
- Download URL: ya-0.0.5-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78b2fafa16dfb3941212059d2c87fbd81a1f7021e922a90ff40517443b3b21f5
|
|
| MD5 |
0c8a0913450e4801974798fcac95da2b
|
|
| BLAKE2b-256 |
82c434203f9bd8fbf966e1617a7acd3a0bebb46f5aa068854149e2d6b0d6e659
|