Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion astropy/coordinates/angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ def to_string(
pad=False,
fields=3,
format=None,
oldstyle=False,
):
"""A string representation of the angle.

Expand Down Expand Up @@ -272,6 +273,14 @@ def to_string(
- 'unicode': Return a string containing non-ASCII unicode
characters, such as the degree symbol

oldstyle : bool, optional
If `False`, include a space between value and unit. If `True`,
no space is included. This option does not work with 'latex' or 'unicode'
format. In v5.3 onwards, a space is generated by default; set ``oldstyle=True``
for backward compatibility with older versions of ``astropy``.

.. versionadded:: 5.3

Returns
-------
strrepr : str or array
Expand Down Expand Up @@ -325,7 +334,12 @@ def to_string(
if format == "latex" or format == "latex_inline":
unit_string = unit_string[1:-1]
format_func = func
func = lambda x: format_func(x) + unit_string
# Add a space if no format(i.e unicode or latex) is given

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if the whole else clause could simply leave it to Quantity.to_string() via

return super().to_string(unit=unit, precision=precision, format=format)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm nice, that will simplify the code ... let me review it again

func = lambda x: (
format_func(x)
+ (" " * ((format == None) and (oldstyle is False)))
+ unit_string
)

def do_format(val):
# Check if value is not nan to avoid ValueErrors when turning it into
Expand Down
10 changes: 5 additions & 5 deletions astropy/coordinates/tests/test_angles.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def string(self, unit=DEGREE, decimal=False, sep=" ", precision=5,
f"Angle as DMS: {angle.to_string(unit=u.degree, precision=4, pad=True)}" == res
)

res = "Angle as rad: 0.0629763rad"
res = "Angle as rad: 0.0629763 rad"
assert f"Angle as rad: {angle.to_string(unit=u.radian)}" == res

res = "Angle as rad decimal: 0.0629763"
Expand Down Expand Up @@ -588,9 +588,9 @@ def test_angle_string():
a = Angle("00:00:59W", u.hour)
assert str(a) == "-0h00m59s"
a = Angle(3.2, u.radian)
assert str(a) == "3.2rad"
assert str(a) == "3.2 rad"
a = Angle(4.2, u.microarcsecond)
assert str(a) == "4.2uarcsec"
assert str(a) == "4.2 uarcsec"
a = Angle("1.0uarcsec")
assert a.value == 1.0
assert a.unit == u.microarcsecond
Expand Down Expand Up @@ -1096,8 +1096,8 @@ def parse_test(i=0):
"[nan, nan, nan] hourangle",
),
(np.nan * u.rad, "nan", "nan rad"),
([np.nan, 1, 0] * u.rad, "[nan 1rad 0rad]", "[nan, 1., 0.] rad"),
([1.50, np.nan, 0] * u.rad, "[1.5rad nan 0rad]", "[1.5, nan, 0.] rad"),
([np.nan, 1, 0] * u.rad, "[nan 1 rad 0 rad]", "[nan, 1., 0.] rad"),
([1.50, np.nan, 0] * u.rad, "[1.5 rad nan 0 rad]", "[1.5, nan, 0.] rad"),
([np.nan, np.nan, np.nan] * u.rad, "[nan nan nan]", "[nan, nan, nan] rad"),
],
)
Expand Down
2 changes: 1 addition & 1 deletion astropy/coordinates/tests/test_formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_to_string_radian_with_precision():

# Check that specifying the precision works
a = Angle(3.0, unit=u.rad)
assert a.to_string(precision=3, sep="fromunit") == "3.000rad"
assert a.to_string(precision=3, sep="fromunit") == "3.000 rad"


def test_sexagesimal_round_down():
Expand Down
1 change: 1 addition & 0 deletions docs/changes/coordinates/14231.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The ``oldstyle`` option is now available for ``to_string``.
Comment thread
Telomelonia marked this conversation as resolved.
Resolved
2 changes: 1 addition & 1 deletion docs/coordinates/angles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ There are many ways to represent the value of an |Angle|::
>>> f"{a.to(u.deg):latex}"
'$57^\\circ17{}^\\prime44.8062471{}^{\\prime\\prime}$'
>>> a.to_string()
'1rad'
'1 rad'
>>> a.to_string(unit=u.degree)
'57d17m44.8062471s'
>>> a.to_string(unit=u.degree, sep=':')
Expand Down