Split out utils into multiple files, add reraising support

This commit is contained in:
Jonas Schneider
2016-04-30 22:23:46 -07:00
parent 99f40a2fc1
commit fc86c6f2aa
6 changed files with 82 additions and 32 deletions

10
gym/utils/__init__.py Normal file
View File

@@ -0,0 +1,10 @@
"""A set of common utilities used within the environments. These are
not intended as API functions, and will not remain stable over time.
"""
# These submodules should not have any import-time dependencies.
# We want this since we use `utils` during our import-time sanity checks
# that verify that our dependencies are actually present.
from .colorize import colorize
from .ezpickle import EzPickle
from .reraise import reraise

35
gym/utils/colorize.py Normal file
View File

@@ -0,0 +1,35 @@
"""A set of common utilities used within the environments. These are
not intended as API functions, and will not remain stable over time.
"""
color2num = dict(
gray=30,
red=31,
green=32,
yellow=33,
blue=34,
magenta=35,
cyan=36,
white=37,
crimson=38
)
def colorize(string, color, bold=False, highlight = False):
"""Return string surrounded by appropriate terminal color codes to
print colorized text. Valid colors: gray, red, green, yellow,
blue, magenta, cyan, white, crimson
"""
# Import six here so that `utils` has no import-time dependencies.
# We want this since we use `utils` during our import-time sanity checks
# that verify that our dependencies (including six) are actually present.
import six
attr = []
num = color2num[color]
if highlight: num += 10
attr.append(six.u(str(num)))
if bold: attr.append(six.u('1'))
attrs = six.u(';').join(attr)
return six.u('\x1b[%sm%s\x1b[0m') % (attrs, string)

View File

@@ -1,35 +1,3 @@
"""A set of common utilities used within the environments. These are
not intended as API functions, and will not remain stable over time.
"""
import six
color2num = dict(
gray=30,
red=31,
green=32,
yellow=33,
blue=34,
magenta=35,
cyan=36,
white=37,
crimson=38
)
def colorize(string, color, bold=False, highlight = False):
"""Return string surrounded by appropriate terminal color codes to
print colorized text. Valid colors: gray, red, green, yellow,
blue, magenta, cyan, white, crimson
"""
attr = []
num = color2num[color]
if highlight: num += 10
attr.append(six.u(str(num)))
if bold: attr.append(six.u('1'))
attrs = six.u(';').join(attr)
return six.u('\x1b[%sm%s\x1b[0m') % (attrs, string)
class EzPickle(object):
"""Objects that are pickled and unpickled via their constructor
arguments.

33
gym/utils/reraise.py Normal file
View File

@@ -0,0 +1,33 @@
import sys
# We keep the actual reraising in different modules since the
# Python 2 version SyntaxError's in Python 3.
if sys.version_info[0] < 3:
from .reraise_impl_py2 import reraise_impl
else:
from .reraise_impl_py3 import reraise_impl
def reraise(prefix=None, suffix=None):
old_exc_type, old_exc_value, traceback = sys.exc_info()
if old_exc_value is None:
old_exc_value = old_exc_type()
e = ReraisedException(old_exc_type, old_exc_value, prefix, suffix)
reraise_impl(e, traceback)
class ReraisedException(Exception):
def __init__(self, old_exc_type, old_exc_value, prefix, suffix):
self.old_exc_type = old_exc_type
self.old_exc_value = old_exc_value
self.prefix = prefix
self.suffix = suffix
def __str__(self):
klass = self.old_exc_type
orig = "%s.%s: %s" % (klass.__module__, klass.__name__, klass.__str__(self.old_exc_value))
prefixpart = suffixpart = ''
if self.prefix != '':
prefixpart = self.prefix + "\n"
if self.suffix != '':
suffixpart = "\n\n" + self.suffix
return "%sThe original exception was:\n\n%s%s" % (prefixpart, orig, suffixpart)

View File

@@ -0,0 +1,2 @@
def reraise_impl(e, traceback):
raise e.__class__, e, traceback

View File

@@ -0,0 +1,2 @@
def reraise_impl(e, traceback):
raise e.with_traceback(traceback)