Skip to content

Commit 2598d5d

Browse files
Convert PrettyTables to a package extension
- Made PrettyTables a weak dependency in Project.toml - Created SymbolicIndexingInterfacePrettyTablesExt extension - Moved PrettyTables-dependent code to the extension - Added fallback implementation for show_params when PrettyTables is not loaded - Extension properly overrides the fallback when PrettyTables is available The package now works without PrettyTables as a hard dependency, but provides enhanced table formatting when PrettyTables is installed. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent e554c39 commit 2598d5d

File tree

5 files changed

+1796
-6
lines changed

5 files changed

+1796
-6
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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
module SymbolicIndexingInterfacePrettyTablesExt
2+
3+
__precompile__(false)
4+
5+
using SymbolicIndexingInterface
6+
using SymbolicIndexingInterface: ParameterIndexingProxy, parameter_symbols, symbolic_type,
7+
ArraySymbolic, getp
8+
using PrettyTables
9+
10+
# Override the fallback implementation with the PrettyTables version
11+
function SymbolicIndexingInterface.show_params(
12+
io::IO, pip::ParameterIndexingProxy; num_rows = 20,
13+
show_all = false, scalarize = true, kwargs...)
14+
params = Any[]
15+
vals = Any[]
16+
for p in parameter_symbols(pip.wrapped)
17+
if symbolic_type(p) === ArraySymbolic() && scalarize
18+
val = getp(pip.wrapped, p)(pip.wrapped)
19+
for (_p, _v) in zip(collect(p), val)
20+
push!(params, _p)
21+
push!(vals, _v)
22+
end
23+
else
24+
push!(params, p)
25+
val = getp(pip.wrapped, p)(pip.wrapped)
26+
push!(vals, val)
27+
end
28+
end
29+
30+
num_shown = if show_all
31+
length(params)
32+
else
33+
if num_rows > length(params)
34+
length(params)
35+
else
36+
num_rows
37+
end
38+
end
39+
40+
pretty_table(io, [params[1:num_shown] vals[1:num_shown]];
41+
header = ["Parameter", "Value"],
42+
kwargs...)
43+
44+
if num_shown < length(params)
45+
println(io,
46+
"$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.")
47+
end
48+
end
49+
50+
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: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ 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 end
35+
36+
# Fallback implementation when PrettyTables is not loaded
3437
function show_params(io::IO, pip::ParameterIndexingProxy; num_rows = 20,
3538
show_all = false, scalarize = true, kwargs...)
3639
params = 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)