Skip to content
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

Implement __format__ on cfdm.Data objects #152

Closed
davidhassell opened this issue Aug 23, 2021 · 2 comments · Fixed by #153
Closed

Implement __format__ on cfdm.Data objects #152

davidhassell opened this issue Aug 23, 2021 · 2 comments · Fixed by #153
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@davidhassell
Copy link
Contributor

davidhassell commented Aug 23, 2021

When including cfdm.Data objects in f-strings that include format codes (such as .3f), size 1 arrays should be formatted like their python scalar counterparts.

I think that the desired behaviour would be:

>>> d = cfdm.Data(9, 'metres')
>>> f"{d}"
'9 metres'
>>> f"{d!s}"
'9 metres'
>>> f"{d!r}"
'<Data(): 9 metres>'
>>> f"{d:.3f}"
'9.000'
>>> d = cfdm.Data([[9]], 'metres')
>>> f"{d}"
'[[9]] metres'
>>> f"{d!s}"
'[[9]] metres'
>>> f"{d!r}"
'<Data(1, 1): [[9]] metres>'
>>> f"{d:.3f}"
'9.000'
>>> d = cfdm.Data([9, 10], 'metres')
>>> f"{d}"
>>> '[9, 10] metres'
>>> f"{d!s}"
>>> '[9, 10] metres'
>>> f"{d!r}"
'<Data(2): [9, 10] metres>'
>>> f"{d:.3f}"
    ...
ValueError: Can't format Data array of size 2 with format code .3f

@JonathanGregory, does this look like what you would expect?

@davidhassell davidhassell added the enhancement New feature or request label Aug 23, 2021
@davidhassell davidhassell self-assigned this Aug 23, 2021
@JonathanGregory
Copy link

JonathanGregory commented Aug 23, 2021 via email

@davidhassell
Copy link
Contributor Author

Thanks - PR to follow ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants