Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
xeBuz committed Oct 16, 2015
1 parent 4ee3a74 commit acf1af9
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 68 deletions.
29 changes: 29 additions & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/inspectionProfiles/profiles_settings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions ChangeLog.yaml
@@ -1,3 +1,7 @@
2015-10-15:
- 1.0
- Code Styling

2012-08-25:
- Bug fixed in progress() function.

Expand Down
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Jesus Roldan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
9 changes: 0 additions & 9 deletions LICENSE.txt

This file was deleted.

38 changes: 24 additions & 14 deletions README.md
@@ -1,40 +1,50 @@
Pacman-progressbar
==================

A ProgressBar for Python, based on Arch progressbar, with "ILoveCandy" option enabled.
A ProgressBar for Python, based on Arch progressbar, with `ILoveCandy` option enabled.

#### Instalation
```bash
pip install pacmanprogressbar
```


Usage
=================
#### Usage


Import the package and create the bar
```python
from pacmanprogressbar import Pacman
p = Pacman()
```

Parameters
----------
start: It should be 0.
end: Defines the bar's dimension in an amount of items or "steps", by default is 100.
width: Size (in chars) of the bar, by default is the console size.
step: Current position in the progrressbar, Default 0.
text: Write some text at the beginning of the line.
#### Parameters

| Parameter | Default | Description |
| --- | --- | --- |
| start | 0 | Initial value |
| end | 100 | Defines the bar's dimension in an amount of items or "steps" |
| width | `console size` | Size (in chars) of the bar, by default is the console size |
| step | 0 | Current position in the progressbar |
| text | `None` | Write some text at the beginning of the line |
| encode | UTF-8 | Encoding |


Methods
-------
#### Methods

``` python
p.update([value])
```

Update the progress, incresing the size by 1, or by the "value" parameter

```python
p.progress(value):
p.progress([value]):
```

Set an specified progress, the parameters is mandatory:


Output
#### Output
----------
![Alt text](http://i.imgur.com/7oh3T6x.gif)
39 changes: 18 additions & 21 deletions example_download.py
@@ -1,28 +1,25 @@
#!/usr/bin/env python2.7
import time
import urllib2
from pacmanprogressbar import Pacman



if __name__ == "__main__":

url="http://www.jesusroldan.com/Alberto%20Laiseca%20-%20Cuentos%20de%20Terror.tar.gz"
url = "http://www.jesusroldan.com/Alberto%20Laiseca%20-%20Cuentos%20de%20Terror.tar.gz"

file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
p = Pacman(end=file_size, text="Downloading")
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
p.progress(value=file_size_dl)
f.close()
print("Finished")
file_name = url.split('/')[-1]
u = urllib2.urlopen(url)
f = open(file_name, 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
p = Pacman(end=file_size, text="Downloading")
file_size_dl = 0
block_sz = 8192
while True:
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
p.progress(value=file_size_dl)
f.close()
print("Finished")
4 changes: 2 additions & 2 deletions example_simple.py
@@ -1,13 +1,13 @@
#!/usr/bin/env python

import time
from pacmanprogressbar import Pacman


if __name__ == "__main__":

p = Pacman(end=100, text="Progress")
for x in range(p.len):
p.update()
time.sleep(.1)

print("Finished")
print("Finished")
51 changes: 34 additions & 17 deletions pacmanprogressbar.py
Expand Up @@ -7,7 +7,7 @@
import os
import itertools

__version__ = "0.3.1"
__version__ = "1.0"

MARGIN = 3
DEFAULT_WIDTH = int(os.popen('stty size', 'r').read().split()[1]) - MARGIN
Expand All @@ -16,30 +16,32 @@
CANDY = ["\033[0;37mo\033[m", "\033[0;37m \033[m", "\033[0;37m \033[m"]


class Pacman():
class Pacman:
"""PacMan Progress Bar"""

def __init__(self, start=0, end=100, width=-1, step=0, text=''):
"""Create a new instance
Parameters:
start: It should be 0
end: Defines the bar's dimenssion in an amount of items or steps
width: Size (in chars) of the bar. Default = Console Size
step: Current position in the progrressbar, Default 0
text: Write some text at the beginning of the line
def __init__(self, start=0, end=100, width=-1, step=0, text='', enconde='UTF-8'):
"""
:param start: Initial Position, default
:param end: Defines the bar's dimenssion in an amount of items or steps
:param width: Size (in chars) of the bar, default is the console size
:param step: Current position in the progrressbar
:param text: Write some text at the beginning of the line
:param enconde: Encoding type
"""
self.start = start
self.end = end
self.percentage = 0
self.step = step
self.text = text + ': ' if text != '' else ''
self.encode = enconde
self.text = '{0}: '.format(text) if text != '' else ''
self.len = self.end - self.start
if ((width - MARGIN) in range(0, DEFAULT_WIDTH)):

if (width - MARGIN) in range(0, DEFAULT_WIDTH):
self.width = width - MARGIN
else:
self.width = DEFAULT_WIDTH

self.bar = "-"
self.pacman = itertools.cycle(PACMAN)
self.candy = itertools.cycle(CANDY)
Expand All @@ -52,22 +54,37 @@ def __init__(self, start=0, end=100, width=-1, step=0, text=''):
self._write(self.candybar[i])
self._write("]")

def _write(self, value='', encode='UTF-8'):
@staticmethod
def _write(value, encode='UTF-8'):
"""
Write the values in the buffer
:param value: Value to write
:param encode: Encoding
"""

if sys.version_info.major == 3:
sys.stdout.buffer.write(bytes(value, encode))
else:
sys.stdout.write(value)

def _set_percentage(self):
"""
Set the current porcentage
"""

step = float(self.step)
end = float(self.end)
self.percentage = format((100 * step / end), '.1f')

def _draw(self):
"""
Draw the values in the console
"""

self._set_percentage()
porc = "\r" + str(self.text) + str(self.percentage) + "%["
pos = (((self.step / (self.end - self.start) * 100)
* (self.width - len(porc))) / 100)
pos = (((self.step / (self.end - self.start) * 100) * (self.width - len(porc))) / 100)
self._write(porc)
for i in range(int(pos)):
self._write(self.bar)
Expand All @@ -81,7 +98,7 @@ def update(self, value=1):
""" Update the progress in the bar
Parameter: value, is the incresing size of the bar. By default 1
"""
self.step = self.step + float(value)
self.step += float(value)
self._draw()

def progress(self, value):
Expand Down
14 changes: 9 additions & 5 deletions setup.py
@@ -1,22 +1,26 @@
#!/usr/bin/env python
# coding: utf-8

from setuptools import setup, find_packages
from setuptools import setup

setup(
name='pacmanprogressbar',
version='0.3.2',
version='1.0',
py_modules=['pacmanprogressbar'],

author='Jesús F. Roldán',
author='Jesús Roldán',
author_email='jesus.roldan@gmail.com',
url='https://github.com/xeBuz/pacman-progressbar',
license='LICENSE.txt',
license='MIT',

description='ProgreessBar for Python, based on Arch progressbar, with "ILoveCandy" option enabled',
long_description=open('README.md').read(),

classifiers=[
"Development Status :: 4 - Beta",
"Environment :: Console",
"License :: OSI Approved :: BSD License",
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
"Programming Language :: Python",
"Topic :: Utilities",
],
Expand Down

0 comments on commit acf1af9

Please sign in to comment.