Skip to content

Added a feature which will allow the user to control space b/w value and unit - #14231

Closed
Telomelonia wants to merge 11 commits into
astropy:mainfrom
Telomelonia:value_space_unit
Closed

Telomelonia wants to merge 11 commits into
astropy:mainfrom
Telomelonia:value_space_unit

Conversation

@Telomelonia

@Telomelonia Telomelonia commented Dec 24, 2022 •

Copy link
Copy Markdown
Member

Description

I added a boolean feature oldstyle in to_string(), giving flexibility to the user to have a space between value and unit.
By default, space should be given in between whenever its possible.
NOTE-For some units like 34'20'' or 11h22m no space should be added whether the feature is set to True or False.

oldstyle : bool, optional
            If `False`, include a space between value and unit. If `True`,
            no space is included. Doesn't work with 'latex' or 'unicode'
            format. By default a space is generated.

TODOs

  • Adding the feature as a boolean keyword
  • Updating the Docstring
  • Add a changelog
  • Update Testing

This pull request is to address ...

Fixes #7455

Testing

image
image

Checklist for package maintainer(s)

This checklist is meant to remind the package maintainer(s) who will review this pull request of some common things to look for. This list is not exhaustive.

  • Do the proposed changes actually accomplish desired goals?
  • Do the proposed changes follow the Astropy coding guidelines?
  • Are tests added/updated as required? If so, do they follow the Astropy testing guidelines?
  • Are docs added/updated as required? If so, do they follow the Astropy documentation guidelines?
  • Is rebase and/or squash necessary? If so, please provide the author with appropriate instructions. Also see "When to rebase and squash commits".
  • Did the CI pass? If no, are the failures related? If you need to run daily and weekly cron jobs as part of the PR, please apply the Extra CI label. Codestyle issues can be fixed by the bot.
  • Is a change log needed? If yes, did the change log check pass? If no, add the no-changelog-entry-needed label. If this is a manual backport, use the skip-changelog-checks label unless special changelog handling is necessary.
  • Is this a big PR that makes a "What's new?" entry worthwhile and if so, is (1) a "what's new" entry included in this PR and (2) the "whatsnew-needed" label applied?
  • Is a milestone set? Milestone must be set but astropy-bot check might be missing; do not let the green checkmark fool you.
  • At the time of adding the milestone, if the milestone set requires a backport to release branch(es), apply the appropriate backport-X.Y.x label(s) before merge.

@pllim pllim added this to the v5.3 milestone Dec 25, 2022
@pllim pllim added the API change PRs and issues that change an existing API, possibly requiring a deprecation period label Dec 25, 2022
@pllim

pllim commented Dec 25, 2022

Copy link
Copy Markdown
Member

The tests need updating. A new test to use the alternate option is also necessary.

Also a change log is necessary (as you have noted in your checklist).

Thanks!

@mhvk mhvk left a comment

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.

Thanks for the PR, but I wonder why this is needed? Most journals insist that a space is used between a number and the unit. Also, while for simple units this might make sense, it does not generalize to composite units (which have spaces in between them).

Overall, my tendency would be to leave this for people to sort for themselves if they want it; generally .to_string(...).replace(' ', '') will be all that's needed.

So, not in favour; marking as "request changes" since if we were to go for adding this option, it definitely should have a different name.

Comment thread astropy/coordinates/angles.py Resolved
@mhvk

mhvk commented Dec 28, 2022

Copy link
Copy Markdown
Contributor

Oops, I'm so sorry, I hadn't even realized that the issue here is to fix that angles do not have spaces between the number and the unit. Very silly of me; I should have read the issue.

Now having done so, I agree with the comments there that the default should be to have the space, with the only exception being when one of the special symbols is used. My tendency would be not to even have an option for this.

Anyway, now in favour as long as we change the default!

@mhvk mhvk left a comment

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.

A few more thoughts.

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

@larrybradley
larrybradley removed their request for review February 3, 2023 19:00
@Telomelonia Telomelonia changed the title Added a feature which will allow the user to control space b/w value and unit [ci skip] Added a feature which will allow the user to control space b/w value and unit Feb 8, 2023
@Telomelonia
Telomelonia force-pushed the value_space_unit branch 2 times, most recently from 947ad3a to 5e9730a Compare February 8, 2023 11:10
Added a space between value and unit and added a feature of old style if someone wants to use the old style no space string
@Telomelonia

Copy link
Copy Markdown
Member Author

@pllim
I have a problem regarding the pre-commit.ci check. Apart from that all is good to go

@pllim

pllim commented Feb 8, 2023

Copy link
Copy Markdown
Member

pre-commit.ci autofix

@pllim pllim left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A test need to be added with oldstyle=True to ensure it works as intended.

Please rename the change log fragment file from 14231.feature.rst to 14231.api.rst as suggested by reviewer. Change log text also need to elaborate what this oldstyle actually does and why it is needed now.

Needs to squash all commits into one before this is ready for merge too. To do that, please remember to pull in pre-commit autofix commit from GitHub back to your local checkout first. Feel free to ask if you are unsure how to do this part.

Comment thread astropy/coordinates/angles.py Resolved
Comment thread astropy/coordinates/angles.py Resolved
@mhvk

mhvk commented Feb 8, 2023

Copy link
Copy Markdown
Contributor

I think there is still one open question: as I commented above, I wonder if we even need the oldstyle option -- I think what we had was just wrong. What do others think?

p.s. If we do go with an option, we should give it a better name (and default should definitely be off).

@eerovaher

Copy link
Copy Markdown
Member

I have a problem regarding the pre-commit.ci check. Apart from that all is good to go

pre-commit can fix most (if not all) issues it highlights, see https://docs.astropy.org/en/latest/development/workflow/development_workflow.html#pre-commit

@Telomelonia

Copy link
Copy Markdown
Member Author

pre-commit.ci autofix

@pllim

pllim commented Feb 9, 2023

Copy link
Copy Markdown
Member

Re: #14231 (comment)

@mhvk , I think that is a discussion for astropy-dev mailing list and/or dev telecon. I am going to turn this PR into draft until this discussion is resolved.

@pllim
pllim marked this pull request as draft February 9, 2023 13:45
@mhvk

mhvk commented Feb 9, 2023

Copy link
Copy Markdown
Contributor

Let's just ping the coordinate team - @eerovaher, @StuartLittlefair, @adrn, @eteq - on whether to make a space between a number and the unit the standard case (as it is for Quantity), or whether it is important to allow users to keep the "old style" where, for Angle only, the space was omitted (likely by mistake). I feel this is a quite clear case of only one correct way, and that effectively this is a bug fix, with little or no impact to anyone since it affects only how angles are shown in string form. So, I'm in favour of just changing, without an option to get the old behavor back (which, after all is just a f"{angle.value}{angle.unit}" away, probably easier to discover than a keyword argument!).

@eerovaher

Copy link
Copy Markdown
Member

I agree with @mhvk that there is no need to make the "old style" option available as a keyword argument to Angle.to_string(). In some cases with Unicode, one of the LaTeX formats or sexagesimal representation the space should be omitted (e.g. 1°00′00″ is correct as is), but that should be automatically taken care of by to_string() and should not rely on manual intervention by the users. The space can still be omitted by handling the value and unit separately, like @mhvk showed, and this technique is already documented:

To format both the value and the unit separately, you can access the |Quantity|
attributes within format strings::
>>> q = 10.5 * u.km
>>> q
<Quantity 10.5 km>
>>> f"{q.value:.3f} in {q.unit}"
'10.500 in km'

@StuartLittlefair

Copy link
Copy Markdown
Contributor

Just to sat that in response to @mhvk's ping, I have no strong opinion on this. The only real issue I can anticipate is if third-parties had based their unit tests on the string output from angles, in which case I don't have much sympathy...

@Telomelonia

Copy link
Copy Markdown
Member Author

I thinking of making another PR to address this issue as just a bug where a space is required in the result in between value and unit.

@mhvk

mhvk commented Feb 13, 2023

Copy link
Copy Markdown
Contributor

@Telomelonia - it might indeed be good to have 2 PRs to compare, though it also is fine to work with this one, Thanks for persisting!!!

@pllim pllim closed this in #14379 Apr 19, 2023
@Telomelonia
Telomelonia deleted the value_space_unit branch April 25, 2023 05:01
@Telomelonia
Telomelonia restored the value_space_unit branch April 25, 2023 05:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

API change PRs and issues that change an existing API, possibly requiring a deprecation period coordinates needs-discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Space between value and unit

5 participants