Skip to content

Commit

Permalink
Merge pull request #18 from nickrobinson251/npr/shows
Browse files Browse the repository at this point in the history
Add pretty printing of a `Network` and its fields
  • Loading branch information
nickrobinson251 authored Sep 27, 2021
2 parents a6efefc + b59eeff commit a57f2fe
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct CaseID <: Tables.AbstractRow
sbase::Float64
end

CaseID() = CaseID(0, 100.0)
CaseID(; ic=0, sbase=100.0) = CaseID(ic, sbase)

Tables.columnnames(::CaseID) = fieldnames(CaseID)
Tables.getcolumn(cid::CaseID, i::Int) = getfield(cid, i)
Expand Down Expand Up @@ -407,3 +407,38 @@ struct Network
"Non-transformer Branch records."
branches::Branches
end

###
### show
###

function Base.show(io::IO, mime::MIME"text/plain", network::T) where {T <: Network}
nfields = fieldcount(T)
print(io, "$T with $nfields data categories:\n ")
show(io, mime, network.caseid)
io_compact = IOContext(io, :compact => true)
foreach(2:nfields) do i
print(io, "\n ")
show(io_compact, mime, getfield(network, i))
end
end

Base.show(io::IO, x::CaseID) = print(io, "CaseID", NamedTuple(x)) # parseable repr
Base.show(io::IO, ::MIME"text/plain", x::CaseID) = print(io, "CaseID: ", NamedTuple(x))

function Base.show(io::IO, mime::MIME"text/plain", x::R) where {R <: Records}
print(io, "$R with $(Tables.rowcount(x)) records")
if !get(io, :compact, false)::Bool
print(io, ":\n")
# show identifiers, e.g. bus numbers, but limit them as there could be very many.
_print_identifiers(IOContext(io, :limit => true), x)
print(io, "\n")
# Always show all columns, as it's helpful and there are never 100s.
show(IOContext(io, :limit => false), mime, Tables.schema(R))
end
end

# default to showing the bus numbers, as all records have this column.
_print_identifiers(io, x::Records) = print(io, " i : ", x.i)
# show both ends of branches
_print_identifiers(io, x::Branches) = print(io, " i => j : ", Pair.(x.i, x.j))
31 changes: 31 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,37 @@ using Test
@test NamedTuple(caseid) == (ic=0, sbase=100.0)
end

@testset "show" begin
net = parse_network("testfiles/synthetic_data_v30.raw")

# CaseID should have a parseable `repr`; `AbstractRows` don't get this for free.
@test repr(net.caseid) == "CaseID(ic = 0, sbase = 100.0)"
@test eval(Meta.parse(repr(net.caseid))) == CaseID(0, 100.0)

mime = MIME("text/plain")
context = :compact => true
@test repr(mime, net) == strip(
"""
Network with 5 data categories:
$(sprint(show, mime, net.caseid))
$(sprint(show, mime, net.buses; context))
$(sprint(show, mime, net.loads; context))
$(sprint(show, mime, net.generators; context))
$(sprint(show, mime, net.branches; context))
"""
)
@test repr(mime, net.caseid) == "CaseID: (ic = 0, sbase = 100.0)"

@test repr(mime, net.buses; context=(:compact => true)) == "Buses with 3 records"
@test repr(mime, net.buses) == strip("""
Buses with 3 records:
i : [111, 112, 113]
$(sprint(show, mime, Tables.schema(Buses); context=(:limit => true)))
"""
)
@test contains(repr(mime, net.branches), "i => j") # custom branches "identifier"
end

@testset "v30 file" begin
net1 = parse_network("testfiles/synthetic_data_v30.raw")
@test net1 isa Network
Expand Down

0 comments on commit a57f2fe

Please sign in to comment.