diff options
| author | Ben Firshman <ben@firshman.co.uk> | 2017-01-18 16:49:35 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-01-18 16:49:35 +0100 |
| commit | 468bb1c5454c0b76effa89e739e7788999367b4a (patch) | |
| tree | 81b26ef0e76707de1bfaa1d50ba878988d7a677a | |
| parent | aed1af6f6f8c97658ad8d11619ba0e7fce7af240 (diff) | |
| parent | 6f239fbf29109d39d3cfed0650c9ffe217c681a8 (diff) | |
| download | docker-py-468bb1c5454c0b76effa89e739e7788999367b4a.tar.gz | |
Merge pull request #1347 from fcurella/hash
Make resources hashable, so that they can be added to `set`s
| -rw-r--r-- | docker/models/resource.py | 3 | ||||
| -rw-r--r-- | tests/unit/models_resources_test.py | 14 |
2 files changed, 17 insertions, 0 deletions
diff --git a/docker/models/resource.py b/docker/models/resource.py index 95712ae..ed3900a 100644 --- a/docker/models/resource.py +++ b/docker/models/resource.py @@ -23,6 +23,9 @@ class Model(object): def __eq__(self, other): return isinstance(other, self.__class__) and self.id == other.id + def __hash__(self): + return hash("%s:%s" % (self.__class__.__name__, self.id)) + @property def id(self): """ diff --git a/tests/unit/models_resources_test.py b/tests/unit/models_resources_test.py index 25c6a3e..5af24ee 100644 --- a/tests/unit/models_resources_test.py +++ b/tests/unit/models_resources_test.py @@ -12,3 +12,17 @@ class ModelTest(unittest.TestCase): container.reload() assert client.api.inspect_container.call_count == 2 assert container.attrs['Name'] == "foobar" + + def test_hash(self): + client = make_fake_client() + container1 = client.containers.get(FAKE_CONTAINER_ID) + my_set = set([container1]) + assert len(my_set) == 1 + + container2 = client.containers.get(FAKE_CONTAINER_ID) + my_set.add(container2) + assert len(my_set) == 1 + + image1 = client.images.get(FAKE_CONTAINER_ID) + my_set.add(image1) + assert len(my_set) == 2 |
