Skip to content

Commit

Permalink
Handle Python 2/3 compat
Browse files Browse the repository at this point in the history
Previously, we were using Nengo core's Python 2/3 compatibility
module, but Nengo core has dropped Python 2 support. Since we
have not yet dropped Python 2 support, we add a `compat.py`
module to handle the things that we use in Nengo GUI.
  • Loading branch information
tbekolay committed Nov 6, 2019
1 parent b6fded5 commit db1902f
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
34 changes: 34 additions & 0 deletions nengo_gui/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from __future__ import absolute_import

import collections
import sys

import numpy as np

# Only test for Python 2 so that we have less changes for Python 4
PY2 = sys.version_info[0] == 2

# If something's changed from Python 2 to 3, we handle that here
if PY2:
from cgi import escape as cgi_escape
from StringIO import StringIO

escape = lambda s, quote=True: cgi_escape(s, quote=quote)
iteritems = lambda d: d.iteritems()

string_types = (str, unicode)
int_types = (int, long)
range = xrange

else:
from html import escape
from io import StringIO

iteritems = lambda d: iter(d.items())


def is_iterable(obj):
if isinstance(obj, np.ndarray):
return obj.ndim > 0 # 0-d arrays give error if iterated over
else:
return isinstance(obj, collections.Iterable)
4 changes: 2 additions & 2 deletions nengo_gui/components/netgraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import numpy as np

import nengo
from nengo.utils.compat import escape
import json

from nengo_gui.compat import escape, iteritems
from nengo_gui.components.component import Component
from nengo_gui.components.value import Value
from nengo_gui.components.slider import OverriddenOutput
Expand Down Expand Up @@ -123,7 +123,7 @@ def _reload(self, code=None): # noqa: C901
# for Nodes, Ensembles, and Networks, this means to find the item
# with the same uid. For Connections, we don't really have a uid,
# so we use the uids of the pre and post objects.
for uid, old_item in nengo.utils.compat.iteritems(dict(self.uids)):
for uid, old_item in iteritems(dict(self.uids)):
try:
new_item = eval(uid, self.page.locals)
except:
Expand Down
12 changes: 1 addition & 11 deletions nengo_gui/components/progress.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
import json

try:
from nengo.utils.compat import escape
except ImportError:
import sys
PY2 = sys.version_info[0] == 2
if PY2:
from cgi import escape as cgi_escape
escape = lambda s, quote=True: cgi_escape(s, quote=quote)
else:
from html import escape

from nengo.utils.progress import ProgressBar, timestamp2timedelta

from nengo_gui.compat import escape
from nengo_gui.components.component import Component


Expand Down
2 changes: 1 addition & 1 deletion nengo_gui/components/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
class Process(object):
pass

from nengo.utils.compat import is_iterable

from nengo_gui.compat import is_iterable
from nengo_gui.components.component import Component


Expand Down
3 changes: 2 additions & 1 deletion nengo_gui/exec_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import threading
import traceback
import sys
from nengo.utils.compat import StringIO

from nengo_gui.compat import StringIO


# list of Simulators to check for
Expand Down
3 changes: 1 addition & 2 deletions nengo_gui/user_action.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Respond to an action from the user on the NetGraph"""

from nengo.utils.compat import iteritems

from nengo_gui.compat import iteritems
import nengo_gui.components


Expand Down

0 comments on commit db1902f

Please sign in to comment.