Skip to content

Commit

Permalink
Replace colored with colorama
Browse files Browse the repository at this point in the history
  • Loading branch information
Xingdong Zuo committed Oct 14, 2019
1 parent f6ff16e commit bc5dfb6
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docs/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ imageio
pyglet
cloudpickle
pyyaml
colored
colorama
opencv-python
lz4
4 changes: 2 additions & 2 deletions lagom/experiment/run_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from lagom.utils import CloudpickleWrapper


@timeit(color='green', attribute='bold')
@timeit(color='green', bold=True)
def run_experiment(run, config, seeds, log_dir, max_workers, chunksize=1, use_gpu=False, gpu_ids=None):
r"""A convenient function to parallelize the experiment (master-worker pipeline).
Expand Down Expand Up @@ -136,5 +136,5 @@ def _run(job):
else:
with ProcessPoolExecutor(max_workers=min(max_workers, len(jobs))) as executor:
results = list(executor.map(CloudpickleWrapper(_run), jobs, chunksize=chunksize))
print(color_str(f'\nExperiment finished. Loggings are stored in {log_path.absolute()}. ', 'cyan', 'bold'))
print(color_str(f'\nExperiment finished. Loggings are stored in {log_path.absolute()}. ', 'cyan', bold=True))
return results
36 changes: 18 additions & 18 deletions lagom/utils/colorize.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import colored
from colored import stylize
from colorama import Fore, Style


def color_str(string, color, attribute=None):
r"""Returns stylized string with color and attribute for printing.
def color_str(string, color, bold=False):
r"""Returns stylized string with coloring and bolding for printing.
Example::
>>> print(color_str('lagom', 'green', attribute='bold'))
See `colored`_ documentation for more details.
.. _colored:
https://pypi.org/project/colored
>>> print(color_str('lagom', 'green', bold=True))
See `colorama`_ documentation for more details.
.. colorama:
https://pypi.org/project/colorama/
Args:
string (str): input string
color (str): color name
attribute (str, optional): attribute. Default: ``None``
bold (bool, optional): if ``True``, then the string is bolded. Default: ``False``
Returns:
str: stylized string
"""
styles = colored.fg(color)
if attribute is not None:
styles += colored.attr(attribute)

out = stylize(string, styles)
out: stylized string
"""
colors = {'red': Fore.RED, 'green': Fore.GREEN, 'blue': Fore.BLUE, 'cyan': Fore.CYAN,
'magenta': Fore.MAGENTA, 'black': Fore.BLACK, 'white': Fore.WHITE}
style = colors[color]
if bold:
style += Style.BRIGHT
out = style + string + Style.RESET_ALL
return out
10 changes: 5 additions & 5 deletions lagom/utils/timing.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@


@contextmanager
def timed(color='green', attribute='bold'):
def timed(color='green', bold=False):
r"""A decorator to print the total time of executing a body function.
Args:
color (str, optional): color name. Default: 'green'
attribute (str, optional): attribute. Default: 'bold'
bold (bool, optional): if ``True``, then the verbose is bolded. Default: ``False``
"""
t = perf_counter()
yield
total_time = timedelta(seconds=round(perf_counter() - t))
timestamp = datetime.now().isoformat(' ', 'seconds')
print(color_str(string=f'\nTotal time: {total_time} at {timestamp}',
color=color,
attribute=attribute))
bold=bold))


def timeit(_func=None, *, color='green', attribute='bold'):
def timeit(_func=None, *, color='green', bold=False):
def decorator_timeit(f):
r"""Print the runtime of the decorated function. """
@functools.wraps(f)
Expand All @@ -36,7 +36,7 @@ def wrapper_timeit(*args, **kwargs):
timestamp = datetime.now().isoformat(' ', 'seconds')
print(color_str(string=f'\nTotal time: {total_time} at {timestamp}',
color=color,
attribute=attribute))
bold=bold))
return out
return wrapper_timeit
if _func is None:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Common
jupyterlab>=1.0.4
colored>=1.3.93
colorama>=0.4.1
lz4>=2.1.10

# Testing, PEP8 code quality
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
'gym>=0.14.0',
'cloudpickle',
'pyyaml',
'colored']
'colorama']
tests_require = ['pytest',
'flake8',
'sphinx',
Expand Down
4 changes: 2 additions & 2 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@


def test_color_str():
assert color_str('lagom', 'green', 'bold') == '\x1b[38;5;2m\x1b[1mlagom\x1b[0m'
assert color_str('lagom', 'white') == '\x1b[38;5;15mlagom\x1b[0m'
assert color_str('lagom', 'green', bold=True) == '\x1b[32m\x1b[1mlagom\x1b[0m'
assert color_str('lagom', 'white') == '\x1b[37mlagom\x1b[0m'


def test_conditioner():
Expand Down

0 comments on commit bc5dfb6

Please sign in to comment.