Skip to content

Commit 0fb384d

Browse files
authored
Merge pull request #379 from riccardomurri/maxdigits
Allow setting max print width of IDs via `Convex.MAXDIGITS`.
2 parents 4f70b9b + 701bfa4 commit 0fb384d

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

docs/src/advanced.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,11 @@ only the first `MAXWIDTH` of the constraints will be printed. Vertical dots,
143143
"⋮", will be printed indicating that some constraints were omitted in the
144144
printing.
145145

146+
A related setting is [`Convex.MAXDIGITS`](@ref), which controls
147+
printing the internal IDs of atoms: if the string representation of an
148+
ID is longer than double the value of `MAXDIGITS`, then it is
149+
shortened by printing only the first and last `MAXDIGITS` characters.
150+
146151
The AbstractTrees methods can also be used to analyze the structure
147152
of a Convex.jl problem. For example,
148153

@@ -197,4 +202,5 @@ Reference
197202
```@docs
198203
Convex.MAXDEPTH
199204
Convex.MAXWIDTH
205+
Convex.MAXDIGITS
200206
```

src/Convex.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,23 @@ Controls width of tree printing globally for Convex.jl; defaults to 15. Set via
6666
"""
6767
const MAXWIDTH= Ref(15)
6868

69+
"""
70+
MAXDIGITS
71+
72+
When priting IDs of variables, only show the initial and final digits
73+
if the full ID has more than double the number of digits specified
74+
here. So, with the default setting MAXDIGITS=3, any ID longer than 7
75+
digits would be shortened; for example, ID `14656210999710729289`
76+
would be printed as `146…289`.
77+
78+
This setting controls tree printing globally for Convex.jl; defaults to 3.
79+
80+
Set via:
81+
82+
Convex.MAXDIGITS[] = 3
83+
"""
84+
const MAXDIGITS= Ref(3)
85+
6986
### modeling framework
7087
include("dcp.jl")
7188
include("expressions.jl")

src/utilities/show.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@ julia> Convex.show_id(stdout, x)
1616
id: 163…906
1717
```
1818
"""
19-
show_id(io::IO, x::Union{AbstractExpr, Constraint}; digits = 3) = print(io, show_id(x; digits=digits))
19+
show_id(io::IO, x::Union{AbstractExpr, Constraint}; digits = MAXDIGITS[]) = print(io, show_id(x; digits=digits))
2020

21-
function show_id(x::Union{AbstractExpr, Constraint}; digits = 3)
21+
function show_id(x::Union{AbstractExpr, Constraint}; digits = MAXDIGITS[])
2222
hash_str = string(x.id_hash)
23-
return "id: " * first(hash_str, digits) * "" * last(hash_str, digits)
23+
if length(hash_str) > (2*digits + 1);
24+
return "id: " * first(hash_str, digits) * "" * last(hash_str, digits)
25+
else
26+
return "id: " * hash_str
27+
end
2428
end
2529

2630
"""

test/test_utilities.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,21 @@ using Convex: AbstractExpr, ConicObj
137137
termination status: OPTIMAL
138138
primal status: FEASIBLE_POINT
139139
dual status: FEASIBLE_POINT"""
140+
141+
# test small `MAXDIGITS`
142+
x = Variable()
143+
old_maxdigits = Convex.MAXDIGITS[]
144+
Convex.MAXDIGITS[] = 2
145+
@test length(Convex.show_id(x)) == length("id: ") + 5
146+
Convex.MAXDIGITS[] = old_maxdigits
147+
148+
# test large `MAXDIGITS`
149+
x = Variable()
150+
old_maxdigits = Convex.MAXDIGITS[]
151+
Convex.MAXDIGITS[] = 100
152+
@test length(Convex.show_id(x)) == length("id: ") + length(string(x.id_hash))
153+
Convex.MAXDIGITS[] = old_maxdigits
154+
140155
end
141156

142157
@testset "ConicObj with type $T" for T = [UInt32, UInt64]

0 commit comments

Comments
 (0)