Skip to content

Commit cdb5698

Browse files
authored
refactor: streamline hash compare 2 (#780)
Signed-off-by: Jan Kowalleck <[email protected]>
1 parent baaa977 commit cdb5698

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

cyclonedx/model/contact.py

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -254,23 +254,23 @@ def phone(self) -> Optional[str]:
254254
def phone(self, phone: Optional[str]) -> None:
255255
self._phone = phone
256256

257+
def __comparable_tuple(self) -> _ComparableTuple:
258+
return _ComparableTuple((
259+
self.name, self.email, self.phone
260+
))
261+
257262
def __eq__(self, other: object) -> bool:
258263
if isinstance(other, OrganizationalContact):
259-
return hash(other) == hash(self)
264+
return self.__comparable_tuple() == other.__comparable_tuple()
260265
return False
261266

262267
def __lt__(self, other: Any) -> bool:
263268
if isinstance(other, OrganizationalContact):
264-
return _ComparableTuple((
265-
self.name, self.email, self.phone
266-
)) < _ComparableTuple((
267-
other.name, other.email, other.phone
268-
))
269+
return self.__comparable_tuple() < other.__comparable_tuple()
269270
return NotImplemented
270271

271272
def __hash__(self) -> int:
272-
# TODO
273-
return hash((self.name, self.phone, self.email))
273+
return hash(self.__comparable_tuple())
274274

275275
def __repr__(self) -> str:
276276
return f'<OrganizationalContact name={self.name}, email={self.email}, phone={self.phone}>'
@@ -364,19 +364,23 @@ def contacts(self) -> 'SortedSet[OrganizationalContact]':
364364
def contacts(self, contacts: Iterable[OrganizationalContact]) -> None:
365365
self._contacts = SortedSet(contacts)
366366

367+
def __comparable_tuple(self) -> _ComparableTuple:
368+
return _ComparableTuple((
369+
self.name, _ComparableTuple(self.urls), _ComparableTuple(self.contacts)
370+
))
371+
367372
def __eq__(self, other: object) -> bool:
368373
if isinstance(other, OrganizationalEntity):
369-
return hash(other) == hash(self)
374+
return self.__comparable_tuple() == other.__comparable_tuple()
370375
return False
371376

372377
def __lt__(self, other: Any) -> bool:
373378
if isinstance(other, OrganizationalEntity):
374-
return hash(self) < hash(other)
379+
return self.__comparable_tuple() < other.__comparable_tuple()
375380
return NotImplemented
376381

377382
def __hash__(self) -> int:
378-
# TODO
379-
return hash((self.name, tuple(self.urls), tuple(self.contacts)))
383+
return hash(self.__comparable_tuple())
380384

381385
def __repr__(self) -> str:
382386
return f'<OrganizationalEntity name={self.name}>'

cyclonedx/model/crypto.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -494,16 +494,20 @@ def nist_quantum_security_level(self, nist_quantum_security_level: Optional[int]
494494
)
495495
self._nist_quantum_security_level = nist_quantum_security_level
496496

497+
def __comparable_tuple(self) -> _ComparableTuple:
498+
return _ComparableTuple((
499+
self.primitive, self._parameter_set_identifier, self.curve, self.execution_environment,
500+
self.implementation_platform, _ComparableTuple(self.certification_levels), self.mode, self.padding,
501+
_ComparableTuple(self.crypto_functions), self.classical_security_level, self.nist_quantum_security_level,
502+
))
503+
497504
def __eq__(self, other: object) -> bool:
498505
if isinstance(other, AlgorithmProperties):
499-
return hash(other) == hash(self)
506+
return self.__comparable_tuple() == other.__comparable_tuple()
500507
return False
501508

502509
def __hash__(self) -> int:
503-
# TODO
504-
return hash((self.primitive, self._parameter_set_identifier, self.curve, self.execution_environment,
505-
self.implementation_platform, tuple(self.certification_levels), self.mode, self.padding,
506-
tuple(self.crypto_functions), self.classical_security_level, self.nist_quantum_security_level,))
510+
return hash(self.__comparable_tuple())
507511

508512
def __repr__(self) -> str:
509513
return f'<AlgorithmProperties primitive={self.primitive}, execution_environment={self.execution_environment}>'

cyclonedx/model/vulnerability.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -461,22 +461,23 @@ def url(self) -> Optional[XsUri]:
461461
def url(self, url: Optional[XsUri]) -> None:
462462
self._url = url
463463

464+
def __comparable_tuple(self) -> _ComparableTuple:
465+
return _ComparableTuple((
466+
self.name, self.url
467+
))
468+
464469
def __eq__(self, other: object) -> bool:
465470
if isinstance(other, VulnerabilitySource):
466-
return hash(other) == hash(self)
471+
return self.__comparable_tuple() == other.__comparable_tuple()
467472
return False
468473

469474
def __lt__(self, other: Any) -> bool:
470475
if isinstance(other, VulnerabilitySource):
471-
return _ComparableTuple((
472-
self.name, self.url
473-
)) < _ComparableTuple((
474-
other.name, other.url
475-
))
476+
return self.__comparable_tuple() < other.__comparable_tuple()
476477
return NotImplemented
477478

478479
def __hash__(self) -> int:
479-
return hash((self.name, self.url))
480+
return hash(self.__comparable_tuple())
480481

481482
def __repr__(self) -> str:
482483
return f'<VulnerabilityAdvisory name={self.name}, url={self.url}>'

0 commit comments

Comments
 (0)