Skip to main content

Utility to diff classes

Project description

classdiff - Python Class Difffer

This is a small library to diff (data) classes or any other objects. It is intended two work one obejcts of the same time or NoneType. Diffing different classes is possible but might result in unexpected result. Different from most existing diff tools that produce data structures that show adds, removes or changes, this tool is intended to use for printing a dataclass and higlight the diff with different color coding, similar to the output of Terraform or Pulumi.

Usage

Just pass your classes to the diff function to get back a representation of the diff.

Given the following two classes:

new = SomeResource(
    name="my-data",
    price=2.3,
    quantity=4,
    dangerous=True,
    other=OtherClass(name="OA"),
    not_changed=OtherClass(name="Same"),
)

old = SomeResource(
    name="my-data",
    price=3.3,
    quantity=4,
    dangerous=False,
    other=OtherClass(name="OB"),
    not_changed=OtherClass(name="Same"),
)

Passing them to classdiff.diff in combinations of (None, new), (old, new) and (old, None) and printing the lines in the returned value, the following will be printed with colored output (green for added, red for removed and yellow for changed). Note that each element in the returned list is of type DiffInfo which implements __repr__ to print with proper prefix and color.

> diff(None, new)
----------------------------------------
+ SomeResource(
+   dangerous = True
+   name = my-data
+   not_changed = OtherClass(
+     name = Same
+   )
+   other = OtherClass(
+     name = OA
+   )
+   price = 2.3
+   quantity = 4
+ )

> diff(old, new)
----------------------------------------
~ SomeResource(
~   dangerous = False => True
    name = my-data
    not_changed = OtherClass(
      name = Same
    )
~   other = OtherClass(
~     name = OB => OA
~   )
~   price = 3.3 => 2.3
    quantity = 4
~ )

> diff(old, None)
----------------------------------------
- SomeResource(
-   dangerous = False
-   name = my-data
-   not_changed = OtherClass(
-     name = Same
-   )
-   other = OtherClass(
-     name = OB
-   )
-   price = 3.3
-   quantity = 4
- )

Development

All code is formatted and analyzed with ruff. Tests are run with pytest.

 poetry run ruff check .
› poetry run ruff format .
› poetry run mypy .
› poetry run pytest tests/

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

classdiff-0.5.2.tar.gz (7.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

classdiff-0.5.2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file classdiff-0.5.2.tar.gz.

File metadata

  • Download URL: classdiff-0.5.2.tar.gz
  • Upload date:
  • Size: 7.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.6 Darwin/25.0.0

File hashes

Hashes for classdiff-0.5.2.tar.gz
Algorithm Hash digest
SHA256 fc950879a4434aae06b010fe281d28e35c01b92e4ef6c4f23912b5e339830517
MD5 8b06a256f1ebb0ab3c4882eef3d0c03f
BLAKE2b-256 7fa8e6ce0755261f849ddadca3f158b2902a2d4d10adb60b1697605f0b211791

See more details on using hashes here.

File details

Details for the file classdiff-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: classdiff-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.11.6 Darwin/25.0.0

File hashes

Hashes for classdiff-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ffb115f2e9b25a2794fbff0e327514cf437f0ce0fd31cf551746ee0a2d39444c
MD5 e5093a5406bbbaafa6518180e281e789
BLAKE2b-256 38fca3496c33b955250a11c8f21415942cf4102b82e1c929d19c373803faf9e9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page