diff options
author | Lars Kellogg-Stedman <lars@redhat.com> | 2017-12-23 08:11:05 -0500 |
---|---|---|
committer | Lars Kellogg-Stedman <lars@redhat.com> | 2017-12-24 18:45:13 -0500 |
commit | 31e9c2732721f0bc1aa27b74f065491e660898c8 (patch) | |
tree | 92325f734874309d290f38cb034f8e69d0c5b78c | |
parent | c29b072e1f6df12e237a1294e249943222879536 (diff) | |
download | urwid-31e9c2732721f0bc1aa27b74f065491e660898c8.tar.gz |
python 3 compatibility changes
this updates the syntax in many of the examples so that they run under
both python 2 and python 3.
-rwxr-xr-x | examples/browse.py | 6 | ||||
-rwxr-xr-x | examples/calc.py | 42 | ||||
-rwxr-xr-x | examples/dialog.py | 6 | ||||
-rwxr-xr-x | examples/fib.py | 2 | ||||
-rwxr-xr-x | examples/graph.py | 2 | ||||
-rwxr-xr-x | examples/subproc.py | 2 | ||||
-rw-r--r-- | examples/subproc2.py | 11 | ||||
-rw-r--r-- | examples/twisted_serve_ssh.py | 2 |
8 files changed, 43 insertions, 30 deletions
diff --git a/examples/browse.py b/examples/browse.py index d5a5f16..ab689c0 100755 --- a/examples/browse.py +++ b/examples/browse.py @@ -31,6 +31,8 @@ Features: - outputs a quoted list of files and directories "selected" on exit """ +from __future__ import print_function + import itertools import re import os @@ -184,7 +186,7 @@ class DirectoryNode(urwid.ParentNode): dirs.append(a) else: files.append(a) - except OSError, e: + except OSError as e: depth = self.get_depth() + 1 self._children[None] = ErrorNode(self, parent=self, key=None, depth=depth) @@ -274,7 +276,7 @@ class DirectoryBrowser: # on exit, write the flagged filenames to the console names = [escape_filename_sh(x) for x in get_flagged_names()] - print " ".join(names) + print(" ".join(names)) def unhandled_input(self, k): # update display of focus directory diff --git a/examples/calc.py b/examples/calc.py index 2ac324a..e56be4a 100755 --- a/examples/calc.py +++ b/examples/calc.py @@ -31,6 +31,8 @@ Features: - outputs commands that may be used to recreate expression on exit """ +from __future__ import print_function + import urwid import urwid.raw_display import urwid.web_display @@ -60,7 +62,7 @@ OPERATORS = { COLUMN_KEYS = list( "?ABCDEF" ) # these lists are used to determine when to display errors -EDIT_KEYS = OPERATORS.keys() + COLUMN_KEYS + ['backspace','delete'] +EDIT_KEYS = list(OPERATORS.keys()) + COLUMN_KEYS + ['backspace','delete'] MOVEMENT_KEYS = ['up','down','left','right','page up','page down'] # Event text @@ -144,7 +146,7 @@ class Cell: if self.child is not None: return self.child.get_result() else: - return long("0"+self.edit.edit_text) + return int("0"+self.edit.edit_text) def get_result(self): """Return the numeric result of this cell's operation.""" @@ -153,7 +155,7 @@ class Cell: return self.get_value() if self.result.text == "": return None - return long(self.result.text) + return int(self.result.text) def set_result(self, result): """Set the numeric result for this cell.""" @@ -212,7 +214,7 @@ class ParentEdit(urwid.Edit): if key == "backspace": raise ColumnDeleteEvent(self.letter, from_parent=True) elif key in list("0123456789"): - raise CalcEvent, E_invalid_in_parent_cell + raise CalcEvent(E_invalid_in_parent_cell) else: return key @@ -344,7 +346,7 @@ class CellColumn( urwid.WidgetWrap ): # cell above is parent if edit.edit_text: # ..and current not empty, no good - raise CalcEvent, E_cant_combine + raise CalcEvent(E_cant_combine) above_pos = 0 else: # above is normal number cell @@ -366,13 +368,13 @@ class CellColumn( urwid.WidgetWrap ): below = self.walker.get_cell(i+1) if cell.child is not None: # this cell is a parent - raise CalcEvent, E_cant_combine + raise CalcEvent(E_cant_combine) if below is None: # nothing below return key if below.child is not None: # cell below is a parent - raise CalcEvent, E_cant_combine + raise CalcEvent(E_cant_combine) edit = self.walker.get_cell(i).edit edit.set_edit_text( edit.edit_text + @@ -453,9 +455,9 @@ class CellColumn( urwid.WidgetWrap ): cell = self.walker.get_cell(i) if cell.child is not None: - raise CalcEvent, E_new_col_cell_not_empty + raise CalcEvent(E_new_col_cell_not_empty) if cell.edit.edit_text: - raise CalcEvent, E_new_col_cell_not_empty + raise CalcEvent(E_new_col_cell_not_empty) child = CellColumn( letter ) cell.become_parent( child, letter ) @@ -579,9 +581,9 @@ class CalcDisplay: # on exit write the formula and the result to the console expression, result = self.get_expression_result() - print "Paste this expression into a new Column Calculator session to continue editing:" - print expression - print "Result:", result + print( "Paste this expression into a new Column Calculator session to continue editing:") + print( expression) + print( "Result:", result) def input_filter(self, input, raw_input): if 'q' in input or 'Q' in input: @@ -593,7 +595,7 @@ class CalcDisplay: self.wrap_keypress(k) self.event = None self.view.footer = None - except CalcEvent, e: + except CalcEvent as e: # display any message self.event = e self.view.footer = e.widget() @@ -607,7 +609,7 @@ class CalcDisplay: try: key = self.keypress(key) - except ColumnDeleteEvent, e: + except ColumnDeleteEvent as e: if e.letter == COLUMN_KEYS[1]: # cannot delete the first column, ignore key return @@ -619,7 +621,7 @@ class CalcDisplay: raise e self.delete_column(e.letter) - except UpdateParentEvent, e: + except UpdateParentEvent as e: self.update_parent_columns() return @@ -628,10 +630,10 @@ class CalcDisplay: if self.columns.get_focus_column() == 0: if key not in ('up','down','page up','page down'): - raise CalcEvent, E_invalid_in_help_col + raise CalcEvent(E_invalid_in_help_col) if key not in EDIT_KEYS and key not in MOVEMENT_KEYS: - raise CalcEvent, E_invalid_key % key.upper() + raise CalcEvent(E_invalid_key % key.upper()) def keypress(self, key): """Handle a keystroke.""" @@ -642,13 +644,13 @@ class CalcDisplay: # column switch i = COLUMN_KEYS.index(key.upper()) if i >= len( self.col_list ): - raise CalcEvent, E_no_such_column % key.upper() + raise CalcEvent(E_no_such_column % key.upper()) self.columns.set_focus_column( i ) return elif key == "(": # open a new column if len( self.col_list ) >= len(COLUMN_KEYS): - raise CalcEvent, E_no_more_columns + raise CalcEvent(E_no_more_columns) i = self.columns.get_focus_column() if i == 0: # makes no sense in help column @@ -672,7 +674,7 @@ class CalcDisplay: parent, pcol = self.get_parent( col ) if parent is None: # column has no parent - raise CalcEvent, E_no_parent_column + raise CalcEvent(E_no_parent_column) new_i = self.col_list.index( pcol ) self.columns.set_focus_column( new_i ) diff --git a/examples/dialog.py b/examples/dialog.py index 7f3a4d5..1328e79 100755 --- a/examples/dialog.py +++ b/examples/dialog.py @@ -100,7 +100,7 @@ class DialogDisplay: self.loop = urwid.MainLoop(self.view, self.palette) try: self.loop.run() - except DialogExit, e: + except DialogExit as e: return self.on_exit( e.args[0] ) def on_exit(self, exitcode): @@ -230,12 +230,12 @@ class MenuItem(urwid.Text): def keypress(self,size,key): if key == "enter": self.state = True - raise DialogExit, 0 + raise DialogExit(0) return key def mouse_event(self,size,event,button,col,row,focus): if event=='mouse release': self.state = True - raise DialogExit, 0 + raise DialogExit(0) return False def get_state(self): return self.state diff --git a/examples/fib.py b/examples/fib.py index e3262b4..ad6acc5 100755 --- a/examples/fib.py +++ b/examples/fib.py @@ -35,7 +35,7 @@ class FibonacciWalker(urwid.ListWalker): positions returned are (value at position-1, value at position) tuples. """ def __init__(self): - self.focus = (0L,1L) + self.focus = (0,1) self.numeric_layout = NumericLayout() def _get_at_pos(self, pos): diff --git a/examples/graph.py b/examples/graph.py index c21c9a9..536fa00 100755 --- a/examples/graph.py +++ b/examples/graph.py @@ -48,7 +48,7 @@ class GraphModel: data_max_value = 100 def __init__(self): - data = [ ('Saw', range(0,100,2)*2), + data = [ ('Saw', list(range(0,100,2))*2), ('Square', [0]*30 + [100]*30), ('Sine 1', [sin100(x) for x in range(100)] ), ('Sine 2', [(sin100(x) + sin100(x*2))/2 diff --git a/examples/subproc.py b/examples/subproc.py index 64eb072..4c7e918 100755 --- a/examples/subproc.py +++ b/examples/subproc.py @@ -21,7 +21,7 @@ def exit_on_enter(key): loop = urwid.MainLoop(frame_widget, unhandled_input=exit_on_enter) def received_output(data): - output_widget.set_text(output_widget.text + data) + output_widget.set_text(output_widget.text + data.decode('utf8')) write_fd = loop.watch_pipe(received_output) proc = subprocess.Popen( diff --git a/examples/subproc2.py b/examples/subproc2.py index 79c73b2..c40a647 100644 --- a/examples/subproc2.py +++ b/examples/subproc2.py @@ -1,8 +1,15 @@ # this is part of the subproc.py example +from __future__ import print_function + import sys +try: + range = xrange +except NameError: + pass + num = int(sys.argv[1]) -for c in xrange(1,10000000): +for c in range(1,10000000): if num % c == 0: - print "factor:", c + print("factor:", c) diff --git a/examples/twisted_serve_ssh.py b/examples/twisted_serve_ssh.py index aad63f9..5335646 100644 --- a/examples/twisted_serve_ssh.py +++ b/examples/twisted_serve_ssh.py @@ -31,6 +31,8 @@ Portions Copyright: 2010, Ian Ward <ian@excess.org> Licence: LGPL <http://opensource.org/licenses/lgpl-2.1.php> """ +from __future__ import print_function + import os import urwid |