Skip to content

Commit

Permalink
add full iteration interface to ProgressWrapper (#299)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcMush authored Feb 15, 2024
1 parent 80f93fc commit d50a39a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/ProgressMeter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -744,8 +744,14 @@ struct ProgressWrapper{T}
meter::Progress
end

Base.IteratorSize(wrap::ProgressWrapper) = Base.IteratorSize(wrap.obj)
Base.axes(wrap::ProgressWrapper, dim...) = Base.axes(wrap.obj, dim...)
Base.size(wrap::ProgressWrapper, dim...) = Base.size(wrap.obj, dim...)
Base.length(wrap::ProgressWrapper) = Base.length(wrap.obj)

Base.IteratorEltype(wrap::ProgressWrapper) = Base.IteratorEltype(wrap.obj)
Base.eltype(wrap::ProgressWrapper) = Base.eltype(wrap.obj)

function Base.iterate(wrap::ProgressWrapper, state...)
ir = iterate(wrap.obj, state...)

Expand All @@ -755,7 +761,7 @@ function Base.iterate(wrap::ProgressWrapper, state...)
next!(wrap.meter)
end

ir
return ir
end

"""
Expand Down
13 changes: 13 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,16 @@ prog.check_iterations = 999
t = time()
prog.tlast = t
@test ProgressMeter.calc_check_iterations(prog, t) == 999

# Test ProgressWrapper
A = rand(3,5,7,11)
prog = Progress(length(A))
wrap = ProgressMeter.ProgressWrapper(A, prog)

@test Base.IteratorSize(wrap) == Base.IteratorSize(A)
@test Base.IteratorEltype(wrap) == Base.IteratorEltype(A)
@test axes(wrap) == axes(A)
@test size(wrap) == size(A)
@test length(wrap) == length(A)
@test eltype(wrap) == eltype(A)
@test collect(wrap) == collect(A)
14 changes: 12 additions & 2 deletions test/test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,15 @@ function testfunc7a(n, dt, tsleep)
@test s == [z for z in 1:n, y in 1:n]
end

function testfunc7b(A, dt, tsleep)
s = ProgressMeter.@showprogress dt=dt desc="Calculating..." [(sleep(tsleep); z) for z in A]
@test s == A
end

println("Testing @showprogress macro on comprehension")
testfunc7(25, 0.1, 0.1)
testfunc7a(5, 0.1, 0.1)

testfunc7b(rand(3,4), 0.1, 0.1) #290

function testfunc8(n, dt, tsleep)
ProgressMeter.@showprogress dt=dt for i in 1:n
Expand Down Expand Up @@ -161,10 +166,15 @@ function testfunc9a(n, dt, tsleep)
@test s == [z for z in 1:n, y in 1:n]
end

function testfunc9b(A, dt, tsleep)
s = ProgressMeter.@showprogress dt=dt desc="Calculating..." Float64[(sleep(tsleep); z) for z in A]
@test s == Float64[A;]
end

println("Testing @showprogress macro on typed comprehension")
testfunc9(100, 0.1, 0.01)
testfunc9a(10, 0.1, 0.01)

testfunc9b(rand(Float32,3,4), 0.1, 0.01) #290

function testfunc10(n, k, dt, tsleep)
p = ProgressMeter.Progress(n; dt=dt)
Expand Down

0 comments on commit d50a39a

Please sign in to comment.