Skip to content

Commit

Permalink
remove six and __future__ imports (openai#1840)
Browse files Browse the repository at this point in the history
* remove six

* remove __future__ imports

* remove six from setup.py, python 2.7 from README.rst
  • Loading branch information
pzhokhov authored Apr 10, 2020
1 parent a8a3d36 commit 6721254
Show file tree
Hide file tree
Showing 17 changed files with 24 additions and 46 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ should know:
Supported systems
-----------------

We currently support Linux and OS X running Python 2.7 or 3.5 -- 3.7.
We currently support Linux and OS X running Python 3.5 -- 3.8
Windows support is experimental - algorithmic, toy_text, classic_control and atari *should* work on Windows (see next section for installation instructions); nevertheless, proceed at your own risk.

Installation
Expand Down
4 changes: 1 addition & 3 deletions examples/agents/cem.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
from __future__ import print_function

import gym
from gym import wrappers, logger
import numpy as np
from six.moves import cPickle as pickle
import pickle
import json, sys, os
from os import path
from _policies import BinaryActionLinearPolicy # Different file so it can be unpickled
Expand Down
2 changes: 0 additions & 2 deletions examples/agents/keyboard_agent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#!/usr/bin/env python
from __future__ import print_function

import sys, gym, time

#
Expand Down
2 changes: 1 addition & 1 deletion gym/envs/algorithmic/algorithmic_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import sys
from contextlib import closing
import numpy as np
from six import StringIO
from io import StringIO


class AlgorithmicEnv(Env):
Expand Down
1 change: 0 additions & 1 deletion gym/envs/algorithmic/duplicated_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
Task is to return every nth character from the input tape.
http://arxiv.org/abs/1511.07275
"""
from __future__ import division
from gym.envs.algorithmic import algorithmic_env


Expand Down
1 change: 0 additions & 1 deletion gym/envs/algorithmic/reversed_addition.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import division
from gym.envs.algorithmic import algorithmic_env


Expand Down
4 changes: 1 addition & 3 deletions gym/envs/classic_control/rendering.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
"""
2D rendering framework
"""
from __future__ import division
import os
import six
import sys

if "Apple" in sys.version:
Expand Down Expand Up @@ -46,7 +44,7 @@ def get_display(spec):
"""
if spec is None:
return None
elif isinstance(spec, six.string_types):
elif isinstance(spec, str):
return pyglet.canvas.Display(spec)
else:
raise error.Error('Invalid display specification: {}. (Must be a string like :0 or None.)'.format(spec))
Expand Down
3 changes: 1 addition & 2 deletions gym/envs/tests/test_envs.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def test_random_rollout():


def test_env_render_result_is_immutable():
from six import string_types
environs = [
envs.make('Taxi-v3'),
envs.make('FrozenLake-v0'),
Expand All @@ -61,5 +60,5 @@ def test_env_render_result_is_immutable():
for env in environs:
env.reset()
output = env.render(mode='ansi')
assert isinstance(output, string_types)
assert isinstance(output, str)
env.close()
1 change: 0 additions & 1 deletion gym/envs/tests/test_envs_semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"""


from __future__ import unicode_literals
import json
import hashlib
import os
Expand Down
2 changes: 1 addition & 1 deletion gym/envs/toy_text/frozen_lake.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from contextlib import closing

import numpy as np
from six import StringIO, b
from io import StringIO

from gym import utils
from gym.envs.toy_text import discrete
Expand Down
2 changes: 1 addition & 1 deletion gym/envs/toy_text/taxi.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import sys
from contextlib import closing
from six import StringIO
from io import StringIO
from gym import utils
from gym.envs.toy_text import discrete
import numpy as np
Expand Down
13 changes: 4 additions & 9 deletions gym/utils/colorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,10 @@ def colorize(string, color, bold=False, highlight = False):
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)
attr.append(str(num))
if bold: attr.append('1')
attrs = ';'.join(attr)
return '\x1b[%sm%s\x1b[0m' % (attrs, string)
5 changes: 2 additions & 3 deletions gym/utils/seeding.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
import numpy as np
import os
import random as _random
from six import integer_types
import struct
import sys

from gym import error

def np_random(seed=None):
if seed is not None and not (isinstance(seed, integer_types) and 0 <= seed):
if seed is not None and not (isinstance(seed, int) and 0 <= seed):
raise error.Error('Seed must be a non-negative integer or omitted, not {}'.format(seed))

seed = create_seed(seed)
Expand Down Expand Up @@ -58,7 +57,7 @@ def create_seed(a=None, max_bytes=8):
a = a.encode('utf8')
a += hashlib.sha512(a).digest()
a = _bigint_from_bytes(a[:max_bytes])
elif isinstance(a, integer_types):
elif isinstance(a, int):
a = a % 2**(8 * max_bytes)
else:
raise error.Error('Invalid type for seed: {} ({})'.format(type(a), a))
Expand Down
7 changes: 2 additions & 5 deletions gym/wrappers/monitor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import gym
from gym import Wrapper
from gym import error, version, logger
import os, json, numpy as np, six
import os, json, numpy as np
from gym.wrappers.monitoring import stats_recorder, video_recorder
from gym.utils import atomic_write, closer
from gym.utils.json_utils import json_encode_np
Expand Down Expand Up @@ -66,10 +66,7 @@ def _start(self, directory, video_callable=None, force=False, resume=False,

if not os.path.exists(directory):
logger.info('Creating monitor directory %s', directory)
if six.PY3:
os.makedirs(directory, exist_ok=True)
else:
os.makedirs(directory)
os.makedirs(directory, exist_ok=True)

if video_callable is None:
video_callable = capped_cubic_video_schedule
Expand Down
18 changes: 8 additions & 10 deletions gym/wrappers/monitoring/video_recorder.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import os.path
import distutils.spawn, distutils.version
import numpy as np
from six import StringIO
import six
from io import StringIO
from gym import error, logger

def touch(path):
Expand Down Expand Up @@ -182,9 +181,8 @@ def __init__(self, output_path, frames_per_sec):
self.frames = []

def capture_frame(self, frame):
from six import string_types
string = None
if isinstance(frame, string_types):
if isinstance(frame, str):
string = frame
elif isinstance(frame, StringIO):
string = frame.getvalue()
Expand All @@ -193,10 +191,10 @@ def capture_frame(self, frame):

frame_bytes = string.encode('utf-8')

if frame_bytes[-1:] != six.b('\n'):
if frame_bytes[-1:] != b'\n':
raise error.InvalidFrame('Frame must end with a newline: """{}"""'.format(string))

if six.b('\r') in frame_bytes:
if b'\r' in frame_bytes:
raise error.InvalidFrame('Frame contains carriage returns (only newlines are allowed: """{}"""'.format(string))

self.frames.append(frame_bytes)
Expand All @@ -208,14 +206,14 @@ def close(self):
# Turn frames into events: clear screen beforehand
# https://rosettacode.org/wiki/Terminal_control/Clear_the_screen#Python
# https://rosettacode.org/wiki/Terminal_control/Cursor_positioning#Python
clear_code = six.b("%c[2J\033[1;1H" % (27))
clear_code = b"%c[2J\033[1;1H" % (27)
# Decode the bytes as UTF-8 since JSON may only contain UTF-8
events = [ (frame_duration, (clear_code+frame.replace(six.b('\n'),six.b('\r\n'))).decode('utf-8')) for frame in self.frames ]
events = [ (frame_duration, (clear_code+frame.replace(b'\n', b'\r\n')).decode('utf-8')) for frame in self.frames ]

# Calculate frame size from the largest frames.
# Add some padding since we'll get cut off otherwise.
height = max([frame.count(six.b('\n')) for frame in self.frames]) + 1
width = max([max([len(line) for line in frame.split(six.b('\n'))]) for frame in self.frames]) + 2
height = max([frame.count(b'\n') for frame in self.frames]) + 1
width = max([max([len(line) for line in frame.split(b'\n')]) for frame in self.frames]) + 2

data = {
"version": 1,
Expand Down
1 change: 0 additions & 1 deletion scripts/generate_json.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from __future__ import unicode_literals
from gym import envs, spaces, logger
import json
import os
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
if package.startswith('gym')],
zip_safe=False,
install_requires=[
'scipy', 'numpy>=1.10.4', 'six', 'pyglet>=1.4.0,<=1.5.0', 'cloudpickle>=1.2.0,<1.4.0',
'scipy', 'numpy>=1.10.4', 'pyglet>=1.4.0,<=1.5.0', 'cloudpickle>=1.2.0,<1.4.0',
'enum34~=1.1.6;python_version<"3.4"',
],
extras_require=extras,
Expand Down

0 comments on commit 6721254

Please sign in to comment.