/usr/lib/python2.7/dist-packages/easydev/progressbar.py is in python-easydev 0.9.35+dfsg-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | # -*- python -*-
# -*- coding: utf-8 -*-
#
# This file is part of the easydev software
#
# Copyright (c) 2011-2017
#
# File author(s): Thomas Cokelaer <cokelaer@gmail.com>
#
# Distributed under the GPLv3 License.
# See accompanying file LICENSE.txt or copy at
# http://www.gnu.org/licenses/gpl-3.0.html
#
# Website: https://github.com/cokelaer/easydev
# Documentation: http://easydev-python.readthedocs.io
#
##############################################################################
"""A progress bar copied and adapted from pyMC code (dec 2014)"""
from __future__ import print_function
import sys
import time
import uuid
try:
from IPython.core.display import HTML, Javascript, display
except ImportError:
pass
__all__ = ['progress_bar', 'TextProgressBar', 'Progress']
class ProgressBar(object):
def __init__(self, iterations, interval=None):
self.iterations = iterations
# if no interval provided, set it to 1%
if interval is None:
if iterations <= 100:
interval = 1 # everything % of the data
else:
interval = int(iterations/100)
self.interval = interval
self.start = time.time()
self.last = 0
def _percentage(self, i):
if self.iterations !=0:
return 100 * i / float(self.iterations)
else:
# could be 100 ?
return 100
def _get_elapsed(self):
return time.time() - self.start
elapsed = property(_get_elapsed)
class TextProgressBar(ProgressBar):
"""Use :class:`Progress`"""
def __init__(self, iterations, printer, width=40, interval=None):
self.fill_char = '-'
self.width = width
self.printer = printer
ProgressBar.__init__(self, iterations, interval=interval)
def animate(self, i, dummy=None):
# dummy=None is for back-compatibility
if dummy is not None:
print("second argument in easydev.progress_bar.animate is deprecated. Update your code")
# +1 if i starts at 0 and finishes at N-1
if divmod(i, self.interval)[1] != 0 and i != self.iterations:
pass
else:
self.printer(self.progbar(i))
def progbar(self, i):
# +1 if i starts at 0 and finishes at N-1
bar = self.bar(self._percentage(i))
return "[%s] %i of %i complete in %.1f sec" % (
bar, (i), self.iterations, round(self.elapsed, 1))
def bar(self, percent):
all_full = self.width - 2
num_hashes = int(percent / 100 * all_full)
bar = self.fill_char * num_hashes + ' ' * (all_full - num_hashes)
info = '%d%%' % percent
loc = (len(bar) - len(info)) // 2
return replace_at(bar, info, loc, loc + len(info))
def replace_at(str, new, start, stop):
return str[:start] + new + str[stop:]
def consoleprint(s):
if sys.platform.lower().startswith('win'):
print(s, '\r', end='')
else:
print('\r', s, end='')
sys.stdout.flush()
def ipythonprint(s):
print('\r', s, end='')
sys.stdout.flush()
class IPythonNotebookPB(ProgressBar):
"""Use :class:`Progress`"""
def __init__(self, iterations, interval=None):
self.divid = str(uuid.uuid4())
self.sec_id = str(uuid.uuid4())
pb = HTML(
"""
<div style="float: left; border: 1px solid black; width:500px">
<div id="%s" style="background-color:blue; width:0%%"> </div>
</div>
<label id="%s" style="padding-left: 10px;" text = ""/>
""" % (self.divid, self.sec_id))
display(pb)
ProgressBar.__init__(self, iterations, interval=interval)
def animate(self, i, dummy=None):
if dummy is not None:
print("second argument in easydev.progress_bar.animate is deprecated. Update your code")
# +1 if i starts at 0 and finishes at N-1
if divmod(i, self.interval)[1] != 0 and i != self.iterations :
pass
else:
percentage = self._percentage(i)
fraction = percentage
display(
Javascript("$('div#%s').width('%i%%')" %
(self.divid, percentage)))
display(
Javascript("$('label#%s').text('%i%% in %.1f sec')" %
(self.sec_id, fraction, round(self.elapsed, 1))))
def _run_from_ipython():
try:
__IPYTHON__
return True
except NameError:
return False
def progress_bar(iters, interval=None):
"""A progress bar for Python/IPython/IPython notebook
:param int iters: number of iterations (steps in the loop)
:param interval: number of intervals to use to update the progress bar (20
by default)
::
from easydev import progress_bar
pb = progress_bar(10)
for i in range(1,10):
import time
time.sleep(0.1)
pb.animate(i)
"""
if _run_from_ipython():
from easydev.misc import in_ipynb
if in_ipynb() is True:
return IPythonNotebookPB(iters, interval=interval)
else:
return TextProgressBar(iters, printer=ipythonprint,
interval=interval)
else:
return TextProgressBar(iters, printer=consoleprint,
interval=interval)
class Progress(object):
"""Generic progress bar for python, IPython, IPython notebook
::
from easydev import Progress
pb = Progress(100, interval=1)
pb.animate(10)
"""
def __init__(self, iters, interval=None):
self.pb = progress_bar(iters, interval=interval)
def animate(self, i):
self.pb.animate(i)
def _get_elapsed(self):
return self.pb.elapsed
elapsed = property(_get_elapsed)
|