From 6f9d78cdedb562e662175bcb8b368d6a9ee90a32 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Fri, 21 Jan 2022 09:39:07 +0000 Subject: [PATCH 1/4] Automatically modernize code using 2to3 --- command_seq_reader.py | 20 ++++++++++---------- pyparsing_helper.py | 8 ++++---- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/command_seq_reader.py b/command_seq_reader.py index 8e63d35..130665f 100644 --- a/command_seq_reader.py +++ b/command_seq_reader.py @@ -21,7 +21,7 @@ def items_of_interest(dct, types_of_interest): return [(k, v) for (k, v) in dct.items() if isinstance(v, types_of_interest)] -def recordingexec(arg, types_of_interest = (int, basestring)): +def recordingexec(arg, types_of_interest = (int, str)): cmdbuf = [] lastcmd = '' commandInProgress = [] @@ -41,14 +41,14 @@ def recordingexec(arg, types_of_interest = (int, basestring)): commandInProgress = [] except IndentationError: pass - except SyntaxError, e: + except SyntaxError as e: if not commandUnfinished.search(str(e)): raise - except Exception, e: - raise Exception, '%s:\n%s' % (e, line) + except Exception as e: + raise Exception('%s:\n%s' % (e, line)) return (lastcmd, dict(lastlocals)) -def last_assignment_or_evaluatable(s, types_of_interest=(basestring, int, float)): +def last_assignment_or_evaluatable(s, types_of_interest=(str, int, float)): s = s.strip() if not s: return None @@ -56,12 +56,12 @@ def last_assignment_or_evaluatable(s, types_of_interest=(basestring, int, float) (lastcmd, lastlocals) = recordingexec(s, types_of_interest) try: return eval(lastcmd) - except SyntaxError, e: + except SyntaxError as e: if 'invalid syntax' not in str(e): raise if len(lastlocals) > 1: - raise ValueError, "Multiple assignments - couldn't determine which one" - return eval(lastlocals.keys()[0]) + raise ValueError("Multiple assignments - couldn't determine which one") + return eval(next(lastlocals.keys())) class RecordingExecTestSuite(unittest.TestCase): def testSimpleAssignments(self): @@ -78,7 +78,7 @@ def testIgnoreObj(self): uts = unittest.TestSuite() ''', (unittest.TestSuite)) self.assertEqual(lastcmd, 'uts = unittest.TestSuite()') - self.assertEqual(lastlocals.keys(), ['uts']) + self.assertEqual(list(lastlocals.keys()), ['uts']) def testExecutableLastLine(self): (lastcmd, lastlocals) = recordingexec(''' a = 1 @@ -116,4 +116,4 @@ def testEmpty(self): if __name__ == '__main__': #unittest.main() - doctest.testmod() \ No newline at end of file + doctest.testmod() diff --git a/pyparsing_helper.py b/pyparsing_helper.py index 5f483a0..0cca424 100755 --- a/pyparsing_helper.py +++ b/pyparsing_helper.py @@ -8,7 +8,7 @@ By Catherine Devlin (http://catherinedevlin.blogspot.com) ''' -from Tkinter import * +from tkinter import * from command_seq_reader import last_assignment_or_evaluatable import pyparsing import time, threading, functools, string, optparse, sys @@ -25,7 +25,7 @@ def _eq_monkeypatch(self, other): if isinstance(other, pyparsing.ParserElement): return self.__dict__ == other.__dict__ - elif isinstance(other, basestring): + elif isinstance(other, str): try: (self + StringEnd()).parseString(_ustr(other)) return True @@ -79,7 +79,7 @@ def apply_grammar(self, i): result = self.grammar.transformString(target) else: result = self.grammar.parseString(target).dump() - except Exception, e: + except Exception as e: result = '%s\n%s' % (str(e.__class__), str(e)) self.set_result(i, result) @@ -102,7 +102,7 @@ def reparse(self, event=None): self.grammar = last_assignment_or_evaluatable(self.grammar, types_of_interest=(pyparsing.ParserElement)) for i in range(self.num_targets): self.apply_grammar(i) - except Exception, e: + except Exception as e: if hasattr(e, 'text'): errtxt = '%s\n\n%s' % (str(e), e.text) else: From 5ecc6ed887c0b5dcf99aeef9a2823131b498d706 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Fri, 21 Jan 2022 09:40:50 +0000 Subject: [PATCH 2/4] Automatically modernize code using pyupgrade --- command_seq_reader.py | 2 +- pyparsing_helper.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/command_seq_reader.py b/command_seq_reader.py index 130665f..1c3aff3 100644 --- a/command_seq_reader.py +++ b/command_seq_reader.py @@ -45,7 +45,7 @@ def recordingexec(arg, types_of_interest = (int, str)): if not commandUnfinished.search(str(e)): raise except Exception as e: - raise Exception('%s:\n%s' % (e, line)) + raise Exception(f'{e}:\n{line}') return (lastcmd, dict(lastlocals)) def last_assignment_or_evaluatable(s, types_of_interest=(str, int, float)): diff --git a/pyparsing_helper.py b/pyparsing_helper.py index 0cca424..f541404 100755 --- a/pyparsing_helper.py +++ b/pyparsing_helper.py @@ -80,7 +80,7 @@ def apply_grammar(self, i): else: result = self.grammar.parseString(target).dump() except Exception as e: - result = '%s\n%s' % (str(e.__class__), str(e)) + result = f'{e.__class__}\n{e}' self.set_result(i, result) def set_result(self, i, txt): @@ -97,14 +97,14 @@ def set_all_results(self, txt): def reparse(self, event=None): self.grammar = self.grammar_text.get(1.0, END).strip() if self.grammar: - self.grammar = '%s\n%s' % (self.import_type.get(), self.grammar) + self.grammar = f'{self.import_type.get()}\n{self.grammar}' try: self.grammar = last_assignment_or_evaluatable(self.grammar, types_of_interest=(pyparsing.ParserElement)) for i in range(self.num_targets): self.apply_grammar(i) except Exception as e: if hasattr(e, 'text'): - errtxt = '%s\n\n%s' % (str(e), e.text) + errtxt = f'{e}\n\n{e.text}' else: errtxt = str(e) self.set_all_results(errtxt) From 4f1a5a07b8bb3efe9a0f9061101afb59fa6be867 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Fri, 21 Jan 2022 09:42:18 +0000 Subject: [PATCH 3/4] Add pyproject.toml --- pyproject.toml | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 pyproject.toml diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..5d2de54 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["setuptools>=46.1.0", "wheel"] +build-backend = "setuptools.build_meta" From 8a720277a140b6bf0a611a853e15d46c013da009 Mon Sep 17 00:00:00 2001 From: Anderson Bravalheri Date: Fri, 21 Jan 2022 12:54:26 +0000 Subject: [PATCH 4/4] Fix importing problems --- pyparsing_helper.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyparsing_helper.py b/pyparsing_helper.py index f541404..a06789b 100755 --- a/pyparsing_helper.py +++ b/pyparsing_helper.py @@ -29,10 +29,10 @@ def _eq_monkeypatch(self, other): try: (self + StringEnd()).parseString(_ustr(other)) return True - except ParseBaseException: + except pyparsing.ParseBaseException: return False else: - return super(ParserElement,self)==other + return super(pyparsing.ParserElement, self) == other pyparsing.ParserElement.__eq__ = _eq_monkeypatch