Skip to content

refactor: streamline hash compare 2 #780

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 16 additions & 12 deletions cyclonedx/model/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,23 +254,23 @@ def phone(self) -> Optional[str]:
def phone(self, phone: Optional[str]) -> None:
self._phone = phone

def __comparable_tuple(self) -> _ComparableTuple:
return _ComparableTuple((
self.name, self.email, self.phone
))

def __eq__(self, other: object) -> bool:
if isinstance(other, OrganizationalContact):
return hash(other) == hash(self)
return self.__comparable_tuple() == other.__comparable_tuple()
return False

def __lt__(self, other: Any) -> bool:
if isinstance(other, OrganizationalContact):
return _ComparableTuple((
self.name, self.email, self.phone
)) < _ComparableTuple((
other.name, other.email, other.phone
))
return self.__comparable_tuple() < other.__comparable_tuple()
return NotImplemented

def __hash__(self) -> int:
# TODO
return hash((self.name, self.phone, self.email))
return hash(self.__comparable_tuple())

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

def __comparable_tuple(self) -> _ComparableTuple:
return _ComparableTuple((
self.name, _ComparableTuple(self.urls), _ComparableTuple(self.contacts)
))

def __eq__(self, other: object) -> bool:
if isinstance(other, OrganizationalEntity):
return hash(other) == hash(self)
return self.__comparable_tuple() == other.__comparable_tuple()
return False

def __lt__(self, other: Any) -> bool:
if isinstance(other, OrganizationalEntity):
return hash(self) < hash(other)
return self.__comparable_tuple() < other.__comparable_tuple()
return NotImplemented

def __hash__(self) -> int:
# TODO
return hash((self.name, tuple(self.urls), tuple(self.contacts)))
return hash(self.__comparable_tuple())

def __repr__(self) -> str:
return f'<OrganizationalEntity name={self.name}>'
14 changes: 9 additions & 5 deletions cyclonedx/model/crypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -494,16 +494,20 @@ def nist_quantum_security_level(self, nist_quantum_security_level: Optional[int]
)
self._nist_quantum_security_level = nist_quantum_security_level

def __comparable_tuple(self) -> _ComparableTuple:
return _ComparableTuple((
self.primitive, self._parameter_set_identifier, self.curve, self.execution_environment,
self.implementation_platform, _ComparableTuple(self.certification_levels), self.mode, self.padding,
_ComparableTuple(self.crypto_functions), self.classical_security_level, self.nist_quantum_security_level,
))

def __eq__(self, other: object) -> bool:
if isinstance(other, AlgorithmProperties):
return hash(other) == hash(self)
return self.__comparable_tuple() == other.__comparable_tuple()
return False

def __hash__(self) -> int:
# TODO
return hash((self.primitive, self._parameter_set_identifier, self.curve, self.execution_environment,
self.implementation_platform, tuple(self.certification_levels), self.mode, self.padding,
tuple(self.crypto_functions), self.classical_security_level, self.nist_quantum_security_level,))
return hash(self.__comparable_tuple())

def __repr__(self) -> str:
return f'<AlgorithmProperties primitive={self.primitive}, execution_environment={self.execution_environment}>'
Expand Down
15 changes: 8 additions & 7 deletions cyclonedx/model/vulnerability.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,22 +461,23 @@ def url(self) -> Optional[XsUri]:
def url(self, url: Optional[XsUri]) -> None:
self._url = url

def __comparable_tuple(self) -> _ComparableTuple:
return _ComparableTuple((
self.name, self.url
))

def __eq__(self, other: object) -> bool:
if isinstance(other, VulnerabilitySource):
return hash(other) == hash(self)
return self.__comparable_tuple() == other.__comparable_tuple()
return False

def __lt__(self, other: Any) -> bool:
if isinstance(other, VulnerabilitySource):
return _ComparableTuple((
self.name, self.url
)) < _ComparableTuple((
other.name, other.url
))
return self.__comparable_tuple() < other.__comparable_tuple()
return NotImplemented

def __hash__(self) -> int:
return hash((self.name, self.url))
return hash(self.__comparable_tuple())

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