Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Daniel Scheffler
py_tools_ds
Commits
c6a8b213
Commit
c6a8b213
authored
Nov 24, 2021
by
Daniel Scheffler
Browse files
Fixed NoneTypeError in ProgressBar in case sys.stderr is None. Added tests for processing module.
parent
f9510154
Pipeline
#35170
passed with stage
in 3 minutes and 36 seconds
Changes
3
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
py_tools_ds/processing/progress_mon.py
View file @
c6a8b213
...
...
@@ -99,6 +99,8 @@ class ProgressBar(object):
self
.
use_as_cb
=
use_as_callback
self
.
out
=
out
self
.
_percdone
=
list
(
range
(
10
,
110
,
10
))
def
print_progress
(
self
,
percent
):
"""Print progress.
...
...
@@ -108,11 +110,14 @@ class ProgressBar(object):
:return:
"""
if
self
.
Timer
.
timed_out
:
self
.
out
.
flush
()
if
self
.
out
is
not
None
:
self
.
out
.
flush
()
if
self
.
use_as_cb
:
# raise a KeyBoardInterrupt instead of a TimeOutError
# as this is catchable by gdal.GetLastException()
raise
KeyboardInterrupt
()
else
:
raise
TimeoutError
(
f
'No progress for
{
self
.
timeout
}
seconds.'
)
formatStr
=
"{0:."
+
str
(
self
.
decimals
)
+
"f}"
percents
=
formatStr
.
format
(
percent
)
...
...
@@ -120,17 +125,30 @@ class ProgressBar(object):
# bar = '█' * filledLength + '-' * (barLength - filledLength) # this is not compatible to shell console
bar
=
'='
*
filledLength
+
'-'
*
(
self
.
barLength
-
filledLength
)
# reset the cursor to the beginning of the line and allows to write over what was previously on the line
self
.
out
.
write
(
'
\r
'
)
if
self
.
out
is
not
None
:
# reset the cursor to the beginning of the line and allows to write over what was previously on the line
self
.
out
.
write
(
'
\r
'
)
# [%s/%s] numberDone
suffix
=
self
.
suffix
if
not
self
.
show_elapsed
else
'%s => %s'
%
(
self
.
suffix
,
self
.
Timer
.
elapsed
)
self
.
out
.
write
(
'%s |%s| %s%s %s'
%
(
self
.
prefix
,
bar
,
percents
,
'%'
,
suffix
))
# [%s/%s] numberDone
suffix
=
self
.
suffix
if
not
self
.
show_elapsed
else
'%s => %s'
%
(
self
.
suffix
,
self
.
Timer
.
elapsed
)
self
.
out
.
write
(
'%s |%s| %s%s %s'
%
(
self
.
prefix
,
bar
,
percents
,
'%'
,
suffix
))
if
percent
>=
100.
:
self
.
out
.
write
(
'
\n
'
)
if
percent
>=
100.
:
self
.
out
.
write
(
'
\n
'
)
self
.
out
.
flush
()
self
.
out
.
flush
()
else
:
# in some environments, sys.stderr can also be None
# pydocs: usually Windows GUI apps that aren’t connected to a console and Python apps started with pythonw
try
:
percnext
=
self
.
_percdone
[
0
]
if
percent
>=
percnext
:
print
(
f
'
{
percents
}
%'
)
self
.
_percdone
.
pop
(
0
)
except
IndexError
:
# pragma: no cover
pass
def
__call__
(
self
,
percent01
,
message
,
user_data
):
"""Allow ProgressBar instances to be callable and thus to be used as callback function, e.g., for GDAL.
...
...
tests/test_processing/__init__.py
0 → 100644
View file @
c6a8b213
tests/test_processing/test_progress_mon.py
0 → 100644
View file @
c6a8b213
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# py_tools_ds - A collection of geospatial data analysis tools that simplify standard
# operations when handling geospatial raster and vector data as well as projections.
#
# Copyright (C) 2016-2021
# - Daniel Scheffler (GFZ Potsdam, daniel.scheffler@gfz-potsdam.de)
# - Helmholtz Centre Potsdam - GFZ German Research Centre for Geosciences Potsdam,
# Germany (https://www.gfz-potsdam.de/)
#
# This software was developed within the context of the GeoMultiSens project funded
# by the German Federal Ministry of Education and Research
# (project grant code: 01 IS 14 010 A-C).
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
test_progress_mon
-----------------
Tests for `py_tools_ds.processing.progress_mon` module.
"""
import
unittest
from
time
import
sleep
from
py_tools_ds.processing.progress_mon
import
ProgressBar
class
Test_ProgressBar
(
unittest
.
TestCase
):
def
test_print_progress
(
self
):
bar
=
ProgressBar
(
prefix
=
'
\t
progress:'
)
for
i
in
range
(
100
):
bar
.
print_progress
((
i
+
1
)
/
100
*
100
)
def
test_timeout
(
self
):
with
self
.
assertRaises
(
TimeoutError
):
bar
=
ProgressBar
(
prefix
=
'
\t
progress:'
,
timeout
=
0.1
)
for
i
in
range
(
5
):
bar
.
print_progress
((
i
+
1
)
/
5
*
100
)
sleep
(
0.2
)
def
test_stderr_is_none
(
self
):
bar
=
ProgressBar
(
prefix
=
'
\t
progress:'
,
out
=
None
)
for
i
in
range
(
73
):
bar
.
print_progress
((
i
+
1
)
/
73
*
100
)
if
__name__
==
'__main__'
:
unittest
.
main
()
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment