Py36+ syntax in gym/utils: derived by running pyupgrade --py36-plus gym/utils/**.py and flynt gym --ll 120 (#2472)

Co-authored-by: Ilya Kamen <ilya.kamenshchikov@bosch.com>
This commit is contained in:
Elijah K
2021-11-14 14:50:53 +01:00
committed by GitHub
parent a4da53c210
commit 61d9f266bc
6 changed files with 11 additions and 13 deletions

View File

@@ -56,5 +56,5 @@ def atomic_write(filepath, binary=False, fsync=False):
finally: finally:
try: try:
os.remove(tmppath) os.remove(tmppath)
except (IOError, OSError): except OSError:
pass pass

View File

@@ -3,7 +3,7 @@ import threading
import weakref import weakref
class Closer(object): class Closer:
"""A registry that ensures your objects get closed, whether manually, """A registry that ensures your objects get closed, whether manually,
upon garbage collection, or upon exit. To work properly, your upon garbage collection, or upon exit. To work properly, your
objects need to cooperate and do something like the following: objects need to cooperate and do something like the following:
@@ -49,7 +49,7 @@ class Closer(object):
Returns: Returns:
int: The registration ID of this object. It is the caller's responsibility to save this ID if early closing is desired. int: The registration ID of this object. It is the caller's responsibility to save this ID if early closing is desired.
""" """
assert hasattr(closeable, "close"), "No close method for {}".format(closeable) assert hasattr(closeable, "close"), f"No close method for {closeable}"
next_id = self.generate_next_id() next_id = self.generate_next_id()
self.closeables[next_id] = closeable self.closeables[next_id] = closeable

View File

@@ -29,4 +29,4 @@ def colorize(string, color, bold=False, highlight=False):
if bold: if bold:
attr.append("1") attr.append("1")
attrs = ";".join(attr) attrs = ";".join(attr)
return "\x1b[%sm%s\x1b[0m" % (attrs, string) return f"\x1b[{attrs}m{string}\x1b[0m"

View File

@@ -1,4 +1,4 @@
class EzPickle(object): class EzPickle:
"""Objects that are pickled and unpickled via their constructor """Objects that are pickled and unpickled via their constructor
arguments. arguments.

View File

@@ -8,7 +8,7 @@ try:
matplotlib.use("TkAgg") matplotlib.use("TkAgg")
import matplotlib.pyplot as plt import matplotlib.pyplot as plt
except ImportError as e: except ImportError as e:
logger.warn("failed to set matplotlib backend, plotting will not work: %s" % str(e)) logger.warn(f"failed to set matplotlib backend, plotting will not work: {str(e)}")
plt = None plt = None
from collections import deque from collections import deque
@@ -143,7 +143,7 @@ def play(env, transpose=True, fps=30, zoom=None, callback=None, keys_to_action=N
pygame.quit() pygame.quit()
class PlayPlot(object): class PlayPlot:
def __init__(self, callback, horizon_timesteps, plot_names): def __init__(self, callback, horizon_timesteps, plot_names):
self.data_callback = callback self.data_callback = callback
self.horizon_timesteps = horizon_timesteps self.horizon_timesteps = horizon_timesteps

View File

@@ -10,9 +10,7 @@ from gym import error
def np_random(seed=None): def np_random(seed=None):
if seed is not None and not (isinstance(seed, int) and 0 <= seed): if seed is not None and not (isinstance(seed, int) and 0 <= seed):
raise error.Error( raise error.Error(f"Seed must be a non-negative integer or omitted, not {seed}")
"Seed must be a non-negative integer or omitted, not {}".format(seed)
)
seed = create_seed(seed) seed = create_seed(seed)
@@ -65,7 +63,7 @@ def create_seed(a=None, max_bytes=8):
elif isinstance(a, int): elif isinstance(a, int):
a = a % 2 ** (8 * max_bytes) a = a % 2 ** (8 * max_bytes)
else: else:
raise error.Error("Invalid type for seed: {} ({})".format(type(a), a)) raise error.Error(f"Invalid type for seed: {type(a)} ({a})")
return a return a
@@ -76,7 +74,7 @@ def _bigint_from_bytes(bytes):
padding = sizeof_int - len(bytes) % sizeof_int padding = sizeof_int - len(bytes) % sizeof_int
bytes += b"\0" * padding bytes += b"\0" * padding
int_count = int(len(bytes) / sizeof_int) int_count = int(len(bytes) / sizeof_int)
unpacked = struct.unpack("{}I".format(int_count), bytes) unpacked = struct.unpack(f"{int_count}I", bytes)
accum = 0 accum = 0
for i, val in enumerate(unpacked): for i, val in enumerate(unpacked):
accum += 2 ** (sizeof_int * 8 * i) * val accum += 2 ** (sizeof_int * 8 * i) * val
@@ -86,7 +84,7 @@ def _bigint_from_bytes(bytes):
def _int_list_from_bigint(bigint): def _int_list_from_bigint(bigint):
# Special case 0 # Special case 0
if bigint < 0: if bigint < 0:
raise error.Error("Seed must be non-negative, not {}".format(bigint)) raise error.Error(f"Seed must be non-negative, not {bigint}")
elif bigint == 0: elif bigint == 0:
return [0] return [0]