From 49e388d3c6a19aeb9e6b7341e2558a1baac8fb04 Mon Sep 17 00:00:00 2001 From: Harmon Date: Wed, 30 Dec 2020 22:27:27 -0600 Subject: [PATCH] Add and use mixins for Status and User --- tweepy/mixins.py | 19 +++++++++++++++++++ tweepy/models.py | 20 +++----------------- 2 files changed, 22 insertions(+), 17 deletions(-) create mode 100644 tweepy/mixins.py 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): -- 2.25.1