Pydiction is a Python library for deep comparisons and assertions on complex data structures. It provides a flexible and customizable way to compare dictionaries and lists while supporting various comparison scenarios, including nested comparisons and negative tests.
These instructions will help you get started with using Pydiction for comparing data structures in your Python projects.
Pydiction has the following prerequisites:
- Python 3.x
You can install Pydiction using pip
:
pip install pydiction
Here's a basic example of how to use Pydiction for deep comparisons:
from pydiction import Matcher, Contains
# Create a Matcher instance
matcher = Matcher()
# Define your actual and expected data structures
actual = {"a": 1, "b": 2}
expected = Contains({"a": 1})
# Perform the comparison and handle errors
try:
matcher.assert_declarative_object(actual, expected)
except AssertionError as e:
print(f"AssertionError: {e}")
from pydiction import ANY_NOT_NONE, Matcher, ANY, Contains, DoesntContains, Expect, ExpectNot
matcher = Matcher()
actual = {
"name": "John",
"email": "[email protected]",
"age": 25,
"friends": [
{
"name": "Alice",
"email": "[email protected]",
"age": 21,
}
],
"comments": [{ "text": "Great post!"}],
"likes": [
{
"title": "First Post",
"content": "This is my first post!",
},
{ "text": "Great post!"},
],
}
expected = {
"name": "John",
"age": Expect(10).__gt__,
"comments": DoesntContains([{"text": "not existing post!"}]),
"email": ExpectNot("gmail.com").__contains__,
"friends": [
{
"age": ANY_NOT_NONE,
"email": ANY_NOT_NONE,
"name": "Alice",
}
],
"likes": Contains(
[
{
"content": "This is my first post!",
"title": "First Post",
},
]
),
}
matcher.assert_declarative_object(actual, expected)
If you'd like to contribute to Pydiction or report issues, please follow these guidelines:
- Fork the repository on GitHub.
- Clone your forked repository to your local machine.
- Create a new branch for your feature or bug fix.
- Make your changes and commit them with clear and concise commit messages.
- Push your changes to your forked repository.
- Create a pull request against the main repository.