New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make hms/dms namedtuples and add absdms
#1988
Conversation
👍 |
@@ -217,7 +222,7 @@ def hms(self): | |||
The angle's value in hours, as a ``(h, m, s)`` tuple |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could be more precise in the description, e.g. "tuple" -> "named tuple" or even "tuple" -> "(h, m, s) tuple" -> "named tuple with (h, m, s) members".
👍 |
>>> a.dms | ||
(57.0, 17.0, 44.806247...) | ||
DMS_tuple(d=57.0, m=17.0, s=44.806247...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add an example with a negative declination here or to the Angle.dms
docstring?
I was surprised that m
and s
are negative in addition to d
in that case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an excellent idea! done in the version I'll push up soon
I've written a function to create IAU designation strings here that covers my needs: Comments on how to improve the implementation are absolutely welcome. If you want this for astropy now (as a coordinate member function?) let me know and I'll give it a try and make it more general / add more tests / docs. I did not find the |
This is primarily intented for use with `dms` to generate string | ||
representations of coordinates that are correct for negative angles. | ||
""" | ||
d, m, s = util.degrees_to_dms(self.degree) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would take the absolute value of self.degree
rather than the absolute value of what is returned by the function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good point - I was concerned some implementations might come out subtly different, but right now it comes out the same, which is what really matters (fixed in a version I'm about to psuh up)
Like the idea, but wonder about |
👍 to the idea of a function that returns (sign, d, m, s) (where d, m and s are all positive). The current behavior is so that the obvious mathematical expression works:
If, for example, only d were signed, you'd have to do something like:
In short, |
Here's the For the declination I ended up using this code, which could be made a bit more readable (by using tuple names instead of indices) and maybe simpler (if sign becomes available as an extra field or maybe even
|
Good idea, @mhvk - I like the |
@@ -12,6 +12,10 @@ New Features | |||
|
|||
- ``astropy.coordinates`` | |||
|
|||
- Updated `Angle.dms` and `Angle.hms` to return `namedtuple`s instead of | |||
regular tuples, and added `Angle.absdms` attribute that gives the absolute |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change absdms
-> signed_dms
Apart from my minor comment about the documentation (and @cdell's corrections), this looks good! |
@@ -23,6 +24,11 @@ | |||
|
|||
TWOPI = math.pi * 2.0 # no need to calculate this all the time | |||
|
|||
# these are used by the `hms` and `dms` attributes | |||
HMS_tuple = namedtuple('HMS_tuple', ('h', 'm', 's')) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a minor question - is there a reason why not use hms_tuple
, dms_tuple
, and signed_dms_tuple
for the names of the tuple types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not really. I was thinking about them as something sort of like classes, and hence the PEP8 first-letter-cap convention led to those. It's pretty ambiguous, though, so if you think lower-case is better, I'm happy to do that instead, @astrofrog .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the reason I felt unsure is because currently it seems like a mix of the two conventions. I'd either go the full way and treat it as a class (HMSTuple
) or use the normal variable convention (hms_tuple
). I have no strong preference :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I see your point. In that case I mildly prefer hms_tuple
so I'll switch it all to the attribute/variable-style convention.
Alright, I think I've implemented all the suggestions here, so if the tests pass I'll go ahead and merge this later today. |
Oh, and @cdeil - The |
Tests passing, so I'll go ahead and merge. Thanks all of you for the valuable feedback! |
Make hms/dms namedtuples and add `signed_dms`
This small change adjusts
Angle
so that thehms
anddms
attributes yieldnamedtuple
s instead of regular tuples. That enables usage along these lines:It also adds an
absdms
attribute that gives the absolute value of thedms
elements. This is valuable for string formatting purposes (this was prompted by an on-list discussion about how to make IAU-style formatted strings.) Note that there is noabshms
because I know of know convention where hour angle is negative. But it could be easily added in.