Skip to content

Use self.symbol as consistent string representation #79

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

Merged
merged 2 commits into from
Nov 10, 2017

Conversation

tv3141
Copy link
Contributor

@tv3141 tv3141 commented Apr 11, 2017

Currently, the string representation uses self.origin - the string used to create the instance. After an arithmetic operation, the returned instance does not have this attribute any more and uses self.name, which is most likely different from self.origin. self.name also spells out the base units which is very verbose.

I suggest using self.symbol as string representation:

import cf_units

watt = cf_units.Unit('kg meter2 second-3')
print(watt)             # kg meter*meter/(second*second*second)
print(watt.symbol)      # W

another_watt = watt * 1
print(another_watt)            # watt -> changed
print(another_watt.symbol)     # W

watt_meter = watt * cf_units.Unit('meter')
print(watt_meter)            # meter^3-kilogram-second^-3  -> verbose
print(watt_meter.symbol)     # m3.kg.s-3

Before merging: Further changes in doc strings might be necessary.

- self.origin is the string to create the unit instance, it only exists in the original instance, not in returned instances after addition, etc.
- self.name spells out the unit and is rather verbose
- self.symbol uses short unit symbols, as used in scientific literature
@djkirkham
Copy link
Contributor

Hi @tv3141, thanks for submitting this PR.

The origin string can give information about the units that is lost when using self.symbol. For example, the units for humidity are 'kg/kg' or 'g/kg', but using self.symbol would just give '1' or '0.001 1'.

Also, the origin string can be more understandable for non-SI units. When using self.symbol, 'miles hour-1' becomes '0.44704 m.s-1'.

I agree that self.name is verbose. Perhaps we could use self.origin or self.symbol instead.

@tv3141
Copy link
Contributor Author

tv3141 commented Apr 13, 2017

I see the value of self.originin special cases, but thought consistent general behavior, i.e. the unit string does not change, unless the unit changes, would outweigh this.

I think using self.origin or self.symbol would be an improvement. The string representation of a unit can then always be used for human consumption, such as in a plot.

self.name is rather verbose
@coveralls
Copy link

coveralls commented Apr 13, 2017

Coverage Status

Coverage remained the same at 90.653% when pulling 05d1764 on tv3141:consistent_string_representation into bd988a4 on SciTools:master.

1 similar comment
@coveralls
Copy link

Coverage Status

Coverage remained the same at 90.653% when pulling 05d1764 on tv3141:consistent_string_representation into bd988a4 on SciTools:master.

@djkirkham djkirkham merged commit b74f3be into SciTools:master Nov 10, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants