From a43a235fea9c87130a49f9a99fbf952202e355fc Mon Sep 17 00:00:00 2001 From: Eric Hanson <5846501+ericphanson@users.noreply.github.com> Date: Tue, 22 Jun 2021 15:41:10 +0200 Subject: [PATCH] add metadata to `show` method (#217) --- src/Arrow.jl | 1 + src/show.jl | 37 +++++++++++++++++++++++++++++++++++++ test/runtests.jl | 30 ++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 src/show.jl diff --git a/src/Arrow.jl b/src/Arrow.jl index 4ca42ba..43f3203 100644 --- a/src/Arrow.jl +++ b/src/Arrow.jl @@ -93,6 +93,7 @@ include("eltypes.jl") include("table.jl") include("write.jl") include("append.jl") +include("show.jl") const LZ4_FRAME_COMPRESSOR = LZ4FrameCompressor[] const ZSTD_COMPRESSOR = ZstdCompressor[] diff --git a/src/show.jl b/src/show.jl new file mode 100644 index 0000000..f0ed42f --- /dev/null +++ b/src/show.jl @@ -0,0 +1,37 @@ +# 2-arg show: show schema and list # of metadata entries if non-zero +function Base.show(io::IO, table::Table) + ncols = length(Tables.columnnames(table)) + print(io, "$(typeof(table)) with $(Tables.rowcount(table)) rows, $(ncols) columns,") + meta = getmetadata(table) + if meta !== nothing && !isempty(meta) + print(io, " ", length(meta), " metadata entries,") + end + sch = Tables.schema(table) + print(io, " and schema:\n") + show(IOContext(io, :print_schema_header => false), sch) + return nothing +end + +# 3-arg show: show schema and show metadata entries adaptively according to `displaysize` +function Base.show(io::IO, mime::MIME"text/plain", table::Table) + limit = get(io, :limit, false)::Bool + display_rows, display_cols = displaysize(io) + ncols = length(Tables.columnnames(table)) + meta = getmetadata(table) + if meta !== nothing + display_rows -= 1 # decrement for metadata header line + display_rows -= min(length(meta), 2) # decrement so we can show at least 2 lines of metadata + end + print(io, "$(typeof(table)) with $(Tables.rowcount(table)) rows, $(ncols) columns, and ") + sch = Tables.schema(table) + print(io, "schema:\n") + schema_context = IOContext(io, :print_schema_header => false, :displaysize => (max(display_rows, 3), display_cols)) + schema_str = sprint(show, mime, sch; context=schema_context) + print(io, schema_str) + display_rows -= (count("\n", schema_str) + 1) # decrement for number of lines printed + if meta !== nothing + print(io, "\n\nwith metadata given by a ") + show(IOContext(io, :displaysize => (max(display_rows, 5), display_cols)), mime, meta) + end + return nothing +end diff --git a/test/runtests.jl b/test/runtests.jl index f5e3a4f..28818dd 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -15,6 +15,7 @@ # limitations under the License. using Test, Arrow, Tables, Dates, PooledArrays, TimeZones, UUIDs, CategoricalArrays, DataAPI +using Random: randstring include(joinpath(dirname(pathof(Arrow)), "ArrowTypes/test/tests.jl")) include(joinpath(dirname(pathof(Arrow)), "../test/testtables.jl")) @@ -355,6 +356,35 @@ tbl = Arrow.Table(Arrow.tobuffer(t)) @test eltype(tbl.col1) == VersionNumber end +@testset "`show`" begin + table = (; a = 1:5, b = fill(1.0, 5)) + arrow_table = Arrow.Table(Arrow.tobuffer(table)) + # 2 and 3-arg show with no metadata + for outer str in (sprint(show, arrow_table), + sprint(show, MIME"text/plain"(), arrow_table)) + @test length(str) < 100 + @test occursin("5 rows", str) + @test occursin("2 columns", str) + @test occursin("Int", str) + @test occursin("Float64", str) + @test !occursin("metadata entries", str) + end + + # 2-arg show with metadata + big_dict = Dict((randstring(rand(5:10)) => randstring(rand(1:3)) for _ = 1:100)) + Arrow.setmetadata!(arrow_table, big_dict) + str2 = sprint(show, arrow_table) + @test length(str2) > length(str) + @test length(str2) < 200 + @test occursin("metadata entries", str2) + + # 3-arg show with metadata + str3 = sprint(show, MIME"text/plain"(), arrow_table; context = IOContext(IOBuffer(), :displaysize => (24, 100), :limit=>true)) + @test length(str3) < 1000 + # some but not too many `=>`'s for printing the metadata + @test 5 < length(collect(eachmatch(r"=>", str3))) < 20 + +end end # @testset "misc" end