Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AlekSIS-Core
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Terraform modules
Monitor
Service Desk
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AlekSIS®
Official
AlekSIS-Core
Commits
aa176305
Verified
Commit
aa176305
authored
4 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
Add iterator interface to record progress from an iterable
parent
f2e29d79
No related branches found
Branches containing commit
No related tags found
Tags containing commit
2 merge requests
!494
Refactor ProgressRecorder for non-optional Celery usage and add doc strings
,
!491
Resolve "Make Celery non-optional"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
aleksis/core/util/celery_progress.py
+25
-1
25 additions, 1 deletion
aleksis/core/util/celery_progress.py
with
25 additions
and
1 deletion
aleksis/core/util/celery_progress.py
+
25
−
1
View file @
aa176305
from
functools
import
wraps
from
numbers
import
Number
from
typing
import
Callable
,
Optional
from
typing
import
Callable
,
Generator
,
Iterable
,
Optional
,
Sequence
,
Union
from
django.contrib
import
messages
...
...
@@ -37,6 +37,8 @@ class ProgressRecorder(AbstractProgressRecorder):
recorder.add_message(messages.SUCCESS,
"
All data were imported successfully.
"
)
You can also use `recorder.iterate` to simplify iterating and counting.
2. Track progress in view:
::
...
...
@@ -67,6 +69,28 @@ class ProgressRecorder(AbstractProgressRecorder):
self
.
_current
=
0
self
.
_total
=
100
def
iterate
(
self
,
data
:
Union
[
Iterable
,
Sequence
],
total
:
Optional
[
int
]
=
None
)
->
Generator
:
"""
Iterate over a sequence or iterable, updating progress on the move.
::
@recorded_task
def do_something(long_list, recorder):
for item in recorder.iterate(long_list):
do_something_with(item)
:param data: A sequence (tuple, list, set,...) or an iterable
:param total: Total number of items, in case data does not support len()
"""
if
total
is
None
and
hasattr
(
data
,
"
__len__
"
):
total
=
len
(
data
)
else
:
raise
TypeError
(
"
No total value passed, and data does not support len()
"
)
for
current
,
item
in
enumerate
(
data
):
self
.
set_progress
(
current
,
total
)
yield
item
def
set_progress
(
self
,
current
:
Optional
[
Number
]
=
None
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment