8000 GitHub - dipanjal/smalldiff: A Python library that identifies differences between two objects
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

dipanjal/smalldiff

Repository files navigation

SmallDiff

SmallDiff is a Python library inspired by assertJ that provides a simple way to identify differences between two Python objects. It is designed to be easy to use and highly flexible, allowing you to compare objects of any type, including dictionaries, lists, and custom classes. SmallDiff uses a recursive algorithm to compare the values of the objects and return a map of the differences. With SmallDiff, you can quickly identify the changes between two objects and take appropriate action. Whether you're working on a small script or a large project, SmallDiff can help you write more robust and reliable code.

codecov

Installation

First, ensure that you have Python 3.x installed on your system. You can check your version of Python by running:

python --version

If you have multiple versions of Python installed, you can use pyenv to manage your versions. To install pyenv, follow the instructions in the official documentation.

Next, you'll need to install pipenv for virtual environment and dependency management. To install pipenv, run:

pip install pipenv

Once you have pipenv installed, you can use a make command to create a new virtual environment for this project and install the dependencies. But make sure you have make installed in your system

Mac/Linux

On Mac/Linux, you can install the project and its dependencies by running:

make install

Windows

On Windows, you'll need to install GNU Make and run it from the command prompt. You can download GNU Make from the official website. Once you have GNU Make installed, you can navigate to the project directory and run:

make install

This will create a new virtual environment and install the dependencies listed in the Pipfile.lock file.

Usage

To use this library, you can import the compare function from the smalldiff.SmallDiff module:

Example 1: Comparing two dictionaries with nested items

from smalldiff import SmallDiff

# the data we expect
expected = {
    "name": "John Doe",
    "age": 28,
    "address": {
        "street": "123 Main St.",
        "dist": "Dhaka",
        "zip": 1227
    }
}

# the actual data we get maybe after an API call
actual = {
    "name": "John Doe",
    "age": 28,
    "address": {
        "street": "123 Main St.",
        "dist": "Magura",
        "zip": 7600
    }
}
diff = SmallDiff.compare(expected, actual)
print(diff)

output

{
    "address.dist": {
        "expected": "Dhaka",
        "actual": "Magura",
    },
    "address.zip": {
        "expected": 1227,
        "actual": 7600,
    }
}

We can also use it to assert test cases. See an example bellow

person1 = PersonModel(
name="John Doe",
age=28,
gender=Gender.M,
address=AddressModel(
street="123 Main St.",
dist="Dhaka",
zip=1227
)
)
person2 = PersonModel(
name="John Doe",
age=28,
gender=Gender.M,
address=AddressModel(
street="123 Main St.",
dist="Dhaka",
zip=1227
)
)
assert SmallDiff.is_equal(person1, person2)

About

A Python library that identifies differences between two objects

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
0