tshort / Eyeball.jl

Object and type viewer for Julia
Other
142 stars 4 forks source link

Eyeball.jl

Object and type viewer for Julia

Build Status

Eyeball exports one main tool to browse Julia objects and types.

eye(object)
eye(object, depth)
eye(object = Main, depth = 10; interactive = true, all = false)

depth controls the depth of folding. all expands options.

The user can interactively browse the object tree using the following keys:

Notes:

Examples

Explore an object:

a = (h=rand(5), e=:(5sin(pi*t)), f=sin, c=33im, set=Set((:a, 9, rand(1:5, 8))), b=(c=1,d=9,e=(i=9,f=0)), x=9 => 99:109, d=Dict(1=>2, 3=>4), ds=Dict(:s=>4,:t=>7), dm=Dict(1=>9, "x"=>8))
eye(a)
julia> eye(a)
[f] fields [d] docs [e] expand [m/M] methodswith [o] open [r] tree [s] show [t] typeof [z] summarize [q] quit
 >   : NamedTuple{(:h, :e, :f, :c, :set, :b, :x, :d, :ds, :dm), Tuple{Vector{Float64}, Expr, typeof(sin), Complex{Int64}   +  h: Vector{Float64} (5,) 40 [0.589398, 0.761107, 0.963494, 0.835393, 0.488657]
      e: Expr  :(5 * sin(pi * t))
       head: Symbol  :call
       args: Vector{Any} (3,) 24 Any[:*, 5, :(sin(pi * t))]
        1: Symbol  :*
        2: Int64  5
        3: Expr  :(sin(pi * t))
         head: Symbol  :call
         args: Vector{Any} (2,) 16 Any[:sin, :(pi * t)]
          1: Symbol  :sin
          2: Expr  :(pi * t)
           head: Symbol  :call
           args: Vector{Any} (3,) 24 Any[:*, :pi, :t]
            1: Symbol  :*
            2: Symbol  :pi
            3: Symbol  :t
      f: typeof(sin)  sin
   +  c: Complex{Int64}  0+33im
      set: Set{Any}  Set(Any[:a, 9, [2, 3, 2, 5, 5, 1, 4, 5]])
       : Symbol  :a
       : Int64  9
   +   : Vector{Int64} (8,) 64 [2, 3, 2, 5, 5, 1, 4, 5]
      b: NamedTuple{(:c, :d, :e), Tuple{Int64, Int64, NamedTuple{(:i, :f), Tuple{Int64, Int64}}}}  (c = 1, d = 9, e = (i       c: Int64  1
       d: Int64  9
       e: NamedTuple{(:i, :f), Tuple{Int64, Int64}}  (i = 9, f = 0)
        i: Int64  9
        f: Int64  0
   +  x: Pair{Int64, UnitRange{Int64}}  9=>99:109
      d: Dict{Int64, Int64}  Dict(3=>4, 1=>2)
       3: Int64  4
       1: Int64  2
      ds: Dict{Symbol, Int64}  Dict(:s=>4, :t=>7)
       s: Int64  4
v      t: Int64  7

Explore a Module:

eye()      # equivalent to `eye(Main)`
Expand results ```jl julia> eye() [f] fields [d] docs [e] expand [m/M] methodswith [o] open [r] tree [s] show [t] typeof [z] summarize [q] quit > : Module Main Base: Module Base Core: Module Core InteractiveUtils: Module InteractiveUtils Main: Module Main a: NamedTuple{(:h, :e, :f, :c, :set, :b, :x, :d, :ds, :dm), Tuple{Vector{Float64}, Expr, typeof(sin), Complex{Int6 + h: Vector{Float64} (5,) 40 [0.589398, 0.761107, 0.963494, 0.835393, 0.488657] e: Expr :(5 * sin(pi * t)) head: Symbol :call args: Vector{Any} (3,) 24 Any[:*, 5, :(sin(pi * t))] 1: Symbol :* 2: Int64 5 3: Expr :(sin(pi * t)) head: Symbol :call args: Vector{Any} (2,) 16 Any[:sin, :(pi * t)] 1: Symbol :sin 2: Expr :(pi * t) head: Symbol :call args: Vector{Any} (3,) 24 Any[:*, :pi, :t] 1: Symbol :* 2: Symbol :pi 3: Symbol :t f: typeof(sin) sin + c: Complex{Int64} 0+33im set: Set{Any} Set(Any[:a, 9, [2, 3, 2, 5, 5, 1, 4, 5]]) : Symbol :a : Int64 9 + : Vector{Int64} (8,) 64 [2, 3, 2, 5, 5, 1, 4, 5] b: NamedTuple{(:c, :d, :e), Tuple{Int64, Int64, NamedTuple{(:i, :f), Tuple{Int64, Int64}}}} (c = 1, d = 9, e = ( c: Int64 1 d: Int64 9 e: NamedTuple{(:i, :f), Tuple{Int64, Int64}} (i = 9, f = 0) i: Int64 9 f: Int64 0 + x: Pair{Int64, UnitRange{Int64}} 9=>99:109 v d: Dict{Int64, Int64} Dict(3=>4, 1=>2) ```

Explore a type tree:

eye(Number)
Expand results ```jl julia> eye(Number) [f] fields [d] docs [e] expand [m/M] methodswith [o] open [r] tree [s] show [t] typeof [z] summarize [q] quit > : DataType Number + : UnionAll Complex : DataType Real : DataType AbstractFloat + : DataType BigFloat : DataType Float16 : DataType Float32 : DataType Float64 : DataType AbstractIrrational + : UnionAll Irrational : DataType Integer : DataType Bool : DataType Signed + : DataType BigInt : DataType Int128 : DataType Int16 : DataType Int32 : DataType Int64 : DataType Int8 : DataType Unsigned : DataType UInt128 : DataType UInt16 : DataType UInt32 : DataType UInt64 : DataType UInt8 + : UnionAll Rational ```

Use noninteractively

With the keyword argument interactive set to false, eye returns the tree as a FoldingTrees.Node. That is automatically displayed via show or by using FoldingTrees.print_tree.

eye(Number, interactive = false)
Expand results ```jl julia> eye(Number, interactive = false) DataType ├─ + : UnionAll Complex └─ : DataType Real ├─ : DataType AbstractFloat │ ├─ + : DataType BigFloat │ ├─ : DataType Float16 │ ├─ : DataType Float32 │ └─ : DataType Float64 ├─ : DataType AbstractIrrational │ └─ + : UnionAll Irrational ├─ : DataType Integer │ ├─ : DataType Bool │ ├─ : DataType Signed │ │ ├─ + : DataType BigInt │ │ ├─ : DataType Int128 │ │ ├─ : DataType Int16 │ │ ├─ : DataType Int32 │ │ ├─ : DataType Int64 │ │ └─ : DataType Int8 │ └─ : DataType Unsigned │ ├─ : DataType UInt128 │ ├─ : DataType UInt16 │ ├─ : DataType UInt32 │ ├─ : DataType UInt64 │ └─ : DataType UInt8 └─ + : UnionAll Rational ```

Summarize

Show a summary of arrays in a named tuple (also useful for DataFrames).

d = (a = rand(100), b = rand(100:200, 100), c = 4rand(Float32, 100))
eye(d)    # then hit `z` to summarize
Expand results ```jl julia> eye(d) [f] fields [d] docs [e] expand [m/M] methodswith [o] open [r] tree [s] show [t] typeof [z] summarize [q] quit > : NamedTuple{(:a, :b, :c), Tuple{Vector{Float64}, Vector{Int64}, Vector{Float32}}} (a = [0.721857, 0.174408, 0.897 > + a: Vector{Float64} (100,) 800 x̄=0.535717, q=[0.0372074, 0.305533, 0.556568, 0.770658, 0.979569] > + b: Vector{Int64} (100,) 800 x̄=145.5, q=[100.0, 117.0, 145.5, 170.0, 200.0] > + c: Vector{Float32} (100,) 400 x̄=1.90419, q=[0.0898504, 1.09705, 1.9039, 2.68442, 3.93898] ```

API

By default, eye shows the properties of an object. That can be customized for different objects. For example, Dicts are shown with the key then the value, and abstract types are shown with subtypes. To customize what's shown for SomeType, define Eyeball.getobjects(x::SomeType). This method should return an iterator that returns a key and a value describing each of the child objects to be shown.

The display of objects can also be customized with the following boolean methods:

Eyeball.shouldrecurse(x)   
Eyeball.foldobject(x)   

shouldrecurse controls whether eye recurses into object x. This defaults to true. For overly large or complex objects, it helps to return false. That's done internally for Modules, Methods, and a few other types. foldobject controls whether eye automatically folds the object. This is useful for types where the components usually don't need to be shown. This defaults to false.

To add additional "summarize" options, define Base.show(io::IO, x::Eyeball.Summarize{T}) for type T.

Under the Hood

Eyeball uses FoldingTrees for display of trees and interactivity. This fork was extended to support customized key presses. TerminalPager is used for paging.

The code was adapted from InteractiveErrors.jl and Cthulhu.jl.