Code Monkey home page Code Monkey logo

matchlib's Introduction

matchlib

PyPI version

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': '[email protected]',
    'posts': [
        {
            'id': 1,
            'text': 'some text'
        },
        {
            'id': 2,
            'text': 'lorem ipsum',
            'comments': [42, 142, 242]
        }
    ]
}

assert matches(
    user,
    {
        'id': ...,
        'name': 'John Doe',
        'email': '[email protected]',
        ...: ...
    }
)

Same can be achieved using standard == operator with matchlib.Partial object:

from matchlib import Partial

assert user == Partial({
    'id': 42,
    'email': '[email protected]',
    ...: ...
})

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': '[email protected]',
    }
}

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.

matchlib's People

Contributors

qweeze avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

matchlib's Issues

Partial which replaces duplicate value with ... fails to match

I think I've found a bug. When I have a value with duplicates and I replace one with ..., the match fails.

Example code:

In [1]: from matchlib import Partial

In [2]: [1, 1, 2] == [1, 1, 2]
Out[2]: True

In [3]: [1, 1, 2] == Partial([1, 1, 2])
Out[3]: True

In [4]: [1, 1, 2] == Partial([..., 1, 2])
Out[4]: False

Add types/py.typed

Currently if you use mypy with matchlib you get error: Skipping analyzing "matchlib": module is installed, but missing library stubs or py.typed marker [import-untyped]

Add assert_matches

Is it possible to add comparison function like matches but where objects (on every level) will be compared in the assert statements. It will be helpful for testing, especially for comparing large structures.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.