Skip to main content

A simple Python base class for immutable objects whose value is determined by a state tuple.

This project has been archived.

The maintainers of this project have marked this project as archived. No new releases are expected.

Project description

state-tuple-value-object

A simple Python base class for immutable objects whose value is determined by a state tuple.

Features

  • Equality (==) via __eq__
  • Hashing via __hash__ for use in sets and as dictionary keys
  • Pickle support via __reduce__
  • Readable representation via __repr__ for easier debugging
  • Compatible with Python 2+
  • Less dynamic features than attrs or dataclasses

Usage

  1. Inherit from StateTupleValueObject
  2. Implement get_state_tuple(self) to return the state as a tuple
  3. Benefit from automated equality, hashing, pickling, and representation
from state_tuple_value_object import StateTupleValueObject


class Point(StateTupleValueObject):
    def __init__(self, x, y):
        self.x = x
        self.y = y

    def get_state_tuple(self):
        return (self.x, self.y)


p1 = Point(1, 2)
p2 = Point(1, 2)
p3 = Point(3, 4)

print(p1 == p2)  # True
print(hash(p1) == hash(p2))  # True
print(p1)        # Point(1, 2)
print(p1 == p3)  # False

my_set = {p1, p2, p3}
print(my_set)  # {Point(1, 2), Point(3, 4)}

Contributing

Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.

License

This project is licensed under the MIT License.

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

state_tuple_value_object-0.1.0a0.tar.gz (2.1 kB view details)

Uploaded Source

Built Distribution

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

state_tuple_value_object-0.1.0a0-py2.py3-none-any.whl (2.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file state_tuple_value_object-0.1.0a0.tar.gz.

File metadata

File hashes

Hashes for state_tuple_value_object-0.1.0a0.tar.gz
Algorithm Hash digest
SHA256 46e4f8489a283c4874de97805680e272f6e9e2e42b223fff08664d4414550acb
MD5 674e64ebf17f874356d23efee8fdf660
BLAKE2b-256 581098ef33a65dae8832e20e1d9bd82fae16a36f2d09a8442d25bf4bc95e7d34

See more details on using hashes here.

File details

Details for the file state_tuple_value_object-0.1.0a0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for state_tuple_value_object-0.1.0a0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c68e9ffcf1c17b67578d704fda92b0881fc25ac7cd287dd59b01af0c6cd4f948
MD5 78dfbe62b6dff5e45c981186c766b528
BLAKE2b-256 5d82a6c4d9fd8ade4a32a9b7c80e55fbcf1dea28c50244408541e6acdd97777a

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