diff --git a/components/common_modules/chunks.py b/components/common_modules/chunks.py index ea4b552..9f58ab5 100644 --- a/components/common_modules/chunks.py +++ b/components/common_modules/chunks.py @@ -291,9 +291,8 @@ def _passes_semantics_test(self): is_event = self._run_classifier(lemma) logger.debug(" baysian classifier result ==> %s" % is_event) return is_event - except bayes.DisambiguationError, (strerror): - pass - logger.debug(" DisambiguationError: %s" % unicode(strerror)) + except bayes.DisambiguationError as e: + logger.debug(" DisambiguationError: %s" % e) # check whether primary sense or some of the senses are events if EVITA_NOM_WNPRIMSENSE_ONLY: is_event = wordnet.primarySenseIsEvent(lemma) diff --git a/components/common_modules/constituent.py b/components/common_modules/constituent.py index d5f12d3..9588f48 100644 --- a/components/common_modules/constituent.py +++ b/components/common_modules/constituent.py @@ -233,7 +233,7 @@ def matchConstituent(self, description): if self.feature_value(feat) == value[1]: return False else: - raise "ERROR specifying description of pattern" + raise Exception("ERROR specifying description of pattern") elif type(value) is ListType: if self.feature_value(feat) not in value: if self.isChunk() and feat == 'text': diff --git a/components/common_modules/tags.py b/components/common_modules/tags.py index 60bac62..7e0f14b 100644 --- a/components/common_modules/tags.py +++ b/components/common_modules/tags.py @@ -86,7 +86,7 @@ def feature_value(self, name): logger.warn("Returning 'epos' instead of 'pos' value") return self.tree.events[self.eid][EPOS] else: - raise AttributeError, name + raise AttributeError(name) def _get_attribute(self, name, default): try: @@ -127,7 +127,7 @@ def feature_value(self, name): EVENTID, EIID, CLASS]: return None else: - raise AttributeError, name + raise AttributeError(name) def isTimex(self): return True diff --git a/components/common_modules/tokens.py b/components/common_modules/tokens.py index c7c6be5..c403036 100644 --- a/components/common_modules/tokens.py +++ b/components/common_modules/tokens.py @@ -80,7 +80,7 @@ def feature_value(self, name): EPOS, MOD, POL, EVENTID, EIID, CLASS]: return None else: - raise AttributeError, name + raise AttributeError(name) def getText(self): """Return the text of the token.""" @@ -156,7 +156,7 @@ def feature_value(self, name): return self.tree.events[self.eid].get(POS,'NONE') return self.tree.events[self.eid][name] else: - raise AttributeError, name + raise AttributeError(name) def isAdjToken(self): return True diff --git a/components/evita/features.py b/components/evita/features.py index 81774c2..dfd8d6d 100644 --- a/components/evita/features.py +++ b/components/evita/features.py @@ -296,13 +296,13 @@ def is_do_auxiliar(self): def normalizeMod(self, form): if form == 'ca': return 'can' elif form == "'d": return 'would' - else: raise "ERROR: unknown modal form: "+str(form) + else: raise Exception("ERROR: unknown modal form: "+str(form)) def normalizeHave(self, form): if form == "'d": return 'had' elif form == "'s": return 'has' elif form == "'ve": return 'have' - else: raise "ERROR: unknown raise form: "+str(form) + else: raise Exception("ERROR: unknown raise form: "+str(form)) def getPolarity(self): if self.negative: diff --git a/components/merging/sputlink/graph.py b/components/merging/sputlink/graph.py index d008314..57deb3c 100644 --- a/components/merging/sputlink/graph.py +++ b/components/merging/sputlink/graph.py @@ -341,28 +341,28 @@ def pp_html(self, filename=None, filehandle=None, standalone=False): fh = open(filename, 'w') if filename else filehandle if standalone: html_graph_prefix(fh) - fh.write("\n") - fh.write("\n\n") + fh.write(u"
\n") + fh.write(u"\n") + fh.write(u"\n\n") - fh.write("\n\n") + fh.write(u"\n\n") - fh.write("
\n") nodes = self.nodes.keys() nodes.sort(compare_id) self._html_nodes_table(fh, nodes) - fh.write("\n") + fh.write(u"\n") self._html_added_table(fh) - fh.write("
\n\n") + fh.write(u"
\n\n") if standalone: - fh.write("\n\n\n") + fh.write(u"\n\n\n") def _html_nodes_table(self, fh, nodes): - fh.write("\n") - fh.write("\n\n\n") - fh.write("
 \n\n") + fh.write(u"\n") + fh.write(u"\n\n\n") + fh.write(u" \n\n") - fh.write(" \n\n") + fh.write(u"
 \n\n") for identifier in nodes: - fh.write(" %s\n" % identifier) + fh.write(u" %s\n" % identifier) for id1 in nodes: - fh.write("\n\n
%s\n" % id1) + fh.write(u"\n\n
%s\n" % id1) for id2 in nodes: edge = self.edges[id1][id2] rel = edge.relset @@ -379,26 +379,26 @@ def _html_nodes_table(self, fh, nodes): classes.append("nocell") # rel = ' ' classes = " class=\"%s\"" % ' '.join(classes) - fh.write(" %s\n" % (classes, rel)) - fh.write("
\n\n") + fh.write(u"
%s\n" % (classes, rel)) + fh.write(u"
\n\n") def _html_added_table(self, fh): - fh.write("\n") + fh.write(u"
\n") if self.added: - fh.write("\n \n" % c) + fh.write(u"\n \n" % c) if isinstance(c.history, tuple): - fh.write("
addedderived from\n") + fh.write(u"
addedderived from\n") for c in self.added: - fh.write("
%s
%s%s\n" % str(c.history[0])) - fh.write(" %s\n" % str(c.history[1])) + fh.write(u" %s\n" % str(c.history[0])) + fh.write(u" %s\n" % str(c.history[1])) elif c.history.__class__.__name__ == 'Tag': tlink = "TLINK(relType=%s)" % c.history.attrs.get('relType') - fh.write(" %s\n" % tlink) + fh.write(u" %s\n" % tlink) elif c.history.__class__.__name__ == 'Constraint': - fh.write(" %s\n" % c.history) + fh.write(u" %s\n" % c.history) else: - fh.write("  \n") - fh.write("
\n\n") + fh.write(u"  \n") + fh.write(u"\n\n") def debug(indent=0, str=''): diff --git a/components/merging/sputlink/main.py b/components/merging/sputlink/main.py index 32a55aa..6283afe 100644 --- a/components/merging/sputlink/main.py +++ b/components/merging/sputlink/main.py @@ -130,7 +130,7 @@ def _debug_init_cycles_file(self): def _debug_print_cycle(self, constraint=None): if DEBUG: fname = "cycle-%02d.html" % self.graph.cycle - self.cycles_fh.write("

Cycle %s - %s

\n" + self.cycles_fh.write(u"

Cycle %s - %s

\n" % (self.graph.cycle, constraint)) graph_file = os.path.join(TTK_ROOT, 'data', 'tmp', fname) self.graph.pp_html(filehandle=self.cycles_fh) diff --git a/components/merging/sputlink/utils.py b/components/merging/sputlink/utils.py index 454b2f4..2edf26d 100644 --- a/components/merging/sputlink/utils.py +++ b/components/merging/sputlink/utils.py @@ -70,31 +70,31 @@ def compose_rels(self, rel1, rel2): def pp(self, filename): """Print an html table to filename.""" file = open(filename, 'w') - file.write("\n") - file.write("\n\n\n") - file.write("\n\n") - file.write("\n") - file.write("\n\n\n") - file.write("
 \n\n") + file.write(u"\n") + file.write(u"\n\n\n") + file.write(u"\n\n") + file.write(u"\n") + file.write(u"\n\n\n") + file.write(u" \n\n") + file.write(u"\n\n\n\n") header = massage(r1) - file.write("
 \n\n") rels = self.data.keys() rels.sort() for rel in rels: rel = massage(rel) - file.write(" %s\n" % rel) + file.write(u" %s\n" % rel) for r1 in rels: - file.write("\n\n
%s\n" % header) + file.write(u" %s\n" % header) for r2 in rels: r = self.data[r1][r2] if r is None: r = ' ' r = massage(r) - file.write(" %s\n" % r) - file.write("
\n\n\n\n") + file.write(u"
%s\n" % r) + file.write(u"
\n\n\n\n") def massage(str): @@ -107,15 +107,15 @@ def massage(str): def html_graph_prefix(fh): - fh.write("\n") - fh.write("\n\n\n") - fh.write("\n\n") + fh.write(u"\n") + fh.write(u"\n\n\n") + fh.write(u"\n\n") diff --git a/docs/notes/python3.md b/docs/notes/python3.md index 87178d6..e51fe7a 100644 --- a/docs/notes/python3.md +++ b/docs/notes/python3.md @@ -10,7 +10,7 @@ This was all dropped. The goal is to get a Python3 version in TTK version 3.0.0. -### 1. Process +## 1. Process For now: @@ -37,11 +37,11 @@ In the following, each section corresponds to one or more commits on the `79-pyt -### 2. Initial syntax changes +## 2. Initial syntax changes These changes are based on https://portingguide.readthedocs.io/en/latest/syntax.html. All changes made in this section are in commit [94b2bce5](https://github.com/tarsqi/ttk/commit/94b2bce5e5b68e688d4d385bcb2b022b4f1e7093). -#### 2.1. Getting rid of tabs +### 2.1. Getting rid of tabs ``` find . -name '*.py' -type f -exec bash -c 'T=$(mktemp); expand -i -t 8 "$0" > "$T" && mv "$T" "$0"' {} \; @@ -59,7 +59,7 @@ $ mv wordnet.new.py wordnet.py No problems with that. -#### 2.2. Tuple Unpacking in Parameter Lists +### 2.2. Tuple Unpacking in Parameter Lists ``` $ python-modernize -wnf lib2to3.fixes.fix_tuple_params . @@ -101,7 +101,7 @@ it is used for finding backward slinks and alinks. However, it turns out that this error also happens with the code before this change, so I will let it go. -#### 2.3. Backticks and other changes +### 2.3. Backticks and other changes The following fixes backtics and there were no problems with it: @@ -113,15 +113,15 @@ There was no need to remove the inequality operator `<>` and there were no assig -### 3. More preparatory changes +## 3. More preparatory changes Based on [http://python3porting.com/preparing.html](http://python3porting.com/preparing.html). These are similar to the above in that they allow the code to still run on Python 2.7. -#### 3.1. Division of integers +### 3.1. Division of integers Using // when we really want to have integers as the result, using / in other cases. Sometimes using `from __future__ import division` and removing explicit conversions into floats. See commit [047d9c28](https://github.com/tarsqi/ttk/commit/047d9c2850b5589e05641e182f69704f8787bb09). -#### 3.2. Using new style classes +### 3.2. Using new style classes Doing this all manually, but used the following code to find the classes. @@ -188,7 +188,7 @@ After this it all worked, but see the TODO comment in `create_dicts.py` which el See commit [0ccd82d9](https://github.com/tarsqi/ttk/commit/0ccd82d9f11e72c75b7bcbb5e71044b87a818385). -#### 3.3. Absolute imports +### 3.3. Absolute imports Changed made here are based on [https://portingguide.readthedocs.io/en/latest/imports.html](https://portingguide.readthedocs.io/en/latest/imports.html). @@ -210,9 +210,9 @@ $ python -m testing.run_tests See commit [465f9dfc](https://github.com/tarsqi/ttk/commit/465f9dfcdb6ef4b5051d6d5732f1ef116c4486f6). -#### 3.4. String handling +### 3.4. String handling -Changed made here are based on [https://portingguide.readthedocs.io/en/latest/strings.html](https://portingguide.readthedocs.io/en/latest/strings.html). +Changed made here are based on [https://portingguide.readthedocs.io/en/latest/strings.html](https://portingguide.readthedocs.io/en/latest/strings.html). See commit [c378f688](https://github.com/tarsqi/ttk/commit/c378f688321ff52111325f29ad7c705cf1292923) for all changes made in this section. For separating binary data and strings I glanced over all the lines with quoted strings in them, they all appear to be text strings. Did this for cases like `"hello"` with the find.pl script and created find.py to do the same for use of `'string'`, `u"string"`, `u'string'`, byte strings with the b prefix and the raw string. @@ -235,7 +235,7 @@ $ python-modernize -wnf libmodernize.fixes.fix_basestring . which didn't make any changes. -##### 3.4.1 File I/O +#### 3.4.1 File I/O ``` $ python-modernize -wnf libmodernize.fixes.fix_open . @@ -333,7 +333,7 @@ val = val[1:-1].split('|') And finally the tarsqi script works and so does the basic test script and the evita regression test... but not for writing the regression report, which takes us to the next section. -##### 3.4.1. Unresolved string issue +#### 3.4.1. Unresolved string issue Generating a report for the regression tests croaks the same way as the logger: @@ -347,4 +347,167 @@ TypeError: write() argument 1 must be unicode, not str This will probably show up all over the place. The annoying part is that the code would work fine on Python3 but to make it run on Python 2 we need to track down all those string literals, which I really do not want to do. So for now we just fix it for the code I need for porting to python3, including showing the results of the regression tests (and whatever else comes up later). +### 3.5. Exceptions + +[https://portingguide.readthedocs.io/en/latest/exceptions.html](https://portingguide.readthedocs.io/en/latest/exceptions.html) + +``` +$ python-modernize -wnf lib2to3.fixes.fix_except . +``` + +This introduced one change, which had to be edited manually to enter some meaningful mains instead of the standard `xxx_todo_changeme`. + +``` +$ python-modernize -wnf libmodernize.fixes.fix_raise -f libmodernize.fixes.fix_raise_six . +``` + +This made a bunch of changes, usually of the following kind: + +```diff +- raise AttributeError, name ++ raise AttributeError(name) +``` + +But some things gave warnings: + +``` +RefactoringTool: ### In file ./utilities/wordnet.py ### +RefactoringTool: Line 856: could not convert: raise "unimplemented" +RefactoringTool: Python 3 does not support string exceptions +``` + +Here is a list of all lines that had this problem: + +``` +### In file ./components/common_modules/constituent.py ### +Line 236: raise "ERROR specifying description of pattern" + +### In file ./utilities/FSA-org.py ### +Line 1641: raise "ERROR (1): expression missing at least one '('" +Line 1373: raise "ERROR specifying description of pattern" +Line 1332: raise "ERROR: possibly label is in dict format, but not the input" +Line 1650: raise "ERROR (2): expression missing at least one ')'" +Line 1881: raise 'unimplemented' +Line 1959: raise 'unimplemented' +Line 1733: raise 'extra character in pattern (possibly ")" )' + +### In file ./utilities/FSA.py ### +Line 1655: raise "ERROR (1): expression missing at least one '('" +Line 1387: raise "ERROR specifying description of pattern" +Line 1344: raise "ERROR: possibly label is in dict format, but not the input" +Line 1664: raise "ERROR (2): expression missing at least one ')'" +Line 1895: raise 'unimplemented' +Line 1973: raise 'unimplemented' +Line 1747: raise 'extra character in pattern (possibly ")" )' + +### In file ./utilities/wordnet.py ### +Line 856: raise "unimplemented" +``` + +Changed those manually by adding wrapping Exception around all strings. + +Some instances were not found by the modernize script: + +``` +$ python utilities/find.py 'raise ' +./components/evita/features.py == else: raise "ERROR: unknown modal form: "+str(form) +./components/evita/features.py == else: raise "ERROR: unknown raise form: "+str(form) +./utilities/wordnet.py == raise "unknown attribute " + key +./utilities/wordnet.py == raise self.getPointers.__doc__ +./utilities/wordnet.py == raise self.getPointers.__doc__ +``` + +These were also changed by hand. + +All these changes resulted in some errors due to non-unicode strings being written to a file in the sputlink code (see 3.4.1), those were fixed manually. + +Raising StandardError does not occur in the code. + +Raising non-exceptions were all caught I think. + +Removing `sys.exc_type`, `sys.exc_value`, `sys.exc_traceback`: + +``` +python utilities/find.py "sys.exc_" +./tarsqi.py == % (name, sys.exc_type, sys.exc_value)) +./tarsqi.py == sys.stderr.write("ERROR: %s\n" % sys.exc_value) +./tarsqi.py == sys.exit('ERROR: ' + str(sys.exc_value)) +``` + +These were manually fixed. + + +#### 3.5.1. Unresolved + +Did not look at the exception scope case. + +The command line invocation for the iteration case seems to be wrong becuase it was the same as for the new except syntax. RUnning it did give cryptic notes on files that needed to be modified. + +``` +$ python-modernize -wnf lib2to3.fixes.fix_except . +``` + +``` +RefactoringTool: Files that need to be modified: +RefactoringTool: ./tarsqi.py +RefactoringTool: ./components/blinker/main.py +RefactoringTool: ./components/classifier/vectors.py +RefactoringTool: ./components/common_modules/chunks.py +RefactoringTool: ./components/common_modules/constituent.py +RefactoringTool: ./components/common_modules/tree.py +RefactoringTool: ./components/evita/bayes.py +RefactoringTool: ./components/evita/features.py +RefactoringTool: ./components/gutime/wrapper.py +RefactoringTool: ./components/preprocessing/chunker.py +RefactoringTool: ./deprecated/xml_parser.py +RefactoringTool: ./deprecated/demo/display.py +RefactoringTool: ./docmodel/document.py +RefactoringTool: ./docmodel/main.py +RefactoringTool: ./docmodel/metadata_parser.py +RefactoringTool: ./library/classifier/create_vectors.py +RefactoringTool: ./library/evita/nominal_trainer.py +RefactoringTool: ./testing/create_slinket_cases.py +RefactoringTool: ./utilities/FSA-org.py +RefactoringTool: ./utilities/FSA.py +RefactoringTool: ./utilities/convert.py +RefactoringTool: ./utilities/make_documentation.py +RefactoringTool: ./utilities/wordnet.py +``` + + + +## 4. Remaining thingies + +List of steps still remaining. + +- standard library + + - [htps://portingguide.readthedocs.io/en/latest/stdlib-reorg.html](tps://portingguide.readthedocs.io/en/latest/stdlib-reorg.html) +- numbers and dictionaries and comprehensions + - [https://portingguide.readthedocs.io/en/latest/numbers.html](https://portingguide.readthedocs.io/en/latest/numbers.html) + - [https://portingguide.readthedocs.io/en/latest/dicts.html](https://portingguide.readthedocs.io/en/latest/dicts.html) + - [https://portingguide.readthedocs.io/en/latest/comprehensions.html](https://portingguide.readthedocs.io/en/latest/comprehensions.html) +- Other core object changes + + - [https://portingguide.readthedocs.io/en/latest/core-obj-misc.html](https://portingguide.readthedocs.io/en/latest/core-obj-misc.html) +- iterators + + - [https://portingguide.readthedocs.io/en/latest/iterators.html](https://portingguide.readthedocs.io/en/latest/iterators.html) +- built-in functions + + - [https://portingguide.readthedocs.io/en/latest/builtins.html](https://portingguide.readthedocs.io/en/latest/builtins.html) +- comparing and sorting, including rich comarison operators + - [https://portingguide.readthedocs.io/en/latest/comparisons.html](https://portingguide.readthedocs.io/en/latest/comparisons.html) + - [http://python3porting.com/preparing.html](http://python3porting.com/preparing.html) +- Other changes + - [https://portingguide.readthedocs.io/en/latest/etc.html](https://portingguide.readthedocs.io/en/latest/etc.html) + +After that we can run `python-modernize` ,`pylint --py3k` and `python -3` when running tarsqi.py and the testing and regression scripts. + +Followed by `2to3`. + +Also see: + +- http://python-future.org/automatic_conversion.html +- http://python-future.org/compatible_idioms.html diff --git a/tarsqi.py b/tarsqi.py index 8713f3e..b91aaeb 100644 --- a/tarsqi.py +++ b/tarsqi.py @@ -226,8 +226,9 @@ def _apply_component(self, name, wrapper, tarsqidocument): try: wrapper(tarsqidocument).process() except: + exc_type, exc_value, exc_traceback = sys.exc_info() logger.error("%s error:\n\t%s\n\t%s\n" - % (name, sys.exc_type, sys.exc_value)) + % (name, exc_type, exc_value)) else: wrapper(tarsqidocument).process() logger.info("%s DONE (%.3f seconds)" % (name, time.time() - t1)) @@ -374,7 +375,7 @@ def _read_arguments(args): (opts, args) = getopt.getopt(args, '', options) return opts, args except getopt.GetoptError: - sys.stderr.write("ERROR: %s\n" % sys.exc_value) + sys.stderr.write("ERROR: %s\n" % sys.exc_info()[1]) sys.exit(_usage_string()) @@ -528,4 +529,4 @@ def load_ttk_document(fname, loglevel=2, trap_errors=False): else: TarsqiWrapper(sys.argv[1:]).run() except TarsqiError: - sys.exit('ERROR: ' + str(sys.exc_value)) + sys.exit('ERROR: ' + str(sys.exc_info()[1])) diff --git a/utilities/FSA-org.py b/utilities/FSA-org.py index 041adb8..3c04ead 100644 --- a/utilities/FSA-org.py +++ b/utilities/FSA-org.py @@ -1329,7 +1329,7 @@ def labelMatches(label, input): return matchDict(label, input) else: #print "LABEL:", label, "\nINPUT:", input.nodeType - raise "ERROR: possibly label is in dict format, but not the input" + raise Exception("ERROR: possibly label is in dict format, but not the input") elif type(label) == InstanceType and hasattr(label, 'matches'): #debugFile.write("\n\tMATCH (1)") #print "\n\tMATCH (1)" @@ -1370,7 +1370,7 @@ def matchDict(label, input): if input[key] == value: return 0 else: - raise "ERROR specifying description of pattern" + raise Exception("ERROR specifying description of pattern") elif type(value) is ListType: #print "\t\t......LIST TYPE" if input[key] not in value: @@ -1638,7 +1638,7 @@ def buildBordersDict(self): or because we are in a subpattern that resetted the currentSubpattern variable to 0 """ if self.__class__.__name__ == "Sequence": pass - else: raise "ERROR (1): expression missing at least one '('" + else: raise Exception("ERROR (1): expression missing at least one '('") elif self.sequence[i] == '|': try: @@ -1647,7 +1647,7 @@ def buildBordersDict(self): except: pass previousBorder[currentSubpattern] = i if currentSubpattern != 0: - raise "ERROR (2): expression missing at least one ')'" + raise Exception("ERROR (2): expression missing at least one ')'") def getNextBorder(self, index): try: return self.bordersDict[index] @@ -1675,7 +1675,7 @@ def atomizeDescription(self): if item[0] in ['(','[','|']: #print "SIT 1" if len(item) > 1: - raise 'ERROR (1): check syntax for pattern: '+ str(self.description) + raise Exception('ERROR (1): check syntax for pattern: '+ str(self.description)) else: res.append(item) if item[0] == '~': @@ -1686,13 +1686,13 @@ def atomizeDescription(self): elif item[-1] in ['(','[','|']: #print "SIT 3" if len(item) > 1: - raise 'ERROR (2): check syntax for pattern: '+ str(self.description) + raise Exception('ERROR (2): check syntax for pattern: '+ str(self.description)) else: pass elif item[-1] in [')',']']: #print "SIT 4" if len(item) > 1: - raise 'ERROR (3): check syntax for pattern: '+ str(self.description) + raise Exception('ERROR (3): check syntax for pattern: '+ str(self.description)) else: res.append(item) elif item[-1] in ['*','+','?']: @@ -1730,7 +1730,7 @@ def compileOP(description, **options): if index < len(pattern): - raise 'extra character in pattern (possibly ")" )' + raise Exception('extra character in pattern (possibly ")" )') fsa.label = pattern.label #debugFile.write( "\n=\n\tFSA MINIMIZED:\n\t\t") #debugFile.write(str(fsa.minimized())) @@ -1878,7 +1878,7 @@ def compileItemOP(pattern, index, options): elif c == '+': fsa = iteration(fsa) else: - raise 'unimplemented' + raise Exception('unimplemented') return fsa, index @@ -1891,7 +1891,7 @@ def compileRE(s, **options): s = string.replace(s, ' ', '') fsa, index = compileREExpr(s + ')', 0, options) if index < len(s): - raise 'extra ' + repr(')') + raise Exception('extra ' + repr(')')) fsa.label = str(s) return fsa.minimized() @@ -1956,7 +1956,7 @@ def compileItem(str, index, options): elif c == '+': fsa = iteration(fsa) else: - raise 'unimplemented' + raise Exception('unimplemented') return fsa, index """ diff --git a/utilities/FSA.py b/utilities/FSA.py index 43fd5f7..8873459 100644 --- a/utilities/FSA.py +++ b/utilities/FSA.py @@ -1341,7 +1341,7 @@ def labelMatches(label, input): return matchDict(label, input) else: #print "LABEL:", label, "\nINPUT:", input.nodeType - raise "ERROR: possibly label is in dict format, but not the input" + raise Exception("ERROR: possibly label is in dict format, but not the input") elif type(label) == InstanceType and hasattr(label, 'matches'): #debugFile.write("\n\tMATCH (1)") #print "\n\tMATCH (1)" @@ -1384,7 +1384,7 @@ def matchDict(label, input): if input[key] == value: return 0 else: - raise "ERROR specifying description of pattern" + raise Exception("ERROR specifying description of pattern") elif type(value) is ListType: #print "\t\t......LIST TYPE" if input[key] not in value: @@ -1652,7 +1652,7 @@ def buildBordersDict(self): or because we are in a subpattern that resetted the currentSubpattern variable to 0 """ if self.__class__.__name__ == "Sequence": pass - else: raise "ERROR (1): expression missing at least one '('" + else: raise Exception("ERROR (1): expression missing at least one '('") elif self.sequence[i] == '|': try: @@ -1661,7 +1661,7 @@ def buildBordersDict(self): except: pass previousBorder[currentSubpattern] = i if currentSubpattern != 0: - raise "ERROR (2): expression missing at least one ')'" + raise Exception("ERROR (2): expression missing at least one ')'") def getNextBorder(self, index): try: return self.bordersDict[index] @@ -1689,7 +1689,7 @@ def atomizeDescription(self): if item[0] in ['(','[','|']: #print "SIT 1" if len(item) > 1: - raise 'ERROR (1): check syntax for pattern: '+ str(self.description) + raise Exception('ERROR (1): check syntax for pattern: '+ str(self.description)) else: res.append(item) if item[0] == '~': @@ -1700,13 +1700,13 @@ def atomizeDescription(self): elif item[-1] in ['(','[','|']: #print "SIT 3" if len(item) > 1: - raise 'ERROR (2): check syntax for pattern: '+ str(self.description) + raise Exception('ERROR (2): check syntax for pattern: '+ str(self.description)) else: pass elif item[-1] in [')',']']: #print "SIT 4" if len(item) > 1: - raise 'ERROR (3): check syntax for pattern: '+ str(self.description) + raise Exception('ERROR (3): check syntax for pattern: '+ str(self.description)) else: res.append(item) elif item[-1] in ['*','+','?']: @@ -1744,7 +1744,7 @@ def compileOP(description, **options): if index < len(pattern): - raise 'extra character in pattern (possibly ")" )' + raise Exception('extra character in pattern (possibly ")" )') fsa.label = pattern.label #debugFile.write( "\n=\n\tFSA MINIMIZED:\n\t\t") #debugFile.write(str(fsa.minimized())) @@ -1892,7 +1892,7 @@ def compileItemOP(pattern, index, options): elif c == '+': fsa = iteration(fsa) else: - raise 'unimplemented' + raise Exception('unimplemented') return fsa, index @@ -1905,7 +1905,7 @@ def compileRE(s, **options): s = string.replace(s, ' ', '') fsa, index = compileREExpr(s + ')', 0, options) if index < len(s): - raise 'extra ' + repr(')') + raise Exception('extra ' + repr(')')) fsa.label = str(s) return fsa.minimized() @@ -1970,7 +1970,7 @@ def compileItem(str, index, options): elif c == '+': fsa = iteration(fsa) else: - raise 'unimplemented' + raise Exception('unimplemented') return fsa, index """ diff --git a/utilities/wntools.py b/utilities/wntools.py index b6c6115..4ebd2b9 100644 --- a/utilities/wntools.py +++ b/utilities/wntools.py @@ -43,9 +43,9 @@ def _requireSource(entity): if not hasattr(entity, 'pointers'): if isinstance(entity, Word): - raise TypeError, repr(entity) + " is not a Sense or Synset. Try " + repr(entity) + "[0] instead." + raise TypeError(repr(entity) + " is not a Sense or Synset. Try " + repr(entity) + "[0] instead.") else: - raise TypeError, repr(entity) + " is not a Sense or Synset" + raise TypeError(repr(entity) + " is not a Sense or Synset") def tree(source, pointerType): """ diff --git a/utilities/wordnet.py b/utilities/wordnet.py index f36d922..79e2d5a 100644 --- a/utilities/wordnet.py +++ b/utilities/wordnet.py @@ -225,12 +225,12 @@ def __init__(self, line): def getPointers(self, pointerType=None): """Pointers connect senses and synsets, not words. Try word[0].getPointers() instead.""" - raise self.getPointers.__doc__ + raise Exception(self.getPointers.__doc__) def getPointerTargets(self, pointerType=None): """Pointers connect senses and synsets, not words. Try word[0].getPointerTargets() instead.""" - raise self.getPointers.__doc__ + raise Exception(self.getPointers.__doc__) def getSenses(self): """Return a sequence of senses. @@ -563,7 +563,7 @@ def __init__(sense, synset, senseTuple, verbFrames=None): elif key == 'ip': sense.position = IMMEDIATE_POSTNOMINAL else: - raise "unknown attribute " + key + raise Exception("unknown attribute " + key) sense.form = string.replace(form, '_', ' ') "orthographic representation of the Word this is a Sense of." @@ -575,7 +575,7 @@ def __getattr__(self, name): elif name == 'lexname': return self.synset.lexname else: - raise AttributeError, name + raise AttributeError(name) def __str__(self): """Return a human-readable representation. @@ -817,7 +817,7 @@ def loader(key=key, line=line, indexFile=self.indexFile): if word: return word else: - raise KeyError, "%s is not in the %s database" % (repr(form), repr(pos)) + raise KeyError("%s is not in the %s database" % (repr(form), repr(pos))) def getSynset(self, offset): pos = self.pos @@ -853,7 +853,7 @@ def __len__(self): def __getslice__(self, a, b): results = [] if type(a) == type('') and type(b) == type(''): - raise "unimplemented" + raise Exception("unimplemented") elif type(a) == type(1) and type(b) == type(1): for i in range(a, b): results.append(self[i]) @@ -877,7 +877,7 @@ def __getitem__(self, index): line = self.indexFile[index] return self.getWord(string.replace(line[:string.find(line, ' ')], '_', ' '), line) else: - raise TypeError, "%s is not a String or Int" % repr(index) + raise TypeError("%s is not a String or Int" % repr(index)) # # Dictionary protocol @@ -997,12 +997,12 @@ def __getitem__(self, index): self.file.seek(self.nextOffset) line = self.file.readline() if line == "": - raise IndexError, "index out of range" + raise IndexError("index out of range") self.nextIndex = self.nextIndex + 1 self.nextOffset = self.file.tell() return line else: - raise TypeError, "%s is not a String or Int" % repr(index) + raise TypeError("%s is not a String or Int" % repr(index)) # # Dictionary protocol @@ -1088,7 +1088,7 @@ def getSynset(pos, offset): def _requirePointerType(pointerType): if pointerType not in POINTER_TYPES: - raise TypeError, repr(pointerType) + " is not a pointer type" + raise TypeError(repr(pointerType) + " is not a pointer type") return pointerType def _compareInstances(a, b, fields): @@ -1381,13 +1381,13 @@ def _normalizePOS(pos): norm = _POSNormalizationTable.get(pos) if norm: return norm - raise TypeError, repr(pos) + " is not a part of speech type" + raise TypeError(repr(pos) + " is not a part of speech type") def _dictionaryFor(pos): pos = _normalizePOS(pos) dict = _POStoDictionaryTable.get(pos) if dict == None: - raise RuntimeError, "The " + repr(pos) + " dictionary has not been created" + raise RuntimeError("The " + repr(pos) + " dictionary has not been created") return dict def buildIndexFiles():