Skip to content

Commit

Permalink
Merge pull request #562 from grant-nations/main
Browse files Browse the repository at this point in the history
Add __hash__, __eq__, and __ne__ support for Observer objects
  • Loading branch information
bmorris3 authored Jul 27, 2023
2 parents 9b4c53d + b2f31a5 commit bc8788e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
79 changes: 79 additions & 0 deletions astroplan/observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,85 @@ def __repr__(self):
attributes_strings.append("{}={}".format(name, value))
return "<{}: {}>".format(class_name, ",\n ".join(attributes_strings))

def _key(self):
"""
Generate a tuple of the attributes that determine uniqueness of
`~astroplan.Observer` objects.
Returns
-------
key : tuple
Examples
--------
>>> from astroplan import Observer
>>> keck = Observer.at_site("Keck", timezone="US/Hawaii")
>>> keck._key()
('Keck', None, None, None, <Longitude -155.47833333 deg>,
<Latitude 19.82833333 deg>, <Quantity 4160. m>,
<DstTzInfo 'US/Hawaii' LMT-1 day, 13:29:00 STD>)
"""

return (self.name,
self.pressure,
self.temperature,
self.relative_humidity,
self.longitude,
self.latitude,
self.elevation,
self.timezone,)

def __hash__(self):
"""
Hash the `~astroplan.Observer` object.
Examples
--------
>>> from astroplan import Observer
>>> keck = Observer.at_site("Keck", timezone="US/Hawaii")
>>> hash(keck)
-3872382927731250571
"""

return hash(self._key())

def __eq__(self, other):
"""
Equality check for `~astroplan.Observer` objects.
Examples
--------
>>> from astroplan import Observer
>>> keck = Observer.at_site("Keck", timezone="US/Hawaii")
>>> keck2 = Observer.at_site("Keck", timezone="US/Hawaii")
>>> keck == keck2
True
"""

if isinstance(other, Observer):
return self._key() == other._key()
else:
return NotImplemented

def __ne__(self, other):
"""
Inequality check for `~astroplan.Observer` objects.
Examples
--------
>>> from astroplan import Observer
>>> keck = Observer.at_site("Keck", timezone="US/Hawaii")
>>> kpno = Observer.at_site("KPNO", timezone="US/Arizona")
>>> keck != kpno
True
"""

return not self.__eq__(other)

@classmethod
def at_site(cls, site_name, **kwargs):
"""
Expand Down
29 changes: 29 additions & 0 deletions astroplan/tests/test_observer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1358,3 +1358,32 @@ def test_observer_lon_lat_el():
obs = Observer.at_site('Subaru')
for attr in ['longitude', 'latitude', 'elevation']:
assert hasattr(obs, attr)


def test_hash_observer():
"""Test that Observer objects are hashable."""
obs1 = Observer.at_site('Subaru')
obs2 = Observer.at_site('Subaru')
assert hash(obs1) == hash(obs2)

obs3 = Observer.at_site('Keck', timezone='US/Hawaii')
assert hash(obs1) != hash(obs3)

obs4 = Observer.at_site('Keck', timezone='US/Hawaii')
assert hash(obs3) == hash(obs4)


def test_eq_observer():
"""Test that Observer objects are comparable."""
obs1 = Observer.at_site('Subaru')
obs2 = Observer.at_site('Subaru')
assert obs1 == obs2

obs3 = Observer.at_site('Keck')
assert obs1 != obs3

obs4 = Observer.at_site('Subaru', timezone='US/Hawaii')
assert obs1 != obs4

obs5 = Observer.at_site('Subaru', timezone='US/Hawaii')
assert obs4 == obs5

0 comments on commit bc8788e

Please sign in to comment.