mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
Hashable TelegramObject
This commit is contained in:
parent
f3f9b3c27a
commit
531b2a4df7
14 changed files with 54 additions and 50 deletions
|
|
@ -244,3 +244,28 @@ class TelegramObject(metaclass=MetaTelegramObject):
|
|||
"""
|
||||
for _, value in self:
|
||||
yield value
|
||||
|
||||
def __hash__(self):
|
||||
def _hash(obj):
|
||||
buf = 0
|
||||
if isinstance(obj, list):
|
||||
for item in obj:
|
||||
buf += _hash(item)
|
||||
elif isinstance(obj, dict):
|
||||
for dict_key, dict_value in obj.items():
|
||||
buf += hash(dict_key) + _hash(dict_value)
|
||||
else:
|
||||
try:
|
||||
buf += hash(obj)
|
||||
except TypeError: # Skip unhashable objects
|
||||
pass
|
||||
return buf
|
||||
|
||||
result = 0
|
||||
for key, value in sorted(self.values.items()):
|
||||
result += hash(key) + _hash(value)
|
||||
|
||||
return result
|
||||
|
||||
def __eq__(self, other):
|
||||
return isinstance(other, self.__class__) and hash(other) == hash(self)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue