From: Harmon Date: Thu, 31 Dec 2020 04:27:27 +0000 (-0600) Subject: Add and use mixins for Status and User X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=49e388d3c6a19aeb9e6b7341e2558a1baac8fb04;p=tweepy.git Add and use mixins for Status and User --- diff --git a/tweepy/mixins.py b/tweepy/mixins.py new file mode 100644 index 0000000..1c3eef6 --- /dev/null +++ b/tweepy/mixins.py @@ -0,0 +1,19 @@ +# Tweepy +# Copyright 2009-2020 Joshua Roesslein +# See LICENSE for details. + +class EqualityComparable: + __slots__ = () + + def __eq__(self, other): + if isinstance(other, self.__class__): + return self.id == other.id + + return NotImplemented + + +class Hashable(EqualityComparable): + __slots__ = () + + def __hash__(self): + return self.id diff --git a/tweepy/models.py b/tweepy/models.py index f290594..296c162 100644 --- a/tweepy/models.py +++ b/tweepy/models.py @@ -2,6 +2,7 @@ # Copyright 2009-2020 Joshua Roesslein # See LICENSE for details. +from tweepy.mixins import Hashable from tweepy.utils import parse_a_href, parse_datetime, parse_html_value @@ -82,7 +83,7 @@ class Model: return '%s(%s)' % (self.__class__.__name__, ', '.join(state)) -class Status(Model): +class Status(Model, Hashable): @classmethod def parse(cls, api, json): @@ -128,14 +129,8 @@ class Status(Model): def favorite(self): return self._api.create_favorite(self.id) - def __eq__(self, other): - if isinstance(other, Status): - return self.id == other.id - return NotImplemented - - -class User(Model): +class User(Model, Hashable): @classmethod def parse(cls, api, json): @@ -205,15 +200,6 @@ class User(Model): *args, **kwargs) - def __eq__(self, other): - if isinstance(other, User): - return self.id == other.id - - return NotImplemented - - def __hash__(self): - return self.id - class DirectMessage(Model):