├── .gitignore ├── .travis.yml ├── LICENSE ├── MANIFEST ├── README.md ├── cprint ├── __init__.py └── cprint.py ├── img └── screen.png ├── setup.py ├── tests ├── __init__.py └── test.py └── tox.ini /.gitignore: -------------------------------------------------------------------------------- 1 | #### joe made this: http://goel.io/joe 2 | 3 | #####=== Python ===##### 4 | 5 | # Byte-compiled / optimized / DLL files 6 | __pycache__/ 7 | *.py[cod] 8 | 9 | # C extensions 10 | *.so 11 | 12 | # Distribution / packaging 13 | .Python 14 | env/ 15 | build/ 16 | develop-eggs/ 17 | dist/ 18 | downloads/ 19 | eggs/ 20 | lib/ 21 | lib64/ 22 | parts/ 23 | sdist/ 24 | var/ 25 | *.egg-info/ 26 | .installed.cfg 27 | *.egg 28 | 29 | # PyInstaller 30 | # Usually these files are written by a python script from a template 31 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 32 | *.manifest 33 | *.spec 34 | 35 | # Installer logs 36 | pip-log.txt 37 | pip-delete-this-directory.txt 38 | 39 | # Unit test / coverage reports 40 | htmlcov/ 41 | .tox/ 42 | .coverage 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | 47 | # Translations 48 | *.mo 49 | *.pot 50 | 51 | # Django stuff: 52 | *.log 53 | 54 | # Sphinx documentation 55 | docs/_build/ 56 | 57 | # PyBuilder 58 | target/ 59 | .venv 60 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: python 2 | python: 3 | - "2.7" 4 | - "3.5" 5 | - "3.6" 6 | - "3.7" 7 | - "3.8" 8 | - "nightly" 9 | install: 10 | - python setup.py -q install 11 | script: "python tests/test.py" 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2012-2021 Erwan Vasseure 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /MANIFEST: -------------------------------------------------------------------------------- 1 | # file GENERATED by distutils, do NOT edit 2 | setup.py 3 | cprint/__init__.py 4 | cprint/cprint.py 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/EVasseure/cprint.svg?branch=master)](https://travis-ci.org/EVasseure/cprint) 2 | 3 | ## cprint 4 | 5 | Do you find it annoying when you have to search for a certain debug, or error, line in your console? Have you ever dreamed of a simple and quick way to make your debug print truly visible? Well here is your solution! 6 | cprint is a minimalist python library which gives you the possibility to print in color. 7 | 8 | ## Install 9 | 10 | `pip install cprint` 11 | 12 | ## Usage 13 | 14 | ```python 15 | from cprint import * 16 | 17 | cprint(arg) # WHITE 18 | cprint.ok(arg) # BLUE 19 | cprint.info(arg) # GREEN 20 | cprint.warn(arg) # YELLOW 21 | cprint.err(arg, interrupt=False) # BROWN 22 | cprint.fatal(arg, interrupt=False) # RED 23 | ``` 24 | 25 | ![Demo](/img/screen.png) 26 | 27 | In case you have issues under Windows, try installing colorama with `pip install colorama`. 28 | -------------------------------------------------------------------------------- /cprint/__init__.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | #!/usr/bin/env python 3 | from .cprint import * 4 | 5 | """ 6 | This module give to possibility to print in color. 7 | """ 8 | 9 | __version__ = "1.2.2" 10 | -------------------------------------------------------------------------------- /cprint/cprint.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | #!/usr/bin/env python 3 | """ 4 | Usage: 5 | >>> from cprint import * 6 | >>> cprint.info(str) 7 | >>> ... 8 | """ 9 | 10 | from __future__ import print_function, unicode_literals 11 | import sys 12 | 13 | 14 | class cprint(object): 15 | 16 | colors = { 17 | 'NONE': '\033[0m', 18 | 'OK': '\033[94m', 19 | 'INFO': '\033[92m', 20 | 'WARNING': '\033[93m', 21 | 'ERR': '\033[91m', 22 | 'FATAL': '\033[31m', 23 | 'ENDC': '\033[0m' 24 | } 25 | 26 | def __init__(self, str): 27 | """ 28 | Prints in white to stdout 29 | """ 30 | print(str, file=sys.stdout) 31 | del self 32 | 33 | @classmethod 34 | def _get_repr(cls, arg): 35 | if isinstance(arg, str): 36 | return arg 37 | return repr(arg) 38 | 39 | @classmethod 40 | def ok(cls, arg, *args, **kwargs): 41 | """ 42 | Prints in blue to stdout 43 | """ 44 | print(cprint.colors['OK'] + cls._get_repr(arg) + cprint.colors['ENDC'], 45 | file=sys.stdout, *args, **kwargs) 46 | 47 | @classmethod 48 | def info(cls, arg, *args, **kwargs): 49 | """ 50 | Prints in green to stdout 51 | """ 52 | print(cprint.colors['INFO'] + cls._get_repr(arg) + cprint.colors['ENDC'], 53 | file=sys.stdout, *args, **kwargs) 54 | 55 | @classmethod 56 | def warn(cls, arg, *args, **kwargs): 57 | """ 58 | Prints in yellow to strerr 59 | """ 60 | print(cprint.colors['WARNING'] + cls._get_repr(arg) + cprint.colors['ENDC'], 61 | file=sys.stderr, *args, **kwargs) 62 | 63 | @classmethod 64 | def err(cls, arg, interrupt=False, fatal_message="Fatal error: Program stopped.", *args, **kwargs): 65 | """ 66 | Prints in brown to stderr 67 | interrupt=True: stops the program 68 | """ 69 | print(cprint.colors['ERR'] + cls._get_repr(arg) + cprint.colors['ENDC'], 70 | file=sys.stderr, *args, **kwargs) 71 | if interrupt: 72 | print(cprint.colors['ERR'] + fatal_message + 73 | cprint.colors['ENDC'], 74 | file=sys.stderr) 75 | exit(1) 76 | 77 | @classmethod 78 | def fatal(cls, arg, interrupt=False, fatal_message="Fatal error: Program stopped.", *args, **kwargs): 79 | """ 80 | Prints in red to stderr 81 | interrupt=True: stops the program 82 | """ 83 | print(cprint.colors['FATAL'] + cls._get_repr(arg) + cprint.colors['ENDC'], 84 | file=sys.stderr, *args, **kwargs) 85 | if interrupt: 86 | print(cprint.colors['FATAL'] + fatal_message + 87 | cprint.colors['ENDC'], 88 | file=sys.stderr) 89 | exit(1) 90 | -------------------------------------------------------------------------------- /img/screen.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evasseure/cprint/cae3fdce19c82608b426a6371c057f7d1c39ea6f/img/screen.png -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # -*- coding: utf-8 -*- 3 | 4 | from setuptools import setup 5 | 6 | import cprint 7 | 8 | with open("README.md", "r") as fh: 9 | long_description = fh.read() 10 | 11 | setup( 12 | name='cprint', 13 | version=cprint.__version__, 14 | author='Erwan Vasseure', 15 | author_email='e.vasseure@gmail.com', 16 | license='MIT', 17 | description='Printing and debugging with color', 18 | long_description=long_description, 19 | long_description_content_type="text/markdown", 20 | url='https://github.com/EVasseure/cprint', 21 | include_package_data=True, 22 | packages=['cprint'], 23 | ) 24 | -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/evasseure/cprint/cae3fdce19c82608b426a6371c057f7d1c39ea6f/tests/__init__.py -------------------------------------------------------------------------------- /tests/test.py: -------------------------------------------------------------------------------- 1 | # coding: utf8 2 | from cprint import * 3 | import pytest 4 | 5 | 6 | class TestClass(object): 7 | def __init__(self): 8 | self.name = "Joe" 9 | def __repr__(self): 10 | return self.name 11 | 12 | def p(str): 13 | print(str) 14 | cprint(str) 15 | cprint.ok(str) 16 | cprint.info(str) 17 | cprint.warn(str) 18 | cprint.err(str) 19 | cprint.fatal(str) 20 | 21 | 22 | def test_simple_print(): 23 | """ Testing cprint with simple string. """ 24 | p("simple1") 25 | return 26 | 27 | 28 | def test_hard_print(): 29 | """ Testing cprint with ugly string. """ 30 | p(u"simple2'''&é'((-è__çà)==)^^¨¨") 31 | return 32 | 33 | 34 | def test_print_int(): 35 | """ Testing cprint with int. """ 36 | p(1) 37 | return 38 | 39 | 40 | def test_print_int_list(): 41 | """ Testing cprint with int list. """ 42 | p([1, 2, 3, 4]) 43 | return 44 | 45 | 46 | def test_print_list(): 47 | """ Testing cprint with list. """ 48 | p(['l1', 'l2', 'l3']) 49 | return 50 | 51 | 52 | def test_print_tuple(): 53 | """ Testing cprint with tuple. """ 54 | p(('t1', 't2', 't3')) 55 | return 56 | 57 | 58 | def test_print_object(): 59 | """ Testing cprint with object. """ 60 | o = TestClass() 61 | p(o) 62 | return -------------------------------------------------------------------------------- /tox.ini: -------------------------------------------------------------------------------- 1 | # Tox (http://tox.testrun.org/) is a tool for running tests 2 | # in multiple virtualenvs. This configuration file will run the 3 | # test suite on all supported python versions. To use it, "pip install tox" 4 | # and then run "tox" from this directory. 5 | 6 | [tox] 7 | envlist = py27, py38 8 | 9 | [testenv] 10 | deps=pytest 11 | commands=py.test tests/test.py 12 | --------------------------------------------------------------------------------