diff --git a/.gitignore b/.gitignore index 6db6608..9ce7e8f 100644 --- a/.gitignore +++ b/.gitignore @@ -58,4 +58,5 @@ target/ # Other -MANIFEST \ No newline at end of file +MANIFEST +README.rst \ No newline at end of file diff --git a/README.md b/README.md index fd9acc3..5ab857b 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,21 @@ Printr is a simple PyPi module that allows for print statements to be replaces w ## Installation -Installation can be done using pip in the standard way; +Installation can be done using pip in the standard way: ``` pip install python-printr ``` If you have python 3 installed, then install using `pip3`. There are no dependancies for this package, so installation should go ahead fine. +Alternatively, you can download directly from Pypi [here](https://pypi.python.org/pypi/Python-Printr/). +## Usage +``` +import printr + +printr.get_version() +>>> 0.0.3 +``` +The printr module contains various different printrs to assist you. You can find more information about these printrs on the wiki. + +The main reason I wrote this project was to simplify status output for my projects, and because I'd never written a PyPi package before. \ No newline at end of file diff --git a/convert-readme.sh b/convert-readme.sh index 6c4fcce..025c0a1 100644 --- a/convert-readme.sh +++ b/convert-readme.sh @@ -1 +1 @@ -pandoc --from=markdown --to=rst --output=README README.md \ No newline at end of file +pandoc --from=markdown --to=rst --output=README.rst README.md \ No newline at end of file diff --git a/details.json b/details.json index da0c39f..2d4f862 100644 --- a/details.json +++ b/details.json @@ -1,11 +1,11 @@ { - 'name' : 'Python-Printr', - 'packages' : ['printr'], - 'version' : '0.0.1', - 'description' : 'Python module to allow a print line to be updated after printing', - 'author' : 'Jake Howard', - 'author_email' : 'me@theorangeone.net ', - 'url' : 'https://github.com/RealOrangeOne/Printr', - 'license':'MIT', - 'platforms':['any'] + "name": "Python-Printr", + "packages": ["printr"], + "version": "0.0.3", + "description": "Python module to allow a print line to be updated after printing", + "author": "Jake Howard", + "author_email": "me@theorangeone.net ", + "url": "https://github.com/RealOrangeOne/Printr", + "license": "MIT", + "platforms": ["any"] } \ No newline at end of file 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 58% rename from printr/itterPrintr.py rename to printr/ItterPrintr/__init__.py index fff155a..0ff24fc 100644 --- a/printr/itterPrintr.py +++ b/printr/ItterPrintr/__init__.py @@ -1,3 +1,7 @@ +from printr.exceptions import FormattingError +from printr.utils import write + + class ItterPrintr(): def __init__(self, string, maxValue, start, diff=1): self.string = string @@ -8,16 +12,18 @@ class ItterPrintr(): self.buildString() def buildString(self): - return self.string.format(c=self.value, m=self.maxValue) + try: + return self.string.format(c=self.value, m=self.maxValue) + except: + raise FormattingError() def reachedLimit(self): 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() def inc(self): - self.value += self.diff + self.value += self.diff \ No newline at end of file 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/exceptions.py b/printr/exceptions.py new file mode 100644 index 0000000..cca1126 --- /dev/null +++ b/printr/exceptions.py @@ -0,0 +1,5 @@ +class FormattingError(Exception): + message = "Provided string doesn't contain formatting placeholders for {c} and/or {m}" + def __init__(self): + super().__init__(self.message) + 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)