Skip to content

Instantly share code, notes, and snippets.

@tshort
Created May 27, 2012 15:39
Show Gist options
  • Save tshort/2814822 to your computer and use it in GitHub Desktop.
Save tshort/2814822 to your computer and use it in GitHub Desktop.
str for Julia
function str(io::IOStream, x, n::Int, indent)
T = typeof(x)
print(io, T, " ")
if isa(T, CompositeKind)
println(io)
if n > 0
for field in T.names
print(io, indent, " ", field, ": ")
str(io, getfield(x, field), n - 1, strcat(indent, " "))
end
end
else
println(io, x)
end
end
function str(io::IOStream, x::Array{Any}, n::Int, indent)
println("Array($(eltype(x)),$(size(x)))")
for i in 1:min(10, length(x))
print(io, indent, " ", i, ": ")
str(io, x[i], n - 1, strcat(indent, " "))
end
end
str(io::IOStream, x::AbstractKind, n::Int, indent) = println(io, x)
str(io::IOStream, x::Symbol, n::Int, indent) = println(io, x)
str(io::IOStream, x::Function, n::Int, indent) = println(io, x)
str(io::IOStream, x::Array, n::Int, indent) = println(io, "Array($(eltype(x)),$(size(x)))", " ", x[1:min(4,length(x))])
str(io::IOStream, x) = str(io, x, 5, "") # default is 5 levels
str(io::IOStream, x, n::Int) = str(io, x, n, "")
str(args...) = str(OUTPUT_STREAM::IOStream, args...)
str(io::IOStream, x::String, n::Int, indent) = println(io, typeof(x), " \"", x, "\"")
function str(io::IOStream, x::Dict, n::Int, indent)
println("Dict", "[", length(x), "]")
i = 1
for (k,v) in x
print(io, indent, " ", k, ": ")
str(io, v, n - 1, strcat(indent, " "))
if i > 10
break
end
i += 1
end
end
function istr(io::IOStream, x, n::Int, indent)
T = typeof(x)
print(io, T, " ")
if isa(T, CompositeKind)
println(io)
if n > 0
for field in T.names
print(io, indent, " ", field, ": ")
istr(io, getfield(x, field), n - 1, strcat(indent, " "))
end
end
else
println(io, x)
end
end
function istr(io::IOStream, x::Array{Any}, n::Int, indent)
println("Array($(eltype(x)),$(size(x)))")
for i in 1:min(10, length(x))
print(io, indent, " ", i, ": ")
istr(io, x[i], n - 1, strcat(indent, " "))
end
end
istr(io::IOStream, x::AbstractKind, n::Int, indent) = println(io, x)
istr(io::IOStream, x::Symbol, n::Int, indent) = println(io, x)
istr(io::IOStream, x::Function, n::Int, indent) = println(io, x)
istr(io::IOStream, x::Array, n::Int, indent) = println(io, "Array($(eltype(x)),$(size(x)))", " ", x[1:min(4,length(x))])
istr(io::IOStream, x) = istr(io, x, 5, "") # default is 5 levels
istr(io::IOStream, x, n::Int) = istr(io, x, n, "")
istr(args...) = istr(OUTPUT_STREAM::IOStream, args...)
julia> str(:(a+b*c))
Expr
head: call
args: Array(Any,(3,))
1: +
2: a
3: Expr
head: call
args: Array(Any,(3,))
1: *
2: b
3: c
typ: Any
typ: Any
julia> # Here's a Dict:
julia> str({:a => "hi", :b => 123, :c => [1,2,3], :d => {1, "asd", {:a => 1, :b => "bye"}}})
Dict[4]
c: Array(Int64,(3,)) [1, 2, 3]
a: ASCIIString "hi"
b: Int64 123
d: Array(Any,(3,))
1: Int64 1
2: ASCIIString "asd"
3: Dict[2]
a: Int64 1
b: ASCIIString "bye"
julia> # The same, but showing the internal structure with istr:
julia> istr({:a => "hi", :b => 123, :c => [1,2,3], :d => {1, "asd", {:a => 1, :b => "bye"}}})
Dict{Symbol,Any}
keys: Array(Any,(16,))
1: c
2: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
3: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
4: a
5: b
6: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
7: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
8: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
9: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
10: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
vals: Array(Any,(16,))
1: Array(Int64,(3,)) [1, 2, 3]
2: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
3: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
4: ASCIIString
data: Array(Uint8,(2,)) [0x68, 0x69]
5: Int64 123
6: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
7: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
8: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
9: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
10: __c782dbf1cf4d6a2e5e3865d7e95634f2e09b5902__
ndel: Int64 0
deleter: identity
julia>
@tshort
Copy link
Author

tshort commented May 27, 2012

This is a version of R's str function for Julia. It shows the structure of an object. Two versions are included. One is str that's designed to show the "user-facing" structure. The other is istr for "internal structure". It is meant to show the raw object contents. For example, str and istr show Dict's differently. Also, for arrays, I provide a method to display each cell of an Array{Any}, because that's often used as a generic container. Other arrays are just shown on one line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment