Skip to content

Commit

Permalink
For #527, try returning arrays from hstr()/dstr()
Browse files Browse the repository at this point in the history
This replaces a behavior that was contributed back in, wow, 2015 — crazy
times!  Also, let’s test this behavior in unit tests for the first time.
  • Loading branch information
brandon-rhodes committed Jan 2, 2021
1 parent 8aec500 commit ce9411c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
35 changes: 28 additions & 7 deletions skyfield/tests/test_units.py
Expand Up @@ -2,7 +2,9 @@

from assay import assert_raises
from numpy import array
from skyfield.units import Angle, Distance, Velocity, UnpackingError
from skyfield.units import (
Angle, Distance, Velocity, UnpackingError, WrongUnitError,
)

try:
from astropy import units as u
Expand Down Expand Up @@ -43,12 +45,31 @@ def test_angle_scalar_strs():
assert str(Angle(hours=array(12))) == '''12h 00m 00.00s'''

def test_angle_array_strs():
assert str(Angle(degrees=array([90, 91, 92]))) == (
'''3 values from 90deg 00' 00.0" to 92deg 00' 00.0"'''
)
assert str(Angle(hours=array([11, 12, 13]))) == (
'''3 values from 11h 00m 00.00s to 13h 00m 00.00s'''
)
h = Angle(hours=array([11, 12, 13]))
d = Angle(degrees=h._degrees)

assert str(h) == '3 values from 11h 00m 00.00s to 13h 00m 00.00s'
assert str(d) == '''3 values from 165deg 00' 00.0" to 195deg 00' 00.0"'''

with assert_raises(WrongUnitError):
h.dstr()
d.hstr()

assert h.hstr() == d.hstr(warn=False) == [
'11h 00m 00.00s',
'12h 00m 00.00s',
'13h 00m 00.00s',
]
assert d.dstr() == h.dstr(warn=False) == [
'165deg 00\' 00.0"',
'180deg 00\' 00.0"',
'195deg 00\' 00.0"',
]

empty = Angle(radians=[])
assert str(empty) == 'Angle []'
assert empty.hstr(warn=False) == []
assert empty.dstr() == []

def test_angle_sexagesimal_args():
assert str(Angle(degrees=(90,))) == '''90deg 00' 00.0"'''
Expand Down
32 changes: 15 additions & 17 deletions skyfield/units.py
Expand Up @@ -258,7 +258,17 @@ def mas(self):
def __str__(self):
if self.radians.size == 0:
return 'Angle []'
return self.dstr() if self.preference == 'degrees' else self.hstr()
if self.preference == 'degrees':
v = self._degrees
f = _dstr
else:
v = self._hours
f = _hstr
shape = getattr(v, 'shape', None)
if shape:
return "{0} values from {1} to {2}".format(
len(v), f(v[0]), f(v[-1]))
return f(v)

def __repr__(self):
if self.radians.size == 0:
Expand Down Expand Up @@ -302,16 +312,10 @@ def hstr(self, places=2, warn=True):
"""Convert to a string like ``12h 07m 30.00s``."""
if warn and self.preference != 'hours':
raise WrongUnitError('hstr')
if self.radians.size == 0:
return '<Angle []>'
hours = self._hours
shape = getattr(hours, 'shape', ())
if shape and shape != (1,):
return "{0} values from {1} to {2}".format(
len(hours),
_hstr(hours[0], places),
_hstr(hours[-1], places),
)
if shape:
return [_hstr(h, places) for h in hours]
return _hstr(hours, places)

def dms(self, warn=True):
Expand Down Expand Up @@ -340,17 +344,11 @@ def dstr(self, places=1, warn=True):
"""Convert to a string like ``181deg 52\' 30.0"``."""
if warn and self.preference != 'degrees':
raise WrongUnitError('dstr')
if self.radians.size == 0:
return '<Angle []>'
degrees = self._degrees
signed = self.signed
shape = getattr(degrees, 'shape', ())
if shape and shape != (1,):
return "{0} values from {1} to {2}".format(
len(degrees),
_dstr(degrees[0], places, signed),
_dstr(degrees[-1], places, signed),
)
if shape:
return [_dstr(d, places, signed) for d in degrees]
return _dstr(degrees, places, signed)

def to(self, unit):
Expand Down

0 comments on commit ce9411c

Please sign in to comment.