{"id":19268553,"url":"https://github.com/qweeze/matchlib","last_synced_at":"2025-04-21T20:31:54.548Z","repository":{"id":57439942,"uuid":"171132307","full_name":"qweeze/matchlib","owner":"qweeze","description":"A tool for partial comparison of (nested) data structures","archived":false,"fork":false,"pushed_at":"2024-04-09T22:20:58.000Z","size":10,"stargazers_count":18,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-01T15:50:12.733Z","etag":null,"topics":["fuzzy-data-matching","partial-comparison","python-tests"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/qweeze.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2019-02-17T14:35:06.000Z","updated_at":"2025-02-06T06:37:22.000Z","dependencies_parsed_at":"2024-04-09T23:33:29.221Z","dependency_job_id":"06a6ea8f-8e1b-4ae8-9b1b-59084156734a","html_url":"https://github.com/qweeze/matchlib","commit_stats":{"total_commits":6,"total_committers":1,"mean_commits":6.0,"dds":0.0,"last_synced_commit":"1352dac0eff654fa6f219d53c76bc03213424d88"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qweeze%2Fmatchlib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qweeze%2Fmatchlib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qweeze%2Fmatchlib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/qweeze%2Fmatchlib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/qweeze","download_url":"https://codeload.github.com/qweeze/matchlib/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647252,"owners_count":21139081,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fuzzy-data-matching","partial-comparison","python-tests"],"created_at":"2024-11-09T20:16:39.570Z","updated_at":"2025-04-21T20:31:54.279Z","avatar_url":"https://github.com/qweeze.png","language":"Python","readme":"## matchlib\n\n[![PyPI version](https://badge.fury.io/py/matchlib.svg)](https://badge.fury.io/py/matchlib)\n\nThis package provides a handy way to partially compare python data structures \n(typically nested lists/dictionaries).\n\n### Installation\n```bash\npip install matchlib\n```\n### Usage\n```python\nfrom matchlib import matches\n\nuser = {\n    'id': 42,\n    'name': 'John Doe',\n    'email': 'johndoe@gmail.com',\n    'posts': [\n        {\n            'id': 1,\n            'text': 'some text'\n        },\n        {\n            'id': 2,\n            'text': 'lorem ipsum',\n            'comments': [42, 142, 242]\n        }\n    ]\n}\n\nassert matches(\n    user,\n    {\n        'id': ...,\n        'name': 'John Doe',\n        'email': 'johndoe@gmail.com',\n        ...: ...\n    }\n)\n```\nSame can be achieved using standard `==` operator with `matchlib.Partial` object:\n```python\nfrom matchlib import Partial\n\nassert user == Partial({\n    'id': 42,\n    'email': 'johndoe@gmail.com',\n    ...: ...\n})\n```\nThe `...` \"wildcard\" could be placed at any nested level. \nLet's say we only need to check that comment `142` is present in specific post: \n```python \nassert user == Partial({\n    'posts': [\n        ...,\n        {\n            'id': 2,\n            'comments': [..., 142, ...],\n            ...: ...\n        }\n    ],\n    ...: ...\n})\n``` \nMatching rules are simple:\n - In __lists__ and __tuples__ `...` matches zero or more elements and order is preserved:\n    ```python\n    Partial([1, 2, ...]) == [1, 2, 3, 4]\n    Partial([1, 2, ...]) == [1, 2]\n    \n    Partial([1, 2, ...]) != [0, 1, 2]\n    Partial([1, 2, ...]) != [2, 1]\n    ```\n - Same for the __sets__ except they are unordered:\n    ```python\n    Partial({1, 2, ...}) == {1, 2}\n    Partial({1, 2, ...}) == {0, 1, 2, 3}\n \n    Partial({1, 2, ...}) != {0, 1, 3}\n    ```\n - As __dict value__ `...` matches any object:\n    ```python\n    Partial({'a': 1, 'b': ...}) == {'a': 1, 'b': 2}\n    ```\n - As __dict key__ `...` matches any key if assosiated values match:\n    ```python\n    Partial({'a': 1, ...: 2}) == {'a': 2, 'b': 2}\n    ``` \n - When passed as __both key and value__ matches zero or more arbitrary key-value pairs:\n    ```python\n    Partial({'a': 1, ...: ...}) == {'a': 1, 'b': 2, 'c': 3}\n    ```\n\n### Some more hacks\n`mathchlib` provides a `Regex` object that allows to match an arbitrary string element \n(except if it is a dict key) against a regular expression.\nAlso `pytest.approx` is supported for floating-point numbers comparison:\n```python\nfrom pytest import approx\nfrom matchlib import Regex, Partial\n\naccount = {\n    'id': 1,\n    'balance': 1007.62,\n    'owner': {\n        'email': 'user42@domain.com',\n    }\n}\n\nassert account == Partial({\n    'id': ...,\n    'balance': approx(1000, 0.1),\n    'owner': {\n        'email': Regex(r'\\w+@domain\\.com')\n    }\n})\n```\nIf for any reason you dislike Ellipsis literal (`...`) \na `matchlib.Any` object can be used interchangeably.","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqweeze%2Fmatchlib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fqweeze%2Fmatchlib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fqweeze%2Fmatchlib/lists"}