Skip to content

Commit 5f7cd5e

Browse files
Merge pull request #128 from ChrisRackauckas-Claude/prettytables-extension
Convert PrettyTables to a package extension
2 parents e554c39 + 10fbe3d commit 5f7cd5e

File tree

4 files changed

+67
-7
lines changed

4 files changed

+67
-7
lines changed

Project.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,15 @@ version = "0.3.42"
66
[deps]
77
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
88
ArrayInterface = "4fba245c-0d91-5ea0-9b3e-6abc04ee57a9"
9-
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
109
RuntimeGeneratedFunctions = "7e49a35a-f44a-4d26-94aa-eba1b4ca6b47"
1110
StaticArraysCore = "1e83bf80-4336-4d27-bf5d-d5a4f845583c"
1211

12+
[weakdeps]
13+
PrettyTables = "08abe8d2-0d0c-5749-adfa-8a2ac140af0d"
14+
15+
[extensions]
16+
SymbolicIndexingInterfacePrettyTablesExt = "PrettyTables"
17+
1318
[compat]
1419
Accessors = "0.1.36"
1520
Aqua = "0.8"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
module SymbolicIndexingInterfacePrettyTablesExt
2+
3+
using SymbolicIndexingInterface
4+
using SymbolicIndexingInterface: ParameterIndexingProxy, parameter_symbols, symbolic_type,
5+
ArraySymbolic, getp
6+
using PrettyTables
7+
8+
# Override the fallback implementation with the PrettyTables version
9+
function SymbolicIndexingInterface.show_params(
10+
io::IO, pip::ParameterIndexingProxy; num_rows = 20,
11+
show_all = false, scalarize = true, kwargs...)
12+
params = Any[]
13+
vals = Any[]
14+
for p in parameter_symbols(pip.wrapped)
15+
if symbolic_type(p) === ArraySymbolic() && scalarize
16+
val = getp(pip.wrapped, p)(pip.wrapped)
17+
for (_p, _v) in zip(collect(p), val)
18+
push!(params, _p)
19+
push!(vals, _v)
20+
end
21+
else
22+
push!(params, p)
23+
val = getp(pip.wrapped, p)(pip.wrapped)
24+
push!(vals, val)
25+
end
26+
end
27+
28+
num_shown = if show_all
29+
length(params)
30+
else
31+
if num_rows > length(params)
32+
length(params)
33+
else
34+
num_rows
35+
end
36+
end
37+
38+
pretty_table(io, [params[1:num_shown] vals[1:num_shown]];
39+
header = ["Parameter", "Value"],
40+
kwargs...)
41+
42+
if num_shown < length(params)
43+
println(io,
44+
"$num_shown of $(length(params)) params shown. To show all the parameters, call `show_params(io, ps, show_all = true)`. Adjust the number of rows with the num_rows kwarg. Consult `show_params` docstring for more options.")
45+
end
46+
end
47+
48+
end

src/SymbolicIndexingInterface.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ using RuntimeGeneratedFunctions
44
import StaticArraysCore: MArray, similar_type
55
import ArrayInterface
66
using Accessors: @reset
7-
using PrettyTables # for pretty printing
87

98
RuntimeGeneratedFunctions.init(@__MODULE__)
109

src/parameter_indexing_proxy.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ Method for customizing the table output. Keyword args:
2929
- num_rows
3030
- show_all: whether to show all parameters. Overrides `num_rows`.
3131
- scalarize: whether to scalarize array symbolics in the table output.
32-
- kwargs... are passed to the pretty_table call.
32+
- kwargs... are passed to the pretty_table call (if PrettyTables is loaded).
3333
"""
34-
function show_params(io::IO, pip::ParameterIndexingProxy; num_rows = 20,
34+
show_params(io, pip; kwargs...) = _show_params(io, pip; kwargs...)
35+
36+
# Fallback implementation when PrettyTables is not loaded
37+
function _show_params(io::IO, pip::ParameterIndexingProxy; num_rows = 20,
3538
show_all = false, scalarize = true, kwargs...)
3639
params = Any[]
3740
vals = Any[]
@@ -59,9 +62,14 @@ function show_params(io::IO, pip::ParameterIndexingProxy; num_rows = 20,
5962
end
6063
end
6164

62-
pretty_table(io, [params[1:num_shown] vals[1:num_shown]];
63-
header = ["Parameter", "Value"],
64-
kwargs...)
65+
# Fallback implementation without PrettyTables
66+
println(io, "Parameter Indexing Proxy")
67+
println(io, "=" ^ 50)
68+
println(io, "Parameter | Value")
69+
println(io, "-" ^ 50)
70+
for i in 1:num_shown
71+
println(io, rpad(string(params[i]), 24) * " | " * string(vals[i]))
72+
end
6573

6674
if num_shown < length(params)
6775
println(io,

0 commit comments

Comments
 (0)