diff --git a/RELEASE.md b/RELEASE.md index b73159f..1dd0e6b 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -2,6 +2,11 @@ All major changes in each released version of the archfx-cloud plugin are listed here. +## 0.17.0 + +- Made `ArchFxCloudSlug` (and all its derived classes) usable as a mapping key by overriding the `__hash__()` method. +- Made `ArchFxCloudSlug` (and all its derived classes) considered equal if their `slug` attributes are equal by overriding the `__eq__()` method. + ## 0.16.0 - Updated `dev` domain in `BaseMain` class to be `http://localhost`. diff --git a/archfx_cloud/utils/slugs.py b/archfx_cloud/utils/slugs.py index 7cad4fd..357b38f 100644 --- a/archfx_cloud/utils/slugs.py +++ b/archfx_cloud/utils/slugs.py @@ -36,7 +36,15 @@ class ArchFxCloudSlug(object): _slug = None def __str__(self): - return self._slug + return self._slug or '' + + def __hash__(self): + return hash(self._slug) + + def __eq__(self, other): + if isinstance(other, ArchFxCloudSlug): + return self._slug == other._slug + return super().__eq__(other) def formatted_id(self): parts = gid_split(self._slug) diff --git a/tests/test_slugs.py b/tests/test_slugs.py index 1bae9db..b2b26b2 100644 --- a/tests/test_slugs.py +++ b/tests/test_slugs.py @@ -33,6 +33,21 @@ def test_ArchFxVariableID(name, input, formatted, scope, var): assert result.scope == scope assert result.scope_hex == f"{scope:04x}" + other = ArchFxVariableID(input) + assert str(result) == str(other) + assert hash(result) == hash(other) + assert result == other + + other2 = ArchFxVariableID('9999-9999') + assert str(result) != str(other2) + assert hash(result) != hash(other2) + assert result != other2 + + mapping = {result: 'value'} + assert mapping[result] == 'value' + assert mapping[other] == 'value' + assert mapping.get(other2) is None + class SlugTestCase(unittest.TestCase): diff --git a/version.py b/version.py index 6e61e44..0631519 100644 --- a/version.py +++ b/version.py @@ -1 +1 @@ -version = '0.16.0' +version = '0.17.0'