-
Notifications
You must be signed in to change notification settings - Fork 8
Hh/general numbers #52
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
base: master
Are you sure you want to change the base?
Changes from 1 commit
d2dafff
3c88392
ed836bd
4a958ee
7d43657
874d042
cf67f99
4479a8a
d07f241
314a4fb
38e386a
82b9b43
befc01c
ab0bdac
7598556
0233e95
9e1d553
0eb2a8c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,7 +29,7 @@ function DefaultSpec(c::AbstractChar, syms...; kwargs...) | |
end | ||
end | ||
|
||
const DEFAULT_FORMATTERS = Dict{DataType, DefaultSpec}() | ||
const DEFAULT_FORMATTERS = Dict{Type{T} where T, DefaultSpec}() | ||
|
||
# adds a new default formatter for this type | ||
default_spec!(::Type{T}, c::AbstractChar) where {T} = | ||
|
@@ -79,6 +79,9 @@ default_spec(::Type{<:AbstractString}) = DEFAULT_FORMATTERS[AbstractString] | |
default_spec(::Type{<:AbstractChar}) = DEFAULT_FORMATTERS[AbstractChar] | ||
default_spec(::Type{<:AbstractIrrational}) = DEFAULT_FORMATTERS[AbstractIrrational] | ||
default_spec(::Type{<:Number}) = DEFAULT_FORMATTERS[Number] | ||
default_spec(::Complex{T} where T<:Integer) = DEFAULT_FORMATTERS[Complex{T} where T<:Integer] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
So I accepted that
But I consider myself still a beginner in type programming, so please fell free to jump in, if you have a better way of putting it.
So |
||
default_spec(::Complex{T} where T<:AbstractFloat) = DEFAULT_FORMATTERS[Complex{T} where T<:AbstractFloat] | ||
default_spec(::Complex{T} where T<:Rational) = DEFAULT_FORMATTERS[Complex{T} where T<:Rational] | ||
|
||
default_spec(::Type{T}) where {T} = | ||
get(DEFAULT_FORMATTERS, T) do | ||
|
@@ -189,7 +192,7 @@ function fmt end | |
# TODO: do more caching to optimize repeated calls | ||
|
||
# creates a new FormatSpec by overriding the defaults and passes it to pyfmt | ||
# note: adding kwargs is only appropriate for one-off formatting. | ||
# note: adding kwargs is only appropriate for one-off formatting. | ||
# normally it will be much faster to change the fmt_default formatting as needed | ||
function fmt(x; kwargs...) | ||
fspec = fmt_default(x) | ||
|
@@ -220,9 +223,12 @@ end | |
for (t, c) in [(Integer,'d'), | ||
(AbstractFloat,'f'), | ||
(AbstractChar,'c'), | ||
(AbstractString,'s')] | ||
(AbstractString,'s'), | ||
(Complex{T} where T<:Integer, 'd' ), | ||
hhaensel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(Complex{T} where T<:AbstractFloat, 'f' )] | ||
default_spec!(t, c) | ||
end | ||
|
||
default_spec!(Number, 's', :right) | ||
default_spec!(AbstractIrrational, 's', :right) | ||
default_spec!(Complex{T} where T<:Rational, 's', :right) | ||
hhaensel marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# core formatting functions | ||
export fmt_Number | ||
|
||
### auxiliary functions | ||
|
||
|
@@ -262,3 +263,40 @@ function _pfmt_specialf(out::IO, fs::FormatSpec, x::AbstractFloat) | |
end | ||
end | ||
|
||
function _pfmt_Number_f(out::IO, fs::FormatSpec, x::Number, _pf::Function) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These still have the issues that I mentioned before. |
||
fsi = FormatSpec(fs, width = -1) | ||
f = x::AbstractFloat->begin | ||
io = IOBuffer() | ||
_pf(io, fsi, x) | ||
String(take!(io)) | ||
end | ||
s = fmt_Number(x, f) | ||
_pfmt_s(out, fs, s) | ||
end | ||
|
||
function _pfmt_Number_i(out::IO, fs::FormatSpec, x::Number, op::Op, _pf::Function) where {Op} | ||
fsi = FormatSpec(fs, width = -1) | ||
f = x::Integer->begin | ||
io = IOBuffer() | ||
_pf(io, fsi, x, op) | ||
String(take!(io)) | ||
end | ||
s = fmt_Number(x, f) | ||
_pfmt_s(out, fs, s) | ||
end | ||
|
||
function _pfmt_i(out::IO, fs::FormatSpec, x::Number, op::Op) where {Op} | ||
_pfmt_Number_i(out, fs, x, op, _pfmt_i) | ||
end | ||
|
||
function _pfmt_f(out::IO, fs::FormatSpec, x::Number) | ||
_pfmt_Number_f(out, fs, x, _pfmt_f) | ||
end | ||
|
||
function _pfmt_e(out::IO, fs::FormatSpec, x::Number) | ||
_pfmt_Number_f(out, fs, x, _pfmt_e) | ||
end | ||
|
||
function fmt_Number(x::Complex, f::Function) | ||
s = f(real(x)) * (imag(x) >= 0 ? " + " : " - ") * f(abs(imag(x))) * "im" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -164,19 +164,30 @@ _srepr(x) = repr(x) | |
_srepr(x::AbstractString) = x | ||
_srepr(x::AbstractChar) = string(x) | ||
_srepr(x::Enum) = string(x) | ||
@static if VERSION < v"1.2.0-DEV" | ||
_srepr(x::Irrational{sym}) where {sym} = string(sym) | ||
end | ||
|
||
function printfmt(io::IO, fs::FormatSpec, x) | ||
cls = fs.cls | ||
ty = fs.typ | ||
if cls == 'i' | ||
ix = Integer(x) | ||
ix = x | ||
hhaensel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try | ||
ix = Integer(x) | ||
catch | ||
end | ||
ty == 'd' || ty == 'n' ? _pfmt_i(io, fs, ix, _Dec()) : | ||
ty == 'x' ? _pfmt_i(io, fs, ix, _Hex()) : | ||
ty == 'X' ? _pfmt_i(io, fs, ix, _HEX()) : | ||
ty == 'o' ? _pfmt_i(io, fs, ix, _Oct()) : | ||
_pfmt_i(io, fs, ix, _Bin()) | ||
elseif cls == 'f' | ||
fx = float(x) | ||
fx = x | ||
hhaensel marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above - if it can't get converted to something that is |
||
fx = float(x) | ||
catch | ||
end | ||
if isfinite(fx) | ||
ty == 'f' || ty == 'F' ? _pfmt_f(io, fs, fx) : | ||
ty == 'e' || ty == 'E' ? _pfmt_e(io, fs, fx) : | ||
|
Uh oh!
There was an error while loading. Please reload this page.