-
Notifications
You must be signed in to change notification settings - Fork 113
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
Matching serialise/deserialise behaviours for units #391
Comments
Just supporting julia syntax for round-tripping and the current syntax for pretty printing would be fine by me. It seems straightforward and explicit. Continuing some conversation from #214
Currently If we wanted to support a more general syntax, some research I did a while ago in #214 (comment), seems relevant. Here's some links to existing prior art for unit string representations:
|
This feature is badly wanted! See my comment in #412 (comment). |
It shouldn't be too hard to write if you want to have a go at it. I haven't had time yet as this is more something I want to work theoretically than something I need immediately. |
@sostock @rafaqz @c42f function print(io::IO, x::Units) # swaped old show(io::IO, x::Unitlike)
is_first =true
sep = " "
foreach(sortexp(x)) do y
is_first || print(io, sep)
showrep(io,y)
is_first = false
end
end
show(io::IO, mime::MIME"text/plain", x::Units) = print(io, x) # for REPL
function show(io::IO, x::Units) # for default, parsable
sep = "*"
is_first = true
foreach(sortexp(x)) do y
p = power(y)
pow = p == 1//1 ? "" :
p.den == 1 ? string("^", p.num) :
p == p.num/p.den ? string("^", "(", p.num, "/" , p.den, ")") :
string("^", "(", p.num, "//", p.den, ")")
is_first || print(io, sep)
print(io, prefix(y))
print(io, usym(y))
print(io, pow)
is_first = false
end
end (The content of |
Discussed from #214 (comment) onwards. Making a separate issue so this is easier to find and track.
We should be able to serialise and deserialise units to strings - so e.g. that a table with a units column can be save and loaded again.The way of doing this suggested by @c42f is to make
show
output the raw julia version thatuparse
can read back into units. We would then addprint
methods and some REPL show methods that keep the current behaviour in normal use.A PR for this would involve:
show
methods to work withprint
show(::IO, ::MIME"text/plain", unit)
so the REPL still looks ok.The text was updated successfully, but these errors were encountered: