Skip to content

Commit 4bc63cc

Browse files
committed
Make __eq__ code more DRY
1 parent d89fe50 commit 4bc63cc

File tree

3 files changed

+23
-12
lines changed

3 files changed

+23
-12
lines changed

geoip2/mixins.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""This package contains utility mixins"""
2+
# pylint: disable=too-few-public-methods
3+
from abc import ABCMeta
4+
5+
6+
class SimpleEquality(object):
7+
8+
"""Naive __dict__ equality mixin"""
9+
10+
__metaclass__ = ABCMeta
11+
12+
def __eq__(self, other):
13+
return (isinstance(other, self.__class__)
14+
and self.__dict__ == other.__dict__)
15+
16+
def __ne__(self, other):
17+
return not self.__eq__(other)

geoip2/models.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
from abc import ABCMeta
1515

1616
import geoip2.records
17+
from geoip2.mixins import SimpleEquality
1718

1819

19-
class Country(object):
20+
class Country(SimpleEquality):
2021

2122
"""Model for the GeoIP2 Precision: Country and the GeoIP2 Country database
2223
@@ -90,9 +91,6 @@ def __init__(self, raw_response, locales=None):
9091
self.traits = geoip2.records.Traits(**raw_response.get('traits', {}))
9192
self.raw = raw_response
9293

93-
def __eq__(self, other):
94-
return self.__dict__ == other.__dict__
95-
9694
def __repr__(self):
9795
return '{module}.{class_name}({data}, {locales})'.format(
9896
module=self.__module__,
@@ -242,15 +240,12 @@ class Insights(City):
242240
"""
243241

244242

245-
class SimpleModel(object):
243+
class SimpleModel(SimpleEquality):
246244

247245
"""Provides basic methods for non-location models"""
248246

249247
__metaclass__ = ABCMeta
250248

251-
def __eq__(self, other):
252-
return self.__dict__ == other.__dict__
253-
254249
def __repr__(self):
255250
# pylint: disable=no-member
256251
return '{module}.{class_name}({data})'.format(

geoip2/records.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
# pylint:disable=R0903
88
from abc import ABCMeta
99

10+
from geoip2.mixins import SimpleEquality
1011

11-
class Record(object):
12+
13+
class Record(SimpleEquality):
1214

1315
"""All records are subclasses of the abstract class ``Record``"""
1416
__metaclass__ = ABCMeta
@@ -22,9 +24,6 @@ def __init__(self, **kwargs):
2224
def __setattr__(self, name, value):
2325
raise AttributeError("can't set attribute")
2426

25-
def __eq__(self, other):
26-
return self.__dict__ == other.__dict__
27-
2827
def __repr__(self):
2928
args = ', '.join('%s=%r' % x for x in self.__dict__.items())
3029
return '{module}.{class_name}({data})'.format(

0 commit comments

Comments
 (0)