matchlib
This package provides a handy way to partially compare python data structures (typically nested lists/dictionaries).
Installation
pip install matchlib
Usage
from matchlib import matches
user = {
'id': 42,
'name': 'John Doe',
'email': 'johndoe@gmail.com',
'posts': [
{
'id': 1,
'text': 'some text'
},
{
'id': 2,
'text': 'lorem ipsum',
'comments': [42, 142, 242]
}
]
}
assert matches(
user,
{
'id': ...,
'name': 'John Doe',
'email': 'johndoe@gmail.com',
...: ...
}
)
Same can be achieved using standard == operator with matchlib.Partial object:
from matchlib import Partial
assert user == Partial({
'id': 42,
'email': 'johndoe@gmail.com',
...: ...
})
The ... "wildcard" could be placed at any nested level.
Let's say we only need to check that comment 142 is present in specific post:
assert user == Partial({
'posts': [
...,
{
'id': 2,
'comments': [..., 142, ...],
...: ...
}
],
...: ...
})
Matching rules are simple:
- In lists and tuples
...matches zero or more elements and order is preserved:Partial([1, 2, ...]) == [1, 2, 3, 4] Partial([1, 2, ...]) == [1, 2] Partial([1, 2, ...]) != [0, 1, 2] Partial([1, 2, ...]) != [2, 1]
- Same for the sets except they are unordered:
Partial({1, 2, ...}) == {1, 2} Partial({1, 2, ...}) == {0, 1, 2, 3} Partial({1, 2, ...}) != {0, 1, 3}
- As dict value
...matches any object:Partial({'a': 1, 'b': ...}) == {'a': 1, 'b': 2}
- As dict key
...matches any key if assosiated values match:Partial({'a': 1, ...: 2}) == {'a': 2, 'b': 2}
- When passed as both key and value matches zero or more arbitrary key-value pairs:
Partial({'a': 1, ...: ...}) == {'a': 1, 'b': 2, 'c': 3}
Some more hacks
mathchlib provides a Regex object that allows to match an arbitrary string element
(except if it is a dict key) against a regular expression.
Also pytest.approx is supported for floating-point numbers comparison:
from pytest import approx
from matchlib import Regex, Partial
account = {
'id': 1,
'balance': 1007.62,
'owner': {
'email': 'user42@domain.com',
}
}
assert account == Partial({
'id': ...,
'balance': approx(1000, 0.1),
'owner': {
'email': Regex(r'\w+@domain\.com')
}
})
If for any reason you dislike Ellipsis literal (...)
a matchlib.Any object can be used interchangeably.
Metadata
Release files for matchlib 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| matchlib-0.2.3.tar.gz | 5.1 kB | Without provenance | Details for matchlib-0.2.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| matchlib-0.2.3-py3-none-any.whl | Python 3 | none | any | Details for matchlib-0.2.3-py3-none-any.whl |
Total release size: 9.4 kB
Release files / matchlib-0.2.3.tar.gz
| Download URL | matchlib-0.2.3.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9c887c30a777ff84ba15a861acec142cd192d681aaa8cb882911e24c2035e83a
|
|
BLAKE2b-256 checksum How to use checksums |
5bc0930702de06410b682e5b0dc0458fdd2a7da0229e9b1c0be5008f80fc4562
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.7
|
Release files / matchlib-0.2.3-py3-none-any.whl
| Download URL | matchlib-0.2.3-py3-none-any.whl |
|---|---|
| Size | 4.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
27f829d601b0a7f51b88601e2282052700d10210d200334c2df0084cc14553ba
|
|
BLAKE2b-256 checksum How to use checksums |
a4c87810ebc2c7fb611830dc9ce5b42aa5b0f6a6cf367407539822e513291b95
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.7
|
Security for matchlib 0.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.