Skip to content

Commit 479f0db

Browse files
author
Zach Moody
authored
Merge pull request #210 from fach/master
Fix using Record objects in hashable collections
2 parents 1c55ff1 + b761930 commit 479f0db

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

pynetbox/core/endpoint.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Endpoint(object):
4242

4343
def __init__(self, api, app, name, model=None):
4444
self.return_obj = self._lookup_ret_obj(name, model)
45+
self.name = name.replace("_", "-")
4546
self.api = api
4647
self.base_url = api.base_url
4748
self.token = api.token
@@ -50,7 +51,7 @@ def __init__(self, api, app, name, model=None):
5051
self.url = "{base_url}/{app}/{endpoint}".format(
5152
base_url=self.base_url,
5253
app=app.name,
53-
endpoint=name.replace("_", "-"),
54+
endpoint=self.name,
5455
)
5556
self._choices = None
5657

pynetbox/core/response.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,19 @@ def __getstate__(self):
213213
def __setstate__(self, d):
214214
self.__dict__.update(d)
215215

216-
def __hash__(self):
216+
def __key__(self):
217217
if hasattr(self, "id"):
218-
return hash((self.endpoint.name, self.id))
218+
return (self.endpoint.name, self.id)
219219
else:
220-
return hash(self.endpoint.name)
220+
return (self.endpoint.name)
221+
222+
def __hash__(self):
223+
return hash(self.__key__())
224+
225+
def __eq__(self, other):
226+
if isinstance(other, Record):
227+
return self.__key__() == other.__key__()
228+
return NotImplemented
221229

222230
def _add_cache(self, item):
223231
key, value = item

tests/unit/test_response.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,14 @@ def test_hash_diff(self):
128128
test2 = Record({}, None, endpoint2)
129129
test2.id = 2
130130
self.assertNotEqual(hash(test1), hash(test2))
131+
132+
def test_compare(self):
133+
endpoint1 = Mock()
134+
endpoint1.name = "test-endpoint"
135+
endpoint2 = Mock()
136+
endpoint2.name = "test-endpoint"
137+
test1 = Record({}, None, endpoint1)
138+
test1.id = 1
139+
test2 = Record({}, None, endpoint2)
140+
test2.id = 1
141+
self.assertEqual(test1, test2)

0 commit comments

Comments
 (0)