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 black and 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.4.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

classdiff-0.4.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: classdiff-0.4.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.7 Darwin/23.2.0

File hashes

Hashes for classdiff-0.4.0.tar.gz
Algorithm Hash digest
SHA256 c3af9da2d9e760a4ef436f61de0c04f663d408b6155a59f3f8df6fb037b05d11
MD5 b10f3e3b2a3b6fb204d2c7b3acf2a202
BLAKE2b-256 d37ae1ea1be22163aba8428513c668ccabfd32c9c57822c063573231394c4719

See more details on using hashes here.

File details

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

File metadata

  • Download URL: classdiff-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 8.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.6.1 CPython/3.11.7 Darwin/23.2.0

File hashes

Hashes for classdiff-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f240f1ebc4cc6f4d052983d6e4b59cd3cdb55cf7a83d1a025133712fc79bb65f
MD5 35270c6b11717aeb82090128d4bedcac
BLAKE2b-256 94e55032952cb265c88e97ee5df46bba5a594bc549d9b17c3d68802a0f9fce59

See more details on using hashes here.

Supported by

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