From 1329866b2c5fddcc9951b86aa678bb704f96c629 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 8 Oct 2015 15:39:22 +0100 Subject: [PATCH] Large restructure --- printr/EllipsisPrintr/__init__.py | 32 +++++++++++++++++++ .../__init__.py} | 6 ++-- printr/__init__.py | 16 ++++------ printr/ellipsisPrintr.py | 23 ------------- printr/simplePrintr.py | 11 ------- printr/utils.py | 6 ++++ setup.py | 23 +++++++------ tests/context_manager.py | 7 ++++ 8 files changed, 65 insertions(+), 59 deletions(-) create mode 100644 printr/EllipsisPrintr/__init__.py rename printr/{itterPrintr.py => ItterPrintr/__init__.py} (80%) delete mode 100644 printr/ellipsisPrintr.py delete mode 100644 printr/simplePrintr.py create mode 100644 printr/utils.py create mode 100644 tests/context_manager.py diff --git a/printr/EllipsisPrintr/__init__.py b/printr/EllipsisPrintr/__init__.py new file mode 100644 index 0000000..5229888 --- /dev/null +++ b/printr/EllipsisPrintr/__init__.py @@ -0,0 +1,32 @@ +from printr.utils import write + +class EllipsisPrintr(): + def __init__(self, string, max=3, erase_after=False): + self.string = string + self.max = max + self.count = -1 + self.erase_after = erase_after + + def update(self, commit=False): + self.clear() + ellipsis = "." * self.count + write(self.string + ellipsis, commit=commit) + if self.count >= self.max: + self.zero() + self.count += 1 + + def zero(self): + self.count = -1 + + def commit(self): + print() + + def clear(self): + print(' ' * (len(self.string) + self.max), end='\r') + + def __enter__(self): + return self + def __exit__(self, type, value, traceback): + if self.erase_after: + self.clear() + self.commit() \ No newline at end of file diff --git a/printr/itterPrintr.py b/printr/ItterPrintr/__init__.py similarity index 80% rename from printr/itterPrintr.py rename to printr/ItterPrintr/__init__.py index 08de82a..0ff24fc 100644 --- a/printr/itterPrintr.py +++ b/printr/ItterPrintr/__init__.py @@ -1,4 +1,5 @@ -from exceptions import FormattingError +from printr.exceptions import FormattingError +from printr.utils import write class ItterPrintr(): @@ -20,8 +21,7 @@ class ItterPrintr(): return self.maxValue <= self.value def update(self, inc=True): - ending = '\r' if not self.reachedLimit() else '\n' - print(self.buildString(), end=ending) + write(self.buildString(), commit=(not self.reachedLimit())) if inc: self.inc() diff --git a/printr/__init__.py b/printr/__init__.py index dbc1e59..540259b 100644 --- a/printr/__init__.py +++ b/printr/__init__.py @@ -1,17 +1,13 @@ -# Import python modules import os -from json import loads as json_loads +from json import load as json_load -# Import Printr Modules -from .simplePrintr import SimplePrintr -from .itterPrintr import ItterPrintr -from .ellipsisPrintr import EllipsisPrintr - -# Initialise PRINTR_PATH = os.path.dirname(os.path.abspath(__file__)) -with open(PRINTR_PATH + '/details.json') as file: - PRINTR_DETAILS = json_loads(file) +from .ItterPrintr import ItterPrintr +from .EllipsisPrintr import EllipsisPrintr + +with open(PRINTR_PATH + '/../details.json') as file: + PRINTR_DETAILS = json_load(file) def get_version(): diff --git a/printr/ellipsisPrintr.py b/printr/ellipsisPrintr.py deleted file mode 100644 index f257bf6..0000000 --- a/printr/ellipsisPrintr.py +++ /dev/null @@ -1,23 +0,0 @@ -class EllipsisPrintr(): - def __init__(self, string, max=5): - self.string = string - self.max = max - self.count = -1 - - def update(self, commit=False): - self.clear() - ellipsis = "." * self.count - ending = '\r' if not commit else '\n' - print(self.string + ellipsis, end=ending) - if self.count >= self.max: - self.zero() - self.count += 1 - - def zero(self): - self.count = -1 - - def commit(self): - print() - - def clear(self): - print(' ' * (len(self.string) + self.max), end='\r') \ No newline at end of file diff --git a/printr/simplePrintr.py b/printr/simplePrintr.py deleted file mode 100644 index bce5911..0000000 --- a/printr/simplePrintr.py +++ /dev/null @@ -1,11 +0,0 @@ -class SimplePrintr(): - @classmethod - def write(string, commit=False): - if commit: - print(string) - else: - print(string, end='\r') - - @classmethod - def commit(): - print() \ No newline at end of file diff --git a/printr/utils.py b/printr/utils.py new file mode 100644 index 0000000..53d3e0a --- /dev/null +++ b/printr/utils.py @@ -0,0 +1,6 @@ +def write(string, commit=False): + ending = '\n' if commit else '\r' + print(string, end=ending) + +def commit(): + print() \ No newline at end of file diff --git a/setup.py b/setup.py index 8bbee43..a97ef88 100644 --- a/setup.py +++ b/setup.py @@ -1,5 +1,5 @@ from distutils.core import setup -from json import loads as json_loads +from json import load as json_load LONG_DESCRIPTION = None try: LONG_DESCRIPTION = open('README.md').read() @@ -8,17 +8,16 @@ except: DETAILS = None with open('details.json') as file: - DETAILS = json_loads(file) - + DETAILS = json_load(file) setup( - name = DETAILS.name, - packages = DETAILS.packages, - version = DETAILS.version, - description = DETAILS.description, + name = DETAILS['name'], + packages = DETAILS['packages'], + version = DETAILS['version'], + description = DETAILS['description'], long_description = LONG_DESCRIPTION, - author = DETAILS.author, - author_email = DETAILS.author_email, - url = DETAILS.url, - license = DETAILS.license, - platforms = DETAILS.platform + author = DETAILS['author'], + author_email = DETAILS['author_email'], + url = DETAILS['url'], + license = DETAILS['license'], + platforms = DETAILS['platforms'] ) \ No newline at end of file diff --git a/tests/context_manager.py b/tests/context_manager.py new file mode 100644 index 0000000..de70e80 --- /dev/null +++ b/tests/context_manager.py @@ -0,0 +1,7 @@ +from printr import EllipsisPrintr +from time import sleep + +with EllipsisPrintr('Printing') as p: + for i in range(30): + p.update() + sleep(0.1)