From 4ee8647190f8a8e4eb2ad6b70ae0cd87d4f01dc0 Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Tue, 29 Mar 2022 15:45:48 +0100 Subject: [PATCH 01/18] pww seems to now work under 3 --- util/pww.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/util/pww.py b/util/pww.py index d6cff9d0..a457545f 100644 --- a/util/pww.py +++ b/util/pww.py @@ -14,6 +14,7 @@ import string import os import re, urllib +import urllib.parse version = "$Id$"[1:-1] verbose = 0 @@ -23,14 +24,14 @@ def baseDirBase(abs): # basedir, tail = os.path.split(abs) while basedir != '/': wb = basedir + '/.web_base' - if verbose: print "# trying "+wb + if verbose: print("# trying " + wb) try: f = open(wb) break except IOError: basedir, tail = os.path.split(basedir) if basedir != '/': continue - print "No .web_base file for ", path + print("No .web_base file for " + path) return None, None if f: base = f.readline() @@ -46,7 +47,7 @@ def baseDirBase(abs): print__doc__ elif arg == "-v": verbose = 1 else: - print """Bad option argument.""" + __doc__ + print("""Bad option argument.""" + __doc__) sys.exit(-1) else: files.append(arg) @@ -57,9 +58,9 @@ def baseDirBase(abs): for path in files: abs = os.path.abspath(path) - if verbose: print "# abs = "+abs + if verbose: print("# abs = "+abs) basedir, newbase = baseDirBase(abs) if basedir: - print urllib.quote(abs.replace(basedir, newbase), ".:/#") + print(urllib.parse.quote(abs.replace(basedir, newbase), ".:/#")) #ends From 0eee443ee7f360376bf3c598e975dcef9ef6ca08 Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Tue, 20 Sep 2022 13:55:48 +0100 Subject: [PATCH 02/18] cant.py seems to work. On joornet from 2to3 ... --- .gitignore | 1 + ArgHandler.py | 63 +-- ConstTerm.py | 6 +- KIFSink.py | 4 +- LX/all.py | 2 +- LX/describer.py | 31 +- LX/engine/__init__.py | 4 +- LX/engine/llynInterface.py | 4 +- LX/engine/otter.py | 28 +- LX/expr.py | 28 +- LX/firstOrderLogic.py | 2 +- LX/formula.py | 6 +- LX/kb.py | 32 +- LX/ladder.py | 4 +- LX/language/__init__.py | 6 +- LX/language/abstract.py | 10 +- LX/language/basicOtter.py | 4 +- LX/language/htables.py | 6 +- LX/language/kifax.py | 30 +- LX/language/lbase.py | 26 +- LX/language/otter.py | 26 +- LX/language/prolog.py | 10 +- LX/language/raptor_rdfxml.py | 6 +- LX/language/rdflib_rdfxml.py | 8 +- LX/loader.py | 42 +- LX/logic.py | 11 +- LX/namespace.py | 4 +- LX/newkb.py | 8 +- LX/nodepath.py | 58 +-- LX/old/rdf.py | 16 +- LX/old/toShortNames.py | 2 +- LX/rdf.py | 51 +- LX/sniff.py | 8 +- LX/store/basic.py | 14 +- LX/term.py | 2 +- LX/test.py | 10 +- NTriplesSink.py | 8 +- OrderedSequence.py | 4 +- RDFSink.py | 16 +- SemEnglish.py | 8 +- StripeSkippingParser.py | 10 +- cant.py | 66 +-- check.py | 28 +- comsyn.py | 34 +- converter-cgi.py | 46 +- crawl.py | 28 +- cwm.py | 20 +- cwm_crypto.py | 12 +- cwm_list.py | 10 +- cwm_math.py | 26 +- cwm_maths.py | 12 +- cwm_os.py | 22 +- cwm_set.py | 10 +- cwm_sparql.py | 48 +- cwm_string.py | 44 +- cwm_time.py | 22 +- cwm_times.py | 18 +- cwm_trigo.py | 8 +- cwm_uri.py | 14 +- cwm_xml.py | 10 +- dbork/SqlDB.py | 154 +++--- dbork/TableRenderer.py | 6 +- dbork/dbview.py | 30 +- dbork/sparqltoy.py | 26 +- de-cr.py | 16 +- delta.py | 66 +-- diag.py | 6 +- formula.py | 64 +-- formulaUnion.py | 32 +- gram2html.py | 6 +- grammar/check-grammar.py | 76 +-- grammar/ebnf2turtle.py | 66 +-- grammar/gramLL1.py | 82 +-- grammar/lexedParser.py | 48 +- grammar/lexjsontest.py | 8 +- grammar/predictiveParser.py | 54 +- grammar/regex.py | 59 +-- grammar/sparql-grammar.py | 22 +- grammar/sparql_tokens.py | 156 +++--- grammar/tokenizer.py | 10 +- grammar/yosiParser.py | 10 +- hotswap.py | 16 +- html.py | 18 +- importList.py | 6 +- isXML.py | 4 +- isodate.py | 6 +- kifForm.py | 6 +- llyn.py | 212 ++++---- local_decimal.py | 36 +- mixin.py | 6 +- myStore.py | 8 +- my_profiler.py | 2 +- n3absyn.py | 42 +- n3p_tm.py | 10 +- n3spark.py | 16 +- notation3.py | 324 ++++++------ pim/,salesTax.py | 6 +- pim/day.py | 46 +- pim/de-space-uris.py | 8 +- pim/dedup-tunes.py | 4 +- pim/fileByDateInFilename.py | 6 +- pim/fin.py | 130 ++--- pim/fromGarmin.py | 38 +- pim/ics2txt.py | 16 +- pim/json2n3.py | 12 +- pim/ldif2n3.py | 36 +- pim/lookout.py | 45 +- plwm.py | 18 +- pretty.py | 50 +- pycwmko.py | 18 +- query.py | 198 +++---- rdf2dot.py | 6 +- rdflib2rdf.py | 14 +- rdflib_user.py | 11 +- rdfn3_yapps.py | 10 +- rdfn3_yappstest.py | 6 +- rdfpath.py | 10 +- rdfx2kif.py | 6 +- rdfxml.py | 4 +- reify.py | 21 +- sax2rdf.py | 235 +++++---- set_importer.py | 53 +- setup.py | 2 +- spark.py | 982 +++++++++++++++++------------------ sparql2cwm.py | 121 ++--- tab2n3.py | 22 +- term.py | 157 +++--- testViaOtter.py | 18 +- thing.py | 32 +- timegm.py | 47 +- toXML.py | 56 +- triple_maker.py | 10 +- update.py | 18 +- uripath.py | 94 ++-- vf2.py | 44 +- webAccess.py | 50 +- why.py | 174 +++---- wrap_llyn.py | 8 +- wrap_n3.py | 9 +- xml2rdf.py | 78 +-- xmlC14n.py | 42 +- xmllib.py | 102 ++-- yapps2.py | 98 ++-- yappsrt.py | 20 +- 144 files changed, 3002 insertions(+), 2995 deletions(-) diff --git a/.gitignore b/.gitignore index bbd18a71..d6dd0502 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ *.pyc +*.py.bak ,* diff --git a/ArgHandler.py b/ArgHandler.py index 98dee097..97739c83 100644 --- a/ArgHandler.py +++ b/ArgHandler.py @@ -10,6 +10,7 @@ import sys import inspect import re +from functools import reduce class Error(RuntimeError): pass @@ -92,15 +93,15 @@ def handle__h__help__helpEQ(self, whichOption=None): width[1], line[1]) s2 = line[2] s = wrap(s1+s2,79) - print re.sub('\\n', "\n"+" "*len(s1), s) + print(re.sub('\\n', "\n"+" "*len(s1), s)) else: member = self.findMember(whichOption) if member is None: return - line = self.genHelpEntry(member, long=1) - print "Option: ", line[0] - print "Arguments: ", line[1] - print "Description: ", wrap(line[2], 79) + line = self.genHelpEntry(member, int=1) + print("Option: ", line[0]) + print("Arguments: ", line[1]) + print("Description: ", wrap(line[2], 79)) def handle__V__version(self): """Output version information. @@ -108,10 +109,10 @@ def handle__V__version(self): (It would be nice if this gave the versions of all the modules, too, and was more automatic given python version conventions.)""" - print self.version + print(self.version) def handleNoArgs(self): - raise Error, "no options or parameters specified." + raise Error("no options or parameters specified.") def run(self): @@ -127,9 +128,9 @@ def run(self): return 0 self.handleArg(arg) self.advance() - except Error, e: - print e - print "Try --help for more information." + except Error as e: + print(e) + print("Try --help for more information.") return 1 def handleArg(self, arg): @@ -149,14 +150,14 @@ def handleArg(self, arg): except ValueError: member = self.findMember(option) args = self.buildArgs(member) - apply(member[1], args) + member[1](*args) return member = self.findMember(left+"EQ") - apply(member[1], [right]) + member[1](*[right]) def handleExtraArgument(self, arg): - raise Error, "Extra argument: \"%s\"" % arg + raise Error("Extra argument: \"%s\"" % arg) def findMember(self, arg): matches=[] @@ -176,25 +177,25 @@ def findMember(self, arg): last_match = member if len(matches) == 0: - raise Error, "unknown argument: \"%s\"" % arg + raise Error("unknown argument: \"%s\"" % arg) if len(matches) == 1: return last_match if len(matches) > 4: - raise Error, ("ambiguous option might be: \"%s\"..." % + raise Error("ambiguous option might be: \"%s\"..." % '", "'.join(matches[0:3])) if len(matches) > 1: - raise Error, ("ambiguous option might be: \"%s\"" % + raise Error("ambiguous option might be: \"%s\"" % '", "'.join(matches)) def buildArgs(self, member): """Call getNext as long as it's not a flag to fill in the argument list. If we run out, fill in with defaults if possible.""" - f = member[1].im_func - argcount = f.func_code.co_argcount - if f.func_code.co_flags & 4 : - raise Error, "handlers may not have variable arg lists" - defaults = f.func_defaults or () + f = member[1].__func__ + argcount = f.__code__.co_argcount + if f.__code__.co_flags & 4 : + raise Error("handlers may not have variable arg lists") + defaults = f.__defaults__ or () i = 1 # skip "self" args = [] @@ -207,7 +208,7 @@ def buildArgs(self, member): if indexIntoDefaults >= 0: #defaultable next = defaults[indexIntoDefaults] else: - raise Error, "not enough arguments to option \""+name+"\"" + raise Error("not enough arguments to option \""+name+"\"") args.append(next) i+=1 return args @@ -216,13 +217,13 @@ def buildArgs(self, member): def genHelpEntry(self, member, long=0): names = member[0].split("__")[1:] - names = map(lambda x: re.sub("_", "-", x), names) - names = map(lambda x: re.sub("EQ$", "=", x), names) + names = [re.sub("_", "-", x) for x in names] + names = [re.sub("EQ$", "=", x) for x in names] # primary sort key = case insensitive version, # secondary is case sensitive sortKey = names[0].lower() + " " + names[0] #print names, member - f = member[1].im_func + f = member[1].__func__ optdesc = "" for name in names: if len(name) == 1: @@ -232,11 +233,11 @@ def genHelpEntry(self, member, long=0): optdesc = optdesc[:-1] parmdesc = "" - argcount = f.func_code.co_argcount - if f.func_code.co_flags & 4 : - raise Error, "handlers may not have variable arg lists" - defaults = f.func_defaults or () - argnames = f.func_code.co_varnames + argcount = f.__code__.co_argcount + if f.__code__.co_flags & 4 : + raise Error("handlers may not have variable arg lists") + defaults = f.__defaults__ or () + argnames = f.__code__.co_varnames i = 1 # skip "self" #print "argcount", argcount #print "defaults", defaults @@ -257,7 +258,7 @@ def genHelpEntry(self, member, long=0): if f.__doc__ is None: docs = "" else: - if long: + if int: docs = re.sub('(?m)^ ', "", f.__doc__) else: doc = f.__doc__.split("\n\n", 2) diff --git a/ConstTerm.py b/ConstTerm.py index 82e0f7ba..3e2756e0 100644 --- a/ConstTerm.py +++ b/ConstTerm.py @@ -65,7 +65,7 @@ class Symbol(str): For now, at least, stick to US-ASCII characters in symbols: - >>> bug=Symbol(u"http://example/#D\u00fcrst") + >>> bug=Symbol(u"http://example/#D\\u00fcrst") Traceback (most recent call last): File "", line 1, in ? File "ConstTerm.py", line 63, in __new__ @@ -124,7 +124,7 @@ def __getitem__(self, lname): return sym -class StringLiteral(unicode): +class StringLiteral(str): _seen = {} def __new__(cls, chars): @@ -132,7 +132,7 @@ def __new__(cls, chars): try: return seen[chars] except KeyError: - lit = unicode.__new__(cls, chars) + lit = str.__new__(cls, chars) seen[chars] = lit return lit diff --git a/KIFSink.py b/KIFSink.py index 4751c141..a2a62925 100644 --- a/KIFSink.py +++ b/KIFSink.py @@ -20,7 +20,7 @@ from string import rfind, split import re -from ConstTerm import Symbol, StringLiteral +from .ConstTerm import Symbol, StringLiteral class Sink(object): @@ -76,7 +76,7 @@ def writeTerm(w, t, prefixes, vmap, level): lit = re.sub(r'[\"\\]', _escchar, t) # escape newlines? hmm... w('"%s"' % lit) else: - raise RuntimeError, "term not implemented: " + str(t) + raise RuntimeError("term not implemented: " + str(t)) def withPrefix(i, prefixes): diff --git a/LX/all.py b/LX/all.py index 2a7eb14c..f50994c9 100644 --- a/LX/all.py +++ b/LX/all.py @@ -10,7 +10,7 @@ import LX for x in LX.__all__: - exec "from LX."+x+" import *" + exec("from LX."+x+" import *") # $Log$ # Revision 1.2 2003-09-17 16:08:29 sandro diff --git a/LX/describer.py b/LX/describer.py index b0d937d4..3ca57515 100644 --- a/LX/describer.py +++ b/LX/describer.py @@ -19,7 +19,7 @@ def describe(self, object, ladder): # for now, let's be strict about it if object.__class__ is not [].__class__: - raise DescriptionFailed, 'not strictly a list' + raise DescriptionFailed('not strictly a list') # here' a more general approach try: @@ -27,14 +27,13 @@ def describe(self, object, ladder): first = object[0] rest = object[1:] except AttributeError: - raise DescriptionFailed, 'not a list -- has no __len__' + raise DescriptionFailed('not a list -- has no __len__') except TypeError: - raise DescriptionFailed, 'not a list -- has no __getitem__' + raise DescriptionFailed('not a list -- has no __getitem__') except IndexError: if ladder.has("term"): - raise RuntimeError, "FLAG GORE" + str(ladder.__dict__) - raise (DescriptionFailed, - 'Cannot describe empty list with a given term') + raise RuntimeError("FLAG GORE" + str(ladder.__dict__)) + raise DescriptionFailed # ... unless we use daml:equivalentTo, or some such else: return LX.rdfns.nil @@ -62,34 +61,34 @@ def describe(self, object, ladder): ladder = ladder.set("depth", ladder.depth+1) ladder = ladder.setIfMissing("tracePrefix", "") ladder = ladder.set("tracePrefix", ladder.tracePrefix+"| ") - print ladder.tracePrefix, "/", "describe a", type(object), object.__class__ - print ladder.tracePrefix, "value:", `object`[0:80] + print(ladder.tracePrefix, "/", "describe a", type(object), object.__class__) + print(ladder.tracePrefix, "value:", repr(object)[0:80]) for describer in self.describers: dname = describer.__class__.__name__ try: if ladder.has("trace"): - print ladder.tracePrefix, dname, "trying..." + print(ladder.tracePrefix, dname, "trying...") result = describer.describe(object, ladder) if result is not None: if ladder.has("trace"): - print ladder.tracePrefix, "\\", dname, "worked!" + print(ladder.tracePrefix, "\\", dname, "worked!") return result else: if ladder.has("trace"): - print ladder.tracePrefix, dname, "failed returning None" + print(ladder.tracePrefix, dname, "failed returning None") # return result - except DescriptionFailed, msg: + except DescriptionFailed as msg: if ladder.has("trace"): - print ladder.tracePrefix, dname, "failed", msg + print(ladder.tracePrefix, dname, "failed", msg) continue if ladder.has("trace"): - print ladder.tracePrefix, dname, "did something odd!" + print(ladder.tracePrefix, dname, "did something odd!") if ladder.has("trace"): - print ladder.tracePrefix, "*** All failed, punt back up stack" + print(ladder.tracePrefix, "*** All failed, punt back up stack") msg = "All available describers failed" else: msg = "Try turning on tracing" - raise DescriptionFailed, msg + raise DescriptionFailed(msg) def _test(): diff --git a/LX/engine/__init__.py b/LX/engine/__init__.py index d9c94fe4..d4962acd 100644 --- a/LX/engine/__init__.py +++ b/LX/engine/__init__.py @@ -62,7 +62,7 @@ def any(prefs=__all__, store=None): continue try: engine = mod.Engine(store=store) - except NotAvailable, e: + except NotAvailable as e: continue if engine is None: return NotAvailable() @@ -72,7 +72,7 @@ def think(engine=None, kb=None): try: mod = __import__("LX.engine."+engine).engine except ImportError: - raise RuntimeError, ("No such engine, \"%s\". Try one of: llyn, %s" % + raise RuntimeError("No such engine, \"%s\". Try one of: llyn, %s" % (engine, ", ".join(__all__))) mod = getattr(mod, engine) diff --git a/LX/engine/llynInterface.py b/LX/engine/llynInterface.py index 155c22cd..71cd2c24 100644 --- a/LX/engine/llynInterface.py +++ b/LX/engine/llynInterface.py @@ -55,7 +55,7 @@ def toLX(store, formula, maxDepth=4, kb=None, kbMode=0, terms={}): result = None if maxDepth < 0: - raise RuntimeError, "Formulas too deeply nested, probably looping" + raise RuntimeError("Formulas too deeply nested, probably looping") for s in formula: #print "statement", s if s[PRED] is store.forAll or s[PRED] is store.forSome: @@ -185,7 +185,7 @@ def addLXFormula(store, context, expr, terms={}, kb=None): t[index] = sym store.storeQuad(t) else: - raise RuntimeError, "Can only convert atomic formula yet, not %s" % expr + raise RuntimeError("Can only convert atomic formula yet, not %s" % expr) # $Log$ diff --git a/LX/engine/otter.py b/LX/engine/otter.py index a36421e9..00eb7a72 100644 --- a/LX/engine/otter.py +++ b/LX/engine/otter.py @@ -34,15 +34,15 @@ def kill(pid): time.sleep(0.10) def think(kb=None): - print "\n\n# Running Otter..." + print("\n\n# Running Otter...") try: proof = LX.engine.otter.run(kb) - print "# Contradiction found." - print "# for details try: cat ,lx.engine.otter.fromOtter" + print("# Contradiction found.") + print("# for details try: cat ,lx.engine.otter.fromOtter") except LX.engine.otter.SOSEmpty: - print "# Consistency proven." - print "# for details try: cat ,lx.engine.otter.fromOtter" - print "# Done running Otter [ Inferences NOT incorporated back into cwm ]" + print("# Consistency proven.") + print("# for details try: cat ,lx.engine.otter.fromOtter") + print("# Done running Otter [ Inferences NOT incorporated back into cwm ]") def run(string, fileBase=",lx.engine.otter", includes=None, maxSeconds=1, inputFileName=None, outputFileName=None): @@ -123,7 +123,7 @@ def runOtter(toOtterFilename, fromOtterFilename=None, maxSeconds=1): timeout = 1 try: kill(pid) - except OSError, e: # should only pass "No such process" + except OSError as e: # should only pass "No such process" if (str(e) == "[Errno 3] No such process"): pass else: @@ -142,22 +142,22 @@ def runOtter(toOtterFilename, fromOtterFilename=None, maxSeconds=1): if line == "Search stopped because sos empty.\n": sosEmpty = 1 outputLog.close() - except KeyboardInterrupt, k: - print - print "Caught Interrupt; killing sub-reasoner process" + except KeyboardInterrupt as k: + print() + print("Caught Interrupt; killing sub-reasoner process") try: kill(pid) except OSError: pass - raise KeyboardInterrupt, k + raise KeyboardInterrupt(k) message = "During:\n otter < %s > %s" % (toOtterFilename, fromOtterFilename) if result == []: if timeout: - raise TimeoutBeforeAnyProof, message + raise TimeoutBeforeAnyProof(message) if sosEmpty: - raise SOSEmpty, message - raise AbnormalRun, message + raise SOSEmpty(message) + raise AbnormalRun(message) #os.unlink(fromOtterFilename) # or maybe sometimes you want it left around? #print "leaving", fromOtterFilename return result diff --git a/LX/expr.py b/LX/expr.py index 271083ba..3e75f92c 100644 --- a/LX/expr.py +++ b/LX/expr.py @@ -23,7 +23,7 @@ from sys import stderr def virtual(): - raise RuntimeError, "Function should have been implemented in subclass" + raise RuntimeError("Function should have been implemented in subclass") class NotRDF(TypeError): """Something was treated as RDF, but it wasn't.""" @@ -43,7 +43,7 @@ def __and__(self, other): return CompoundExpr(pythonOperators["and"], self, o def __or__(self, other): return CompoundExpr(pythonOperators["or"], self, other) def __rshift__(self, other): return CompoundExpr(pythonOperators[">>"], self, other) def __neg__(self, other): return CompoundExpr(pythonOperators["-"], self) - def __call__(self, *args): return apply(CompoundExpr, (self,)+args) # (need apply() the handle varargs) + def __call__(self, *args): return CompoundExpr(*(self,)+args) # (need apply() the handle varargs) def isAtomic(self): return 0 @@ -118,7 +118,7 @@ def __new__(class_, function, *args): self = tuple.__new__(class_, (function,)+tuple(args)) for arg in self: if not isinstance(arg, Expr): - raise RuntimeError, "What's %s doing here?"%arg + raise RuntimeError("What's %s doing here?"%arg) #if hasattr(function, "checkArgs"): # function.checkArgs(args) @@ -218,14 +218,14 @@ def dump(self, levels=32): """Like __str__ but it gives useful output before dying on badly-formed structures.""" if levels <= 0: - print "" + print("") return - print "(", + print("(", end=' ') for arg in self.all: #print str(type(arg))+":", arg.dump(levels-1) - print " ", - print ")", + print(" ", end=' ') + print(")", end=' ') def serializeWithOperators(self, nameTable, operators, parentLooseness=9999, @@ -250,7 +250,7 @@ def serializeWithOperators(self, nameTable, operators, if prec >= parentLooseness: prefix = "("; suffix = ")" if callable(form): - result = prefix+apply(form, [text, self, nameTable, operators, prec, linePrefix])+suffix + result = prefix+form(*[text, self, nameTable, operators, prec, linePrefix])+suffix elif form == "xfx" or form == "xfy" or form == "yfx": if (len(self.args) == 2): left = self.args[0].serializeWithOperators(nameTable, operators, prec, linePrefix) @@ -264,7 +264,7 @@ def serializeWithOperators(self, nameTable, operators, result = (prefix + "\n" + linePrefix + left + text + "\n" + linePrefix + right + suffix) else: - raise RuntimeError, ("%s args on a binary operator %s" % + raise RuntimeError("%s args on a binary operator %s" % (len(self.args), self.args[0])) elif form == "fxy": assert(len(self.args) == 2) @@ -274,7 +274,7 @@ def serializeWithOperators(self, nameTable, operators, assert(len(self.args) == 1) result = prefix + text + self.args[0].serializeWithOperators(nameTable, operators, prec, linePrefix) else: - raise RuntimeError, "unknown associativity form in table of operators" + raise RuntimeError("unknown associativity form in table of operators") return result @@ -317,7 +317,7 @@ def __str__(self): return self.suggestedName def dump(self, levels): - print self.suggestedName, + print(self.suggestedName, end=' ') def getNameInScope(self, nameTable): """return a string which names this thing unambiguously in @@ -359,15 +359,15 @@ def getNameInScope(self, nameTable): if m: n = m.group(1) except KeyError: pass - for extra in xrange(1, 100000000): + for extra in range(1, 100000000): if extra == 1: newName = n else: newName = n + str(extra) - if newName not in nameTable.values(): # @ linear performance hit + if newName not in list(nameTable.values()): # @ linear performance hit nameTable[self] = newName return newName - raise RuntimeError, "wayyyyy to many similarly named expressions" + raise RuntimeError("wayyyyy to many similarly named expressions") def serializeWithOperators(self, nameTable, operators, diff --git a/LX/firstOrderLogic.py b/LX/firstOrderLogic.py index af710bc9..a4095783 100644 --- a/LX/firstOrderLogic.py +++ b/LX/firstOrderLogic.py @@ -187,7 +187,7 @@ def getOpenVariables(expr): return [expr] elif isinstance(expr.function, Quantifier): # this kind of makes us think we should be instantiating a subclass - raise RuntimeError, "Not Implemented" + raise RuntimeError("Not Implemented") else: result = [] for child in expr.all: diff --git a/LX/formula.py b/LX/formula.py index 03f77259..7b49e78b 100644 --- a/LX/formula.py +++ b/LX/formula.py @@ -91,10 +91,10 @@ def openVars(self): result = self[2] .openVars() if not isinstance(var, varClass): msg = "found a %s expecting a %s" % (var.__class__, varClass) - raise TypeError, msg + raise TypeError(msg) if var not in result: # maybe this should only be a warning? - raise RuntimeError, "Variable does not occur in child" + raise RuntimeError("Variable does not occur in child") result.remove(var) elif self.operator is LX.ATOMIC_SENTENCE: result = [] @@ -119,7 +119,7 @@ def nameVars(self, nameOverridesTable={}, namesUsedOutside=()): # We could do: and name not in self.openVars() # to allow: all x ( P(x) & all x ( Q(x) ) ) # which makes sense, but is too confusing for humans - for x in xrange(2, 100000000): + for x in range(2, 100000000): newname = "%s_%d" % (name, x) if newname not in namesUsedOutside: break diff --git a/LX/kb.py b/LX/kb.py index aba82435..decf28e5 100644 --- a/LX/kb.py +++ b/LX/kb.py @@ -14,8 +14,8 @@ import LX import LX.rdf -import sniff -import urllib +from . import sniff +import urllib.request, urllib.parse, urllib.error import LX.language import pluggable import LX.nodepath @@ -120,7 +120,7 @@ def prep(kb): if isinstance(kb, KB): return kb if isinstance(kb, list): return KB(kb) # nothing else for now - raise RuntimeError, "Not convertable to a KB" + raise RuntimeError("Not convertable to a KB") prep = staticmethod(prep) def addSupportingTheory(self, term): @@ -172,7 +172,7 @@ def addSupportingTheory(self, term): assert(val != 0) # would have been handled in LX.logic assert(val > 0) if val > 50: - raise UnsupportedDatatype, "Int %s too big" % val + raise UnsupportedDatatype("Int %s too big" % val) for n in range(1, val+1): # print "Describing",n,"via:", n-1, "succ", n lesser = LX.logic.ConstantForDatatypeValue(str(n-1), dturi) @@ -190,9 +190,9 @@ def addSupportingTheory(self, term): if val == 0: raise RuntimeError("Int 0 not caught in LX.logic? "+repr(lexrep)+", "+repr(dturi)) if val > 50: - raise UnsupportedDatatype, "Int %s too big" % val + raise UnsupportedDatatype("Int %s too big" % val) if val < 0: - raise UnsupportedDatatype, "Int %s too small" % val + raise UnsupportedDatatype("Int %s too small" % val) for n in range(1, val+1): # print "Describing",n,"via:", n-1, "succ", n lesser = LX.logic.ConstantForDatatypeValue(str(n-1), dturi) @@ -208,14 +208,14 @@ def addSupportingTheory(self, term): if m is None: return # it's not real... Report this somehow? if m.group(3) is not None and int(m.group(3)) != 0: - raise UnsupportedDatatype, "Decimal really a decimal!" % val + raise UnsupportedDatatype("Decimal really a decimal!" % val) val = int(m.group(1)) if val == 0: raise RuntimeError("Int 0 not caught in LX.logic? "+repr(lexrep)+", "+repr(dturi)) if val > 50: - raise UnsupportedDatatype, "Int %s too big" % val + raise UnsupportedDatatype("Int %s too big" % val) if val < 0: - raise UnsupportedDatatype, "Int %s too small" % val + raise UnsupportedDatatype("Int %s too small" % val) for n in range(1, val+1): # print "Describing",n,"via:", n-1, "succ", n lesser = LX.logic.ConstantForDatatypeValue(str(n-1), dturi) @@ -239,7 +239,7 @@ def addSupportingTheory(self, term): self.__datatypeValuesChecked[pair] = 1 return - raise UnsupportedDatatype, ("unsupported datatype: "+str(lexrep)+ ", type "+str(dturi)) + raise UnsupportedDatatype("unsupported datatype: "+str(lexrep)+ ", type "+str(dturi)) def add(self, formula, p=None, o=None): @@ -346,9 +346,9 @@ def gather(self, prefixMap={}): LX.logic.gatherURIs(formula, uris) #print "Did Formula", "Got", uris loadable = { } - for uri in uris.keys(): + for uri in list(uris.keys()): #print "Do with: ", uri - for (key, value) in prefixMap.iteritems(): + for (key, value) in prefixMap.items(): if uri.startswith(key): if value is None: uri = None @@ -365,7 +365,7 @@ def gather(self, prefixMap={}): pass loadable[uri] = 1 #print " ... done, as ", uri - for uri in loadable.keys(): + for uri in list(loadable.keys()): self.load(uri) def load(self, uri): @@ -384,12 +384,12 @@ def __getattr__(self, name): try: (pre, post) = name.split("_", 2) except ValueError: - raise AttributeError, ("KB has no %s attribute" % name) + raise AttributeError("KB has no %s attribute" % name) ns = getattr(self.ns, pre) term = getattr(ns, post) # if this is always true, we could just maintain an inverse map... if name != self.nickname(term): - raise RuntimeError, "NAME: %s, TERM: %s , NICK: %s" % (name, term, self.nickname(term)) + raise RuntimeError("NAME: %s, TERM: %s , NICK: %s" % (name, term, self.nickname(term))) return self.getNode(term) def getNode(self, term): @@ -458,7 +458,7 @@ def nickname(self, term): pass try: nick = "_".join(self.ns.inverseLookup(term)) - except KeyError, e: + except KeyError as e: raise KeyError(term) self.nicknames[term] = nick return nick diff --git a/LX/ladder.py b/LX/ladder.py index 4c311ba5..3a45d657 100644 --- a/LX/ladder.py +++ b/LX/ladder.py @@ -37,7 +37,7 @@ def __init__(self, *sources): else: msg = ("cant handle source of type %s class %s" % (type(source), source.__class__)) - raise RuntimeError, msg + raise RuntimeError(msg) #def __getattr__(self, key): # return self.__dict__.get(key, None) @@ -59,7 +59,7 @@ def setIfMissing(self, key, value): def __setattr__(self, name, value): # yes, they could go into __dict__ themselves... - raise RuntimeError, "Immutable Object" + raise RuntimeError("Immutable Object") def has(self, key): return key in self.__dict__ diff --git a/LX/language/__init__.py b/LX/language/__init__.py index e77b9593..1db6301c 100644 --- a/LX/language/__init__.py +++ b/LX/language/__init__.py @@ -38,8 +38,8 @@ def getSerializer(language=None, stream=None, flags=""): __import__(moduleName) return eval(moduleName+".Serializer(stream=stream, flags=flags)") else: - have = ", ".join(serializers.keys()) - raise RuntimeError, ("No such serializer: \"%s\"\nWe have: %s" % + have = ", ".join(list(serializers.keys())) + raise RuntimeError("No such serializer: \"%s\"\nWe have: %s" % (language, have)) def getParser(language=None, sink=None, flags=""): @@ -52,7 +52,7 @@ def getParser(language=None, sink=None, flags=""): # obsolete interface? return eval(moduleName+".Parser(sink=sink, flags=flags)") else: - raise RuntimeError, "No such parser: \"%s\"\nWe have: %s" % (language, ", ".join(parsers.keys())) + raise RuntimeError("No such parser: \"%s\"\nWe have: %s" % (language, ", ".join(list(parsers.keys())))) # $Log$ # Revision 1.9 2003-11-07 06:53:05 sandro diff --git a/LX/language/abstract.py b/LX/language/abstract.py index 534eea2c..5bf63cf7 100644 --- a/LX/language/abstract.py +++ b/LX/language/abstract.py @@ -37,7 +37,7 @@ def __init__(self): def serialize(self, arg): if isinstance(arg, LX.Formula): return self.serializeFormula(arg) if isinstance(arg, list): return self.serializeKB(arg) - raise RuntimeError, "Don't know how to serialize \""+`arg`+"\"" + raise RuntimeError("Don't know how to serialize \""+repr(arg)+"\"") def serializeKB(self, kb): kb = LX.KB.prep(kb) @@ -98,12 +98,12 @@ def serializeFormula(self, f, parentPrecedence=9999, linePrefix=""): assert(len(f.all) == 2) result = prefix + text + self.serializeFormula(f.all[1], prec, linePrefix) else: - raise RuntimeError, "unknown form in opTable" + raise RuntimeError("unknown form in opTable") return result def addAbbreviation(self, short, long): - self.abbrev[short] = long - self.abbrev[long] = short + self.abbrev[short] = int + self.abbrev[int] = short def serializeTerm(self, t, parentPrecedence): # print "SerializeTerm "+`t`+", "+t.racine @@ -113,7 +113,7 @@ def serializeTerm(self, t, parentPrecedence): except: # should we do auto-abbreviation??? return "'<"+t.value+">'" - raise RuntimeError, "No serialization for term: "+`t` + raise RuntimeError("No serialization for term: "+repr(t)) # $Log$ diff --git a/LX/language/basicOtter.py b/LX/language/basicOtter.py index 0dac31be..b7351b6d 100644 --- a/LX/language/basicOtter.py +++ b/LX/language/basicOtter.py @@ -177,5 +177,5 @@ def parse(rule, text): f = open(argv[2],'r') else: f = stdin - print parse(argv[1], f.read()) - else: print 'Args: []' + print(parse(argv[1], f.read())) + else: print('Args: []') diff --git a/LX/language/htables.py b/LX/language/htables.py index 9ffa75bb..4f877b55 100644 --- a/LX/language/htables.py +++ b/LX/language/htables.py @@ -10,7 +10,7 @@ from html import * import LX -import cStringIO +import io import re class Entry: @@ -56,7 +56,7 @@ def serializeKB(self, kb): for t in kb: assert t.function == LX.fol.RDF for term in t.args: - if not entry.has_key(term): + if term not in entry: e = Entry(term) e.label = term.getNameInScope(scope) # interpretations? @@ -68,7 +68,7 @@ def serializeKB(self, kb): entry[t.args[1]].asPredicate.append(t) entry[t.args[2]].asObject.append(t) - entries = entry.values() + entries = list(entry.values()) entries.sort(lambda a, b: cmp(a.label, b.label)) p1 = p("Terms used: ") diff --git a/LX/language/kifax.py b/LX/language/kifax.py index f042867d..abf61314 100644 --- a/LX/language/kifax.py +++ b/LX/language/kifax.py @@ -100,7 +100,7 @@ def t_NUMERAL(t): try: t.value = int(t.value) except ValueError: - print "Integer value too large", t.value + print("Integer value too large", t.value) t.value = 0 return t @@ -117,7 +117,7 @@ def t_newline(t): t.lineno += t.value.count("\n") def t_error(t): - print "Illegal character '%s'" % t.value[0] + print("Illegal character '%s'" % t.value[0]) t.skip(1) # Build the lexer @@ -208,7 +208,7 @@ def p_term_simple1(t): # It seems like what's actually meant is this: # @@@ uri = "http://example.com#"+t[1] uri = t[1] - if constants.has_key(uri): + if uri in constants: t[0] = constants[uri] else: tt = LX.logic.Constant(uri) @@ -236,7 +236,7 @@ def p_term_simple1(t): def p_term_numeral(t): '''term : NUMERAL''' - if constants.has_key(t[1]): + if t[1] in constants: t[0] = constants[t[1]] else: tt = LX.logic.Constant(str(t[1])) @@ -254,7 +254,7 @@ def p_term_var(t): if t[1] == frame.name: t[0] = frame.variable return - if variables.has_key(t[1]): + if t[1] in variables: t[0] = variables[t[1]] else: tt = LX.fol.UniVar(t[1]) @@ -268,9 +268,9 @@ def p_term_compound(t): # funterm # for now just read it in as FOL. args = t[2] if len(args) == 3: - t[0] = apply(LX.fol.RDF, (args[1], args[0], args[2])) + t[0] = LX.fol.RDF(*(args[1], args[0], args[2])) else: - t[0] = apply(holds[len(args)], args) + t[0] = holds[len(args)](*args) def p_sequse(t): '''sequse : @@ -325,10 +325,10 @@ def p_formula_2(t): "<=>": LX.fol.MEANS}[t[1]] #print "APPLYING", f, t[2] if (len(t[2]) > 2): - print "WARNING, too-high-arity on", f, " ** EXTRA IGNORED" - t[0] = apply(f, t[2][0:2]) + print("WARNING, too-high-arity on", f, " ** EXTRA IGNORED") + t[0] = f(*t[2][0:2]) else: - t[0] = apply(f, t[2]) + t[0] = f(*t[2]) # is LX.fol.AND allowed to be n-ary...??? # FIX HERE OR THERE???? @@ -363,7 +363,7 @@ def p_quantification(t): for var in varlist: frame = Frame() frame.name = var - frame.variable = apply(varclass, (var,)) + frame.variable = varclass(*(var,)) frame.quantifier = quantifier varStack.insert(0, frame) #print "varStack built up to", varStack @@ -374,7 +374,7 @@ def p_formula_4(t): while 1: frame = varStack.pop(0) if frame == frameSep: break - f = apply(frame.quantifier, [frame.variable, f]) + f = frame.quantifier(*[frame.variable, f]) #print "f built up to ", f #print "varStack chopped to", varStack, "@@@ forgot to quantify" t[0] = f @@ -388,7 +388,7 @@ def p_sentence_2(t): t[0] = t[1] def p_error(t): - print "Syntax error at '%s' on line %s" % (t.value, t.lineno) + print("Syntax error at '%s' on line %s" % (t.value, t.lineno)) import ply.yacc ply.yacc.yacc(tabmodule="lx_language_kifax_tab") @@ -422,7 +422,7 @@ def parse(s, to_kb): kb = to_kb ply.yacc.parse(s) -import urllib +import urllib.request, urllib.parse, urllib.error class Parser: @@ -430,7 +430,7 @@ def __init__(self, sink=None, flags=""): self.kb = sink def load(self, inputURI): - stream = urllib.urlopen(inputURI) + stream = urllib.request.urlopen(inputURI) s = stream.read() global kb kb = self.kb diff --git a/LX/language/lbase.py b/LX/language/lbase.py index 6d0a9fba..163d34ef 100644 --- a/LX/language/lbase.py +++ b/LX/language/lbase.py @@ -79,7 +79,7 @@ def t_NUMERAL(t): try: t.value = int(t.value) except ValueError: - print "Integer value too large", t.value + print("Integer value too large", t.value) t.value = 0 return t @@ -96,7 +96,7 @@ def t_newline(t): t.lineno += t.value.count("\n") def t_error(t): - print "Illegal character '%s'" % t.value[0] + print("Illegal character '%s'" % t.value[0]) t.skip(1) # Build the lexer @@ -251,13 +251,13 @@ def p_term_simple2(t): try: (pre, post) = t[1].split(":", 1) except ValueError: - raise RuntimeError, "Can't find the : in %s\n" % t[1] + raise RuntimeError("Can't find the : in %s\n" % t[1]) uri = prefixes[pre]+post t[0] = LX.logic.ConstantForURI(uri) def p_term_numeral(t): '''term : NUMERAL''' - if constants.has_key(t[1]): + if t[1] in constants: t[0] = constants[y[1]] else: tt = LX.logic.Constant(str(t[1])) @@ -279,7 +279,7 @@ def p_term_var(t): if t[1] == frame.name: t[0] = frame.variable return - if variables.has_key(t[1]): + if t[1] in variables: t[0] = variables[t[1]] else: tt = LX.fol.UniVar(t[1]) @@ -293,9 +293,9 @@ def p_term_compound(t): # for now just read it in as FOL. args = [t[1]] + t[3] if len(args) == 3: - t[0] = apply(LX.fol.RDF, (args[1], args[0], args[2])) + t[0] = LX.fol.RDF(*(args[1], args[0], args[2])) else: - t[0] = apply(holds[len(args)], args) + t[0] = holds[len(args)](*args) def p_termlist_singleton(t): '''termlist : term''' @@ -335,7 +335,7 @@ def p_formula_2(t): "or": LX.fol.OR, "implies": LX.fol.IMPLIES, "iff": LX.fol.MEANS}[t[2]] - t[0] = apply(f, (t[1], t[3])) + t[0] = f(*(t[1], t[3])) def p_formula_3(t): 'formula : NOT formula' @@ -359,7 +359,7 @@ def p_quantification(t): for var in varlist: frame = Frame() frame.name = var - frame.variable = apply(varclass, (var,)) + frame.variable = varclass(*(var,)) frame.quantifier = quantifier varStack.insert(0, frame) #print "varStack built up to", varStack @@ -370,7 +370,7 @@ def p_formula_4(t): while 1: frame = varStack.pop(0) if frame == frameSep: break - f = apply(frame.quantifier, [frame.variable, f]) + f = frame.quantifier(*[frame.variable, f]) #print "f built up to ", f #print "varStack chopped to", varStack, "@@@ forgot to quantify" t[0] = f @@ -380,7 +380,7 @@ def p_formula_5(t): t[0] = t[2] def p_error(t): - print "Syntax error at '%s' on line %s" % (t.value, t.lineno) + print("Syntax error at '%s' on line %s" % (t.value, t.lineno)) import ply.yacc ply.yacc.yacc(tabmodule="lx_language_lbase_tab") @@ -413,7 +413,7 @@ def parse(s, to_kb): kb = to_kb ply.yacc.parse(s) -import urllib +import urllib.request, urllib.parse, urllib.error class Parser: @@ -421,7 +421,7 @@ def __init__(self, sink=None, flags=""): self.kb = sink def load(self, inputURI): - stream = urllib.urlopen(inputURI) + stream = urllib.request.urlopen(inputURI) s = stream.read() global kb kb = self.kb diff --git a/LX/language/otter.py b/LX/language/otter.py index dc01921e..8b6f3050 100644 --- a/LX/language/otter.py +++ b/LX/language/otter.py @@ -5,7 +5,7 @@ __version__ = "$Revision$" # $Id$ -import cStringIO +import io import LX import LX.kb import LX.logic @@ -114,7 +114,7 @@ def serializeKB(self, expr): result += ".\n" result = result[:-1] - nspres = self.nsused.keys() + nspres = list(self.nsused.keys()) nspres.sort() for pre in nspres: self.makeComment(" prefix %s_ <%s#>" % (pre, self.nsused[pre])) @@ -128,8 +128,8 @@ def serializeKB(self, expr): def prename(self, f, names, counter): global ns - if names.has_key(f): return - if operators.has_key(f): return + if f in names: return + if f in operators: return if f.isAtomic(): #print "What to do with:", f result = None @@ -167,7 +167,7 @@ def prename(self, f, names, counter): result = exivar(+counter["e"]) counter["e"] += 1 else: - raise RuntimeError, ("Can't serialize term %s of class %s" % + raise RuntimeError("Can't serialize term %s of class %s" % (str(f), f.__class__)) #print "names %s is %s" % (f, result) names[f] = result @@ -177,13 +177,13 @@ def prename(self, f, names, counter): def serialize(kb): - str = cStringIO.StringIO() + str = io.StringIO() s = Serializer(str) s.serializeKB(kb) return str.getvalue() -import basicOtter -import urllib +from . import basicOtter +import urllib.request, urllib.parse, urllib.error import re prefixPattern = re.compile("^%\s*@prefix\s+(?P\w+)\s+<(?P.+)>\s*$") @@ -196,7 +196,7 @@ def __init__(self, sink=None, flags=""): self.consts = { } def load(self, inputURI): - stream = urllib.urlopen(inputURI) + stream = urllib.request.urlopen(inputURI) s = stream.read() tree = basicOtter.parse("inputDocument", s) @@ -205,13 +205,13 @@ def load(self, inputURI): m=prefixPattern.match(line) if m is not None: prefix[m.group("short")] = m.group("long") - allShorts = "|".join(prefix.keys()) + allShorts = "|".join(list(prefix.keys())) self.allShortsPattern = re.compile(allShorts) # traverse tree converting to kb. for f in tree: if f[0] in ("include", "set", "assign"): - print ("Warning: otter '%s' directive ignored" % f[0]) + print(("Warning: otter '%s' directive ignored" % f[0])) continue if f[0] == "formula_list": continue @@ -250,7 +250,7 @@ def convertFormula(self, f, scope={}): q=LX.logic.EXISTS vc=LX.logic.ExiVar else: - raise RuntimeError, "bad quantifier string" + raise RuntimeError("bad quantifier string") n=2 newscope=scope.copy() while isinstance(f[n], ""): @@ -262,7 +262,7 @@ def convertFormula(self, f, scope={}): terms = [] for term in f: terms.append(self.convertFormula(term)) - return apply(LX.expr.CompoundExpr, terms) + return LX.expr.CompoundExpr(*terms) # $Log$ diff --git a/LX/language/prolog.py b/LX/language/prolog.py index 077691cd..c68fc77b 100644 --- a/LX/language/prolog.py +++ b/LX/language/prolog.py @@ -23,11 +23,11 @@ def test(): t3 = LX.Term([3]) t4 = LX.Term([4]) s = Serializer() - print s.serialize((t1 & t2) | t3) - print s.serialize(t1 & (t2 | t3)) - print s.serialize(t1 & (t2 | t3) | t4) - print s.serialize(t1 & (t2 | t3 | t4)) - print s.serialize([(t1 & t2) | t3, t1 & (t2 | t3)]) + print(s.serialize((t1 & t2) | t3)) + print(s.serialize(t1 & (t2 | t3))) + print(s.serialize(t1 & (t2 | t3) | t4)) + print(s.serialize(t1 & (t2 | t3 | t4))) + print(s.serialize([(t1 & t2) | t3, t1 & (t2 | t3)])) if __name__ =='__main__': test() diff --git a/LX/language/raptor_rdfxml.py b/LX/language/raptor_rdfxml.py index 71c58872..9a814858 100644 --- a/LX/language/raptor_rdfxml.py +++ b/LX/language/raptor_rdfxml.py @@ -4,7 +4,7 @@ __version__ = "$Revision$" # $Id$ -import urllib +import urllib.request, urllib.parse, urllib.error import rdflib.URIRef import rdflib.BNode @@ -37,7 +37,7 @@ def __init__(self, sink=None, flags=""): self.bnodes = { } def load(self, inputURI): - self.parse(urllib.urlopen(inputURI)) + self.parse(urllib.request.urlopen(inputURI)) def termFor(self, s): if isinstance(s, rdflib.URIRef.URIRef): @@ -58,7 +58,7 @@ def termFor(self, s): self.bnodes[s] = tt return tt # that should be about it, right? - raise RuntimeError, "conversion from rdflib of: "+s.n3() + raise RuntimeError("conversion from rdflib of: "+s.n3()) def add(self, t): # Store in RAISED (FOL, FlatBread) form for now.... diff --git a/LX/language/rdflib_rdfxml.py b/LX/language/rdflib_rdfxml.py index 54248ffe..85e3ff6e 100644 --- a/LX/language/rdflib_rdfxml.py +++ b/LX/language/rdflib_rdfxml.py @@ -4,7 +4,7 @@ __version__ = "$Revision$" # $Id$ -import urllib +import urllib.request, urllib.parse, urllib.error import rdflib.URIRef import rdflib.BNode @@ -38,11 +38,11 @@ def __init__(self, sink=None, flags=""): self.bnodes = { } def load(self, inputURI): - self.parse(urllib.urlopen(inputURI)) + self.parse(urllib.request.urlopen(inputURI)) def termFor(self, s): if isinstance(s, rdflib.URIRef.URIRef): - return LX.logic.ConstantForURI(unicode(s).encode('UTF-8')) + return LX.logic.ConstantForURI(str(s).encode('UTF-8')) if isinstance(s, rdflib.Literal.Literal): # print "LIT: ", str(s), s.datatype if s.datatype: @@ -59,7 +59,7 @@ def termFor(self, s): self.bnodes[s] = tt return tt # that should be about it, right? - raise RuntimeError, "conversion from rdflib of: "+s.n3() + raise RuntimeError("conversion from rdflib of: "+s.n3()) def add(self, t): self.kb.add(self.termFor(t[0]), diff --git a/LX/loader.py b/LX/loader.py index b64b908a..5144d182 100644 --- a/LX/loader.py +++ b/LX/loader.py @@ -8,13 +8,13 @@ import time import os.path import os -from reporter import theNullReporter +from .reporter import theNullReporter from sys import stderr import LX import LX.kb -import sniff -import urllib -from cPickle import Pickler, Unpickler +from . import sniff +import urllib.request, urllib.parse, urllib.error +from pickle import Pickler, Unpickler defaultSuffixes=[("rdf", "applicate/rdf+xml"), ("xml", "applicate/xml"), @@ -30,10 +30,10 @@ def __init__(self, headers): class AlreadyLoaded(RuntimeError): pass -class Opener(urllib.FancyURLopener): +class Opener(urllib.request.FancyURLopener): def __init__(self, *args, **kwargs): - urllib.FancyURLopener.__init__(self, *args, **kwargs) + urllib.request.FancyURLopener.__init__(self, *args, **kwargs) # http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.25 def http_error_304(self, url, fp, errcode, errmsg, headers, data=None): @@ -48,12 +48,12 @@ def __init__(self, stream_info): self.lastModText = stream_info.getheader("Last-Modified") try: self.lastMod = time.mktime(stream_info.getdate("Last-Modified")) - except TypeError, error: + except TypeError as error: self.lastMod = None try: self.expires = time.mktime(stream_info.getdate("Expires")) - except TypeError, error: + except TypeError as error: # make up an expiration time... now=time.time() age = now-self.lastMod @@ -166,7 +166,7 @@ def run(self): try: self.openStream() self.cacheAction = "fetched" - except AlreadyLoaded, error: + except AlreadyLoaded as error: #print >>stderr, "Used cached version" self.cacheAction = "used cache" self.reporter.end("used cache") @@ -225,7 +225,7 @@ def openStream(self): if mode <= 1: try: self.loadLocal() - except NotCached, error: + except NotCached as error: if mode == 0: raise error # fall through to trying the network @@ -250,7 +250,7 @@ def openStream(self): self.reporter.msg("cached version expired %f days ago"% ((time.time() - meta.expires)/86400)) else: - print >>stderr, "Assuming cache has expired" + print("Assuming cache has expired", file=stderr) opener = Opener() @@ -277,10 +277,10 @@ def openStream(self): self.stream = opener.open(self.uri+suffix) self.reporter.msg("open connection for "+self.uri+suffix) self.stream.baseURI = self.uri - except NotImplemented, error: + except NotImplemented as error: if firstError is None: firstError = error continue - except NotModified, e: + except NotModified as e: self.reporter.msg("server says \"304 Not Modified\"; using cached version") # self.reporter.msg(str(e.headers)) #self.reporter.msg("New Expires: "+time.mktime(e.headers.getdate("Expires"))) @@ -310,7 +310,7 @@ def guessLanguageFromContent(self): pass def filename(self): - q=urllib.quote(self.uri, safe='') + q=urllib.parse.quote(self.uri, safe='') return os.path.expanduser(os.path.expandvars(self.cacheDirectory+q)) def loadLocal(self): @@ -318,7 +318,7 @@ def loadLocal(self): or raise NotCached.""" try: f=file(self.filename()+",kb", "r") - except IOError, error: + except IOError as error: self.reporter.msg("not found in internal cache") raise NotCached() self.reporter.msg( "found in internal cache") @@ -335,7 +335,7 @@ def savedMeta(self): try: f=file(self.filename()+",meta", "r") - except IOError, error: + except IOError as error: raise NotCached() p=Unpickler(f) return p.load() @@ -347,7 +347,7 @@ def saveInCache(self, meta, kb): try: os.makedirs(os.path.dirname(self.filename())) - except OSError, error: + except OSError as error: if error.strerror != "File exists": raise error f=file(self.filename()+",meta", "w") @@ -424,16 +424,16 @@ def __test1(): l=Loader(kb2, "http://www.w3.org/TR/2003/CR-owl-test-20030818/Manifest.rdf") l.cacheDirectory = tdir l.cachePolicy="remote" - print "Get Manifest" + print("Get Manifest") t0 = time.time() l.run() assert(l.cacheAction=="fetched") - print "%fs" % (time.time() - t0) - print "Get Manifest" + print("%fs" % (time.time() - t0)) + print("Get Manifest") l.cachePolicy="auto" t0 = time.time() l.run() - print "%fs" % (time.time() - t0) + print("%fs" % (time.time() - t0)) assert(l.cacheAction=="used cache") diff --git a/LX/logic.py b/LX/logic.py index 9004ee06..37ac9811 100644 --- a/LX/logic.py +++ b/LX/logic.py @@ -19,6 +19,7 @@ from sys import stderr import LX.expr +import sys # from LX.namespace import ns BELOW to avoid loop problem ################################################################ @@ -73,7 +74,7 @@ class URIConstant(Constant): __slots__ = ["uri", "suggestedName"] def __new__(class_, uri): - uri=intern(uri) + uri=sys.intern(uri) #if uri.endswith("Run"): # print >>stderr, "New called, uri:",uri," uri(id):",id(uri) try: @@ -155,8 +156,8 @@ def __new__(class_, value=None, lexrep=None, datatype=None): #print " creating new" result = Constant.__new__(class_) #print " filling in" - result.suggestedName = u"lit_"+datapair[0][:30] - result.suggestedName = u"lit" + result.suggestedName = "lit_"+datapair[0][:30] + result.suggestedName = "lit" result.lexrep = datapair[0] result.datatype= datapair[1] if value is None: @@ -323,7 +324,7 @@ def getOpenVariables(expr, unusedButQuantified=None): elif isinstance(expr, Variable): return [expr] else: - raise RuntimeError, "unknown atomic term: %s" % expr + raise RuntimeError("unknown atomic term: %s" % expr) else: if isinstance(expr.function, Quantifier): assert(len(expr.args) == 2) @@ -414,7 +415,7 @@ def asDataPair(value): if isinstance(value, int): # do nonNegativeInteger sometimes, ...? return (str(value), ns.xsd.int) - if isinstance(value, (str, unicode)): + if isinstance(value, str): return (value, None) raise NoSuitableDatatype(value) diff --git a/LX/namespace.py b/LX/namespace.py index a1058910..ca51e3cf 100644 --- a/LX/namespace.py +++ b/LX/namespace.py @@ -39,9 +39,9 @@ class NamespaceCluster: def inverseLookup(self, term): try: uri = term.uri - except AttributeError, e: + except AttributeError as e: raise TermHasNoURI(term) - for (key, value) in self.__dict__.iteritems(): + for (key, value) in self.__dict__.items(): if uri.startswith(value.uri): rest = uri[len(value.uri):] if not value.strict: diff --git a/LX/newkb.py b/LX/newkb.py index 77762810..ff500648 100644 --- a/LX/newkb.py +++ b/LX/newkb.py @@ -212,7 +212,7 @@ def prep(kb): if isinstance(kb, KB): return kb if isinstance(kb, list): return KB(copyFrom=kb) # nothing else for now - raise RuntimeError, "Not convertable to a KB" + raise RuntimeError("Not convertable to a KB") prep = staticmethod(prep) ################################################################ @@ -278,7 +278,7 @@ def __str__(self): result+= "\n exivars: "+", ".join(map(LX.expr.getNameInScope, self.exivars, [scope] * len(self.exivars))) result+= "\n univars: "+", ".join(map(LX.expr.getNameInScope, self.univars, [scope] * len(self.univars))) result+= "\n interpretation: " - for (key,valueList) in self.__interpretation.iteritems(): + for (key,valueList) in self.__interpretation.items(): result+="\n %s --> %s"%(key.getNameInScope(scope), ", ".join(map(str, valueList))) result+= "\n formulas: " result+= "\n " @@ -349,8 +349,8 @@ def add(self, formula, p=None, o=None): needed for rdf.py's flatten kind of stuff. """ if (p): - raise RuntimeError, "Use LX.rdf.Statement instead" - print formula, p, o + raise RuntimeError("Use LX.rdf.Statement instead") + print(formula, p, o) self.append(LX.logic.RDF(formula ,p,o)) # self.append(p(formula, o)) return diff --git a/LX/nodepath.py b/LX/nodepath.py index 4176d016..584c4005 100644 --- a/LX/nodepath.py +++ b/LX/nodepath.py @@ -19,7 +19,7 @@ __version__ = "$Revision$" # $Id$ -from __future__ import generators + import re import LX @@ -48,7 +48,7 @@ def __getattr__(self, name): try: (pre, post) = name2.split("_", 2) except ValueError: - raise AttributeError, ("no %s attribute" % name) + raise AttributeError("no %s attribute" % name) ns = getattr(self.kb.ns, pre) term = getattr(ns, post) result = Path(self, term, invert) @@ -59,15 +59,15 @@ def __getattr__(self, name): def __str__(self): try: name = self.kb.nickname(self.fromTerm) - except LX.namespace.NoShortNameDeclared, error: + except LX.namespace.NoShortNameDeclared as error: name = "Node("+str(self.fromTerm)+")" - except LX.namespace.TermHasNoURI, error: + except LX.namespace.TermHasNoURI as error: name = "Node("+str(self.fromTerm)+")" done = {} return name+" -- "+self.dump(done) def __repr__(self): - return "Node"+"("+`self.kb`+","+`self.fromTerm`+")" + return "Node"+"("+repr(self.kb)+","+repr(self.fromTerm)+")" def preFill(self, attr, value, term, invert): @@ -80,7 +80,7 @@ def preFill(self, attr, value, term, invert): def dump(self, done): """needs shorter names, maybe line breaking, is-of handling, ...""" - if done.has_key(self): + if self in done: return "[loop]" done[self] = 1 result = "[ " @@ -89,7 +89,7 @@ def dump(self, done): except: pass keytexts = [] # keytexts.append("**"+str(self.arcLists)) - for (key, values) in self.arcLists.iteritems(): + for (key, values) in self.arcLists.items(): try: nick = self.kb.nickname(key[0]) @@ -104,7 +104,7 @@ def dump(self, done): if isinstance(v, Node): valuetexts.append(v.dump(done)) else: - valuetexts.append("<"+`v`+">") + valuetexts.append("<"+repr(v)+">") keytext += ", ".join(valuetexts) keytexts.append(keytext) result += "; ".join(keytexts) @@ -139,7 +139,7 @@ def __getattr__(self, name): try: (pre, post) = name.split("_", 2) except ValueError: - raise AttributeError, ("no %s attribute" % name) + raise AttributeError("no %s attribute" % name) ns = getattr(self.kb.ns, pre) return self.stepTo(getattr(ns, post), invert) @@ -152,9 +152,9 @@ def __str__(self): def __repr__(self): if self.invert: - return "Path"+"("+`self.from_`+","+`self.via`+", invert=1)" + return "Path"+"("+repr(self.from_)+","+repr(self.via)+", invert=1)" else: - return "Path"+"("+`self.from_`+","+`self.via`+")" + return "Path"+"("+repr(self.from_)+","+repr(self.via)+")" def first(self): if isinstance(self.from_, Path): @@ -182,12 +182,12 @@ def __iter__(self): def only(self): i = self.__iter__() try: - a = i.next(); + a = next(i); except StopIteration: - raise KeyError, `self`+" 'only' violation, too few" + raise KeyError(repr(self)+" 'only' violation, too few") try: - b = i.next(); - raise KeyError, `self`+" 'only' violation, too many" + b = next(i); + raise KeyError(repr(self)+" 'only' violation, too many") except StopIteration: pass return a @@ -202,9 +202,9 @@ def uriGetOnly(self): uri = node.fromTerm.uri else: if uri != node.fromTerm.uri: - raise KeyError, 'more than one value for URI' + raise KeyError('more than one value for URI') if uri is None: - raise KeyError, 'no value for URI' + raise KeyError('no value for URI') return uri # this doesnt work because we have a __getattr_ defined @@ -217,36 +217,36 @@ def dataGetOnly(self): data = node.fromTerm.data else: if data != node.fromTerm.data: - raise KeyError, 'more than one value for DATA' + raise KeyError('more than one value for DATA') if data is None: - raise KeyError, 'no value for DATA' + raise KeyError('no value for DATA') return data def x(self): """test""" - print "---------------" - print "I am: ",`self` - print "I have:",self.preFilled + print("---------------") + print("I am: ",repr(self)) + print("I have:",self.preFilled) self.from_.x() def dump(self, done): """broken""" - if done.has_key(self): - print "[loop]" + if self in done: + print("[loop]") return done[self] = 1 - print "[ ", + print("[ ", end=' ') for sub in self.preFilled: - print key, + print(key, end=' ') if isinstance(value, Path): value.dump(done) else: - print "<"+`value`+">" + print("<"+repr(value)+">") try: - print '"'+'"^^<'.join(LX.logic.valuesForConstants[self.fromTerm])+'>', + print('"'+'"^^<'.join(LX.logic.valuesForConstants[self.fromTerm])+'>', end=' ') except KeyError: pass - print " ]", + print(" ]", end=' ') #class PathResultIterator: # def __init__(self, path): diff --git a/LX/old/rdf.py b/LX/old/rdf.py index 02e69c2d..ac5342c4 100644 --- a/LX/old/rdf.py +++ b/LX/old/rdf.py @@ -48,7 +48,7 @@ def n3NameGen(scope): while 1: name = "_:g%d" % val val = val + 1 - if not scope.has_key(name): break + if name not in scope: break scope["__next"] = val return name @@ -57,7 +57,7 @@ def prologNameGen(scope): while 1: name = "g%d" % val val = val + 1 - if not scope.has_key(name): break + if name not in scope: break scope["__next"] = val return name @@ -94,7 +94,7 @@ def getN3(self, scope): if self.uri != None: return "<" + self.uri + ">" lr = repr(self) - if scope.has_key(lr): + if lr in scope: return scope[lr] else: name = n3NameGen(scope) @@ -107,7 +107,7 @@ def getProlog(self, scope): if self.uri != None: return "'<" + self.uri + ">'" lr = repr(self) - if scope.has_key(lr): + if lr in scope: return scope[lr] else: name = prologNameGen(scope) @@ -120,7 +120,7 @@ def getOtter(self, scope): if self.uri != None: return "'<" + self.uri + ">'" lr = repr(self) - if scope.has_key(lr): + if lr in scope: return scope[lr] else: name = prologNameGen(scope) @@ -148,12 +148,12 @@ def getFromN3(term, scope): m = n3bnodepat.match(term) if m: k = m.group(0) - if scope.has_key(k): return scope[k] + if k in scope: return scope[k] i = Ident() scope[k] = i; scope[i] = k; return i - raise RuntimeError, "Can't parse N3 Term: %s" % term + raise RuntimeError("Can't parse N3 Term: %s" % term) def read(filename): global genidCount @@ -167,7 +167,7 @@ def read(filename): line = s.readline() if not line: break parts = string.split(line[:-3], ' ', 2) - transparts = map(lambda x: getFromN3(x, mapping), parts); + transparts = [getFromN3(x, mapping) for x in parts]; result.append(transparts) return result diff --git a/LX/old/toShortNames.py b/LX/old/toShortNames.py index 160f5e43..868f6b1d 100755 --- a/LX/old/toShortNames.py +++ b/LX/old/toShortNames.py @@ -44,6 +44,6 @@ def trans(line, table=globalTable): line = sys.stdin.readline() while line: line = trans(line) - print line, + print(line, end=' ') line = sys.stdin.readline() diff --git a/LX/rdf.py b/LX/rdf.py index 829ae5db..96ecfb5e 100644 --- a/LX/rdf.py +++ b/LX/rdf.py @@ -48,7 +48,7 @@ def unflatten(f): The triples used to make those descriptions are optionally removed from the result, depending on the kind of inference required. """ - raise RuntimeError, "Not Implemented" + raise RuntimeError("Not Implemented") class Reconstructed: def __init__(self, fromTerm): @@ -56,25 +56,25 @@ def __init__(self, fromTerm): self.args=[] self.op=None def dump(self, done): - if done.has_key(self): - print "[loop]" + if self in done: + print("[loop]") return done[self] = 1 - print "[ ", - for (key, value) in self.__dict__.iteritems(): + print("[ ", end=' ') + for (key, value) in self.__dict__.items(): if key is "fromTerm": continue if key is "args": continue if key is "op": continue - print key, + print(key, end=' ') if isinstance(value, Reconstructed): value.dump(done) else: - print "<"+str(value)+">" + print("<"+str(value)+">") try: - print '"'+'"^^<'.join(LX.logic.valuesForConstants[self.fromTerm])+'>', + print('"'+'"^^<'.join(LX.logic.valuesForConstants[self.fromTerm])+'>', end=' ') except KeyError: pass - print " ]", + print(" ]", end=' ') # could construct this from class info... decode = { @@ -114,28 +114,28 @@ def reconstruct(kb, keys, recons, byClass=None, class_=Reconstructed, cluster=No for f in kb: if f.function != LX.logic.RDF: - raise RuntimeError, "Not pure-RDF KB!" + raise RuntimeError("Not pure-RDF KB!") continue (subj, pred, obj) = f.args - subjRecon = recons.setdefault(subj, apply(class_,[subj])) + subjRecon = recons.setdefault(subj, class_(*[subj])) if pred == ns.rdf.type and byClass is not None: byClass.setdefault(obj, []).append(subjRecon) if keys is not None: try: k = keys[pred] except KeyError: - print "ignoring triple, pred", pred + print("ignoring triple, pred", pred) continue elif cluster is not None: try: k = "_".join(cluster.inverseLookup(pred)) except KeyError: - print "ignoring triple, pred not in any NS", pred + print("ignoring triple, pred not in any NS", pred) continue else: k = localKeys.setdefault(pred, pred.getNameInScope(nameTable)) - objRecon = recons.setdefault(obj, apply(class_,[obj])) + objRecon = recons.setdefault(obj, class_(*[obj])) if isinstance(k, type("x")) or k[0] is None: setattr(subjRecon, k, objRecon) else: @@ -183,7 +183,7 @@ def asExpr(r, map): newmap=map.copy() newmap[t] = v result=LX.expr.CompoundExpr(r.op, v, asExpr(r.args[0], newmap)) - print "Result", result + print("Result", result) return result if r.op == LX.logic.EXISTS: t=r.args[0] @@ -191,24 +191,24 @@ def asExpr(r, map): newmap=map.copy() newmap[t] = v result=LX.expr.CompoundExpr(r.op, v, asExpr(r.args[0], newmap)) - print "Result", result + print("Result", result) return result e = [r.op] for t in r.args: e.append(asExpr(t, map)) - result = apply(LX.expr.CompoundExpr, e) - print "Result", result + result = LX.expr.CompoundExpr(*e) + print("Result", result) return result if hasattr(r, "uri"): uri = str(r.uri.fromTerm) - print "URI", uri + print("URI", uri) result= LX.logic.ConstantForURI(uri) - print "Result", result + print("Result", result) return result result=LX.logic.Constant() # constant with URI? Odd for RDF. - print "Result", result + print("Result", result) return result ################################################################ @@ -218,10 +218,9 @@ class VariableDescriber: def describe(self, object, ladder): if not isinstance(object, LX.logic.Variable): - raise DescriptionFailed, 'not a variable term' + raise DescriptionFailed('not a variable term') if ladder.has("term"): - raise (DescriptionFailed, - 'Cannot describe variable as some term w/out eq') + raise DescriptionFailed else: #@@@ keep a global map to exivars? global vars @@ -249,7 +248,7 @@ def describe(self, object, ladder): try: uri=object.uri except AttributeError: - raise DescriptionFailed, 'not a uriref term' + raise DescriptionFailed('not a uriref term') if ladder.has("term"): term = ladder.term else: @@ -275,7 +274,7 @@ class FormulaDescriber: def describe(self, object, ladder): if object.isAtomic(): - raise DescriptionFailed, "atomic (can't be formula)" + raise DescriptionFailed("atomic (can't be formula)") if ladder.has("term"): term = ladder.term else: diff --git a/LX/sniff.py b/LX/sniff.py index 2b836831..cfd58e6a 100644 --- a/LX/sniff.py +++ b/LX/sniff.py @@ -5,7 +5,7 @@ # $Id$ import re -import cStringIO +import io import xml.sax ianaBase = "http://www.isi.edu/in-notes/iana/assignments/media-types/" @@ -127,7 +127,7 @@ def makeSeekable(stream): """ tmp = stream.read() - s = cStringIO.StringIO(tmp) + s = io.StringIO(tmp) stream.fp = s stream.read = stream.fp.read stream.readline = stream.fp.readline @@ -163,7 +163,7 @@ def sniffXML(stream, lang): parser.setFeature(xml.sax.handler.feature_namespaces, 1) try: parser.parse(stream) - except EnoughHasBeenRead, j: + except EnoughHasBeenRead as j: if j.nsname.endswith("#"): return j.nsname+j.elname else: @@ -179,7 +179,7 @@ def __init__(self, nsname, elname): class sniffXMLHandler(xml.sax.handler.ContentHandler): def startElementNS(self, name, qname, attrs): - raise EnoughHasBeenRead, name + raise EnoughHasBeenRead(name) diff --git a/LX/store/basic.py b/LX/store/basic.py index 3d25dab5..2caf5410 100644 --- a/LX/store/basic.py +++ b/LX/store/basic.py @@ -144,11 +144,11 @@ def remove(self, formula, why, formulaRemoved=None): """ try: reasons = self._why[formula] - except KeyError, e: + except KeyError as e: raise FormulaNotFound try: del reasons[why] - except KeyError, e: + except KeyError as e: raise GivenReasonNotUsed() if len(reasons) == 0: self._formulas.remove(formula) @@ -164,10 +164,10 @@ def __iter__(self): return self._formulas.__iter__() def why(self, formula): - return self._why[formula].keys() + return list(self._why[formula].keys()) def __in__(self, formula): - return formula in self._why.keys() + return formula in list(self._why.keys()) def getFormulas(self): return self._formulas @@ -190,11 +190,11 @@ def removeFrom(self, other): class Print: def add(self, formula, why, formulaAdded=None): - print "(PRINT) Add '%s', because '%s', formulaAdded=%s" % ( - formula, why, formulaAdded) + print("(PRINT) Add '%s', because '%s', formulaAdded=%s" % ( + formula, why, formulaAdded)) def remove(self, formula, why, formulaRemoved=None): - print "(PRINT) Remove '%s', because '%s', formulaRemoved=%s" % (formula, why, formulaRemoved) + print("(PRINT) Remove '%s', because '%s', formulaRemoved=%s" % (formula, why, formulaRemoved)) if __name__ == "__main__": diff --git a/LX/term.py b/LX/term.py index 884fb888..7fcfd867 100644 --- a/LX/term.py +++ b/LX/term.py @@ -16,7 +16,7 @@ def __str__(self): def getVars(self): """not called OpenVars to give us more type checking""" - raise RuntimeError, "please override" + raise RuntimeError("please override") class Variable(Term): def __init__(self, name=None, uriref=None): diff --git a/LX/test.py b/LX/test.py index af1496c6..8c75f597 100644 --- a/LX/test.py +++ b/LX/test.py @@ -10,15 +10,15 @@ import LX for x in LX.__all__: - print "Module", "%-25s" %x, + print("Module", "%-25s" %x, end=' ') lx = __import__("LX."+x) m = getattr(lx, x) - print "failed %3d of %3d doctest lines." % doctest.testmod(m) + print("failed %3d of %3d doctest lines." % doctest.testmod(m)) - for (name, value) in m.__dict__.iteritems(): + for (name, value) in m.__dict__.items(): if name.startswith("__test") and callable(value): - print "...running",x+"."+name+"()" - apply(value, []) + print("...running",x+"."+name+"()") + value(*[]) # $Log$ # Revision 1.7 2003-09-17 16:09:58 sandro diff --git a/NTriplesSink.py b/NTriplesSink.py index 246cf4ff..3832ef93 100644 --- a/NTriplesSink.py +++ b/NTriplesSink.py @@ -33,13 +33,15 @@ def termFor(self, value=None, uri=None): # do URI-escaping to 7-bit ascii? what's the spec say? result = "<"+uri+">" else: - raise Error, "You can't give both a value and a uri" + raise Error("You can't give both a value and a uri") return result def insert(self, arg): for a in arg: - print a, - print "." + print(a, + end=" " + ) + print(".") if __name__ == "__main__": import doctest, sys diff --git a/OrderedSequence.py b/OrderedSequence.py index 76cac5ff..21b9afa5 100644 --- a/OrderedSequence.py +++ b/OrderedSequence.py @@ -70,13 +70,13 @@ def minus(a,b): if j==n: # No more of b, return rest of a return result + a[i:] if i==m: # No more of a, some of b - error - raise ValueError("Cannot remove items" + `b[j:]`) + raise ValueError("Cannot remove items" + repr(b[j:])) return result + b[j:] if a[i] < b[j]: result.append(a[i]) i = i + 1 elif a[i] > b[j]: - raise ValueError("Cannot remove item" + `b[j]`) + raise ValueError("Cannot remove item" + repr(b[j])) j = j + 1 else: # a[i]=b[j] i = i + 1 diff --git a/RDFSink.py b/RDFSink.py index 0acfa327..3dfbb3cf 100644 --- a/RDFSink.py +++ b/RDFSink.py @@ -19,11 +19,11 @@ __version__ = "$Id$" -import uripath +from . import uripath import time from warnings import warn -from diag import progress +from .diag import progress # The statement is stored as a quad - affectionately known as a triple ;-) # offsets when a statement is stored as a Python tuple (c, p, s, o) @@ -86,10 +86,10 @@ # Standard python modules: from os import getpid from time import time -from uripath import base, join +from .uripath import base, join # SWAP modules: -from diag import verbosity, progress +from .diag import verbosity, progress from os import environ runNamespaceValue = None @@ -103,7 +103,7 @@ def runNamespace(): runNamespaceValue = environ["CWM_RUN_NS"] except KeyError: runNamespaceValue = uripath.join( - uripath.base(), ".run-" + `time()` + "p"+ `getpid()` +"#") + uripath.base(), ".run-" + repr(time()) + "p"+ repr(getpid()) +"#") runNamespaceValue = join(base(), runNamespaceValue) # absolutize return runNamespaceValue @@ -112,7 +112,7 @@ def uniqueURI(): "A unique URI" global nextu nextu += 1 - return runNamespace() + "u_" + `nextu` + return runNamespace() + "u_" + repr(nextu) class URISyntaxError(ValueError): """A parameter is passed to a routine that requires a URI reference""" @@ -262,7 +262,7 @@ def genId(self): while not subj: subj = self._genPrefix assert subj # don't mask bugs - subj = subj + `self._nextId` + subj = subj + repr(self._nextId) self._nextId = self._nextId + 1 try: self.checkNewId(subj) # For a store, just in case, check really unique @@ -363,7 +363,7 @@ def endFormulaObject(self, pred, subj): # Remove context pass -from diag import printState +from .diag import printState class TracingRDFSink: """An implementation of the RDFSink interface which helps me understand it, especially how it gets used by parsers vs. by an diff --git a/SemEnglish.py b/SemEnglish.py index ed15031c..37c64d19 100644 --- a/SemEnglish.py +++ b/SemEnglish.py @@ -16,7 +16,7 @@ from string import * import re -from yappsrt import * +from .yappsrt import * class SemEnglishScanner(Scanner): def __init__(self, str): @@ -53,7 +53,7 @@ def Statement(self): AddPredicate = self.AddPredicate() if self._peek('"\\\\."', 'END', '"}"', 'Word', 'Number', '"\\\\("', '"\\\\["') == '"\\\\."': self._scan('"\\\\."') - print "@@got one statement" + print("@@got one statement") def Subject(self): _token_ = self._peek('Word', 'Number', '"\\\\("', '"\\\\["') @@ -175,5 +175,5 @@ def parse(rule, text): f = open(argv[2],'r') else: f = stdin - print parse(argv[1], f.read()) - else: print 'Args: []' + print(parse(argv[1], f.read())) + else: print('Args: []') diff --git a/StripeSkippingParser.py b/StripeSkippingParser.py index c11888f3..465c8b3a 100644 --- a/StripeSkippingParser.py +++ b/StripeSkippingParser.py @@ -26,7 +26,7 @@ import inspect import re import xml.sax -from NTriplesSink import FormulaSink +from .NTriplesSink import FormulaSink class Error(RuntimeError): pass @@ -112,23 +112,23 @@ def startElementNS(self, name, qname, attrs): self.parents.insert(0, me) self.awaitingFirstValue = 1 else: - raise RuntimeError, "not upper or lower?" + raise RuntimeError("not upper or lower?") def prepareForIndividual(self): if len(self.parents) % 2 == 1: - print "# need to infer a property stripe" + print("# need to infer a property stripe") self.parents.insert(0, self.rdfli) def prepareForProperty(self): if len(self.parents) % 2 == 0: - print "# need to infer an individual stripe" + print("# need to infer an individual stripe") i = self.instanceOf() self.parents.insert(0, i) self.sink.insert((self.parents[2], self.parents[1], self.parents[0])) def endElementNS(self, name, qname): if self.awaitingFirstValue: - raise Error, "property with no value given" # use this syntax for named things? + raise Error("property with no value given") # use this syntax for named things? if self.firstValueBuffer is not None: self.sink.insert((self.parents[1], self.parents[0], self.firstValueBuffer)) self.firstValueBuffer = None diff --git a/cant.py b/cant.py index 64040366..5783a38b 100755 --- a/cant.py +++ b/cant.py @@ -15,7 +15,7 @@ and compared with the input files. the result is a list of differences instead of the canonicalizd graph. This is NOT a minimal diff. Exits with nonzero system status if graphs do not match. - + This is an independent n-triples cannonicalizer. It uses heuristics, and will not terminate on all graphs. It is designed for testing: the output and the reference output are both canonicalized and compared. @@ -40,18 +40,18 @@ .google graph isomorphism See also eg http://www.w3.org/2000/10/rdf-tests/rdfcore/utils/ntc/compare.cc NTriples: see http://www.w3.org/TR/rdf-testcases/#ntriples - + Not to mention, published this month by coincidence: Kelly, Brian, [Whitehead Institute] "Graph cannonicalization", Dr Dobb's Journal, May 2003. - + $Id$ This is or was http://www.w3.org/2000/10/swap/cant.py W3C open source licence . 2004-02-31 Serious bug fixed. This is a test program, shoul dbe itself tested. Quis custodiet ipsos custodes? - - + + From Manu Sporny 2012-01-16: Differences between cant.py and our implementation from Dave Longley: @@ -62,7 +62,7 @@ # canticle - Canonicalizer of NTriples Independent of Cwm , Llyn, Etc. ? import os import sys -import urllib +import urllib.request, urllib.parse, urllib.error try: from swap import uripath # http://www.w3.org/2000/10/swap/ except ImportError: @@ -82,19 +82,19 @@ language = r'[a-z0-9]+(?:-[a-z0-9]+)?' string_pattern = r'".*"' # We know in ntriples that there can only be one string on the line langString = string_pattern + r'(?:@' + language + r')?' -datatypeString = langString + '(?:\^\^' + uriref + r')?' +datatypeString = langString + '(?:\^\^' + uriref + r')?' #literal = langString + "|" + datatypeString object = r'(' + nodeID + "|" + datatypeString + "|" + uriref + r')' ws = r'[ \t]*' -com = ws + r'(#.*)?[\r\n]*' +com = ws + r'(#.*)?[\r\n]*' comment = re.compile("^"+com+"$") -statement = re.compile( ws + object + ws + object + ws + object + com) # +statement = re.compile( ws + object + ws + object + ws + object + com) # #" def usage(): - print __doc__ + print(__doc__) def loadFiles(testFiles): graph = [] @@ -104,10 +104,10 @@ def loadFiles(testFiles): if verbose: stderr.write("Loading data from %s\n" % fn) uri = uripath.join(WD, fn) - inStream = urllib.urlopen(uri) + inStream = urllib.request.urlopen(uri) while 1: line = inStream.readline() - if line == "" : break + if line == "" : break # if verbose: stderr.write("%s\n" % line) m = comment.match(line) if m != None: continue @@ -152,12 +152,12 @@ def main(): if o in ("-d", "--diff", "--to"): diffFiles.append(a) - + if testFiles == []: testFiles = [ "/dev/stdin" ] graph = loadFiles(testFiles) graph = canonicalize(graph) - + if diffFiles != []: graph2 = loadFiles(diffFiles) graph2 = canonicalize(graph2) @@ -197,9 +197,9 @@ def compareCanonicalGraphs(g1, g2): inserted.append(g2[i2]) i2 += 1 for triple in deleted: - print "- %s %s %s." % triple + print("- %s %s %s." % triple) for triple in inserted: - print "+ %s %s %s." % triple + print("+ %s %s %s." % triple) number = len(deleted) + len(inserted) if verbose: if number == 0: stderr.write("FILES MATCH.\n") @@ -218,40 +218,40 @@ def canonicalize(g): def serialize(graph): graph.sort() - if verbose: print "# Canonicalized:" + if verbose: print("# Canonicalized:") for t in graph: for x in t: if x.startswith("__"): x = x[1:] - print x, - print "." + print(x, end=' ') + print(".") def compareFirst(a,b): "Compare consistently nested lists of strings" - d = cmp(`a[0]`, `b[0]`) + d = cmp(repr(a[0]), repr(b[0])) if verbose: - if d<0: stderr.write("Comparing: %s]\n LESS THAN %s\n" % (`a`,`b`)) - elif d>0: stderr.write("Comparing: %s]\n LESS THAN %s\n" % (`b`,`a`)) - else: stderr.write("Comparing: %s]\n EQUALS %s\n" % (`b`,`a`)) + if d<0: stderr.write("Comparing: %s]\n LESS THAN %s\n" % (repr(a),repr(b))) + elif d>0: stderr.write("Comparing: %s]\n LESS THAN %s\n" % (repr(b),repr(a))) + else: stderr.write("Comparing: %s]\n EQUALS %s\n" % (repr(b),repr(a))) return d - + #@@@@@@@@@@@@ if a==None and b == None: return 0 if a == None: return -1 if b == None: return 1 - if isinstance(a, types.IntType): - if isinstance (b,types.IntType): return a-b + if isinstance(a, int): + if isinstance (b,int): return a-b else: return -1 # Ints are less than strings or lists - if isinstance(a, types.StringTypes): - if isinstance (b, types.IntType): return 1 - if isinstance (b,types.StringTypes): + if isinstance(a, (str,)): + if isinstance (b, int): return 1 + if isinstance (b,(str,)): if a < b: return -1 if a > b: return 1 return 0 else: return -1 # Strings are less than lists else: # a is list - if isinstance (b,types.StringTypes): + if isinstance (b,(str,)): return 1 else: # list vs list # assert isinstance(a, types.ListType) or isinstance(a, TupleType) @@ -261,7 +261,7 @@ def compareFirst(a,b): d = compare(a[i], b[i], level+1) if d != 0: return d return 0 - + def canon(graph, c0=0): """Try one pass at canonicalizing this using 1 step sigs. Return as a triple: @@ -301,7 +301,7 @@ def canon(graph, c0=0): if verbose: stderr.write( "Bnode %3i) %s\n\n" % (i, signature[i])) s.append((signature[i], i)) s.sort(compareFirst) - + dups = 0 c = c0 if verbose: stderr.write("\nIn order\n") @@ -320,7 +320,7 @@ def canon(graph, c0=0): canonical[original] = c if verbose: stderr.write( "\tBnode#%i canonicalized to new fixed C%i\n" %(s[i][1], c)) c = c + 1 - + newGraph = [] for j in range(len(graph)): triple = graph[j] diff --git a/check.py b/check.py index b660eb79..22cd2300 100644 --- a/check.py +++ b/check.py @@ -81,7 +81,7 @@ def get(self, uri): f = load(uri, flags="B").close() # why B? -DWC setVerbosity(0) self._loaded[uri] = f - assert f.canonical is f, `f.canonical` + assert f.canonical is f, repr(f.canonical) return f def topLevelLoad(uri=None, flags=''): @@ -134,7 +134,7 @@ def n3Entails(f, g, skipIncludes=0, level=0): continue for t in f.statements: - fyi("Trying unify statement %s" %(`t`), level=level+1, thresh=70) + fyi("Trying unify statement %s" %(repr(t)), level=level+1, thresh=70) if (t[PRED].unify(pred) != [] and t[SUBJ].unify(subj) != [] and t[OBJ].unify(obj) != []): @@ -222,7 +222,7 @@ def result(self, r, policy, level=0): f = proof.any(r, reason.gives) if f != None: assert isinstance(f, Formula), \ - "%s gives: %s which should be Formula" % (`r`, f) + "%s gives: %s which should be Formula" % (repr(r), f) fs = " proof of %s" % f else: fs = "" @@ -231,7 +231,7 @@ def result(self, r, policy, level=0): if r == None: if f is None: txt = 'None' else: txt = f.n3String() - raise InvalidProof("No reason for "+`f` + " :\n\n"+ txt +"\n\n", level=level) + raise InvalidProof("No reason for "+repr(f) + " :\n\n"+ txt +"\n\n", level=level) classesOfReason = knownReasons.intersection(proof.each(subj=r, pred=rdf.type)) if len(classesOfReason) < 1: raise InvalidProof("%s does not have the type of any reason" % r) @@ -361,7 +361,7 @@ def report(self, out, f=None, step=None): if isinstance(termq, Literal) \ or isinstance(termq, Formula): - bindings[varname] = `termq` + bindings[varname] = repr(termq) else: term = proof.any(subj=termq, pred=rei.uri) if term: @@ -431,7 +431,7 @@ def report(self, out, f=None, step=None): out.write("@@num/name: %s [Premise]\n" % body) else: - raise RuntimeError, t + raise RuntimeError(t) self._num[step] = num @@ -472,7 +472,7 @@ def checkExtraction(r, f, checker, policy, level=0): proof = checker._pf r2 = proof.the(r, reason.because) if r2 == None: - raise InvalidProof("Extraction: no source formula given for %s." % (`r`), level) + raise InvalidProof("Extraction: no source formula given for %s." % (repr(r)), level) f2 = checker.result(r2, policy, level) if not isinstance(f2, Formula): raise InvalidProof("Extraction of %s gave something odd, %s" % (r2, f2), level) @@ -578,7 +578,7 @@ def checkGMP(r, f, checker, policy, level=0): # Check: Every antecedent statement must be included as evidence antecedent = proof.newFormula() - for k in bindings.values(): + for k in list(bindings.values()): if k in existentials: #k in evidenceFormula.existentials() or antecedent.declareExistential(k) antecedent.loadFormulaWithSubstitution(ruleStatement[SUBJ], bindings) @@ -748,7 +748,7 @@ def atomicFormulaTerms(f): (color, sky, blue) """ # """ help emacs - if len(f) <> 1: + if len(f) != 1: raise ValueError("expected atomic formula; got: %s." % f.statements) @@ -766,7 +766,7 @@ def checkSupports(r, f, checker, policy, level): #log:includes is very special r2 = proof.the(r, reason.because) if r2 is None: - raise InvalidProof("Extraction: no source formula given for %s." % (`r`), level) + raise InvalidProof("Extraction: no source formula given for %s." % (repr(r)), level) fyi("Starting nested conclusion", level=level) f2 = checker.result(r2, Assumption(subj), level) if not isinstance(f2, Formula): @@ -858,7 +858,7 @@ def main(argv): proof = topLevelLoad(flags=flags) # setVerbosity(60) - fyi("Length of proof formula: "+`len(proof)`, thresh=5) + fyi("Length of proof formula: "+repr(len(proof)), thresh=5) try: c = Checker(proof) @@ -872,9 +872,9 @@ def main(argv): fyi("Proof looks OK. %i Steps" % proofSteps, thresh=5) setVerbosity(0) txt = proved.n3String().encode('utf-8') - print "\n".join([' ' + ln.strip() for ln in txt.split("\n")]) + print("\n".join([' ' + ln.strip() for ln in txt.split("\n")])) - except InvalidProof, e: + except InvalidProof as e: progress("Proof invalid:", e) sys.exit(-1) @@ -920,7 +920,7 @@ def _s2f(s, base): """ # """ emacs python mode needs help - import notation3 + from . import notation3 graph = formula() graph.setClosureMode("e") # Implement sameAs by smushing p = notation3.SinkParser(graph.store, openFormula=graph, baseURI=base, diff --git a/comsyn.py b/comsyn.py index 0913fcfa..7eb187b9 100644 --- a/comsyn.py +++ b/comsyn.py @@ -111,9 +111,9 @@ __version__ = "$Revision$" # $Id$ -import rdfpath -import hotswap -import pluggable +from . import rdfpath +from . import hotswap +from . import pluggable #class Serializer(pluggable.Serializer): @@ -138,7 +138,7 @@ def t_NUMERAL(t): try: t.value = int(t.value) except ValueError: - print "Integer value too large", t.value + print("Integer value too large", t.value) t.value = 0 return t @@ -151,11 +151,11 @@ def t_QUOTEDSTRING(t): # t_ignore = " \t" def t_error(t): - print "Illegal character '%s'" % t.value[0] + print("Illegal character '%s'" % t.value[0]) t.skip(1) # Build the lexer -import ply.lex +from . import ply.lex ply.lex.lex() # Test it out @@ -233,20 +233,20 @@ def p_name_2(t): t[0] = t[2] def p_error(t): - print "Syntax error at '%s'" % (t.value) + print("Syntax error at '%s'" % (t.value)) raise RuntimeError -import ply.yacc +from . import ply.yacc ply.yacc.yacc(tabmodule="comsyn_ply_tab") debug=0 -print ply.yacc.parse("hello='world'", debug=debug) -print ply.yacc.parse("hello=35", debug=debug) -print ply.yacc.parse("hello=35;foo=bar;baz='Bux'", debug=debug) -print ply.yacc.parse("a=(b='c')", debug=debug) -print ply.yacc.parse("a=(x.b='c')", debug=debug) -print ply.yacc.parse("a=(x.b.x.q='c');a.b=c.d", debug=debug) -print ply.yacc.parse("=x;x.y=z", debug=debug) +print(ply.yacc.parse("hello='world'", debug=debug)) +print(ply.yacc.parse("hello=35", debug=debug)) +print(ply.yacc.parse("hello=35;foo=bar;baz='Bux'", debug=debug)) +print(ply.yacc.parse("a=(b='c')", debug=debug)) +print(ply.yacc.parse("a=(x.b='c')", debug=debug)) +print(ply.yacc.parse("a=(x.b.x.q='c');a.b=c.d", debug=debug)) +print(ply.yacc.parse("=x;x.y=z", debug=debug)) # the left is always a path # if it has only one element, the subject is implied # if it has 2 or more, the subject is given, and the others @@ -270,7 +270,7 @@ class Parser(pluggable.Parser): def parse(self, stream, host): self.sink = host.pluginManager.get("store", rdfpath.Store) x = ply.yacc.parse(stream) - print "Result:", x + print("Result:", x) class xxxParser: @@ -279,7 +279,7 @@ def __init__(self, sink=None, flags=""): self.kb = sink def load(self, inputURI): - stream = urllib.urlopen(inputURI) + stream = urllib.request.urlopen(inputURI) s = stream.read() global kb kb = self.kb diff --git a/converter-cgi.py b/converter-cgi.py index 03898750..e32f71ec 100755 --- a/converter-cgi.py +++ b/converter-cgi.py @@ -23,16 +23,16 @@ """ import string -import urlparse +import urllib.parse # import re # import StringIO #import urllib # for log:content import md5, binascii # for building md5 URIs -urlparse.uses_fragment.append("md5") #@@kludge/patch -urlparse.uses_relative.append("md5") #@@kludge/patch +urllib.parse.uses_fragment.append("md5") #@@kludge/patch +urllib.parse.uses_relative.append("md5") #@@kludge/patch -import notation3 # N3 parsers and generators, and RDF generator +from . import notation3 # N3 parsers and generators, and RDF generator @@ -44,7 +44,7 @@ import time import cgi import sys -import StringIO +import io def serveRequest(env): import random #for message identifiers. Hmm... should seed from request @@ -53,22 +53,22 @@ def serveRequest(env): form = cgi.FieldStorage() - if form.has_key('data'): + if 'data' in form: try: convert(form, env) - except BadSyntax, e: - print "Status: 500 syntax error in input data" - print "Content-type: text/plain" - print - print e + except BadSyntax as e: + print("Status: 500 syntax error in input data") + print("Content-type: text/plain") + print() + print(e) except: import traceback - print "Status: 500 error in python script. traceback follows" - print "Content-type: text/plain" - print + print("Status: 500 error in python script. traceback follows") + print("Content-type: text/plain") + print() traceback.print_exc(sys.stdout) else: @@ -83,31 +83,31 @@ def convert(form, env): data = form['data'].value - if form.has_key('genspace'): + if 'genspace' in form: genspace = form['genspace'].value else: genspace = thisMessage + '#_' - if form.has_key('baseURI'): baseURI = form['baseURI'].value - elif env.has_key('HTTP_REFERER'): baseURI = env['HTTP_REFERER'] + if 'baseURI' in form: baseURI = form['baseURI'].value + elif 'HTTP_REFERER' in env: baseURI = env['HTTP_REFERER'] else: baseURI = None # output is buffered so that we only send # 200 OK if all went well - buf = StringIO.StringIO() + buf = io.StringIO() xlate = notation3.ToRDFParser(buf, baseURI, thisMessage, genspace) xlate.startDoc() xlate.feed(data) xlate.endDoc() - print "Content-Type: text/xml" + print("Content-Type: text/xml") #hmm... other headers? last-modified? # handle if-modified-since? i.e. handle input by reference? - print # end of HTTP response headers - print buf.getvalue() + print() # end of HTTP response headers + print(buf.getvalue()) def showForm(): - print """Content-Type: text/html + print("""Content-Type: text/html Notation3 to RDF Service @@ -135,7 +135,7 @@ def showForm(): -""" +""") ############################################################ Main program diff --git a/crawl.py b/crawl.py index 16779883..636e8fa3 100755 --- a/crawl.py +++ b/crawl.py @@ -6,15 +6,15 @@ Crawler""" -import llyn -from term import Fragment +from . import llyn +from .term import Fragment #import thing -import diag +from . import diag import sys -from myStore import load, symbol -from uripath import join, base +from .myStore import load, symbol +from .uripath import join, base cvsRevision = "$Revision$" @@ -40,14 +40,14 @@ def crawl(this): global already global successes uri = this.uriref() - print - print "<%s> a" % uri, + print() + print("<%s> a" % uri, end=' ') try: f = load(uri) except: - print ":Inaccessible." + print(":Inaccessible.") return - print ":Accessible", + print(":Accessible", end=' ') successes = successes + 1 thisd = getDoc(this) for s in f.statements: @@ -57,10 +57,10 @@ def crawl(this): r = x.resource if r not in thisd.mentions: thisd.mentions.append(r) - print ";\n\t:mentionsSomethingIn <%s>" % r.uriref(), + print(";\n\t:mentionsSomethingIn <%s>" % r.uriref(), end=' ') if r not in agenda and r not in already: agenda.append(r) - print "." + print(".") @@ -79,15 +79,15 @@ def doCommand(): uri = join(base(), uriref) r = symbol(uri) diag.setVerbosity(0) - print "@prefix : ." - print "# Generated by crawl.py ", cvsRevision[1:-1] + print("@prefix : .") + print("# Generated by crawl.py ", cvsRevision[1:-1]) agenda = [r] while agenda != []: r = agenda[0] agenda = agenda[1:] already.append(r) crawl(r) - print "# ", len(already), "attempts,", successes, "successes." + print("# ", len(already), "attempts,", successes, "successes.") ############################################################ Main program diff --git a/cwm.py b/cwm.py index d7149437..c3a7a27b 100755 --- a/cwm.py +++ b/cwm.py @@ -288,12 +288,12 @@ def doCommand(): elif arg == "-reify": option_reify = 1 elif arg == "-flat": option_flat = 1 elif arg == "-help": - print doCommand.__doc__ - print notation3.ToN3.flagDocumentation - print toXML.ToRDF.flagDocumentation + print(doCommand.__doc__) + print(notation3.ToN3.flagDocumentation) + print(toXML.ToRDF.flagDocumentation) try: from swap import sax2rdf # RDF1.0 syntax parser to N3 RDF stream - print sax2rdf.RDFXMLParser.flagDocumentation + print(sax2rdf.RDFXMLParser.flagDocumentation) except: pass return @@ -358,7 +358,7 @@ def doCommand(): _outSink = notation3.Reifier(_outSink, _outURI+ "#_formula", flat=1) if diag.tracking: - myReason = BecauseOfCommandLine(`sys.argv`) + myReason = BecauseOfCommandLine(repr(sys.argv)) # @@ add user, host, pid, pwd, date time? Privacy! else: myReason = None @@ -498,7 +498,7 @@ def filterize(): diag.tracking = int(_rhs) elif option_pipe: ############## End of pipable options - print "# Command line error: %s illegal option with -pipe", arg + print("# Command line error: %s illegal option with -pipe", arg) break elif arg == "-triples" or arg == "-ntriples": @@ -659,12 +659,12 @@ def _handler(s): _tmpstore = llyn.RDFStore( _outURI+"#_g", metaURI=_metaURI, argv=option_with, crypto=option_crypto) tmpContext = _tmpstore.newFormula(_uri+ "#_formula") - _newURI = join(_baseURI, "_w_"+`_genid`) # Intermediate + _newURI = join(_baseURI, "_w_"+repr(_genid)) # Intermediate _genid = _genid + 1 _newContext = _tmpstore.newFormula(_newURI+ "#_formula") _tmpstore.loadURI(_uri) - print targetkb + print(targetkb) elif arg == "-flatten": #raise NotImplementedError @@ -695,7 +695,7 @@ def _handler(s): option_outputStyle = "-no" elif arg == '-sparqlResults': - from cwm_sparql import outputString, SPARQL_NS + from .cwm_sparql import outputString, SPARQL_NS ns = _store.newSymbol(SPARQL_NS) if not sparql_query_formula: raise ValueError('No query') @@ -735,7 +735,7 @@ def _handler(s): elif option_outputStyle == "-no": pass elif option_outputStyle == "-debugString": - print workingContext.debugString() + print(workingContext.debugString()) else: # "-best" _store.dumpNested(workingContext, _outSink, flags=option_flags[option_format]) diff --git a/cwm_crypto.py b/cwm_crypto.py index e8fa91fe..a0461389 100755 --- a/cwm_crypto.py +++ b/cwm_crypto.py @@ -17,7 +17,7 @@ # 2010: DeprecationWarning: the sha module is deprecated; use the hashlib module instead import hashlib # http://pydoc.org/2.5.1/hashlib.html # import, binascii, quopri, base64 -from term import Function, ReverseFunction, LightBuiltIn +from .term import Function, ReverseFunction, LightBuiltIn USE_PKC = 1 @@ -69,10 +69,10 @@ def quoToKey(strkey, spl='\n\n'): """Returns a key from quopri (ee then m) version of a key.""" bunc = base64.decodestring(quopri.decodestring(strkey.strip())) bits = bunc.split(spl) - if len(bits) == 2: return newKey(long(bits[0]), long(bits[1])) + if len(bits) == 2: return newKey(int(bits[0]), int(bits[1])) else: e, n, d, p, q = bits - return newKey(long(e), long(n), long(d), long(p), long(q)) + return newKey(int(e), int(n), int(d), int(p), int(q)) # Signature encoding and decoding @@ -169,7 +169,7 @@ def evaluate(self, subj_py, obj_py): keypair, (hash, signature) = subj_py, obj_py hash = hash.encode('ascii') RSAKey = quoToKey(keypair) # Dequote the key - signature = (long(baseDecode(signature)),) # convert the signature back + signature = (int(baseDecode(signature)),) # convert the signature back return RSAKey.verify(hash, signature) class BI_verifyBoolean(LightBuiltIn, Function): @@ -178,7 +178,7 @@ def evaluateObject(self, subj_py): keypair, hash, signature = subj_py hash = hash.encode('ascii') RSAKey = quoToKey(keypair) # Dequote the key - signature = (long(baseDecode(signature)),) + signature = (int(baseDecode(signature)),) result = RSAKey.verify(hash, signature) return str(result) @@ -207,4 +207,4 @@ def register(store): str.internFrag('publicKey', BI_publicKey) if __name__=="__main__": - print __doc__.strip() + print(__doc__.strip()) diff --git a/cwm_list.py b/cwm_list.py index 90d97e65..76e3e85c 100755 --- a/cwm_list.py +++ b/cwm_list.py @@ -12,16 +12,16 @@ """ -from term import LightBuiltIn, RDFBuiltIn, Function, ReverseFunction, \ +from .term import LightBuiltIn, RDFBuiltIn, Function, ReverseFunction, \ MultipleFunction, MultipleReverseFunction, \ CompoundTerm, N3Set, List, EmptyList, NonEmptyList -from set_importer import Set +from .set_importer import Set -from diag import verbosity, progress -import uripath +from .diag import verbosity, progress +from . import uripath -from RDFSink import List_NS, Logic_NS +from .RDFSink import List_NS, Logic_NS ListOperationsNamespace = "http://www.w3.org/2000/10/swap/list#" diff --git a/cwm_math.py b/cwm_math.py index abb14a25..40bf8f1d 100755 --- a/cwm_math.py +++ b/cwm_math.py @@ -18,17 +18,17 @@ __cvsid__ = '$Id$' __version__ = '$Revision$' -import sys, string, re, urllib +import sys, string, re, urllib.request, urllib.parse, urllib.error -from term import LightBuiltIn, Function, ReverseFunction, ArgumentNotLiteral, Literal -from local_decimal import Decimal +from .term import LightBuiltIn, Function, ReverseFunction, ArgumentNotLiteral, Literal +from .local_decimal import Decimal import types # from RDFSink import DAML_LISTS, RDF_type_URI, DAML_sameAs_URI MATH_NS_URI = 'http://www.w3.org/2000/10/swap/math#' -from diag import progress +from .diag import progress import sys, traceback def obsolete(): @@ -45,7 +45,7 @@ def tidy(x): def isString(x): # in 2.2, evidently we can test for isinstance(types.StringTypes) - return type(x) is type('') or type(x) is type(u'') + return type(x) is type('') or type(x) is type('') # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # @@ -64,14 +64,14 @@ def isString(x): # add, take, multiply, divide def numeric(s): - if type(s) == types.IntType or \ - type(s) == types.LongType or \ - type(s) is types.FloatType or \ + if type(s) == int or \ + type(s) == int or \ + type(s) is float or \ isinstance(s,Decimal): return s - if not isinstance(s, (Literal, str, unicode)): + if not isinstance(s, (Literal, str)): raise ArgumentNotLiteral(s) s = s.strip() # 2009 in practice e.g. OFX values have leading spaces - if s.find('.') < 0 and s.find('e') < 0 : return long(s) + if s.find('.') < 0 and s.find('e') < 0 : return int(s) if 'e' not in s and 'E' not in s: return Decimal(s) return float(s) @@ -125,13 +125,13 @@ def evaluateSubject(self, obj_py): class BI_quotient(LightBuiltIn, Function): def evaluateObject(self, subj_py): if len(subj_py) == 2: - if isinstance(numeric(subj_py[0]), long): + if isinstance(numeric(subj_py[0]), int): return numeric(subj_py[1]).__rtruediv__(numeric(subj_py[0])) return numeric(subj_py[0]).__truediv__(numeric(subj_py[1])) class BI_integerQuotient(LightBuiltIn, Function): def evaluateObject(self, subj_py): - if len(subj_py) == 2: return long(subj_py[0]) / long(subj_py[1]) + if len(subj_py) == 2: return int(subj_py[0]) / int(subj_py[1]) class BI_bit(LightBuiltIn, Function): """@@needs a test.""" @@ -248,4 +248,4 @@ def register(store): str.internFrag('memberCount', BI_memberCount) if __name__=="__main__": - print string.strip(__doc__) + print(string.strip(__doc__)) diff --git a/cwm_maths.py b/cwm_maths.py index d95a18dd..36332d73 100755 --- a/cwm_maths.py +++ b/cwm_maths.py @@ -19,10 +19,10 @@ __cvsid__ = '$Id$' __version__ = '$Revision$' -import sys, string, re, urllib +import sys, string, re, urllib.request, urllib.parse, urllib.error -from term import LightBuiltIn, Function, ReverseFunction -from local_decimal import Decimal +from .term import LightBuiltIn, Function, ReverseFunction +from .local_decimal import Decimal MATHS_NS_URI = 'http://www.w3.org/2000/10/swap/maths#' @@ -36,7 +36,7 @@ def tidy(x): def isString(x): # in 2.2, evidently we can test for isinstance(types.StringTypes) - return type(x) is type('') or type(x) is type(u'') + return type(x) is type('') or type(x) is type('') # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # @@ -113,7 +113,7 @@ def evaluateObject(self, subj_py): class BI_integerQuotient(LightBuiltIn, Function): def evaluateObject(self, subj_py): t = None - if len(subj_py) == 2: t = long(subj_py[0]) / long(subj_py[1]) + if len(subj_py) == 2: t = int(subj_py[0]) / int(subj_py[1]) return tidy(t) class BI_quotientOf(LightBuiltIn, ReverseFunction): @@ -227,4 +227,4 @@ def register(store): str.internFrag('memberCount', BI_memberCount) if __name__=="__main__": - print string.strip(__doc__) + print(string.strip(__doc__)) diff --git a/cwm_os.py b/cwm_os.py index e2c5f40b..f69c15dd 100755 --- a/cwm_os.py +++ b/cwm_os.py @@ -13,9 +13,9 @@ import os -from term import LightBuiltIn, Function, ReverseFunction -from diag import verbosity, progress -import uripath +from .term import LightBuiltIn, Function, ReverseFunction +from .diag import verbosity, progress +from . import uripath OS_NS_URI = "http://www.w3.org/2000/10/swap/os#" @@ -35,7 +35,7 @@ class BI_environ(LightBuiltIn, Function): def evaluateObject(self, subj_py): if isString(subj_py): return os.environ.get(subj_py, None) - progress("os:environ input is not a string: "+`subj_py`) + progress("os:environ input is not a string: "+repr(subj_py)) class BI_baseAbsolute(LightBuiltIn, Function): """The baseAbsolute function generates an absolute URIref from a string, @@ -44,20 +44,20 @@ class BI_baseAbsolute(LightBuiltIn, Function): It is not a reverse function, because sereral different relativisations exist for the same absolute URI. See uripath.py.""" def evaluateObject(self, subj_py): - if verbosity() > 80: progress("os:baseAbsolute input:"+`subj_py`) + if verbosity() > 80: progress("os:baseAbsolute input:"+repr(subj_py)) if isString(subj_py): return uripath.join(uripath.base(), subj_py) - progress("Warning: os:baseAbsolute input is not a string: "+`subj_py`) + progress("Warning: os:baseAbsolute input is not a string: "+repr(subj_py)) class BI_baseRelative(LightBuiltIn, Function, ReverseFunction): """The baseRelative of a URI is its expression relation to the process base URI. It is 1:1, being an arbitrary cannonical form. It is a reverse function too, as you can always work the other way.""" def evaluateObject(self, subj_py): - if verbosity() > 80: progress("os:baseRelative input:"+`subj_py`) + if verbosity() > 80: progress("os:baseRelative input:"+repr(subj_py)) if isString(subj_py): return uripath.refTo(uripath.base(), subj_py) - progress("Warning: os:baseRelative input is not a string: "+`subj_py`) + progress("Warning: os:baseRelative input is not a string: "+repr(subj_py)) def evaluateSubject(self, subj_py): return BI_baseAbsolute.evaluateObject(self, subj_py) @@ -67,20 +67,20 @@ def evaluateSubject(self, subj_py): # Not fatal if not defined class BI_argv(LightBuiltIn, Function): def evaluateObject(self, subj_py): - if verbosity() > 80: progress("os:argv input:"+`subj_py`) + if verbosity() > 80: progress("os:argv input:"+repr(subj_py)) if self.store.argv: # Not None or []. was also: isString(subj_py) and try: argnum = int(subj_py) -1 except ValueError: if verbosity() > 30: - progress("os:argv input is not a number: "+`subj_py`) + progress("os:argv input is not a number: "+repr(subj_py)) return None if argnum < len(self.store.argv): return self.store.argv[argnum] def isString(x): # in 2.2, evidently we can test for isinstance(types.StringTypes) - return type(x) is type('') or type(x) is type(u'') + return type(x) is type('') or type(x) is type('') # Register the string built-ins with the store diff --git a/cwm_set.py b/cwm_set.py index d9355ca8..def7e122 100644 --- a/cwm_set.py +++ b/cwm_set.py @@ -12,16 +12,16 @@ """ -from term import LightBuiltIn, Function, ReverseFunction, MultipleFunction,\ +from .term import LightBuiltIn, Function, ReverseFunction, MultipleFunction,\ MultipleReverseFunction, \ CompoundTerm, N3Set, List, EmptyList, NonEmptyList -from set_importer import Set +from .set_importer import Set -from diag import verbosity, progress -import uripath +from .diag import verbosity, progress +from . import uripath -from RDFSink import List_NS, Logic_NS +from .RDFSink import List_NS, Logic_NS SetOperationsNamespace = "http://www.w3.org/2000/10/swap/set#" diff --git a/cwm_sparql.py b/cwm_sparql.py index 9bda09fe..f23316f7 100644 --- a/cwm_sparql.py +++ b/cwm_sparql.py @@ -10,22 +10,22 @@ MultipleReverseFunction, typeMap, LabelledNode, \ CompoundTerm, N3Set, List, EmptyList, NonEmptyList, \ Symbol, Fragment, Literal, Term, AnonymousNode, HeavyBuiltIn, toBool -import diag +from . import diag progress = diag.progress -from RDFSink import RDFSink -from set_importer import Set +from .RDFSink import RDFSink +from .set_importer import Set -import uripath +from . import uripath -from toXML import XMLWriter +from .toXML import XMLWriter try: from decimal import Decimal except ImportError: - from local_decimal import Decimal + from .local_decimal import Decimal -from term import ErrorFlag as MyError +from .term import ErrorFlag as MyError SPARQL_NS = 'http://www.w3.org/2000/10/swap/sparqlCwm' @@ -38,7 +38,7 @@ def eval(self, subj, obj, queue, bindings, proof, query): ## print '%s makes %s' % (obj, toBool(str(obj), obj.datatype.fragid)) ## print 'I got here on %s, %s, returning %s' % (subj, obj, toBool(str(subj), subj.datatype.fragid) is toBool(str(obj), obj.datatype.fragid)) return toBool(str(subj), subj.datatype) is toBool(str(obj), obj.datatype) - raise TypeError("%s type cannot be converted to boolean" % `subj.__class`) + raise TypeError("%s type cannot be converted to boolean" % repr(subj.__class)) class BI_typeErrorIsTrue(LightBuiltIn): """ @@ -129,7 +129,7 @@ def evalObj(self,subj, queue, bindings, proof, query): def evalSubj(self, obj, queue, bindings, proof, query): if not isinstance(obj, Literal): - raise TypeError('I can only find the datatype of a Literal, not a %s' % `obj.__class__.__name__`) + raise TypeError('I can only find the datatype of a Literal, not a %s' % repr(obj.__class__.__name__)) return self.store.newList([self.store.newLiteral(str(obj)), obj.datatype]) class BI_langLit(LightBuiltIn, Function, ReverseFunction): @@ -150,7 +150,7 @@ def evalObj(self,subj, queue, bindings, proof, query): def evalSubj(self, obj, queue, bindings, proof, query): if not isinstance(obj, Literal): - raise TypeError('I can only find the datatype of a Literal, not a %s' % `obj.__class__.__name__`) + raise TypeError('I can only find the datatype of a Literal, not a %s' % repr(obj.__class__.__name__)) lang = obj.lang if not obj.lang: lang = '' @@ -166,13 +166,13 @@ def evalSubj(self, obj, queue, bindings, proof, query): retValCopy = Set() n = 0 while retVals != retValCopy: - print n, retVals, retValCopy + print(n, retVals, retValCopy) n += 1 retValCopy = retVals.copy() for node in retValCopy: - retVals.update(node._prec.values()) + retVals.update(list(node._prec.values())) a = query.workingContext.occurringIn(retVals) ## Really slow. Need to generate this on the fly? - print 'a=', a + print('a=', a) return a ############################# @@ -186,7 +186,7 @@ def evalSubj(self, obj, queue, bindings, proof, query): class BI_query(LightBuiltIn, Function): def evalObj(self,subj, queue, bindings, proof, query): - from query import applySparqlQueries + from .query import applySparqlQueries ns = self.store.newSymbol(SPARQL_NS) assert isinstance(subj, List) subj = [a for a in subj] @@ -236,8 +236,8 @@ def sparql_output(query, F): try: resultFormulae.sort(Term.compareAnyTerm) except: - print [type(x) for x in resultFormulae] - print Term + print([type(x) for x in resultFormulae]) + print(Term) raise for resultFormula in resultFormulae: xwr.startElement(RESULTS_NS+'result', [], prefixTracker.prefixes) @@ -260,7 +260,7 @@ def sparql_output(query, F): if binding.lang: props.append(("http://www.w3.org/XML/1998/namespace lang", binding.lang)) xwr.startElement(RESULTS_NS+'literal', props, prefixTracker.prefixes) - xwr.data(unicode(binding)) + xwr.data(str(binding)) xwr.endElement() xwr.endElement() else: @@ -270,7 +270,7 @@ def sparql_output(query, F): xwr.endElement() xwr.endElement() xwr.endDocument() - return u''.join(outputList) + return ''.join(outputList) if query.contains(obj=ns['AskQuery']): node = query.the(pred=store.type, obj=ns['AskQuery']) outputList = [] @@ -301,14 +301,14 @@ def sparql_output(query, F): def sparql_queryString(source, queryString): - from query import applySparqlQueries + from .query import applySparqlQueries store = source.store ns = store.newSymbol(SPARQL_NS) - from sparql import sparql_parser - import sparql2cwm + from .sparql import sparql_parser + from . import sparql2cwm convertor = sparql2cwm.FromSparql(store) - import StringIO - p = sparql_parser.N3Parser(StringIO.StringIO(queryString), sparql_parser.branches, convertor) + import io + p = sparql_parser.N3Parser(io.StringIO(queryString), sparql_parser.branches, convertor) q = p.parse(sparql_parser.start).close() F = store.newFormula() applySparqlQueries(source, q, F) @@ -336,7 +336,7 @@ def evalObj(self, subj, queue, bindings, proof, query): else: doc = subj F = store.any((store._experience, store.semantics, doc, None)) if F != None: - if diag.chatty_flag > 10: progress("Already read and parsed "+`doc`+" to "+ `F`) + if diag.chatty_flag > 10: progress("Already read and parsed "+repr(doc)+" to "+ repr(F)) return F if diag.chatty_flag > 10: progress("Reading and parsing " + doc.uriref()) diff --git a/cwm_string.py b/cwm_string.py index bd8a7591..0b40a0e2 100755 --- a/cwm_string.py +++ b/cwm_string.py @@ -12,12 +12,12 @@ import string import re -from diag import verbosity, progress +from .diag import verbosity, progress -import urllib # for hasContent +import urllib.request, urllib.parse, urllib.error # for hasContent -from term import LightBuiltIn, ReverseFunction, Function, UnknownType -from local_decimal import Decimal +from .term import LightBuiltIn, ReverseFunction, Function, UnknownType +from .local_decimal import Decimal LITERAL_URI_prefix = "data:text/n3;" @@ -100,11 +100,11 @@ def normalizeWhitespace(s): return res # String Constructors - more light built-ins -make_string = unicode +make_string = str class BI_concat(LightBuiltIn, ReverseFunction): def evaluateSubject(self, obj_py): - if verbosity() > 80: progress("Concat input:"+`obj_py`) + if verbosity() > 80: progress("Concat input:"+repr(obj_py)) str = "" for x in obj_py: if not isString(x): return None # Can't @@ -113,15 +113,15 @@ def evaluateSubject(self, obj_py): class BI_concatenation(LightBuiltIn, Function): def evaluateObject(self, subj_py): - if verbosity() > 80: progress("Concatenation input:"+`subj_py`) + if verbosity() > 80: progress("Concatenation input:"+repr(subj_py)) str = "" for x in subj_py: if not isString(x): - if type(x) == type(long()) or isinstance(x, Decimal): + if type(x) == type(int()) or isinstance(x, Decimal): x = make_string(x) else: - x = `x` - if verbosity() > 34: progress("Warning: Coercing to string for concat:"+`x`) + x = repr(x) + if verbosity() > 34: progress("Warning: Coercing to string for concat:"+repr(x)) # return None # Can't str = str + x return str @@ -129,20 +129,20 @@ def evaluateObject(self, subj_py): def evalObj45(self, subj, queue, bindings, proof, query): # raise RuntimeError('I got here!') subj_py = list(subj) - if verbosity() > 80: progress("Concatenation input:"+`subj_py`) + if verbosity() > 80: progress("Concatenation input:"+repr(subj_py)) retVal = [] for x in subj_py: try: val = x.value() if not isString(val): - if type(val) == type(long()) or isinstance(val, Decimal): + if type(val) == type(int()) or isinstance(val, Decimal): val = make_string(val) else: - val = `val` - if verbosity() > 34: progress("Warning: Coercing to string for concat:"+`val`) + val = repr(val) + if verbosity() > 34: progress("Warning: Coercing to string for concat:"+repr(val)) retVal.append(val) except UnknownType: - progress("Warning: Coercing to string for concat:"+`x`) + progress("Warning: Coercing to string for concat:"+repr(x)) retVal.append(x.string) return subj.store.newLiteral(''.join(retVal)) @@ -157,7 +157,7 @@ class BI_scrape(LightBuiltIn, Function): """ def evaluateObject(self, subj_py): - if verbosity() > 80: progress("scrape input:"+`subj_py`) + if verbosity() > 80: progress("scrape input:"+repr(subj_py)) str, pat = subj_py patc = re.compile(pat) @@ -192,7 +192,7 @@ class BI_search(LightBuiltIn, Function): def evaluateObject(self, subj_py): # raise Error store = self.store - if verbosity() > 80: progress("search input:"+`subj_py`) + if verbosity() > 80: progress("search input:"+repr(subj_py)) str, pat = subj_py patc = re.compile(pat) @@ -247,7 +247,7 @@ class BI_stringToList(LightBuiltIn, Function, ReverseFunction): """ def evaluateObject(self, subj_py): - print "hello, I'm at it" + print("hello, I'm at it") try: return [a for a in subj_py] except TypeError: @@ -318,20 +318,20 @@ class BI_encodeForURI(LightBuiltIn, Function): http://www.w3.org/TR/2005/CR-xpath-functions-20051103/#func-encode-for-uri""" def evaluateObject(self, subj_py): - return urllib.quote(subj_py, "#!~*'()") + return urllib.parse.quote(subj_py, "#!~*'()") class BI_encodeForFragID(LightBuiltIn, Function): """Take a unicode string and return it encoded so as to pass in a URI grament identifier.""" def evaluateObject(self, subj_py): - return urllib.quote(subj_py) + return urllib.parse.quote(subj_py) class BI_resolve_uri(LightBuiltIn, Function): """see http://www.w3.org/2006/xpath-functions#resolve-uri""" def evaluateObject(self, subj_py): - import uripath + from . import uripath there, base = subj_py return uripath.join(base, there) @@ -340,7 +340,7 @@ def evaluateObject(self, subj_py): def isString(x): # in 2.2, evidently we can test for isinstance(types.StringTypes) - return type(x) is type('') or type(x) is type(u'') + return type(x) is type('') or type(x) is type('') def register(store): str = store.symbol(STRING_NS_URI[:-1]) diff --git a/cwm_time.py b/cwm_time.py index a204696c..b2a7bf52 100644 --- a/cwm_time.py +++ b/cwm_time.py @@ -19,12 +19,12 @@ import string import re -import notation3 # N3 parsers and generators, and RDF generator -import isodate # Local, by mnot. implements +from . import notation3 # N3 parsers and generators, and RDF generator +from . import isodate # Local, by mnot. implements -from diag import progress, verbosity -from term import LightBuiltIn, Function, ReverseFunction +from .diag import progress, verbosity +from .term import LightBuiltIn, Function, ReverseFunction import time, calendar # Python standard distribution @@ -41,7 +41,7 @@ class BI_inSeconds(LightBuiltIn, Function, ReverseFunction): def evaluateObject(self, subj_py): try: return float(isodate.parse(subj_py)) - except ValueError, AssertionError: + except ValueError as AssertionError: progress("Warning: Failed to parse time string '%s'" % subj_py) return None @@ -121,7 +121,7 @@ def evaluateObject(self, subj_py): class BI_format(LightBuiltIn, Function): def evaluateObject(self, subj_py): """params are ISO time string, format string. Returns reformatted. Ignores TZ@@""" - if verbosity() > 80: progress("strTime:format input:"+`subj_py`) + if verbosity() > 80: progress("strTime:format input:"+repr(subj_py)) str, format = subj_py try: return time.strftime(format, time.gmtime(isodate.parse(str))) @@ -133,7 +133,7 @@ class BI_gmTime(LightBuiltIn, Function): def evaluateObject(self, subj_py): """Subject is (empty string for standard formatting or) format string. Returns formatted.""" - if verbosity() > 80: progress("time:gmTime input:"+`subj_py`) + if verbosity() > 80: progress("time:gmTime input:"+repr(subj_py)) format = subj_py if format =="" : format="%Y-%m-%dT%H:%M:%SZ" try: @@ -145,7 +145,7 @@ class BI_localTime(LightBuiltIn, Function): def evaluateObject(self, subj_py): """Subject is format string or empty string for std formatting. Returns reformatted. @@@@ Ignores TZ""" - if verbosity() > 80: progress("time:localTime input:"+`subj_py`) + if verbosity() > 80: progress("time:localTime input:"+repr(subj_py)) format = subj_py if format =="" : return isodate.asString(time.time()) return time.strftime(format, time.localtime(time.time())) @@ -159,7 +159,7 @@ def evaluateObject(self, subj_py): class BI_formatSeconds(LightBuiltIn, Function): def evaluateObject(self, subj_py): """params are epoch-seconds time string, format string. Returns reformatted""" - if verbosity() > 80: progress("strTime:format input:"+`subj_py`) + if verbosity() > 80: progress("strTime:format input:"+repr(subj_py)) str, format = subj_py try: return time.strftime(format, time.gmtime(int(str))) @@ -168,7 +168,7 @@ def evaluateObject(self, subj_py): class BI_parseToSeconds(LightBuiltIn, Function): def evaluateObject(self, subj_py): - if verbosity() > 80: progress("strTime:parse input:"+`subj_py`) + if verbosity() > 80: progress("strTime:parse input:"+repr(subj_py)) str, format = subj_py try: return calendar.timegm(time.strptime(str, format)) @@ -177,7 +177,7 @@ def evaluateObject(self, subj_py): class BI_parse(LightBuiltIn, Function): def evaluateObject(self, subj_py): - if verbosity() > 80: progress("strTime:parse input:"+`subj_py`) + if verbosity() > 80: progress("strTime:parse input:"+repr(subj_py)) str, format = subj_py try: return isodate.fullString(int(calendar.timegm(time.strptime(str, format)))); diff --git a/cwm_times.py b/cwm_times.py index b2f8cbfd..5177804d 100644 --- a/cwm_times.py +++ b/cwm_times.py @@ -18,12 +18,12 @@ """ import re -import notation3 # N3 parsers and generators, and RDF generator -import isodate # Local, by mnot. implements +from . import notation3 # N3 parsers and generators, and RDF generator +from . import isodate # Local, by mnot. implements -from diag import progress, verbosity -from term import LightBuiltIn, Function, ReverseFunction +from .diag import progress, verbosity +from .term import LightBuiltIn, Function, ReverseFunction import time, calendar # Python standard distribution @@ -119,7 +119,7 @@ def evaluateObject(self, subj_py): class BI_format(LightBuiltIn, Function): def evaluateObject(self, subj_py): """params are ISO time string, format string. Returns reformatted. Ignores TZ@@""" - if verbosity() > 80: progress("strTime:format input:"+`subj_py`) + if verbosity() > 80: progress("strTime:format input:"+repr(subj_py)) str, format = subj_py try: return time.strftime(format, time.gmtime(isodate.parse(str))) @@ -131,7 +131,7 @@ class BI_gmTime(LightBuiltIn, Function): def evaluateObject(self, subj_py): """Subject is (empty string for standard formatting or) format string. Returns formatted.""" - if verbosity() > 80: progress("time:gmTime input:"+`subj_py`) + if verbosity() > 80: progress("time:gmTime input:"+repr(subj_py)) format = subj_py if format =="" : format="%Y-%m-%dT%H:%M:%SZ" try: @@ -143,7 +143,7 @@ class BI_localTime(LightBuiltIn, Function): def evaluateObject(self, subj_py): """Subject is format string or empty string for std formatting. Returns reformatted. @@@@ Ignores TZ""" - if verbosity() > 80: progress("time:localTime input:"+`subj_py`) + if verbosity() > 80: progress("time:localTime input:"+repr(subj_py)) format = subj_py if format =="" : return isodate.asString(time.time()) return time.strftime(format, time.localtime(time.time())) @@ -157,7 +157,7 @@ def evaluateObject(self, subj_py): class BI_formatSeconds(LightBuiltIn, Function): def evaluateObject(self, subj_py): """params are epoch-seconds time string, format string. Returns reformatted""" - if verbosity() > 80: progress("strTime:format input:"+`subj_py`) + if verbosity() > 80: progress("strTime:format input:"+repr(subj_py)) str, format = subj_py try: return time.strftime(format, time.gmtime(int(str))) @@ -166,7 +166,7 @@ def evaluateObject(self, subj_py): class BI_parseToSeconds(LightBuiltIn, Function): def evaluateObject(self, subj_py): - if verbosity() > 80: progress("strTime:parse input:"+`subj_py`) + if verbosity() > 80: progress("strTime:parse input:"+repr(subj_py)) str, format = subj_py try: return str(calendar.timegm(time.strptime(str, format))) diff --git a/cwm_trigo.py b/cwm_trigo.py index 7babd6c3..b77c2137 100644 --- a/cwm_trigo.py +++ b/cwm_trigo.py @@ -19,10 +19,10 @@ __all__ = ["evaluateObject"] from math import sin, acos, asin, atan, atan2, cos, cosh, sinh, tan, tanh -from term import LightBuiltIn, Function, ReverseFunction +from .term import LightBuiltIn, Function, ReverseFunction import types -from diag import progress -from cwm_math import * +from .diag import progress +from .cwm_math import * MATH_NS_URI = 'http://www.w3.org/2000/10/swap/math#' @@ -144,4 +144,4 @@ def register(store): str.internFrag('tanh', BI_tanh) if __name__=="__main__": - print __doc__.strip() + print(__doc__.strip()) diff --git a/cwm_uri.py b/cwm_uri.py index 91aa963b..2698184c 100644 --- a/cwm_uri.py +++ b/cwm_uri.py @@ -22,10 +22,10 @@ import string import re -import notation3 # N3 parsers and generators, and RDF generator -import isodate # Local, by mnot. implements -from diag import progress, verbosity -from term import LightBuiltIn, Function, ReverseFunction +from . import notation3 # N3 parsers and generators, and RDF generator +from . import isodate # Local, by mnot. implements +from .diag import progress, verbosity +from .term import LightBuiltIn, Function, ReverseFunction URI_NS_URI = "http://www.w3.org/2000/10/swap/uri#" @@ -40,7 +40,7 @@ class BI_uri_canon(LightBuiltIn, Function): def evaluateObject(self, subj_py): try: return uripath.canon(subj_py) - except ValueError, AssertionError: + except ValueError as AssertionError: progress("Warning: Failed to parse uri string '%s'" % subj_py) return None @@ -50,7 +50,7 @@ class BI_uri_scheme(LightBuiltIn, Function): def evaluateObject(self, subj_py): try: return uripath.scheme(subj_py) - except ValueError, AssertionError: + except ValueError as AssertionError: progress("Warning: Failed to parse uri string '%s'" % subj_py) return None @@ -60,7 +60,7 @@ class BI_uri_host(LightBuiltIn, Function): def evaluateObject(self, subj_py): try: return uripath.host(subj_py) - except ValueError, AssertionError: + except ValueError as AssertionError: progress("Warning: Failed to parse uri string '%s'" % subj_py) return None diff --git a/cwm_xml.py b/cwm_xml.py index 37c71103..88cdd25a 100644 --- a/cwm_xml.py +++ b/cwm_xml.py @@ -1,12 +1,12 @@ """XML built-ins for cwm """ -from diag import progress, verbosity -from term import LightBuiltIn, Function, ReverseFunction, MultipleFunction, \ +from .diag import progress, verbosity +from .term import LightBuiltIn, Function, ReverseFunction, MultipleFunction, \ HeavyBuiltIn -from llyn import loadToStore # for caching in store's experience -from sax2rdf import XMLtoDOM # for fn:doc +from .llyn import loadToStore # for caching in store's experience +from .sax2rdf import XMLtoDOM # for fn:doc #from Ft.Xml.XPath import Evaluate as evalXPath # http://packages.debian.org/unstable/python/python-xml @@ -172,7 +172,7 @@ def evalObj(self, subj, queue, bindings, proof, query): try: lit = loadToStore(sym, ["application/xml", "text/xml"]) - except IOError, e: + except IOError as e: progress("@@ioerror", e) return None # hmm... is built-in API evolving to support exceptions? diff --git a/dbork/SqlDB.py b/dbork/SqlDB.py index 341dde77..34314c3b 100755 --- a/dbork/SqlDB.py +++ b/dbork/SqlDB.py @@ -22,8 +22,8 @@ import re import imp import MySQLdb -import TableRenderer -from TableRenderer import TableRenderer +from . import TableRenderer +from .TableRenderer import TableRenderer from diag import progress, progressIndent, verbosity, tracking from RDFSink import FORMULA, LITERAL, ANONYMOUS, SYMBOL @@ -49,11 +49,11 @@ def NTriplesAtom(s, rowBindings, interner): else: try: subj = rowBindings[s.symbol()] - except TypeError, e: + except TypeError as e: subj = '"'+str(rowBindings[s.symbol()])+'"' - except KeyError, e: - print s.toString() - print s._literal() + except KeyError as e: + print(s.toString()) + print(s._literal()) subj = '"'+"KeyError:"+str(e)+" "+s.symbol()+'"' return subj @@ -157,36 +157,36 @@ def myIntern(self, q, index, variables, existentials): try: index = self.varIndex[q] symbol = self.fragments[q] - except KeyError, e: + except KeyError as e: index = self.varIndex[q] = len(self.indexVar) self.indexVar.append(q) symbol = self.ensureUniqueSymbol(symbol, q) return (VariableQueryPiece(symbol, index, 1)) # uri1#foo and uri2#foo may collide - except ValueError, e: + except ValueError as e: try: variables.index(q) try: index = self.varIndex[q] symbol = self.fragments[q] - except KeyError, e: + except KeyError as e: index = self.varIndex[q] = len(self.indexVar) self.indexVar.append(q) symbol = self.ensureUniqueSymbol(symbol, q) return (VariableQueryPiece(symbol, index, 0)) - except ValueError, e: + except ValueError as e: try: return (LiteralQueryPiece(q.string)) - except AttributeError, e: + except AttributeError as e: try: return (UriQueryPiece(q.uriref())) - except Exception, e: - raise RuntimeError, "what's a \""+q+"\"?" + except Exception as e: + raise RuntimeError("what's a \""+q+"\"?") def ensureUniqueSymbol(self, symbol, q): i = 0 base = symbol - while self.symbols.has_key(symbol): - symbol = base + `i` + while symbol in self.symbols: + symbol = base + repr(i) i = i + 1 # This is always called for new symbols so no need to check the # existent fragment to symbol mapping. @@ -221,7 +221,7 @@ def buildQuerySetsFromArray(self, sentences): symbol = m.group("symbol") try: index = self.varIndex[symbol] - except KeyError, e: + except KeyError as e: index = self.varIndex[symbol] = len(self.indexVar) self.indexVar.append(symbol) sentStruc.append(VariableQueryPiece(symbol, index, 0)) @@ -234,7 +234,7 @@ def buildQuerySetsFromArray(self, sentences): if (m): sentStruc.append(LiteralQueryPiece(m.group("literal"))) else: - raise RuntimeError, "what's a \""+word+"\"?" + raise RuntimeError("what's a \""+word+"\"?") return SetQueryPiece(set) def getVarIndex(self, symbol): return self.varIndex[symbol] @@ -280,10 +280,10 @@ def __init__(self, baseURI, tableDescModuleName, user, password, host, database, tableDescModule = imp.load_module(tableDescModuleName, fp, path, stuff) if (fp): fp.close self.structure = tableDescModule._AllTables - except ImportError, e: - print tableDescModuleName, " not found\n" + except ImportError as e: + print(tableDescModuleName, " not found\n") self.structure = None - except TypeError, e: + except TypeError as e: self.structure = None self.baseUri = baseURI @@ -403,7 +403,7 @@ def _walkQuery (self, term, row, flags): # uri = uri[:-5] sAlias = self._bindTableToConstant(sTable, s) else: - raise RuntimeError, "not implemented" + raise RuntimeError("not implemented") else: sAlias = self._bindTableToVariable(sTable, s) @@ -425,7 +425,7 @@ def _walkQuery (self, term, row, flags): firstAlias, firstField = scalarReference self._addWhere(sAlias+"."+field+"="+firstAlias+"."+firstField, term) self._addReaches(sAlias, firstAlias, term) - except KeyError, e: + except KeyError as e: col = self._addSelect(sAlias+"."+field, o.symbol()+"_"+field, "\n ") self.scalarBindings.append([o, [col]]) self.scalarReferences[o.symbol()] = [sAlias, field] @@ -435,7 +435,7 @@ def _preferredID (self, table, alias, qp): try: pk.isdigit() # Lord, there's got to be a better way. @@@ pk = [pk] - except AttributeError, e: + except AttributeError as e: pk = pk cols = [] fieldz = [] @@ -455,8 +455,8 @@ def _addSelect (self, name, label, punct): try: ret = self.fieldBindings[name] if (self.duplicateFieldsInQuery): - raise KeyError, "force duplicate fields" - except KeyError, e: + raise KeyError("force duplicate fields") + except KeyError as e: self.selects.append(name) self.labels.append(label) self.selectPunct.append(punct) @@ -466,14 +466,14 @@ def _addSelect (self, name, label, punct): def _bindTableToVariable (self, table, qp): try: self.symbolBindings[qp.symbol()] - except KeyError, e: + except KeyError as e: self.symbolBindings[qp.symbol()] = {} try: binding = self.symbolBindings[qp.symbol()][table] - except KeyError, e: + except KeyError as e: try: maxForTable = self.tableBindings[table] - except KeyError, e: + except KeyError as e: maxForTable = self.tableBindings[table] = 0 binding = table+"_"+repr(maxForTable) self.symbolBindings[qp.symbol()][table] = binding @@ -490,14 +490,14 @@ def _bindTableToConstant (self, table, qp): # assert (0, "don't go there") try: self.symbolBindings[uri] - except KeyError, e: + except KeyError as e: self.symbolBindings[uri] = {} try: binding = self.symbolBindings[uri][table] - except KeyError, e: + except KeyError as e: try: maxForTable = self.tableBindings[table] - except KeyError, e: + except KeyError as e: maxForTable = self.tableBindings[table] = 0 binding = table+"_"+repr(maxForTable) self.symbolBindings[uri][table] = binding @@ -515,12 +515,12 @@ def _lookupPredicate(self, predicate): field = m.group("field") try: fieldDesc = self.structure[table]['-fields'][field] - except KeyError, e: + except KeyError as e: fieldDesc = None try: target = fieldDesc['-target'] return table, field, target[0], target[1] - except KeyError, e: + except KeyError as e: target = None return table, field, None, None @@ -528,8 +528,8 @@ def _uniquesFor(self, table): try: pk = self.structure[table]['-primaryKey'] return pk; - except KeyError, e: - raise RuntimeError, "no primary key for table \"table\"" + except KeyError as e: + raise RuntimeError("no primary key for table \"table\"") def _composeUniques(self, values, table): segments = [] @@ -537,7 +537,7 @@ def _composeUniques(self, values, table): try: pk.isdigit() # Lord, there's got to be a better way. @@@ pk = [pk] - except AttributeError, e: + except AttributeError as e: pk = pk for field in pk: lvalue = CGI_escape(field) @@ -552,7 +552,7 @@ def _decomposeUniques(self, uri, tableAs, table): table1 = m.group("table") field = m.group("field") if (table1 != table): - raise RuntimeError, "\""+uri+"\" not based on "+self.baseUri.uri+table + raise RuntimeError("\""+uri+"\" not based on "+self.baseUri.uri+table) recordId = CGI_unescape(field) parts = string.split(recordId, '.') constraints = []; @@ -637,7 +637,7 @@ def _buildResults (self, cursor, implQuerySets, row, statements): Assure(nextResults[-1], queryPiece.getVarIndex(), st) # nextResults[-1][queryPiece.getVarIndex()] = uri rowBindings[queryPiece.symbol()] = st # Grab sub-expressions from the results - for qpStr in self.disjunctionBindings.keys(): + for qpStr in list(self.disjunctionBindings.keys()): binding = self.disjunctionBindings[qpStr] qp, cols = binding rowBindings[qp] = self.interner.intern((LITERAL, answerRow[cols[0]])) @@ -675,15 +675,15 @@ def _bindingsToStatements (self, term, rowBindings, uniqueStatementsCheat): try: statement = uniqueStatementsCheat[pred][subj][obj] - except KeyError, e: + except KeyError as e: statement = ['', pred, subj, obj] try: byPred = uniqueStatementsCheat[pred] try: bySubj = byPred[subj] - except KeyError, e: + except KeyError as e: uniqueStatementsCheat[pred][subj] = {obj : statement} - except KeyError, e: + except KeyError as e: uniqueStatementsCheat[pred] = {subj : {obj : statement}} return [statement] @@ -692,40 +692,40 @@ def _addReaches (self, frm, to, term): # versa. The first path is the first way the tables were constrained. # Additional paths represent over-constraints. self._fromReachesToAndEverythingToReaches(frm, to, [term]) - for fromFrom in self.constraintReaches[frm].keys(): + for fromFrom in list(self.constraintReaches[frm].keys()): if (fromFrom != to): self._fromReachesToAndEverythingToReaches(fromFrom, to, [self.constraintReaches[frm][fromFrom], term]) def _fromReachesToAndEverythingToReaches (self, frm, to, path): try: self.constraintReaches[frm] - except KeyError, e: + except KeyError as e: self.constraintReaches[frm] = {} try: self.constraintReaches[to] - except KeyError, e: + except KeyError as e: self.constraintReaches[to] = {} try: self.overConstraints[frm][to].append(path) - except KeyError, e: + except KeyError as e: # print " [c]-[g]->[p]\n" try: - for toTo in self.constraintReaches[to].keys(): + for toTo in list(self.constraintReaches[to].keys()): toPath = [self.constraintReaches[to][toTo], path] try: self.overConstraints[frm][toTo].append(path) - except KeyError, e: + except KeyError as e: self.constraintReaches[frm][toTo] = toPath self.constraintReaches[toTo][frm] = toPath - except KeyError, e: + except KeyError as e: self.constraintReaches[to] = {} self.constraintReaches[frm][to] = path self.constraintReaches[to][frm] = path def _showConstraints (self): ret = [] - for frm in self.constraintReaches.keys(): - for to in self.constraintReaches[frm].keys(): + for frm in list(self.constraintReaches.keys()): + for to in list(self.constraintReaches[frm].keys()): ret.append(self._showConstraint(frm, to)) return string.join(ret, "\n") @@ -734,34 +734,34 @@ def _showConstraints (self): # return "frm:to: pathStr" def _mergeCommonConstraints (self, reachesSoFar, term, subTerm): - for fromTable in self.constraintReaches.keys(): + for fromTable in list(self.constraintReaches.keys()): try: reachesSoFar[fromTable] - except KeyError, e: + except KeyError as e: try: self.constraintHints[fromTable] - except KeyError, e: + except KeyError as e: self.constraintHints[fromTable] = {} # push (@{self.constraintHints[fromTable]}, "for fromTable") - for toTable in self.constraintReaches[fromTable].keys(): + for toTable in list(self.constraintReaches[fromTable].keys()): try: reachesSoFar[fromTable][toTable] - except KeyError, e: + except KeyError as e: self.constraintHints[fromTable][toTable].append([term, subTerm]) self.constraintHints[toTable][fromTable].append([term, subTerm]) - for fromTable in eachesSoFa.keys(): + for fromTable in list(eachesSoFa.keys()): try: self.constraintReaches[fromTable] - except KeyError, e: + except KeyError as e: try: self.constraintHints[fromTable] - except KeyError, e: + except KeyError as e: self.constraintHints[fromTable] = {} # push (@{self.constraintHints[fromTable]}, "for fromTable") - for toTable in reachesSoFar[fromTable].keys(): + for toTable in list(reachesSoFar[fromTable].keys()): try: self.constraintReaches[fromTable][toTable] - except KeyError, e: + except KeyError as e: del reachesSoFar[fromTable][toTable] del reachesSoFar[toTable][fromTable] self.constraintHints[fromTable][toTable].append([term, subTerm]) @@ -774,46 +774,46 @@ def _checkForUnderConstraint (self): table, alias = self.tableAliases[iAliasSet] try: self.constraintReaches[firstAlias][alias] - except KeyError, e: + except KeyError as e: messages.append(" %s not constrained against %s" % (firstAlias, alias)) - if (self.constraintReaches.has_key(firstAlias)): - for reaches in self.constraintReaches[firstAlias].keys(): + if (firstAlias in self.constraintReaches): + for reaches in list(self.constraintReaches[firstAlias].keys()): messages.append(" %s reaches %s" % (firstAlias, reaches)) else: messages.append(" %s reaches NOTHING" % (firstAlias)) - if (self.constraintReaches.has_key(alias)): - for reaches in self.constraintReaches[alias].keys(): + if (alias in self.constraintReaches): + for reaches in list(self.constraintReaches[alias].keys()): messages.append(" %s reaches %s" % (alias, reaches)) else: messages.append(" %s reaches NOTHING" % (alias)) - if (self.constraintHints.has_key(firstAlias) and self.constraintHints[firstAlias].has_key(alias)): + if (firstAlias in self.constraintHints and alias in self.constraintHints[firstAlias]): for terms in self.constraintHints[firstAlias][alias]: constrainedByStr = terms[1].toString({-brief : 1}) constrainedInStr = terms[0].toString({-brief : 1}) messages.append(" partially constrained by 'constrainedByStr' in 'constrainedInStr'") if (len(messages) > 0): - raise RuntimeError, "underconstraints exception:\n"+string.join(messages, "\n") + raise RuntimeError("underconstraints exception:\n"+string.join(messages, "\n")) def _checkForOverConstraint (self): messages = [] done = {} - for frm in self.overConstraints.keys(): + for frm in list(self.overConstraints.keys()): try: done[frm] - except KeyError, e: + except KeyError as e: break done[frm] = 1 - for to in self.overConstraints[frm].keys(): + for to in list(self.overConstraints[frm].keys()): try: done[frm] - except KeyError, e: + except KeyError as e: break if (to != frm): messages.append(" frm over-constrained against to"._showPath(self.constraintReaches[frm][to])) for path in self.overConstraints[frm][to]: messages.append(' '._showPath(path)) if (len(messages)): - raise RuntimeError, "overconstraints exception:\n"+string.join(messages, "\n") + raise RuntimeError("overconstraints exception:\n"+string.join(messages, "\n")) def _showPath(path) : # static pathStrs = [] @@ -897,13 +897,13 @@ def CGI_unescape(a): rs.results = nextResults def df(datum): return re.sub("http://localhost/SqlDB#", "mysql:", datum) - print string.join(messages, "\n") - print "query matrix \"\"\""+rs.toString({'dataFilter' : None})+"\"\"\" .\n" + print(string.join(messages, "\n")) + print("query matrix \"\"\""+rs.toString({'dataFilter' : None})+"\"\"\" .\n") for solutions in nextStatements: - print "query solution {" + print("query solution {") for statement in solutions: - print ShowStatement(statement) - print "} ." + print(ShowStatement(statement)) + print("} .") blah = """ testing with `python2.2 ./cwm.py test/dbork/aclQuery.n3 -think` diff --git a/dbork/TableRenderer.py b/dbork/TableRenderer.py index 9d03e36d..7dffda29 100755 --- a/dbork/TableRenderer.py +++ b/dbork/TableRenderer.py @@ -123,7 +123,7 @@ def _addData(self, data, theType, filter): try: data[0].isdigit() # Lord, there's got to be a better way. @@@ (@@ to do what? --tim) data = [data] - except AttributeError, e: + except AttributeError as e: data = data # @@? what does this do - tim mean "pass"? for row in data: rowEntry = [] @@ -135,7 +135,7 @@ def _addData(self, data, theType, filter): self.widths.append(0) datum = str(row[columnNo]) if (filter): - datum = filter(datum) + datum = list(filter(datum)) self._checkWidth(datum, len(self.data) - 1, columnNo) rowEntry.append([datum, theType]) @@ -222,4 +222,4 @@ def df(datum): renderer.underline(index=1) # Let's see what hath we wrought. - print renderer.toString()+"\n" + print(renderer.toString()+"\n") diff --git a/dbork/dbview.py b/dbork/dbview.py index da686d22..1c5775f7 100644 --- a/dbork/dbview.py +++ b/dbork/dbview.py @@ -85,7 +85,7 @@ from RDFSink import SYMBOL, LITERAL import toXML # RDF/XML sink -import BaseHTTPServer +import http.server import cgi # for URL-encoded query parsing @@ -93,7 +93,7 @@ #"what mime type to use for RDF?" issue... -class DBViewServer(BaseHTTPServer.HTTPServer): +class DBViewServer(http.server.HTTPServer): """Export an SQL database, read-only, into HTTP/RDF. @@integration with http://www.w3.org/DesignIssues/RDB-RDF.html @@ -119,7 +119,7 @@ class DBViewServer(BaseHTTPServer.HTTPServer): def __init__(self, addr, handlerClass, db, home, dbName): - BaseHTTPServer.HTTPServer.__init__(self, addr, handlerClass) + http.server.HTTPServer.__init__(self, addr, handlerClass) self._db = db self._home = home @@ -127,7 +127,7 @@ def __init__(self, addr, handlerClass, db, home, dbName): self._base = 'http://%s:%s%s%s' % (addr[0], addr[1], home, dbName) -class DBViewHandler(BaseHTTPServer.BaseHTTPRequestHandler): +class DBViewHandler(http.server.BaseHTTPRequestHandler): QPath = '/.dbq' # not an SQL name UIPath = '/.ui' @@ -616,7 +616,7 @@ def parseQuery(qs): while 1: nameN = 'name%s' % i - if not form.has_key(nameN): break + if nameN not in form: break tname = form[nameN][0] tables.append(tname) @@ -672,8 +672,8 @@ def testSvc(): hostPort = (httpHost, int(httpPort)) httpd = DBViewServer(hostPort, DBViewHandler, db, '/', dbName) - print "base:", httpd._base - print "Serving HTTP on port", httpPort, "..." + print("base:", httpd._base) + print("Serving HTTP on port", httpPort, "...") httpd.serve_forever() @@ -717,12 +717,12 @@ def testSQL(): for path in cases: path, fields = split(path, '?') - print "CGI parse:", cgi.parse_qs(fields) + print("CGI parse:", cgi.parse_qs(fields)) fields, tables, keys, joins, cond = parseQuery(fields) - print "SQL parse:", fields, tables, keys, joins, cond + print("SQL parse:", fields, tables, keys, joins, cond) - print "as SQL:", asSQL(fields, tables, keys, joins, cond) + print("as SQL:", asSQL(fields, tables, keys, joins, cond)) def testQ(): @@ -734,12 +734,12 @@ def testQ(): path='/administration/.dbq?name1=users&fields1=family%2Cemail%2Ccity%2Cid&key1=id&name2=techplenary2002&fields2=plenary%2Cmeal_choice&key2=&kj2_1=id&name3=&fields3=&key3=&kj3_1=&kj3_2=&name4=&fields4=&key4=&kj4_1=&kj4_2=&kj4_3=' path, fields = split(path, '?') - print "CGI parse:", cgi.parse_qs(fields) + print("CGI parse:", cgi.parse_qs(fields)) fields, tables, keys, joins, cond = parseQuery(fields) - print "SQL parse:", fields, tables, keys, joins, cond + print("SQL parse:", fields, tables, keys, joins, cond) - print "as SQL:", asSQL(fields, tables, keys, joins, cond) + print("as SQL:", asSQL(fields, tables, keys, joins, cond)) sink = toXML.ToRDF(sys.stdout, 'stdout:') @@ -767,8 +767,8 @@ def main(argv): hostPort = (httpHost, int(httpPort)) httpd = DBViewServer(hostPort, DBViewHandler, db, '/', dbName) - print "base:", httpd._base - print "Serving database %s HTTP on port %s ..." % (dbName, httpPort) + print("base:", httpd._base) + print("Serving database %s HTTP on port %s ..." % (dbName, httpPort)) httpd.serve_forever() diff --git a/dbork/sparqltoy.py b/dbork/sparqltoy.py index 55194358..838500d9 100644 --- a/dbork/sparqltoy.py +++ b/dbork/sparqltoy.py @@ -80,11 +80,11 @@ def constructParts(s): if k.lower() == 'where': return ns, s, conc else: - raise ValueError, "expecting 'where'; found " + s + raise ValueError("expecting 'where'; found " + s) else: - raise ValueError, "expecting '{'; found " + s + raise ValueError("expecting '{'; found " + s) else: - raise ValueError, "expecting 'construct'; found " + s + raise ValueError("expecting 'construct'; found " + s) def mkSelectFormula(vars, ns, ant, kb=None): @@ -99,7 +99,7 @@ def mkSelectFormula(vars, ns, ant, kb=None): n3p = notation3.SinkParser(kb, baseURI="file:/") pfxDecls = '' - for p, u in ns.items(): + for p, u in list(ns.items()): pfxDecls += ("@prefix %s: <%s>.\n" % (p, u)) r = pfxDecls + "<> " + ant + ("\n ; { (%s) a <#Answer> }." % (" ".join(vars))) @@ -118,7 +118,7 @@ def mkConstructFormula(ns, ant, conc, kb=None): n3p = notation3.SinkParser(kb, baseURI="file:/") pfxDecls = '' - for p, u in ns.items(): + for p, u in list(ns.items()): pfxDecls += ("@prefix %s: <%s>.\n" % (p, u)) r = pfxDecls + "[] " + ant + \ @@ -143,23 +143,23 @@ def queryTriples(s): return f ###### -import BaseHTTPServer +import http.server import cgi # for URL-encoded query parsing RDF_MediaType = "application/rdf+xml" XML_MediaType = 'text/xml; charset="utf-8"' -class SparqlServer(BaseHTTPServer.HTTPServer): +class SparqlServer(http.server.HTTPServer): """export toy SPARQL service interface, for generating HTTP traces """ def __init__(self, addr, handlerClass): - BaseHTTPServer.HTTPServer.__init__(self, addr, handlerClass) + http.server.HTTPServer.__init__(self, addr, handlerClass) self._kb = llyn.RDFStore() -class SparqleHandler(BaseHTTPServer.BaseHTTPRequestHandler): +class SparqleHandler(http.server.BaseHTTPRequestHandler): QPath = '/sq?' def do_GET(self): @@ -184,7 +184,7 @@ def doQ(self): self.wfile.write("no query param") return - print "@@got query:", sparql + print("@@got query:", sparql) dataSet = kb.newFormula() try: @@ -194,11 +194,11 @@ def doQ(self): else: for d in ds: load(kb, d, openFormula = dataSet) - print "@@loaded", d, "; size=", dataSet.size() + print("@@loaded", d, "; size=", dataSet.size()) ns, ant, conc = constructParts(sparql) #@@ assume construct qf = mkConstructFormula(ns, ant, conc, kb) - print "@@query:", qf.n3String() + print("@@query:", qf.n3String()) results = kb.newFormula() applyQueries(dataSet, qf, results) @@ -239,7 +239,7 @@ def main(argv): hostPort = ('127.0.0.1', int(port)) httpd = SparqlServer(hostPort, SparqleHandler) - print "sparqltoy %s\nserving on port %s ..." % (__version__, port) + print("sparqltoy %s\nserving on port %s ..." % (__version__, port)) httpd.serve_forever() diff --git a/de-cr.py b/de-cr.py index 4abd4f23..8fd76520 100755 --- a/de-cr.py +++ b/de-cr.py @@ -28,7 +28,7 @@ def strip(path): input = open(path, "r") buf = input.read() # Read the file if not ascii: - buf = buf.decode('utf8') + buf = buf.decode('utf8') input.close() if buf.find("\r") >=0 or buf.find("\0") >=0: if not nochange: @@ -40,7 +40,7 @@ def strip(path): if c == "\r" : crs = crs + 1 if lineNumbers: - print "CR at ", lines + print("CR at ", lines) if i < n-1 and buf[i+1] != "\n": newlines += 1 if not nochange: output.write("\n") @@ -61,10 +61,10 @@ def strip(path): if crs > 0 or nulls > 0 or verbose: if nochange: sys.stderr.write("de-cr: %i CRs found, %i needed LFs, %i nulls, %i non-cr non-null characters in %s.\n"%( - crs, newlines, nulls, total, path)) + crs, newlines, nulls, total, path)) else: sys.stderr.write("de-cr: %i CRs removed, %i LFs inserted, %i nulls, %i non-CR non-null characters left in %s.\n"%( - crs, newlines, nulls, total, path)) + crs, newlines, nulls, total, path)) def do(path): @@ -103,18 +103,18 @@ def do(path): elif arg == "-v": verbose = 1 # Tell me even about files which were ok else: - print """Bad option argument. + print("""Bad option argument. -r recursive -a do all files, not just .n3 .py and .rdf -f fix files instead of just looking - -ascii use ascii not UTF8 - -0 allow null characters + -ascii use ascii not UTF8 + -0 allow null characters This program restores a file to standard unix LF conventions for line end. It removes CR characters, inserting a new LF to replace them if they are not followed by a LF in the original file. It will not change any files unless -f is given. -""" +""") sys.exit(-1) else: files.append(arg) diff --git a/delta.py b/delta.py index 7a05c98a..9cb0882b 100755 --- a/delta.py +++ b/delta.py @@ -50,19 +50,19 @@ from swap.notation3 import RDF_NS_URI from swap.llyn import Formula, CONTEXT, PRED, SUBJ, OBJ except ImportError: - import llyn, diag - from myStore import loadMany - from diag import verbosity, setVerbosity, progress - import notation3 # N3 parsers and generators + from . import llyn, diag + from .myStore import loadMany + from .diag import verbosity, setVerbosity, progress + from . import notation3 # N3 parsers and generators - from RDFSink import FORMULA, LITERAL, ANONYMOUS, Logic_NS - import uripath - from uripath import base - from myStore import Namespace - import myStore - from notation3 import RDF_NS_URI - from llyn import Formula, CONTEXT, PRED, SUBJ, OBJ + from .RDFSink import FORMULA, LITERAL, ANONYMOUS, Logic_NS + from . import uripath + from .uripath import base + from .myStore import Namespace + from . import myStore + from .notation3 import RDF_NS_URI + from .llyn import Formula, CONTEXT, PRED, SUBJ, OBJ #daml = Namespace("http://www.daml.org/2001/03/daml+oil#") OWL = Namespace("http://www.w3.org/2002/07/owl#") @@ -71,18 +71,18 @@ DELTA = Namespace("http://www.w3.org/2004/delta#") def debugBnode(n, f): - progress("For node %s" % `n`) + progress("For node %s" % repr(n)) for s in f.statementsMatching(subj=n): - progress(" %s %s; # could be ifp?" %(`s.predicate()`, `s.object()`)) + progress(" %s %s; # could be ifp?" %(repr(s.predicate()), repr(s.object()))) for s in f.statementsMatching(obj=n): - progress(" is %s of %s; # could be fp?" %(`s.predicate()`, `s.subject()`)) + progress(" is %s of %s; # could be fp?" %(repr(s.predicate()), repr(s.subject()))) def lookUp(predicates, assumptions=Set()): """Look up all the schemas for the predicates given""" global verbose schemas = assumptions for pred in predicates: - if verbose > 3: progress("Predicate: %s" % `pred`) + if verbose > 3: progress("Predicate: %s" % repr(pred)) u = pred.uriref() hash = u.find("#") if hash <0: @@ -108,9 +108,9 @@ def nailFormula(f, assumptions=Set()): for node in nodes: if node.generated() or node in f.existentials(): bnodes.add(node) - if verbose >=5: progress("Blank node: %s" % `node`) + if verbose >=5: progress("Blank node: %s" % repr(node)) else: - if verbose >=5: progress("Fixed node: %s" % `node`) + if verbose >=5: progress("Fixed node: %s" % repr(node)) sofar[node] = [] meta = lookUp(predicates, assumptions) @@ -146,12 +146,12 @@ def nailFormula(f, assumptions=Set()): if y not in loose: # y is the possible anchor defi = (x, inverse, pred, y) if x in loose: # this node - if verbose > 4: progress(" Nailed %s as %s%s%s" % (`x`, `y`, `char`, `pred`)) + if verbose > 4: progress(" Nailed %s as %s%s%s" % (repr(x), repr(y), repr(char), repr(pred))) loose.discard(x) newNailed.add(x) else: if verbose >=6 : progress( - " (ignored %s as %s%s%s)" % (`x`, `y`, `char`, `pred`)) + " (ignored %s as %s%s%s)" % (repr(x), repr(y), repr(char), repr(pred))) definitions.append(defi) # if verbose > 9: progress(" Definition[x] is now", definition[x]) if inverse: equivalentSet = Set(f.each(obj=y, pred=pred)) @@ -208,7 +208,7 @@ def nailFormula(f, assumptions=Set()): "Can't nail: %s all have %s of %s." % (others, pred, obj)) continue # Defn would be ambiguous in this graph defi = (x, 1, pred, obj) - if verbose>2: progress(" Weakly-nailed %s as %s%s%s" % (`x`, `obj`, "^", `pred`)) + if verbose>2: progress(" Weakly-nailed %s as %s%s%s" % (repr(x), repr(obj), "^", repr(pred))) loose.discard(x) newNailed.add(x) definitions.append(defi) @@ -220,7 +220,7 @@ def nailFormula(f, assumptions=Set()): for n in loose: progress("For node %s" % n) for s in f.statementsMatching(subj=n): - progress(" %s %s; # could be ifp?" %(`s.predicate()`, `s.object()`)) + progress(" %s %s; # could be ifp?" %(repr(s.predicate()), repr(s.object()))) for s in f.statementsMatching(obj=n): progress(" is %s of %s; # could be fp?" %(s.predicate(), s.subject())) @@ -336,7 +336,7 @@ def patches(delta, f, only_f, originalBnodes, definitions, deleting=0): lhs = lhs.close() rhs = rhs.close() delta.add(subj=lhs, pred=patchVerb, obj=rhs) - if verbose >1: progress("PATCH: %s %s %s\n" %(lhs.n3String(), `patchVerb`, rhs.n3String())) + if verbose >1: progress("PATCH: %s %s %s\n" %(lhs.n3String(), repr(patchVerb), rhs.n3String())) return def consolidate(delta, patchVerb): @@ -354,7 +354,7 @@ def consolidate(delta, patchVerb): list = [] agenda[s.subject()] = list list.append(s) - for lhs, list in agenda.items(): + for lhs, list in list(agenda.items()): if verbose >3: progress("Patches lhs= %s: %s" %(lhs, list)) if len(list) > 1: rhs = delta.newFormula() @@ -382,19 +382,19 @@ def differences(f, g, assumptions): if x in match: continue # done already if x in f._redirections: - if verbose > 3: progress("Redirected %s to %s. Ignoring" % (`x`, `f._redirections[x]`)) + if verbose > 3: progress("Redirected %s to %s. Ignoring" % (repr(x), repr(f._redirections[x]))) unmatched.discard(x) continue - if verbose > 3: progress("Definition of %s = %s%s%s"% (`x`, `y` , ".!^"[inverse], `pred`)) + if verbose > 3: progress("Definition of %s = %s%s%s"% (repr(x), repr(y) , ".!^"[inverse], repr(pred))) if y.generated(): while y in f._redirections: y = f._redirections[y] - if verbose>4: progress(" redirected to %s = %s%s%s"% (`x`, `y`, "!^"[inverse], `pred`)) + if verbose>4: progress(" redirected to %s = %s%s%s"% (repr(x), repr(y), "!^"[inverse], repr(pred))) yg = match.get(y, None) if yg == None: - if verbose>4: progress(" Had definition for %s in terms of %s which is not matched"%(`x`,`y`)) + if verbose>4: progress(" Had definition for %s in terms of %s which is not matched"%(repr(x),repr(y))) continue else: yg = y @@ -408,12 +408,12 @@ def differences(f, g, assumptions): if len(matches) > 1: raise RuntimeError("""Rats. Wheras in the first graph %s%s%s uniquely selects %s, - in the other graph there are more than 1 matches: %s""" % (`y`, "!^"[inverse], `pred`, `x`, `matches`)) + in the other graph there are more than 1 matches: %s""" % (repr(y), "!^"[inverse], repr(pred), repr(x), repr(matches))) for q in matches: # pick only one @@ python function? z = q break if verbose > 2: - progress("Found match for %s in %s " % (`x`,`z`)) + progress("Found match for %s in %s " % (repr(x),repr(z))) match[x] = z unmatched.discard(x) @@ -512,14 +512,14 @@ def main(): version = "$Id$"[1:-1] if diffFiles == []: nailFormula(graph, assumptions) - if verbose > 1: print "# Smush generated by " + version - print graph.close().n3String(base=base(), flags="a") + if verbose > 1: print("# Smush generated by " + version) + print(graph.close().n3String(base=base(), flags="a")) sys.exit(0) graph2 = loadFiles(diffFiles) delta = differences(graph, graph2, assumptions) - if verbose >1: print "# Differences by " + version - print delta.close().n3String(base=base()) + if verbose >1: print("# Differences by " + version) + print(delta.close().n3String(base=base())) # sys.exit(len(delta)) # sys.exit(0) # didn't crash if delta.contains(): # Any statements in delta at all? diff --git a/diag.py b/diag.py index d0a49712..badb94c0 100644 --- a/diag.py +++ b/diag.py @@ -21,14 +21,14 @@ def progress(*args): sys.stderr.write(" "*level) for a in args: i = 0 - a = unicode(a) + a = str(a) while 1: ## lineCount[0] += 1 i = a.find("\n", i) if i < 0: break a = a[:i+1] + (" "*level) + a[i+1:] i = i+1 - q = utf_8_encode(u"%s " % (a,))[0] + q = utf_8_encode("%s " % (a,))[0] sys.stderr.write(q) ## if lineCount[0] > 20: ## lineCount[0] = 0 @@ -70,7 +70,7 @@ def printState(prefix="#trace# "): sys.stderr.write(prefix+ str(frame.f_code.co_name)+" "+ str(frame.f_locals['self'])+"\n") - for varname in frame.f_locals.keys(): + for varname in list(frame.f_locals.keys()): if varname == "self": continue sys.stderr.write("%s %-8s: %s\n" % (prefix, varname, frame.f_locals[varname])) diff --git a/formula.py b/formula.py index 9281c94e..c65362be 100755 --- a/formula.py +++ b/formula.py @@ -1,4 +1,4 @@ -from __future__ import generators + #! /usr/bin/python """ @@ -24,27 +24,27 @@ __version__ = '$Id$'[1:-1] import types -import StringIO +import io import sys # for outputstrings. shouldn't be here - DWC -from set_importer import Set, ImmutableSet, sorted +from .set_importer import Set, ImmutableSet, sorted -import notation3 # N3 parsers and generators, and RDF generator +from . import notation3 # N3 parsers and generators, and RDF generator -import diag # problems importing the tracking flag, must be explicit it seems diag.tracking -from diag import progress, verbosity, tracking -from term import matchSet, \ +from . import diag # problems importing the tracking flag, must be explicit it seems diag.tracking +from .diag import progress, verbosity, tracking +from .term import matchSet, \ AnonymousNode , AnonymousExistential, AnonymousUniversal, \ Term, CompoundTerm, List, \ unifySequence, unify -from RDFSink import Logic_NS -from RDFSink import CONTEXT, PRED, SUBJ, OBJ -from RDFSink import FORMULA, SYMBOL +from .RDFSink import Logic_NS +from .RDFSink import CONTEXT, PRED, SUBJ, OBJ +from .RDFSink import FORMULA, SYMBOL -from why import Because, isTopLevel +from .why import Because, isTopLevel ###################################### Forumula @@ -92,7 +92,7 @@ def __repr__(self): return "{}" if len(self.statements) == 1: st = self.statements[0] - return "{"+`st[SUBJ]`+" "+`st[PRED]`+" "+`st[OBJ]`+"}" + return "{"+repr(st[SUBJ])+" "+repr(st[PRED])+" "+repr(st[OBJ])+"}" s = Term.__repr__(self) return "{%i}" % len(self.statements) @@ -104,7 +104,7 @@ def compareTerm(self, other): "Assume is also a Formula - see function compareTerm below" for f in self, other: if f.canonical is not f: - progress("@@@@@ Comparing formula NOT canonical", `f`) + progress("@@@@@ Comparing formula NOT canonical", repr(f)) s = self.statements o = other.statements ls = len(s) @@ -132,10 +132,10 @@ def compareTerm(self, other): diff = cmp(s[i],o[i]) if diff != 0: return diff return 0 - import why + from . import why raise RuntimeError("%s\n%s" % (dict(why.proofsOf), self.debugString())) raise RuntimeError("Identical formulae not interned! Length %i: %s\n\t%s\n vs\t%s" % ( - ls, `s`, self.debugString(), other.debugString())) + ls, repr(s), self.debugString(), other.debugString())) def existentials(self): @@ -355,11 +355,11 @@ def substitution(self, bindings, why=None, cannon=False, keepOpen=False): store = self.store if self in bindings: return bindings[self] - oc = self.occurringIn(bindings.keys()) + oc = self.occurringIn(list(bindings.keys())) if oc == Set(): return self # phew! y = store.newFormula() - if verbosity() > 90: progress("substitution: formula"+`self`+" becomes new "+`y`, + if verbosity() > 90: progress("substitution: formula"+repr(self)+" becomes new "+repr(y), " because of ", oc) y.loadFormulaWithSubstitution(self, bindings, why=why) if keepOpen: @@ -417,9 +417,9 @@ def loadFormulaWithSubstitution(self, old, bindings={}, why=None, cannon=False): why=why) realStatementList.append((subj, pred, obj)) except AssertionError: - print 'subj=%s' % subj.debugString() - print 'oldSubj=%s' % (s[SUBJ].debugString(),) - print 'subj.canonical=%s' % subj.canonical.debugString() + print('subj=%s' % subj.debugString()) + print('oldSubj=%s' % (s[SUBJ].debugString(),)) + print('subj.canonical=%s' % subj.canonical.debugString()) raise if diag.chatty_flag > 80: def thing2string(x): @@ -576,7 +576,7 @@ def n3EntailedBy(pattern, kb, vars=Set([]), existentials=Set([]), bindings={}): """ if diag.chatty_flag > 99: progress("n3EntailedBy: %s entailed by %s ?" % - (`pattern`, `kb`)) + (repr(pattern), repr(kb))) if diag.chatty_flag > 139: progress("Pattern is %s\n\nKB is %s" % (pattern.debugString(), kb.debugString())) assert isinstance(kb, Formula), kb @@ -585,7 +585,7 @@ def n3EntailedBy(pattern, kb, vars=Set([]), existentials=Set([]), bindings={}): vars | pattern.existentials(), # | pattern.universals(), bindings) - if diag.chatty_flag > 99: progress("n3EntailedBy: match result: ", `nbs`) + if diag.chatty_flag > 99: progress("n3EntailedBy: match result: ", repr(nbs)) if nbs == []: return [] res = [] for nb, rea in nbs: @@ -599,7 +599,7 @@ def n3EntailedBy(pattern, kb, vars=Set([]), existentials=Set([]), bindings={}): pu = Set([ nb.get(v,v) for v in pattern.universals()]) if diag.chatty_flag > 99: progress("\tpu=%s; ku=%s" %(pu,ku)) if not pu.issubset(ku): return [] # KB stronger - more u's - if diag.chatty_flag > 99: progress("n3EntailwsBy: success with ", `nb`) + if diag.chatty_flag > 99: progress("n3EntailwsBy: success with ", repr(nb)) res.append((nb, None)) # That works return res @@ -624,7 +624,7 @@ def add(self, subj, pred, obj, why=None): why may be a reason for use when a proof will be required. """ if self.canonical != None: - raise RuntimeError("Attempt to add statement to canonical formula "+`self`) + raise RuntimeError("Attempt to add statement to canonical formula "+repr(self)) self.store.size += 1 @@ -640,7 +640,7 @@ def removeStatement(self, s): This implementation is alas slow, as removal of items from tha hash is slow. """ - assert self.canonical == None, "Cannot remove statement from canonical "+`self` + assert self.canonical == None, "Cannot remove statement from canonical "+repr(self) self.store.size = self.store.size-1 self.statements.remove(s) return @@ -662,7 +662,7 @@ def canonicalize(F): store = F.store if F.canonical != None: if verbosity() > 70: - progress("Canonicalize -- @@ already canonical:"+`F`) + progress("Canonicalize -- @@ already canonical:"+repr(F)) return F.canonical # @@@@@@@@ no canonicalization @@ warning F.canonical = F @@ -671,7 +671,7 @@ def canonicalize(F): def n3String(self, base=None, flags=""): "Dump the formula to an absolute string in N3" - buffer=StringIO.StringIO() + buffer=io.StringIO() _outSink = notation3.ToN3(buffer.write, quiet=1, base=base, flags=flags) self.store.dumpNested(self, _outSink) @@ -679,7 +679,7 @@ def n3String(self, base=None, flags=""): def ntString(self, base=None, flags="bravestpun"): "Dump the formula to an absolute string in N3" - buffer=StringIO.StringIO() + buffer=io.StringIO() _outSink = notation3.ToN3(buffer.write, quiet=1, base=base, flags=flags) self.store.dumpBySubject(self, _outSink) @@ -688,8 +688,8 @@ def ntString(self, base=None, flags="bravestpun"): def rdfString(self, base=None, flags=""): "Dump the formula to an absolute string in RDF/XML" - buffer=StringIO.StringIO() - import toXML + buffer=io.StringIO() + from . import toXML _outURI = 'http://example.com/' _outSink = toXML.ToRDF(buffer, _outURI, base=base, flags=flags) self.store.dumpNested(self, _outSink) @@ -805,7 +805,7 @@ def __getitem__(self, i): # So that we can index the stored thing directly return self.quad[i] def __repr__(self): - return "{"+`self[SUBJ]`+" "+`self[PRED]`+" "+`self[OBJ]`+"}" + return "{"+repr(self[SUBJ])+" "+repr(self[PRED])+" "+repr(self[OBJ])+"}" # The order of statements is only for canonical output # We cannot override __cmp__ or the object becomes unhashable, @@ -880,7 +880,7 @@ def statements(self): def occurringIn(self, vars): "Which variables in the list occur in this?" set = Set() - if verbosity() > 98: progress("----occuringIn: ", `self`) + if verbosity() > 98: progress("----occuringIn: ", repr(self)) for p in PRED, SUBJ, OBJ: y = self[p] if y is self: diff --git a/formulaUnion.py b/formulaUnion.py index cadadcba..acec13db 100755 --- a/formulaUnion.py +++ b/formulaUnion.py @@ -8,37 +8,37 @@ reifyNS = 'http://www.w3.org/2004/06/rei#' owlOneOf = 'http://www.w3.org/2002/07/owl#oneOf' -from __future__ import generators + import types import string import re -import StringIO +import io import sys import time -import uripath +from . import uripath -from OrderedSequence import merge +from .OrderedSequence import merge -import urllib # for log:content +import urllib.request, urllib.parse, urllib.error # for log:content import md5, binascii # for building md5 URIs -import notation3 # N3 parsers and generators, and RDF generator +from . import notation3 # N3 parsers and generators, and RDF generator # import sax2rdf # RDF1.0 syntax parser to N3 RDF stream -import diag # problems importing the tracking flag, must be explicit it seems diag.tracking -from diag import progress, verbosity, tracking -from term import BuiltIn, LightBuiltIn, \ +from . import diag # problems importing the tracking flag, must be explicit it seems diag.tracking +from .diag import progress, verbosity, tracking +from .term import BuiltIn, LightBuiltIn, \ HeavyBuiltIn, Function, ReverseFunction, \ Literal, AnonymousNode , AnonymousExistential, AnonymousUniversal, \ Symbol, Fragment, FragmentNil, Term, CompoundTerm, List, EmptyList, NonEmptyList -from RDFSink import Logic_NS, RDFSink, forSomeSym, forAllSym -from RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 -from RDFSink import N3_nil, N3_first, N3_rest, OWL_NS, N3_Empty, N3_List, List_NS -from RDFSink import RDF_NS_URI -from RDFSink import RDF_type_URI -from RDFSink import FORMULA, LITERAL, ANONYMOUS, SYMBOL +from .RDFSink import Logic_NS, RDFSink, forSomeSym, forAllSym +from .RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 +from .RDFSink import N3_nil, N3_first, N3_rest, OWL_NS, N3_Empty, N3_List, List_NS +from .RDFSink import RDF_NS_URI +from .RDFSink import RDF_type_URI +from .RDFSink import FORMULA, LITERAL, ANONYMOUS, SYMBOL @@ -47,7 +47,7 @@ # Magic resources we know about -from why import Because, BecauseBuiltIn, BecauseOfRule, \ +from .why import Because, BecauseBuiltIn, BecauseOfRule, \ BecauseOfExperience, becauseSubexpression, BecauseMerge ,report diff --git a/gram2html.py b/gram2html.py index 3279010f..95c153d3 100644 --- a/gram2html.py +++ b/gram2html.py @@ -17,7 +17,7 @@ """ from string import find -import yapps2, yappsrt +from . import yapps2, yappsrt def toHTML(self, xwr): @@ -39,7 +39,7 @@ def toHTML(self, xwr): xwr.endElement() -from yapps2 import Eval, Terminal, NonTerminal, \ +from .yapps2 import Eval, Terminal, NonTerminal, \ Sequence, Choice, Option, Plus, Star def expToHTML(sym, xwr, toplevel=1): @@ -100,7 +100,7 @@ def generate(title, inputfilename, outputfilename=''): and an output filename (defaulting to X.py).""" import sys, codecs - from toXML import XMLWriter + from .toXML import XMLWriter dummy, dummy, dummy, encWriter = codecs.lookup('utf-8') if not outputfilename: diff --git a/grammar/check-grammar.py b/grammar/check-grammar.py index 3a2a7a98..690393df 100644 --- a/grammar/check-grammar.py +++ b/grammar/check-grammar.py @@ -62,8 +62,8 @@ def toYacc(x, tokenRegexps): if isinstance(x, Literal): return "'" + str(x.value()) + "'" # @@@ Escaping if x in tokenRegexps: - return deColonise(`x`).upper() - return deColonise(`x`) + return deColonise(repr(x)).upper() + return deColonise(repr(x)) def yaccConvert(yacc, top, tokenRegexps): @@ -86,10 +86,10 @@ def yaccConvert(yacc, top, tokenRegexps): def yaccProduction(yacc, lhs, tokenRegexps): if lhs is BNF.void: - print "\nvoid" + print("\nvoid") return if lhs is BNF.eof: - print "\nEOF" + print("\nEOF") return if isinstance(lhs, Literal): literalTerminals[lhs.value()] = 1 @@ -98,15 +98,15 @@ def yaccProduction(yacc, lhs, tokenRegexps): rhs = g.the(pred=BNF.matches, subj=lhs) if rhs != None: - print "\nToken %s matches regexp %s" %(lhs, rhs) + print("\nToken %s matches regexp %s" %(lhs, rhs)) # tokenRegexps[lhs] = re.compile(rhs.value()) return rhs = g.the(pred=BNF.mustBeOneSequence, subj=lhs) if rhs == None: - print recordError("No definition of " + `lhs`) - raise ValueError("No definition of %s in\n %s" %(`lhs`, `g`)) + print(recordError("No definition of " + repr(lhs))) + raise ValueError("No definition of %s in\n %s" %(repr(lhs), repr(g))) options = rhs - print "\nProduction %s :: %s ie %s" %(`lhs`, `options` , `options.value()`) + print("\nProduction %s :: %s ie %s" %(repr(lhs), repr(options) , repr(options.value()))) yacc.write("\n%s:" % toYacc(lhs, tokenRegexps)) branches = g.each(subj=lhs, pred=BNF.branch) @@ -116,7 +116,7 @@ def yaccProduction(yacc, lhs, tokenRegexps): yacc.write("\t|\t") first = 0 option = g.the(subj=branch, pred=BNF.sequence) - print "\toption: "+`option.value()` + print("\toption: "+repr(option.value())) yacc.write("\t") if option.value() == [] and yacc: yacc.write(" /* empty */") for part in option: @@ -131,10 +131,10 @@ def yaccProduction(yacc, lhs, tokenRegexps): def doProduction(lhs): global branchTable if lhs is BNF.void: - print "\nvoid" + print("\nvoid") return if lhs is BNF.eof: - print "\nEOF" + print("\nEOF") return if isinstance(lhs, Literal): literalTerminals[lhs.value()] = 1 @@ -145,41 +145,41 @@ def doProduction(lhs): rhs = g.the(pred=BNF.matches, subj=lhs) if rhs != None: - print "\nToken %s matches regexp %s" %(lhs, rhs) + print("\nToken %s matches regexp %s" %(lhs, rhs)) tokenRegexps[lhs] = re.compile(rhs.value()) cc = g.each(subj=lhs, pred=BNF.canStartWith) - if cc == []: print recordError("No recod of what token %s can start with" % `lhs`) - print "\tCan start with: %s" % cc + if cc == []: print(recordError("No recod of what token %s can start with" % repr(lhs))) + print("\tCan start with: %s" % cc) return rhs = g.the(pred=BNF.mustBeOneSequence, subj=lhs) if rhs == None: - print recordError("No definition of " + `lhs`) + print(recordError("No definition of " + repr(lhs))) return # raise RuntimeError("No definition of %s in\n %s" %(`lhs`, `g`)) options = rhs - print "\nProduction %s :: %s ie %s" %(`lhs`, `options` , `options.value()`) + print("\nProduction %s :: %s ie %s" %(repr(lhs), repr(options) , repr(options.value()))) succ = g.each(subj=lhs, pred=BNF.canPrecede) - print "\tCan precede ", succ + print("\tCan precede ", succ) branches = g.each(subj=lhs, pred=BNF.branch) for branch in branches: option = g.the(subj=branch, pred=BNF.sequence) - print "\toption: "+`option.value()` + print("\toption: "+repr(option.value())) for part in option: if part not in already and part not in agenda: agenda.append(part) - y = `part` + y = repr(part) conditions = g.each(subj=branch, pred=BNF.condition) if conditions == []: - print recordError(" NO SELECTOR for %s option %s ie %s" %(`lhs`, `option`, `option.value()` )) + print(recordError(" NO SELECTOR for %s option %s ie %s" %(repr(lhs), repr(option), repr(option.value()) ))) if option.value == []: # Void case - the tricky one succ = g.each(subj=lhs, pred=BNF.canPrecede) for y in succ: - print "\t\t\tCan precede ", `y` - print "\t\tConditions: %s" %(conditions) + print("\t\t\tCan precede ", repr(y)) + print("\t\tConditions: %s" %(conditions)) for str1 in conditions: if str1 in branchDict: - print recordError("Conflict: %s is also the condition for %s" % ( - str1, branchDict[str1].value())) + print(recordError("Conflict: %s is also the condition for %s" % ( + str1, branchDict[str1].value()))) branchDict[str1.__str__()] = option # break @@ -190,8 +190,8 @@ def doProduction(lhs): s2 = str2.__str__() # @@ check that selectors are distinct, not substrings if (s1.startswith(s2) or s2.startswith(s1)) and branchDict[str1] is not branchDict[str2]: - print "WARNING: for %s, %s indicates %s, but %s indicates %s" % ( - lhs, s1, branchDict[str1], s2, branchDict[str2]) + print("WARNING: for %s, %s indicates %s, but %s indicates %s" % ( + lhs, s1, branchDict[str1], s2, branchDict[str2])) # print recordError("Conflict: for %s, %s indicates %s, but %s indicates %s" % ( # option.value(), s1, branchDict[str1], s2, branchDict[str2])) branchTable[lhs] = branchDict @@ -232,8 +232,8 @@ def token(parser, str, i): if i == len(str): return "", i # eof - if parser.verb: print "%i) Looking at: ...%s$%s..." % ( - parser.lineNumber, str[i-10:i],str[i:i+10]) + if parser.verb: print("%i) Looking at: ...%s$%s..." % ( + parser.lineNumber, str[i-10:i],str[i:i+10])) for double in "=>", "<=", "^^": if double == str[i:i+2]: return double, i @@ -284,8 +284,8 @@ def parseProduction(parser, lhs, str, tok=None, this=0): rhs = lookupTable.get(tok, None) # Predict branch from token if rhs == None: raise SyntaxError( "Found %s when expecting some form of %s,\n\tsuch as %s\n\t%s" - %(utf_8_encode(tok)[0], lhs, lookupTable.keys(), parser.around(str, this))) - print "%i %s means expand %s as %s" %(parser.lineNumber,tok, lhs, rhs.value()) + %(utf_8_encode(tok)[0], lhs, list(lookupTable.keys()), parser.around(str, this))) + print("%i %s means expand %s as %s" %(parser.lineNumber,tok, lhs, rhs.value())) for term in rhs: if isinstance(term, Literal): lit = term.value() @@ -314,11 +314,11 @@ def parseProduction(parser, lhs, str, tok=None, this=0): # The Grammar formula grammarFile = argv[1].split("#")[0] -print "Loading", grammarFile +print("Loading", grammarFile) start = clock() g = load(grammarFile) taken = clock() - start -print "Loaded %i statements in %fs, ie %f/s." % (len(g), taken, len(g)/taken) +print("Loaded %i statements in %fs, ie %f/s." % (len(g), taken, len(g)/taken)) document = g.newSymbol(argv[2]) @@ -336,16 +336,16 @@ def parseProduction(parser, lhs, str, tok=None, this=0): doProduction(x) if errors != []: - print "###### FAILED with %i errors." % len(errors) - for s in errors: print "\t%s" % s + print("###### FAILED with %i errors." % len(errors)) + for s in errors: print("\t%s" % s) exit(-2) else: - print "Ok for predictive parsing" + print("Ok for predictive parsing") #print "Branch table:", branchTable -print "Literal terminals:", literalTerminals.keys() -print "Token regular expressions:" -for r in tokenRegexps: print "\t%s matches %s" %(r, tokenRegexps[r].pattern) +print("Literal terminals:", list(literalTerminals.keys())) +print("Token regular expressions:") +for r in tokenRegexps: print("\t%s matches %s" %(r, tokenRegexps[r].pattern)) yacc=open(argv[1]+"-yacc.y", "w") yaccConvert(yacc, document, tokenRegexps) diff --git a/grammar/ebnf2turtle.py b/grammar/ebnf2turtle.py index 5113aab9..9961c2e3 100644 --- a/grammar/ebnf2turtle.py +++ b/grammar/ebnf2turtle.py @@ -398,7 +398,7 @@ def token(s): elif s[0] in '(?)*+|-': return ((s[0],) , s[1:]) else: - raise ValueError, "unrecognized token: %s" % s + raise ValueError("unrecognized token: %s" % s) ########## @@ -406,25 +406,25 @@ def token(s): # def startTurtle(pfx, ns, lang, start): - print "@prefix rdf: ." - print "@prefix rdfs: ." - print "@prefix %s: <%s>." % (pfx, ns) - print "@prefix : <%s>." % ns - print "@prefix re: ." - print "@prefix g: ." - print - print ":%s rdfs:isDefinedBy <>; g:start :%s." % (lang, start) + print("@prefix rdf: .") + print("@prefix rdfs: .") + print("@prefix %s: <%s>." % (pfx, ns)) + print("@prefix : <%s>." % ns) + print("@prefix re: .") + print("@prefix g: .") + print() + print(":%s rdfs:isDefinedBy <>; g:start :%s." % (lang, start)) def asTurtle(num, sym, expr, isToken, orig): - print - print ':%s rdfs:label "%s"; rdf:value "%s";' % (sym, sym, num) - print ' rdfs:comment "%s";' % esc(orig.strip()) + print() + print(':%s rdfs:label "%s"; rdf:value "%s";' % (sym, sym, num)) + print(' rdfs:comment "%s";' % esc(orig.strip())) if isToken: pfx = 're' else: pfx = 'g' ttlExpr(expr, pfx, indent=' ', obj=0) - print "." + print(".") def ttlExpr(expr, pfx, indent, obj=1): @@ -437,52 +437,52 @@ def ttlExpr(expr, pfx, indent, obj=1): bra = ket = '' if op == ',': - print indent + bra + "%s:seq (" % pfx + print(indent + bra + "%s:seq (" % pfx) for a in args: ttlExpr(a, pfx, indent + ' ') - print indent + " )" + ket + print(indent + " )" + ket) elif op == '|': - print indent + bra + "%s:alt (" % pfx + print(indent + bra + "%s:alt (" % pfx) for a in args: ttlExpr(a, pfx, indent + ' ') - print indent + " )" + ket + print(indent + " )" + ket) elif op == '-': - print indent + bra + "%s:diff (" % pfx + print(indent + bra + "%s:diff (" % pfx) for a in args: ttlExpr(a, pfx, indent + ' ') - print indent + " )" + ket + print(indent + " )" + ket) elif op == '?': - print indent + bra + "%s:opt " % pfx + print(indent + bra + "%s:opt " % pfx) ttlExpr(args, pfx, indent + ' ') - if ket: print indent + ket + if ket: print(indent + ket) elif op == '+': - print indent + bra + "%s:plus " % pfx + print(indent + bra + "%s:plus " % pfx) ttlExpr(args, pfx, indent + ' ') - if ket: print indent + ket + if ket: print(indent + ket) elif op == '*': - print indent + bra + "%s:star " % pfx + print(indent + bra + "%s:star " % pfx) ttlExpr(args, pfx, indent + ' ') - if ket: print indent + ket + if ket: print(indent + ket) elif op == 'id': if obj: - print "%s:%s" % (indent, args) + print("%s:%s" % (indent, args)) else: - print "%sg:seq ( :%s )" % (indent, args) + print("%sg:seq ( :%s )" % (indent, args)) elif op == "'": - print '%s"%s"' %(indent, esc(args)) + print('%s"%s"' %(indent, esc(args))) elif op == "[": - print '%s%s re:matches "[%s]" %s' % (indent, bra, cclass(args), ket) + print('%s%s re:matches "[%s]" %s' % (indent, bra, cclass(args), ket)) elif op == "#": assert not('"' in args) - print r'%s%s re:matches "[%s]" %s' % (indent, bra, cclass(args), ket) + print(r'%s%s re:matches "[%s]" %s' % (indent, bra, cclass(args), ket)) else: - raise RuntimeError, op + raise RuntimeError(op) def cclass(txt): @@ -521,12 +521,12 @@ def cclass(txt): elif len(hx) < 10: hx = ("0000" + hx)[-8:] # This is just about hopelessly ugly - if unichr(int(hx, 16)) in "\\[]": ret += '\\\\' + if chr(int(hx, 16)) in "\\[]": ret += '\\\\' if len(hx) == 4: ret += r"\u" + hx elif len(hx) == 8: ret += r"\U" + hx else: - raise ValueError, "#x must preceede 1 to 8 hex digits" + raise ValueError("#x must preceede 1 to 8 hex digits") txt = txt[m.end(0):] diff --git a/grammar/gramLL1.py b/grammar/gramLL1.py index d090b44c..db2fd435 100644 --- a/grammar/gramLL1.py +++ b/grammar/gramLL1.py @@ -30,7 +30,7 @@ def main(argv): import simplejson #http://cheeseshop.python.org/pypi/simplejson import sys start = it['rules'][0][0] - print "SYNTAX_%s = " % start, + print("SYNTAX_%s = " % start, end=' ') simplejson.dump(it, sys.stdout) @@ -49,9 +49,9 @@ def asGrammar(f, lang): seq = f.the(subj=lhs, pred=EBNF.seq) if alts is None and seq is None: - raise ValueError, "no alt nor seq for %s" % lhs + raise ValueError("no alt nor seq for %s" % lhs) elif alts and seq: - raise ValueError, "both alt and seq for %s" % lhs + raise ValueError("both alt and seq for %s" % lhs) elif alts: for alt in alts: seq = f.the(subj=alt, pred=EBNF.seq) @@ -87,11 +87,11 @@ def tokens(f, lang): def pattern(f, s): if isinstance(s, Literal): - return reesc(unicode(s)) + return reesc(str(s)) pat = f.the(subj=s, pred=REGEX.matches) if pat: - return '(?:%s)' % unicode(pat) + return '(?:%s)' % str(pat) parts = f.the(subj=s, pred=REGEX.seq) if parts: @@ -111,7 +111,7 @@ def pattern(f, s): part = f.the(subj=s, pred=REGEX.opt) if part: return '(?:%s?)' % pattern(f, part) - raise ValueError, s + raise ValueError(s) def reesc(txt): r"""turn a string into a regex string constant for that string @@ -141,7 +141,7 @@ def asSymbol(f, x): And we assume no non-terminal fragids start with '_'. """ if isinstance(x, Literal): - return tokid(unicode(x)) #hmm... + return tokid(str(x)) #hmm... elif x is EBNF.empty or f.the(subj=x, pred=EBNF.seq) == RDF.nil: return 'EMPTY' elif x == EBNF.eof: @@ -163,60 +163,60 @@ def asSymbol(f, x): y = f.the(subj=x, pred=EBNF.seq) if y: return '_seq_%d' % abs(id(x)) - raise ValueError, "umm...@@" + raise ValueError("umm...@@") else: return x.fragid elif f.each(pred=EBNF.terminal, obj=x): return 'TOK_%s' % x.fragid else: - raise ValueError, x + raise ValueError(x) def toYacc(it): - print """ + print(""" %{ int yylex (void); void yyerror (char const *); %} -""" +""") tokens = {} for pat, tok, dummy in it['tokens']: tokens[tok] = 1 if tok.startswith("TOK"): - print "%%token %s" % tok + print("%%token %s" % tok) - print "%%" + print("%%") for rule in it['rules']: lhs = rule[0] rhs = rule[1:] - print "%s: " % lhs, + print("%s: " % lhs, end=' ') for sym in rhs: - if tokens.has_key(sym): + if sym in tokens: if sym.startswith("TOK"): - print sym, + print(sym, end=' ') else: - print '"%s"' % sym, + print('"%s"' % sym, end=' ') else: - print sym, - print ";" - print "%%" + print(sym, end=' ') + print(";") + print("%%") def toPly(it): t2r = dict([(tokid(tok), pat) for pat, tok, func in it['tokens']]) t2t = dict([(tok, tokid(tok)) for pat, tok, func in it['tokens']]) - print "tokens = ", `tuple(t2r.keys())` + print("tokens = ", repr(tuple(t2r.keys()))) - print "# Tokens " + print("# Tokens ") - for t, pat in t2r.iteritems(): - print "t_%s = %s" % (t, `pat`) + for t, pat in t2r.items(): + print("t_%s = %s" % (t, repr(pat))) - print - print "import lex" - print "lex.lex()" - print + print() + print("import lex") + print("lex.lex()") + print() # collect all the rules about one symbol... rules = {} @@ -229,33 +229,33 @@ def toPly(it): # find all the non-terminals; make sure the start symbol is 1st start = it['rules'][0][0] - nts = rules.keys() + nts = list(rules.keys()) del nts[nts.index(start)] nts.insert(0, start) for lhs in nts: rhss = rules[lhs] - print "def p_%s(t):" % lhs + print("def p_%s(t):" % lhs) for rhs in rhss: rhs = ' '.join([t2t.get(s, s) for s in rhs]) if lhs: - print ' """%s : %s' % (lhs, rhs) + print(' """%s : %s' % (lhs, rhs)) lhs = None else: - print ' | %s' % rhs - print ' """' - print " pass" - print + print(' | %s' % rhs) + print(' """') + print(" pass") + print() - print - print "import yacc" - print "yacc.yacc()" - print - print """ + print() + print("import yacc") + print("yacc.yacc()") + print() + print(""" if __name__ == '__main__': import sys yacc.parse(file(sys.argv[1]).read()) -""" +""") def tokid(s): diff --git a/grammar/lexedParser.py b/grammar/lexedParser.py index 57d28cf9..7bc4f6b5 100644 --- a/grammar/lexedParser.py +++ b/grammar/lexedParser.py @@ -34,7 +34,7 @@ __version__ = "$Id$" # SWAP http://www.w3.org/2000/10/swap -import sparql_tokens +from . import sparql_tokens try: from swap import webAccess, uripath, llyn, myStore, term, diag @@ -93,8 +93,8 @@ def toYacc(x, tokenRegexps): if isinstance(x, Literal): return "'" + str(x.value()) + "'" # @@@ Escaping if x in tokenRegexps: - return deColonise(`x`).upper() - return deColonise(`x`) + return deColonise(repr(x)).upper() + return deColonise(repr(x)) def yaccConvert(yacc, top, tokenRegexps): @@ -135,11 +135,11 @@ def yaccProduction(yacc, lhs, tokenRegexps): return rhs = g.the(pred=BNF.mustBeOneSequence, subj=lhs) if rhs == None: - progress( recordError("No definition of " + `lhs`)) - raise ValueError("No definition of %s in\n %s" %(`lhs`, `g`)) + progress( recordError("No definition of " + repr(lhs))) + raise ValueError("No definition of %s in\n %s" %(repr(lhs), repr(g))) options = rhs if chatty_flag: - progress ("\nProduction %s :: %s ie %s" %(`lhs`, `options` , `options.value()`)) + progress ("\nProduction %s :: %s ie %s" %(repr(lhs), repr(options) , repr(options.value()))) yacc.write("\n%s:" % toYacc(lhs, tokenRegexps)) branches = g.each(subj=lhs, pred=BNF.branch) @@ -149,7 +149,7 @@ def yaccProduction(yacc, lhs, tokenRegexps): yacc.write("\t|\t") first = 0 option = g.the(subj=branch, pred=BNF.sequence) - if chatty_flag: progress( "\toption: "+`option.value()`) + if chatty_flag: progress( "\toption: "+repr(option.value())) yacc.write("\t") if option.value() == [] and yacc: yacc.write(" /* empty */") for part in option: @@ -182,30 +182,30 @@ def doProduction(lhs): return rhs = g.the(pred=BNF.mustBeOneSequence, subj=lhs) if rhs == None: - progress (recordError("I can't find a definition of " + `lhs`)) + progress (recordError("I can't find a definition of " + repr(lhs))) return # raise RuntimeError("No definition of %s in\n %s" %(`lhs`, `g`)) options = rhs - if chatty_flag: progress ( "\nProduction %s :: %s ie %s" %(`lhs`, `options` , `options.value()`)) + if chatty_flag: progress ( "\nProduction %s :: %s ie %s" %(repr(lhs), repr(options) , repr(options.value()))) succ = g.each(subj=lhs, pred=BNF.canPrecede) if chatty_flag: progress("\tCan precede ", succ) branches = g.each(subj=lhs, pred=BNF.branch) for branch in branches: option = g.the(subj=branch, pred=BNF.sequence) - if chatty_flag: progress( "\toption: "+`option.value()`) + if chatty_flag: progress( "\toption: "+repr(option.value())) for part in option: if part not in already and part not in agenda: agenda.append(part) - y = `part` + y = repr(part) conditions = g.each(subj=branch, pred=BNF.condition) if conditions == []: progress( recordError(" NO SELECTOR for %s option %s ie %s" % - (`lhs`, `option`, `option.value()` ))) + (repr(lhs), repr(option), repr(option.value()) ))) if option.value == []: # Void case - the tricky one succ = g.each(subj=lhs, pred=BNF.canPrecede) for y in succ: - if chatty_flag: progress("\t\t\tCan precede ", `y`) + if chatty_flag: progress("\t\t\tCan precede ", repr(y)) if chatty_flag: progress("\t\tConditions: %s" %(conditions)) for str1 in conditions: if str1 in branchDict: @@ -217,8 +217,8 @@ def doProduction(lhs): for str1 in branchDict: for str2 in branchDict: - s1 = unicode(str1) - s2 = unicode(str2) + s1 = str(str1) + s2 = str(str2) # @@ check that selectors are distinct, not substrings if (s1.startswith(s2) or s2.startswith(s1)) and branchDict[str1] is not branchDict[str2]: progress("WARNING: for %s, %s indicates %s, but %s indicates %s" % ( @@ -228,8 +228,8 @@ def doProduction(lhs): ######################### Parser based on the RDF Context-free grammar -whiteSpace = re.compile(ur'[ \t]*((#[^\n]*)?\r?\n)?') -singleCharacterSelectors = u"\t\r\n !\"#$%&'()*.,+/;<=>?[\\]^`{|}~" +whiteSpace = re.compile(r'[ \t]*((#[^\n]*)?\r?\n)?') +singleCharacterSelectors = "\t\r\n !\"#$%&'()*.,+/;<=>?[\\]^`{|}~" notQNameChars = singleCharacterSelectors + "@" # Assume anything else valid qname :-/ notNameChars = notQNameChars + ":" # Assume anything else valid name :-/ @@ -249,7 +249,7 @@ def __init__(parser, sink, top, branchTable, tokenSet, keywords = None): parser.atMode = False else: parser.keywords = [ "a", "is", "of", "this" ] - print parser.keywords + print(parser.keywords) parser.verb = 1 # Verbosity parser.keywordMode = 0 # In a keyword statement, adding keywords @@ -286,9 +286,9 @@ def parseProduction(parser, lhs, tok, stream): rhs = lookupTable.get(name, None) # Predict branch from token if rhs == None: progress("""Found %s when expecting some form of %s, -\tsuch as %s\n\t%s""" % (tok(), lhs, lookupTable.keys(), parser.around(None, None))) +\tsuch as %s\n\t%s""" % (tok(), lhs, list(lookupTable.keys()), parser.around(None, None))) raise SyntaxError("""Found %s when expecting some form of %s, -\tsuch as %s\n\t%s""" % (tok(), lhs, lookupTable.keys(), parser.around(None, None))) +\tsuch as %s\n\t%s""" % (tok(), lhs, list(lookupTable.keys()), parser.around(None, None))) if parser.verb: progress( "%i %s means expand %s as %s" %(parser.lineNumber,tok(), lhs, rhs.value())) tree = [lhs] for term in rhs: @@ -296,10 +296,10 @@ def parseProduction(parser, lhs, tok, stream): if lit != name: # Not token if lit in parser.tokenSet: progress("Houston, we have a problem. %s is not equal to %s" % (lit, name)) - progress("recursing on %s, which is not %s. Token is %s" % (lit, name, `tok()`)) + progress("recursing on %s, which is not %s. Token is %s" % (lit, name, repr(tok()))) tree.append(parser.parseProduction(term, tok, stream)) else: - progress("We found %s, which matches %s" % (lit, `tok()`)) + progress("We found %s, which matches %s" % (lit, repr(tok()))) tree.append(tok()) tok(parser.token(stream)) # Next token if tok(): @@ -398,7 +398,7 @@ def main(): #if parser.verb: progress "Branch table:", branchTable if verbose: - progress( "Literal terminals: %s" % literalTerminals.keys()) + progress( "Literal terminals: %s" % list(literalTerminals.keys())) progress("Token regular expressions:") for r in tokenRegexps: progress( "\t%s matches %s" %(r, tokenRegexps[r].pattern) ) @@ -424,7 +424,7 @@ def main(): p.verb = 1 start = clock() #print lexer.token() - print p.parse(lexer.token) + print(p.parse(lexer.token)) taken = clock() - start + 1 # progress("Loaded %i chars in %fs, ie %f/s." % # (len(str), taken, len(str)/taken)) diff --git a/grammar/lexjsontest.py b/grammar/lexjsontest.py index 395a2d28..1115bf2e 100644 --- a/grammar/lexjsontest.py +++ b/grammar/lexjsontest.py @@ -17,9 +17,9 @@ def main(argv): s = Scanner(it['tokens'], ) s.feed(sys.stdin.read()) while 1: - t, txt = s.next() + t, txt = next(s) if not t: break - print t, txt + print(t, txt) class Scanner(object): def __init__(self, tokens): @@ -35,7 +35,7 @@ def __init__(self, tokens): def feed(self, d): self._in += d - def next(self): + def __next__(self): while 1: if self._idx >= len(self._in): return None, None @@ -55,7 +55,7 @@ def next(self): return besttok, beststr # else whitespace/comment... try again else: - raise SyntaxError, self._in[self._idx:self._idx+30] + raise SyntaxError(self._in[self._idx:self._idx+30]) def scanner(tokens, input): diff --git a/grammar/predictiveParser.py b/grammar/predictiveParser.py index a81d9c98..27f0e75f 100644 --- a/grammar/predictiveParser.py +++ b/grammar/predictiveParser.py @@ -61,10 +61,10 @@ # There is a problem that 16-bit-character builds of python can't deal with # our regexps -wide_build = (len(u"\U00012345") == 1) +wide_build = (len("\U00012345") == 1) def smartCompile(pattern, flags=0): if not wide_build: - pattern = pattern.replace(u"\U00010000-\U000effff", u"\ud800-\udb7f\udc00-\udfff") + pattern = pattern.replace("\U00010000-\U000effff", "\ud800-\udb7f\udc00-\udfff") return re.compile(pattern, flags) @@ -89,8 +89,8 @@ def toYacc(x, tokenRegexps): if isinstance(x, Literal): return "'" + str(x.value()) + "'" # @@@ Escaping if x in tokenRegexps: - return deColonise(`x`).upper() - return deColonise(`x`) + return deColonise(repr(x)).upper() + return deColonise(repr(x)) def yaccConvert(yacc, top, tokenRegexps): @@ -131,11 +131,11 @@ def yaccProduction(yacc, lhs, tokenRegexps): return rhs = g.the(pred=BNF.mustBeOneSequence, subj=lhs) if rhs == None: - progress( recordError("No definition of " + `lhs`)) - raise ValueError("No definition of %s in\n %s" %(`lhs`, `g`)) + progress( recordError("No definition of " + repr(lhs))) + raise ValueError("No definition of %s in\n %s" %(repr(lhs), repr(g))) options = rhs if chatty_flag: - progress ("\nProduction %s :: %s ie %s" %(`lhs`, `options` , `options.value()`)) + progress ("\nProduction %s :: %s ie %s" %(repr(lhs), repr(options) , repr(options.value()))) yacc.write("\n%s:" % toYacc(lhs, tokenRegexps)) branches = g.each(subj=lhs, pred=BNF.branch) @@ -145,7 +145,7 @@ def yaccProduction(yacc, lhs, tokenRegexps): yacc.write("\t|\t") first = 0 option = g.the(subj=branch, pred=BNF.sequence) - if chatty_flag: progress( "\toption: "+`option.value()`) + if chatty_flag: progress( "\toption: "+repr(option.value())) yacc.write("\t") if option.value() == [] and yacc: yacc.write(" /* empty */") for part in option: @@ -178,53 +178,53 @@ def doProduction(lhs): try: tokenRegexps[lhs] = smartCompile(rhs.value(), re.U) except: - print rhs.value().encode('utf-8') + print(rhs.value().encode('utf-8')) raise cc = g.each(subj=lhs, pred=BNF.canStartWith) if cc == []: progress (recordError( - "No record of what token %s can start with" % `lhs`)) + "No record of what token %s can start with" % repr(lhs))) if chatty_flag: progress("\tCan start with: %s" % cc) return if g.contains(subj=lhs, pred=RDF.type, obj=REGEX.Regex): - import regex + from . import regex rhs = regex.makeRegex(g, lhs) try: tokenRegexps[lhs] = smartCompile(rhs, re.U) except: - print rhs + print(rhs) raise cc = g.each(subj=lhs, pred=BNF.canStartWith) if cc == []: progress (recordError( - "No record of what token %s can start with" % `lhs`)) + "No record of what token %s can start with" % repr(lhs))) if chatty_flag: progress("\tCan start with: %s" % cc) return rhs = g.the(pred=BNF.mustBeOneSequence, subj=lhs) if rhs == None: - progress (recordError("No definition of " + `lhs`)) + progress (recordError("No definition of " + repr(lhs))) return # raise RuntimeError("No definition of %s in\n %s" %(`lhs`, `g`)) options = rhs - if chatty_flag: progress ( "\nProduction %s :: %s ie %s" %(`lhs`, `options` , `options.value()`)) + if chatty_flag: progress ( "\nProduction %s :: %s ie %s" %(repr(lhs), repr(options) , repr(options.value()))) succ = g.each(subj=lhs, pred=BNF.canPrecede) if chatty_flag: progress("\tCan precede ", succ) branches = g.each(subj=lhs, pred=BNF.branch) for branch in branches: option = g.the(subj=branch, pred=BNF.sequence) - if chatty_flag: progress( "\toption: "+`option.value()`) + if chatty_flag: progress( "\toption: "+repr(option.value())) for part in option: if part not in already and part not in agenda: agenda.append(part) - y = `part` + y = repr(part) conditions = g.each(subj=branch, pred=BNF.condition) if conditions == []: progress( recordError(" NO SELECTOR for %s option %s ie %s" % - (`lhs`, `option`, `option.value()` ))) + (repr(lhs), repr(option), repr(option.value()) ))) if option.value == []: # Void case - the tricky one succ = g.each(subj=lhs, pred=BNF.canPrecede) for y in succ: - if chatty_flag: progress("\t\t\tCan precede ", `y`) + if chatty_flag: progress("\t\t\tCan precede ", repr(y)) if chatty_flag: progress("\t\tConditions: %s" %(conditions)) for str1 in conditions: if str1 in branchDict: @@ -236,8 +236,8 @@ def doProduction(lhs): for str1 in branchDict: for str2 in branchDict: - s1 = unicode(str1) - s2 = unicode(str2) + s1 = str(str1) + s2 = str(str2) # @@ check that selectors are distinct, not substrings if (s1.startswith(s2) or s2.startswith(s1)) and branchDict[str1] is not branchDict[str2]: progress("WARNING: for %s, %s indicates %s, but %s indicates %s" % ( @@ -247,8 +247,8 @@ def doProduction(lhs): ######################### Parser based on the RDF Context-free grammar -whiteSpace = smartCompile(ur'[ \t]*((#[^\n]*)?\r?\n)?') -singleCharacterSelectors = u"\t\r\n !\"#$%&'()*.,+/;<=>?[\\]^`{|}~" +whiteSpace = smartCompile(r'[ \t]*((#[^\n]*)?\r?\n)?') +singleCharacterSelectors = "\t\r\n !\"#$%&'()*.,+/;<=>?[\\]^`{|}~" notQNameChars = singleCharacterSelectors + "@" # Assume anything else valid qname :-/ notNameChars = notQNameChars + ":" # Assume anything else valid name :-/ @@ -352,9 +352,9 @@ def parseProduction(parser, lhs, str, tok=None, here=0): rhs = lookupTable.get(tok, None) # Predict branch from token if rhs == None: progress("""Found %s when expecting some form of %s, -\tsuch as %s\n\t%s""" % (tok, lhs, lookupTable.keys(), parser.around(str, here))) +\tsuch as %s\n\t%s""" % (tok, lhs, list(lookupTable.keys()), parser.around(str, here))) raise SyntaxError("""Found %s when expecting some form of %s, -\tsuch as %s\n\t%s""" % (tok, lhs, lookupTable.keys(), parser.around(str, here))) +\tsuch as %s\n\t%s""" % (tok, lhs, list(lookupTable.keys()), parser.around(str, here))) if parser.verb: progress( "%i %s means expand %s as %s" %(parser.lineNumber,tok, lhs, rhs.value())) for term in rhs: if isinstance(term, Literal): # CFG Terminal @@ -364,7 +364,7 @@ def parseProduction(parser, lhs, str, tok=None, here=0): elif "@"+str[here:next-1] == lit: next = next-1 else: raise SyntaxError( "Found %s where %s expected\n\t %s" % - (`str[here:next]`, lit, parser.around(str, here))) + (repr(str[here:next]), lit, parser.around(str, here))) else: rexp = tokenRegexps.get(term, None) if rexp == None: # Not token @@ -469,7 +469,7 @@ def main(): #if parser.verb: progress "Branch table:", branchTable if verbose: - progress( "Literal terminals: %s" % literalTerminals.keys()) + progress( "Literal terminals: %s" % list(literalTerminals.keys())) progress("Token regular expressions:") for r in tokenRegexps: progress( "\t%s matches %s" %(r, tokenRegexps[r].pattern) ) diff --git a/grammar/regex.py b/grammar/regex.py index 5c9d22bb..6e043f9d 100644 --- a/grammar/regex.py +++ b/grammar/regex.py @@ -11,9 +11,9 @@ from swap.RDFSink import RDF_NS_URI rdf = myStore.symbol(RDF_NS_URI[:-1]) -knownClasses = {regex['Dot']: u'.', - regex['Start']: u'^', - regex['End']: u'$'} +knownClasses = {regex['Dot']: '.', + regex['Start']: '^', + regex['End']: '$'} alreadyDone = {} @@ -21,7 +21,7 @@ def processProduction(f, prod): if prod in alreadyDone: pass elif isinstance(prod, term.Literal): - alreadyDone[prod] = escape(unicode(prod)), regex['Sequence'] + alreadyDone[prod] = escape(str(prod)), regex['Sequence'] #print [x.asFormula().n3String() for x in f.statementsMatching(subj=prod)] else: for prop in knownProps: @@ -34,23 +34,23 @@ def processProduction(f, prod): try: alreadyDone[prod] = knownClasses[prod], regex['CharClass'] except: - print [x.asFormula().n3String() for x in f.statementsMatching(subj=prod)] + print([x.asFormula().n3String() for x in f.statementsMatching(subj=prod)]) raise return alreadyDone[prod] -knownModifications = {regex['Star'] : u'*', - regex['NonGreedyStar'] : u'*?', - regex['Plus'] : u'+', - regex['Optional'] : u'?', - regex['NonGreedyPlus'] : u'+?', - regex['NonGreedyOptional'] : u'??'} +knownModifications = {regex['Star'] : '*', + regex['NonGreedyStar'] : '*?', + regex['Plus'] : '+', + regex['Optional'] : '?', + regex['NonGreedyPlus'] : '+?', + regex['NonGreedyOptional'] : '??'} def simpleModification(f, k, s): if s[-1:] in ')]': return s + k else: - return u'(?:%s)%s' % (s, k) + return '(?:%s)%s' % (s, k) def modifies(f, subj, obj): @@ -71,13 +71,13 @@ def modifies(f, subj, obj): def group_literal(f, subj, obj): substring, cs = processProduction(f, obj) - return (u'[%s]' % substring), regex['ExplicitCharClass'] + return ('[%s]' % substring), regex['ExplicitCharClass'] def complement(f, subj, obj): substring, cs = processProduction(f, obj) if cs is not regex['ExplicitCharClass']: raise ValueError - return (u'[^%s]' % substring[1:-1]), regex['CharClass'] + return ('[^%s]' % substring[1:-1]), regex['CharClass'] def disjunction(f, subj, obj): k = [(x, processProduction(f, x)) for x in obj] @@ -87,7 +87,7 @@ def disjunction(f, subj, obj): valuesAndTypes = [processProduction(f, x) for x in obj] def processSingleChar(x,y): if es_len(x) == 1 and y is not regex['CharClass']: - return u'[' + x + u']' + return '[' + x + ']' return x bigClass = [] notClasses = [] @@ -97,7 +97,7 @@ def processSingleChar(x,y): else: notClasses.append(group((x,y), regex['Sequence'])) - bigClassString = u'[' + u''.join([processSingleChar(x,y)[1:-1] for x,y in bigClass]) + u']', regex['ExplicitCharClass'] + bigClassString = '[' + ''.join([processSingleChar(x,y)[1:-1] for x,y in bigClass]) + ']', regex['ExplicitCharClass'] if not notClasses: return bigClassString if len(bigClass) == 1: @@ -105,7 +105,7 @@ def processSingleChar(x,y): elif bigClass: notClasses.append(group(bigClassString, regex['Sequence'])) - return u'(?:' + '|'.join(notClasses) + ')', regex['Disjunction'] + return '(?:' + '|'.join(notClasses) + ')', regex['Disjunction'] def sequence(f, subj, obj): k = [(x, processProduction(f, x)) for x in obj] @@ -113,31 +113,32 @@ def sequence(f, subj, obj): if len(d) != 2: raise RuntimeError ((c,d)) values = [group(processProduction(f, x), regex['Disjunction']) for x in obj] - return u''.join(values), regex['Sequence'] + return ''.join(values), regex['Sequence'] -def group((val, t), condition): +def group(xxx_todo_changeme, condition): + (val, t) = xxx_todo_changeme if t is condition: - return u'(?:%s)' % val + return '(?:%s)' % val return val def lookAhead(f, subj, obj): substring, cs = processProduction(f, obj) - return u'(?=%s)' % substring, regex['Regex'] + return '(?=%s)' % substring, regex['Regex'] def negativeLookAhead(f, subj, obj): substring, cs = processProduction(f, obj) - return u'(?!%s)' % substring, regex['Regex'] + return '(?!%s)' % substring, regex['Regex'] def lookBehind(f, subj, obj): substring, cs = processProduction(f, obj) - return u'(?<=%s)' % substring, regex['Regex'] + return '(?<=%s)' % substring, regex['Regex'] def negativeLookBehind(f, subj, obj): substring, cs = processProduction(f, obj) - return u'(?': - return u'\\' + s + return '\\' + s return s.replace('\\','\\\\').replace('.','\\.').replace('?','\\?').replace('+','\\+').replace("[", "\\[").replace("]", "\\]").replace("(", "\\(").replace(")", "\\)") def es_len(s): @@ -173,11 +174,11 @@ def main(): baseRegex = sys.argv[2] f = myStore.load(inputFile) base = myStore.symbol(baseRegex) - g = u'(?P%s)' % makeRegex(f, base) - print `g` + g = '(?P%s)' % makeRegex(f, base) + print(repr(g)) import re c = re.compile(g) - print c.match('3.4E-4').groups() + print(c.match('3.4E-4').groups()) #print alreadyDone diff --git a/grammar/sparql-grammar.py b/grammar/sparql-grammar.py index 93c3876f..5292e333 100644 --- a/grammar/sparql-grammar.py +++ b/grammar/sparql-grammar.py @@ -27,7 +27,7 @@ def newOr(self): self.things = [] self.ors.append(self.things) def __repr__(self): - return '(%s): %s' % (`self.repeat`, `self.things`) + return '(%s): %s' % (repr(self.repeat), repr(self.things)) def __str__(self): if len(self.ors) == 1 and len(self.ors[0]) == 1 and self.repeat != ONCE: @@ -47,7 +47,7 @@ def __str__(self): retVal += '\n' + " "*level + " cfg:mustBeOneSequence ( () ( " level = level + 6 else: - raise RuntimeError('how did I get here? %s' & `self.repeat`) + raise RuntimeError('how did I get here? %s' & repr(self.repeat)) if many and self.repeat != ONCE: retVal += '\n ' + " "*level + '[' @@ -91,7 +91,7 @@ def __str__(self): web = False def makeGrammar(): - import urllib + import urllib.request, urllib.parse, urllib.error retVal = """#SPARQL in Notation3 # Context Free Grammar without tokenization # @@ -107,7 +107,7 @@ def makeGrammar(): """ if web: - File = urllib.urlopen('http://www.w3.org/2005/01/yacker/uploads/sparqlTest/bnf') + File = urllib.request.urlopen('http://www.w3.org/2005/01/yacker/uploads/sparqlTest/bnf') this = "" ws = re.compile(r'\S') for string in File: @@ -141,7 +141,7 @@ def makeGrammar(): i = this.find('%%') i2 = this.find('%%', i+5) whole = this[i:i2] - next_rule = re.compile(ur'\n(?=\w)') + next_rule = re.compile(r'\n(?=\w)') rules = next_rule.split(whole) rules = rules[1:] rules[0] = rules[0] + ' cfg:eof' @@ -153,7 +153,7 @@ def makeGrammar(): ## return rules = [(a[0], makeList(a[1].split(' '), a[0])) for a in rules] retVal += '. \n\n'.join([b[0] + ' ' + str(b[1]) for b in rules]) - print "[" + ",\n".join(["(" + `a` + ")" for a in rules]) + "]" + print("[" + ",\n".join(["(" + repr(a) + ")" for a in rules]) + "]") retVal += '. \n\n\n' # # Here comes the hard part. I gave up @@ -220,7 +220,7 @@ def makeList(matchString, ww): patterns.pop() if len(patterns) == 1: return patterns[0] - print patterns[1] + print(patterns[1]) return None class l(object): pass @@ -238,9 +238,9 @@ def f(): r_unilower = re.compile(r'(?<=\\u)([0-9a-f]{4})|(?<=\\U)([0-9a-f]{8})') r_hibyte = re.compile(r'[\x00-\x08\x0B-\x0C\x0E-\x1F\x7F-\xFF]') def quote(s): - if not isinstance(s, unicode): - s = unicode(s, 'utf-8') # @@ not required? - if not (u'\\'.encode('unicode-escape') == '\\\\'): + if not isinstance(s, str): + s = str(s, 'utf-8') # @@ not required? + if not ('\\'.encode('unicode-escape') == '\\\\'): s = s.replace('\\', r'\\') s = s.replace('"', r'\"') # s = s.replace(r'\\"', r'\"') @@ -257,5 +257,5 @@ def writeGrammar(): b.close() if __name__ == '__main__': - print 'Overwriting sparql.n3' + print('Overwriting sparql.n3') writeGrammar() diff --git a/grammar/sparql_tokens.py b/grammar/sparql_tokens.py index 95187653..300e93cb 100644 --- a/grammar/sparql_tokens.py +++ b/grammar/sparql_tokens.py @@ -13,87 +13,87 @@ class Tokens(object): #literal strings - t_IT_SELECT = u'SELECT' - t_IT_DISTINCT = u'DISTINCT' - t_IT_CONSTRUCT = u'CONSTRUCT' - t_IT_DESCRIBE = u'DESCRIBE' - t_IT_ASK = u'ASK' - t_IT_BASE = u'BASE' - t_IT_PREFIX = u'PREFIX' - t_FROM_NAMED = u'FROM(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))+NAMED' - t_IT_FROM = u'FROM' - t_IT_WHERE = u'WHERE' - t_IT_ORDER = u'ORDER' - t_IT_BY = u'BY' - t_IT_ASC = u'ASC' - t_IT_DESC = u'DESC' - t_IT_LIMIT = u'LIMIT' - t_IT_OFFSET = u'OFFSET' - t_IT_UNION = u'UNION' - t_IT_OPTIONAL = u'OPTIONAL' - t_IT_GRAPH = u'GRAPH' - t_IT_FILTER = u'FILTER' - t_IT_STR = u'STR' - t_IT_LANG = u'LANG' - t_IT_DATATYPE = u'DATATYPE' - t_IT_REGEX = u'REGEX' - t_IT_BOUND = u'BOUND' - t_IT_isURI = u'isURI' - t_IT_isBLANK = u'isBLANK' - t_IT_isLITERAL = u'isLITERAL' - t_IT_true = u'true' - t_IT_false = u'false' - - - t_QuotedIRIref = u'<[^> ]*>' - - - t_FLOATING_POINT = u'(?:[0-9]+\\.[0-9]*(?:[eE][\\+-]?[0-9]+)?)|(?:(?:\\.[0-9]+(?:[eE][\\+-]?[0-9]+)?)|(?:[0-9]+(?:[eE][\\+-]?[0-9]+)))' - t_DECIMAL = u'(?:[0-9]+\\.[0-9]*)|(?:\\.[0-9]+)' - t_INTEGER = u'[0-9]+' - t_STRING_LITERAL_LONG1 = u'"""(?:(?:[^"\\\\])|(?:(?:(?:\\\\[^\\n\\r]))|(?:(?:(?:"[^"]))|(?:(?:""[^"])))))*"""' - t_STRING_LITERAL_LONG2 = u'\'\'\'(?:(?:[^\'\\\\])|(?:(?:(?:\\\\[^\\n\\r]))|(?:(?:(?:\'[^\']))|(?:(?:\'\'[^\'])))))*\'\'\'' - t_STRING_LITERAL1 = u'\'(?:(?:[^\'\\\\\\n\\r])|(?:(?:\\\\[^\\n\\r])))*\'' - t_STRING_LITERAL2 = u'"(?:(?:[^"\\\\\\n\\r])|(?:(?:\\\\[^\\n\\r])))*"' - t_LANGTAG = u'@[a-zA-Z]+(?:-[a-zA-Z0-9]+)*' - t_BNODE_LABEL = u'_:(?:(?:(?:_)|(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE]))))))))))))))(?:(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE])))))))))))))|(?:(?:_)|(?:(?:-)|(?:(?:\\.)|(?:(?:[0-9])|(?:\u00B7))))))*)' - t_QNAME = u'(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE]))))))))))))(?:(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE])))))))))))))|(?:(?:_)|(?:(?:-)|(?:(?:\\.)|(?:(?:[0-9])|(?:\u00B7))))))*)?:(?:(?:(?:_)|(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE]))))))))))))))(?:(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE])))))))))))))|(?:(?:_)|(?:(?:-)|(?:(?:\\.)|(?:(?:[0-9])|(?:\u00B7))))))*)' - t_QNAME_NS = u'(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE]))))))))))))(?:(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE])))))))))))))|(?:(?:_)|(?:(?:-)|(?:(?:\\.)|(?:(?:[0-9])|(?:\u00B7))))))*)?:' - t_VAR2 = u'\\$(?:(?:(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE])))))))))))))|(?:(?:_)|(?:(?:[0-9])|(?:\u00B7))))*)' - t_VAR1 = u'\\?(?:(?:(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE])))))))))))))|(?:(?:_)|(?:(?:[0-9])|(?:\u00B7))))*)' - t_CloseSquare = u',?(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*;?(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*\\]' - t_CloseCurly = u',?(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*;?(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*\\.?(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*\\}' - t_OptDot = u'\\.(?=((?:(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*(?:(?:(?:(?:UNION)|(?:(?:OPTIONAL)|(?:(?:GRAPH)|(?:FILTER))))[^a-z])|(?:\\{)))))' - t_EmptyPattern = u'\\{(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*\\}' + t_IT_SELECT = 'SELECT' + t_IT_DISTINCT = 'DISTINCT' + t_IT_CONSTRUCT = 'CONSTRUCT' + t_IT_DESCRIBE = 'DESCRIBE' + t_IT_ASK = 'ASK' + t_IT_BASE = 'BASE' + t_IT_PREFIX = 'PREFIX' + t_FROM_NAMED = 'FROM(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))+NAMED' + t_IT_FROM = 'FROM' + t_IT_WHERE = 'WHERE' + t_IT_ORDER = 'ORDER' + t_IT_BY = 'BY' + t_IT_ASC = 'ASC' + t_IT_DESC = 'DESC' + t_IT_LIMIT = 'LIMIT' + t_IT_OFFSET = 'OFFSET' + t_IT_UNION = 'UNION' + t_IT_OPTIONAL = 'OPTIONAL' + t_IT_GRAPH = 'GRAPH' + t_IT_FILTER = 'FILTER' + t_IT_STR = 'STR' + t_IT_LANG = 'LANG' + t_IT_DATATYPE = 'DATATYPE' + t_IT_REGEX = 'REGEX' + t_IT_BOUND = 'BOUND' + t_IT_isURI = 'isURI' + t_IT_isBLANK = 'isBLANK' + t_IT_isLITERAL = 'isLITERAL' + t_IT_true = 'true' + t_IT_false = 'false' + + + t_QuotedIRIref = '<[^> ]*>' + + + t_FLOATING_POINT = '(?:[0-9]+\\.[0-9]*(?:[eE][\\+-]?[0-9]+)?)|(?:(?:\\.[0-9]+(?:[eE][\\+-]?[0-9]+)?)|(?:[0-9]+(?:[eE][\\+-]?[0-9]+)))' + t_DECIMAL = '(?:[0-9]+\\.[0-9]*)|(?:\\.[0-9]+)' + t_INTEGER = '[0-9]+' + t_STRING_LITERAL_LONG1 = '"""(?:(?:[^"\\\\])|(?:(?:(?:\\\\[^\\n\\r]))|(?:(?:(?:"[^"]))|(?:(?:""[^"])))))*"""' + t_STRING_LITERAL_LONG2 = '\'\'\'(?:(?:[^\'\\\\])|(?:(?:(?:\\\\[^\\n\\r]))|(?:(?:(?:\'[^\']))|(?:(?:\'\'[^\'])))))*\'\'\'' + t_STRING_LITERAL1 = '\'(?:(?:[^\'\\\\\\n\\r])|(?:(?:\\\\[^\\n\\r])))*\'' + t_STRING_LITERAL2 = '"(?:(?:[^"\\\\\\n\\r])|(?:(?:\\\\[^\\n\\r])))*"' + t_LANGTAG = '@[a-zA-Z]+(?:-[a-zA-Z0-9]+)*' + t_BNODE_LABEL = '_:(?:(?:(?:_)|(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE]))))))))))))))(?:(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE])))))))))))))|(?:(?:_)|(?:(?:-)|(?:(?:\\.)|(?:(?:[0-9])|(?:\u00B7))))))*)' + t_QNAME = '(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE]))))))))))))(?:(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE])))))))))))))|(?:(?:_)|(?:(?:-)|(?:(?:\\.)|(?:(?:[0-9])|(?:\u00B7))))))*)?:(?:(?:(?:_)|(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE]))))))))))))))(?:(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE])))))))))))))|(?:(?:_)|(?:(?:-)|(?:(?:\\.)|(?:(?:[0-9])|(?:\u00B7))))))*)' + t_QNAME_NS = '(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE]))))))))))))(?:(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE])))))))))))))|(?:(?:_)|(?:(?:-)|(?:(?:\\.)|(?:(?:[0-9])|(?:\u00B7))))))*)?:' + t_VAR2 = '\\$(?:(?:(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE])))))))))))))|(?:(?:_)|(?:(?:[0-9])|(?:\u00B7))))*)' + t_VAR1 = '\\?(?:(?:(?:(?:(?:[A-Z])|(?:(?:[a-z])|(?:(?:[\u00C0-\u00D6])|(?:(?:[\u00D8-\u00F6])|(?:(?:[\u00F8-\u02FF])|(?:(?:[\u0370-\u037D])|(?:(?:[\u037F-\u1FFF])|(?:(?:[\u200C-\u200D])|(?:(?:[\u2070-\u218F])|(?:(?:[\u2C00-\u2FEF])|(?:(?:[\u3001-\uD7FF])|(?:[\uF900-\uFFFE])))))))))))))|(?:(?:_)|(?:(?:[0-9])|(?:\u00B7))))*)' + t_CloseSquare = ',?(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*;?(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*\\]' + t_CloseCurly = ',?(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*;?(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*\\.?(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*\\}' + t_OptDot = '\\.(?=((?:(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*(?:(?:(?:(?:UNION)|(?:(?:OPTIONAL)|(?:(?:GRAPH)|(?:FILTER))))[^a-z])|(?:\\{)))))' + t_EmptyPattern = '\\{(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))*\\}' #ignored - t_PASSED_TOKENS = u'(?:(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))+)|(?:(?:#[^\\n]*\\n)|(?:/\\*(?:(?:/[^\\*])|(?:[^/\\*]))*\\*/))' + t_PASSED_TOKENS = '(?:(?:(?:\u0008)|(?:(?:\\n)|(?:(?:\\r)|(?:(?: )|(?:(?:\u00A0)|(?:(?:[\u2000-\u200B])|(?:(?:\u202F)|(?:(?:\u205F)|(?:\u3000)))))))))+)|(?:(?:#[^\\n]*\\n)|(?:/\\*(?:(?:/[^\\*])|(?:[^/\\*]))*\\*/))' #two characters - t_GT_OR = u'\\|\\|' - t_GT_AND = u'&&' - t_GT_NEQUAL = u'!=' - t_GT_LE = u'<=' - t_GT_GE = u'>=' - t_GT_DTYPE = u'\\^\\^' + t_GT_OR = '\\|\\|' + t_GT_AND = '&&' + t_GT_NEQUAL = '!=' + t_GT_LE = '<=' + t_GT_GE = '>=' + t_GT_DTYPE = '\\^\\^' #single character - t_GT_TIMES = u'\\*' - t_GT_LPAREN = u'\\(' - t_GT_RPAREN = u'\\)' - t_GT_SEMI = u';' - t_GT_COMMA = u',' - t_IT_a = u'a' - t_GT_EQUAL = u'=' - t_GT_LT = u'<' - t_GT_GT = u'>' - t_GT_PLUS = u'\\+' - t_GT_MINUS = u'-' - t_GT_DIVIDE = u'/' - t_GT_NOT = u'!' - t_Dot = u'\\.' - t_OpenCurly = u'\\{' - t_OpenSquare = u'\\[' + t_GT_TIMES = '\\*' + t_GT_LPAREN = '\\(' + t_GT_RPAREN = '\\)' + t_GT_SEMI = ';' + t_GT_COMMA = ',' + t_IT_a = 'a' + t_GT_EQUAL = '=' + t_GT_LT = '<' + t_GT_GT = '>' + t_GT_PLUS = '\\+' + t_GT_MINUS = '-' + t_GT_DIVIDE = '/' + t_GT_NOT = '!' + t_Dot = '\\.' + t_OpenCurly = '\\{' + t_OpenSquare = '\\[' tokens = ('IT_SELECT', @@ -205,14 +205,14 @@ def tokenize(self): yield (name, token, self.line) endpos = (m.end() - waslen) if not endpos: - raise ValueError, "Got zero-length token" + raise ValueError("Got zero-length token") self.chunk = self.chunk[endpos:] else: break def token(self): if not self.tokenStream: return None try: - return self.tokenStream.next() + return next(self.tokenStream) except: return None @@ -249,7 +249,7 @@ def runLexer(): while 1: tok = lexer.token() if not tok: break # No more input - print tok + print(tok) if __name__ == '__main__': runLexer() diff --git a/grammar/tokenizer.py b/grammar/tokenizer.py index 6cf322bd..1ffaa4a2 100644 --- a/grammar/tokenizer.py +++ b/grammar/tokenizer.py @@ -46,19 +46,19 @@ def tokenize(string, singleChars, whiteSpace, regexps): yield string[i:j] i = j else: - raise ValueError('I didn\'t match! %s' % `string[i:i+10]`) + raise ValueError('I didn\'t match! %s' % repr(string[i:i+10])) def deleteme(x): - print `x` + print(repr(x)) return re.compile(x, re.S+re.U) def testharness(): import sys - import dekeywordizer - import deprefixizer + from . import dekeywordizer + from . import deprefixizer explicitURI = "<[^>]*>" comment = '#[^\\n]*' numericLiteral = """[-+]?[0-9]+(\\.[0-9]+)?(e[-+]?[0-9]+)?""" @@ -87,4 +87,4 @@ def testharness(): re.compile(explicitURI, re.S+re.U)) if __name__ == '__main__': - print [a for a in testharness()] + print([a for a in testharness()]) diff --git a/grammar/yosiParser.py b/grammar/yosiParser.py index 8b954156..28c59f6b 100644 --- a/grammar/yosiParser.py +++ b/grammar/yosiParser.py @@ -5,9 +5,9 @@ import sys import re -import dekeywordizer -import deprefixizer -import tokenizer +from . import dekeywordizer +from . import deprefixizer +from . import tokenizer #import triple_maker import toXML import notation3 @@ -175,7 +175,7 @@ def _parse(self, x): elif numericLiteral.match(token): m = numericLiteral.match(token) if '.' not in token: - tm.addLiteral(long(token)) + tm.addLiteral(int(token)) elif m is None: raise ValueError("How exactly did I get here?") elif m.groups(3) is not None: @@ -183,7 +183,7 @@ def _parse(self, x): elif m.groups(2) is not None: tm.addLiteral(float(token)) else: - tm.addLiteral(long(token)) + tm.addLiteral(int(token)) elif string.match(token): if token[0:3] == '"""': a = 3 diff --git a/hotswap.py b/hotswap.py index 1c51bff8..ec563514 100644 --- a/hotswap.py +++ b/hotswap.py @@ -73,7 +73,7 @@ import sys import inspect import re -from cStringIO import StringIO +from io import StringIO from types import ClassType class Error(RuntimeError): @@ -111,7 +111,7 @@ def Collection(location): """ if re.match(r"^[-a-zA-Z0-9_.]+$", location): return Module(location) - raise Error, "unknown location syntax" + raise Error("unknown location syntax") class A: pass class B: pass @@ -142,8 +142,8 @@ def __init__(self, location): def open(self): try: mod = __import__(self.location) - except ImportError, e: - raise Error, ("Python can't import %s.\nsys.path is currently: %s\nImportError was: %s" % + except ImportError as e: + raise Error("Python can't import %s.\nsys.path is currently: %s\nImportError was: %s" % ( self.location, sys.path, str(e))) components = self.location.split('.') for comp in components[1:]: @@ -274,14 +274,14 @@ def get(self, service, interfaces=None, properties={}, # construct a new instance, copied from any existing one if current is None: - new = apply(matchingClass, initargs, initkwargs) + new = matchingClass(*initargs, **initkwargs) else: try: kw = initkwargs kw["copyFrom"] = current - new = apply(matchingClass, initargs, kw) + new = matchingClass(*initargs, **kw) except TypeError: - raise CantTransfer, "Can't switch service '%s' to class %s\nperhaps it lacks a copyFrom constructor parameter" % (service, str(matchingClass)) + raise CantTransfer("Can't switch service '%s' to class %s\nperhaps it lacks a copyFrom constructor parameter" % (service, str(matchingClass))) # save and return it self.services[service] = new @@ -340,7 +340,7 @@ def get(*arg1, **arg2): the current version is too clever about using kwargs. """ - return apply(defaultPluginManager.get, arg1, arg2) + return defaultPluginManager.get(*arg1, **arg2) ################################################################ diff --git a/html.py b/html.py index 53c33d8a..65132b68 100644 --- a/html.py +++ b/html.py @@ -145,14 +145,14 @@ __version__ = "$Revision$" # $Id$ -import cStringIO +import io import re class Streamer: """An base class for things which implement writeTo instead of __str__.""" def __str__(self): - s = cStringIO.StringIO() + s = io.StringIO() self.writeTo(s) return s.getvalue() @@ -246,7 +246,7 @@ def writeTo(self, s, prefix=""): s.write(prefix) s.write("<") s.write(self.tag) - keys = self.attrs.keys() + keys = list(self.attrs.keys()) keys.sort() for key in keys: if self.attrs[key] is not None: @@ -313,7 +313,7 @@ def __init__(self, content=None, inline=0): self.inline = inline def __str__(self): - s = cStringIO.StringIO() + s = io.StringIO() self.writeTo(s) return s.getvalue() @@ -354,7 +354,7 @@ class Flow: def __getattr__(self, name): def func(*content, **kw): kw["inline"] = 0 - return apply(Element, [name]+list(content), kw) + return Element(*[name]+list(content), **kw) return func class Inline: @@ -367,13 +367,13 @@ class Inline: def __getattr__(self, name): def func(*content, **kw): kw["inline"] = 1 - return apply(Element, [name]+list(content), kw) + return Element(*[name]+list(content), **kw) return func """Foo""" def createStdElements(): - import html + from . import html flow = Flow() inline = Inline() @@ -407,8 +407,8 @@ def createStdElements(): if __name__ =='__main__': import doctest, html - print "Performing doctest..." - print doctest.testmod(html) + print("Performing doctest...") + print(doctest.testmod(html)) # $Log$ # Revision 1.6 2003-09-04 05:40:13 sandro diff --git a/importList.py b/importList.py index c326fa71..07dc37f0 100644 --- a/importList.py +++ b/importList.py @@ -10,7 +10,7 @@ import os.path import os import sys -import uripath +from . import uripath @@ -20,12 +20,12 @@ def main(argv): mf = ModuleFinder(path) for f in argv: mf.run_script(f) - paths = sorted(list(set([os.path.abspath(x.__file__) for x in mf.modules.values() if x.__file__]))) + paths = sorted(list(set([os.path.abspath(x.__file__) for x in list(mf.modules.values()) if x.__file__]))) cwd = os.getcwd() paths = [x for x in paths if x.startswith(cwd)] m = len(cwd) + 1 paths = argv + [x[m:] for x in paths] - print ' '.join(paths) + print(' '.join(paths)) diff --git a/isXML.py b/isXML.py index 533e05e7..2960bbdd 100644 --- a/isXML.py +++ b/isXML.py @@ -7,7 +7,7 @@ import string from unicodedata import category -from set_importer import Set +from .set_importer import Set LETTER_CATEGORIES = Set(["Ll", "Lu", "Lo", "Lt", "Nl"]) NCNAME_CATEGORIES = LETTER_CATEGORIES.union(Set(["Mc", "Me", "Mn", "Lm", "Nd"])) @@ -25,7 +25,7 @@ def isXMLChar10(character, char_class): usual use is isXMLChar(character, isXML.NCNameChar) """ num = ord(character) - character = unicode(character) + character = str(character) if char_class == Letter: return isXMLChar10(character, BaseChar) or \ isXMLChar10(character, Ideographic) diff --git a/isodate.py b/isodate.py index e91889f3..034e7a33 100644 --- a/isodate.py +++ b/isodate.py @@ -80,10 +80,10 @@ def parse(s): try: a = r.groupdict('0') except: - raise ValueError, 'invalid date string format:'+s + raise ValueError('invalid date string format:'+s) y = int(a['year']) if y < 1970: - raise ValueError, 'Sorry, date must be in Unix era (1970 or after):'+s + raise ValueError('Sorry, date must be in Unix era (1970 or after):'+s) d = calendar.timegm(( int(a['year']), int(a['month']) or 1, int(a['day']) or 1, @@ -102,7 +102,7 @@ def parse(s): def fullString(i): """ given seconds since the epoch, return a full dateTime string in Z timezone. """ - assert type(i) in [IntType, FloatType, LongType], "Wrong type: "+ `type(i)` +`i` + assert type(i) in [IntType, FloatType, LongType], "Wrong type: "+ repr(type(i)) +repr(i) year, month, day, hour, minute, second, wday, jday, dst = time.gmtime(i) return str(year) + '-%2.2d-%2.2dT%2.2d:%2.2d:%2.2dZ' % (month, day, hour, minute, second) diff --git a/kifForm.py b/kifForm.py index 8155f93c..647a6da0 100644 --- a/kifForm.py +++ b/kifForm.py @@ -19,7 +19,7 @@ from string import * import re -from yappsrt import * +from .yappsrt import * class KIFParserScanner(Scanner): def __init__(self, str): @@ -300,5 +300,5 @@ def parse(rule, text): f = open(argv[2],'r') else: f = stdin - print parse(argv[1], f.read()) - else: print 'Args: []' + print(parse(argv[1], f.read())) + else: print('Args: []') diff --git a/llyn.py b/llyn.py index 77ea7189..315c5ed1 100755 --- a/llyn.py +++ b/llyn.py @@ -41,63 +41,63 @@ # emacsbug="""emacs got confused by long string above""" -from __future__ import generators + # see http://www.amk.ca/python/2.2/index.html#SECTION000500000000000000000 -from set_importer import Set, ImmutableSet +from .set_importer import Set, ImmutableSet import types import string import xml.dom.minidom import re -import StringIO +import io import sys import time, xml from warnings import warn -import urllib # for log:content +import urllib.request, urllib.parse, urllib.error # for log:content -import uripath -from uripath import canonical +from . import uripath +from .uripath import canonical -from sax2rdf import XMLtoDOM +from .sax2rdf import XMLtoDOM import xml.dom.minidom -from why import smushedFormula, Premise, newTopLevelFormula, isTopLevel +from .why import smushedFormula, Premise, newTopLevelFormula, isTopLevel -import notation3 # N3 parsers and generators, and RDF generator -from webAccess import webget +from . import notation3 # N3 parsers and generators, and RDF generator +from .webAccess import webget -import diag # problems importing the tracking flag, +from . import diag # problems importing the tracking flag, # and chatty_flag must be explicit it seems: use diag.tracking -from diag import progress, verbosity, tracking -from term import BuiltIn, LightBuiltIn, RDFBuiltIn, HeavyBuiltIn, Function, \ +from .diag import progress, verbosity, tracking +from .term import BuiltIn, LightBuiltIn, RDFBuiltIn, HeavyBuiltIn, Function, \ MultipleFunction, ReverseFunction, MultipleReverseFunction, \ Literal, XMLLiteral, Symbol, Fragment, FragmentNil, Term, LabelledNode, \ CompoundTerm, List, EmptyList, NonEmptyList, AnonymousNode, N3Set, \ UnknownType -from formula import Formula, StoredStatement -import reify +from .formula import Formula, StoredStatement +from . import reify from weakref import WeakValueDictionary -from query import think, applyRules, testIncludes -import webAccess -from webAccess import DocumentAccessError -from local_decimal import Decimal +from .query import think, applyRules, testIncludes +from . import webAccess +from .webAccess import DocumentAccessError +from .local_decimal import Decimal -from RDFSink import Logic_NS, RDFSink, forSomeSym, forAllSym -from RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 -from RDFSink import N3_nil, N3_first, N3_rest, OWL_NS, N3_Empty, N3_List, \ +from .RDFSink import Logic_NS, RDFSink, forSomeSym, forAllSym +from .RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 +from .RDFSink import N3_nil, N3_first, N3_rest, OWL_NS, N3_Empty, N3_List, \ N3_li, List_NS -from RDFSink import RDF_NS_URI +from .RDFSink import RDF_NS_URI -from RDFSink import FORMULA, LITERAL, LITERAL_DT, LITERAL_LANG, ANONYMOUS, SYMBOL +from .RDFSink import FORMULA, LITERAL, LITERAL_DT, LITERAL_LANG, ANONYMOUS, SYMBOL -from pretty import Serializer +from .pretty import Serializer LITERAL_URI_prefix = "data:application/rdf+n3-literal;" Delta_NS = "http://www.w3.org/2004/delta#" @@ -106,9 +106,9 @@ # Magic resources we know about -from RDFSink import RDF_type_URI, DAML_sameAs_URI +from .RDFSink import RDF_type_URI, DAML_sameAs_URI -from why import Because, BecauseBuiltIn, BecauseOfRule, \ +from .why import Because, BecauseBuiltIn, BecauseOfRule, \ BecauseOfExperience, becauseSubexpression, BecauseMerge ,report STRING_NS_URI = "http://www.w3.org/2000/10/swap/string#" @@ -149,7 +149,7 @@ def __getItem__(pred): # Use . or [] ? def arg_hash(arg): if isinstance(arg, dict): g = [] - for k, v in arg.items(): + for k, v in list(arg.items()): g.append((arg_hash(k), arg_hash(v))) return hash(tuple(g)) if isinstance(arg, (tuple, list)): @@ -342,7 +342,7 @@ def add(self, subj, pred, obj, why=None): why may be a reason for use when a proof will be required. """ if self.canonical != None: - raise RuntimeError("Attempt to add statement to closed formula "+`self`) + raise RuntimeError("Attempt to add statement to closed formula "+repr(self)) store = self.store if not isinstance(subj, Term): subj = store.intern(subj) @@ -358,19 +358,19 @@ def add(self, subj, pred, obj, why=None): obj = obj.substituteEquals(self._redirections, newBindings) if diag.chatty_flag > 90: - progress(u"Add statement (size before %i, %i statements) to %s:\n {%s %s %s}" % ( - self.store.size, len(self.statements),`self`, `subj`, `pred`, `obj`) ) + progress("Add statement (size before %i, %i statements) to %s:\n {%s %s %s}" % ( + self.store.size, len(self.statements),repr(self), repr(subj), repr(pred), repr(obj)) ) if self.statementsMatching(pred, subj, obj): if diag.chatty_flag > 97: progress("Add duplicate SUPPRESSED %s: {%s %s %s}" % ( self, subj, pred, obj) ) return 0 # Return no change in size of store - assert not isinstance(pred, Formula) or pred.canonical is pred, "pred Should be closed"+`pred` + assert not isinstance(pred, Formula) or pred.canonical is pred, "pred Should be closed"+repr(pred) assert (not isinstance(subj, Formula) or subj is self - or subj.canonical is subj), "subj Should be closed or self"+`subj` - assert not isinstance(obj, Formula) or obj.canonical is obj, "obj Should be closed"+`obj`+`obj.canonical` + or subj.canonical is subj), "subj Should be closed or self"+repr(subj) + assert not isinstance(obj, Formula) or obj.canonical is obj, "obj Should be closed"+repr(obj)+repr(obj.canonical) store.size = store.size+1 # rather nominal but should be monotonic if False and isTopLevel(self): @@ -429,7 +429,7 @@ def add(self, subj, pred, obj, why=None): or Term.compareAnyTerm(obj, subj) < 0): var, val = subj, obj else: var, val = obj, subj newBindings[var] = val - if diag.chatty_flag > 90: progress("Equality: %s = %s" % (`var`, `val`)) + if diag.chatty_flag > 90: progress("Equality: %s = %s" % (repr(var), repr(val))) self.substituteEqualsInPlace(newBindings) return 1 @@ -450,7 +450,7 @@ def add(self, subj, pred, obj, why=None): if diag.tracking: if (why is None): raise RuntimeError( - "Tracking reasons but no reason given for"+`s`) + "Tracking reasons but no reason given for"+repr(s)) report(s, why) # Build 8 indexes. @@ -518,7 +518,7 @@ def removeStatement(self, s): This is really a low-level method, used within add() and for cleaning up the store to save space in purge() etc. """ - assert self.canonical is None, "Cannot remove statement from canonnical"+`self` + assert self.canonical is None, "Cannot remove statement from canonnical"+repr(self) self.store.size = self.store.size-1 if diag.chatty_flag > 97: progress("removing %s" % (s)) context, pred, subj, obj = s.quad @@ -536,7 +536,7 @@ def removeStatement(self, s): def newCanonicalize(F): ## Horrible name! if self._hashval is not None: return - from term import Existential, Universal + from .term import Existential, Universal statements = [] def convert(n): if isinstance(n, Universal): @@ -564,11 +564,11 @@ def canonicalize(F, cannon=False): store = F.store if F.canonical != None: if diag.chatty_flag > 70: - progress("End formula -- @@ already canonical:"+`F`) + progress("End formula -- @@ already canonical:"+repr(F)) return F.canonical if F.stayOpen: if diag.chatty_flag > 70: - progress("Canonicalizion ignored: @@ Knowledge base mode:"+`F`) + progress("Canonicalizion ignored: @@ Knowledge base mode:"+repr(F)) return F F.store._equivalentFormulae.add(F) @@ -606,7 +606,7 @@ def canonicalize(F, cannon=False): gl = G.statements gkey = len(gl), len(G.universals()), len(G.existentials()) if gkey != l: raise RuntimeError("@@Key of %s is %s instead of %s" - %(G, `gkey`, `l`)) + %(G, repr(gkey), repr(l))) gl.sort() for se, oe, in ((fe, G.existentials()), @@ -653,7 +653,7 @@ def canonicalize(F, cannon=False): F.canonical = F F.reallyCanonical = True if diag.chatty_flag > 70: - progress("End formula, a fresh one:"+`F`) + progress("End formula, a fresh one:"+repr(F)) ## for k in possibles: ## print 'one choice is' ## print k.n3String() @@ -726,7 +726,7 @@ def outputStrings(self, channel=None, relation=None): pairs.sort(comparePair) for key, str in pairs: if not hasattr(str, "string"): - print `str` + print(repr(str)) channel.write(str.string.encode('utf-8')) @@ -736,18 +736,18 @@ def debugString(self, already=[]): This formula is dumped, using ids for nested formula. Then, each nested formula mentioned is dumped.""" red = "" - if self._redirections != {}: red = " redirections:" + `self._redirections` - str = `self`+ red + unicode(id(self)) + " is {" + if self._redirections != {}: red = " redirections:" + repr(self._redirections) + str = repr(self)+ red + str(id(self)) + " is {" for vv, ss in ((self.universals().copy(), "@forAll"),(self.existentials().copy(), "@forSome")): if vv != Set(): - str = str + " " + ss + " " + `vv.pop()` + str = str + " " + ss + " " + repr(vv.pop()) for v in vv: - str = str + ", " + `v` + str = str + ", " + repr(v) str = str + "." todo = [] for s in self.statements: subj, pred, obj = s.spo() - str = str + "\n%28s %20s %20s ." % (`subj`, `pred`, `obj`) + str = str + "\n%28s %20s %20s ." % (repr(subj), repr(pred), repr(obj)) for p in PRED, SUBJ, OBJ: if (isinstance(s[p], CompoundTerm) and s[p] not in already and s[p] not in todo and s[p] is not self): @@ -767,10 +767,10 @@ def _noteNewList(self, bnode, list, newBindings): This function is extraordinarily slow, .08 seconds per call on reify/reify3.n3. It can hit the python recursion limit with a long list!""" - if diag.chatty_flag > 80: progress("New list was %s, now %s = %s"%(`bnode`, `list`, `list.value()`)) + if diag.chatty_flag > 80: progress("New list was %s, now %s = %s"%(repr(bnode), repr(list), repr(list.value()))) if isinstance(bnode, List): return ##@@@@@ why is this necessary? weid. newBindings[bnode] = list - if diag.chatty_flag > 80: progress("...New list newBindings %s"%(`newBindings`)) + if diag.chatty_flag > 80: progress("...New list newBindings %s"%(repr(newBindings))) self._existentialVariables.discard(bnode) possibles = self.statementsMatching(pred=self.store.rest, obj=bnode) # What has this as rest? for s in possibles[:]: @@ -794,7 +794,7 @@ def _noteNewList(self, bnode, list, newBindings): def _noteNewSet(self, bnode, set, newBindings): newBindings[bnode] = set - if diag.chatty_flag > 80: progress("...New set newBindings %s"%(`newBindings`)) + if diag.chatty_flag > 80: progress("...New set newBindings %s"%(repr(newBindings))) self._existentialVariables.discard(bnode) def substituteEqualsInPlace(self, redirections, why=None): @@ -827,7 +827,7 @@ def unifySecondary(self, other, env1, env2, vars, if self.canonical and other.canonical and self.store._equivalentFormulae.connected(self, other): yield (env1, env2) else: - from query import n3Equivalent, testIncludes + from .query import n3Equivalent, testIncludes freeVars = self.freeVariables() ## We can't use these retVal = n3Equivalent(self, other, env1, env2, vars, universals, existentials, @@ -916,7 +916,7 @@ def evaluateObject(self, subj_py): if dt is self.store.symbol("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral"): try: dom = XMLtoDOM(lex) - except SyntaxError, e: + except SyntaxError as e: raise UnknownType # really malformed literal return self.store.newXMLLiteral(dom) else: @@ -948,7 +948,7 @@ def evalObj(self, subj, queue, bindings, proof, query): elif isinstance(subj, AnonymousNode): y = store.Blank else: y = store.Other # None? store.Other? if diag.chatty_flag > 91: - progress("%s rawType %s." %(`subj`, y)) + progress("%s rawType %s." %(repr(subj), y)) return y @@ -1016,7 +1016,7 @@ def evalObj(self, subj, queue, bindings, proof, query): F = store.any((store._experience, store.semantics, doc, None)) if F != None: if diag.chatty_flag > 10: - progress("Already read and parsed "+`doc`+" to "+ `F`) + progress("Already read and parsed "+repr(doc)+" to "+ repr(F)) return F if diag.chatty_flag > 10: progress("Reading and parsing " + doc.uriref()) @@ -1040,7 +1040,7 @@ def evalObj(self, subj, queue, bindings, proof, query): else: doc = subj F = store.any((store._experience, store.semanticsWithImportsClosure, doc, None)) if F != None: - if diag.chatty_flag > 10: progress("Already read and parsed "+`doc`+" to "+ `F`) + if diag.chatty_flag > 10: progress("Already read and parsed "+repr(doc)+" to "+ repr(F)) return F if diag.chatty_flag > 10: progress("Reading and parsing with closure " + doc.uriref()) @@ -1057,7 +1057,7 @@ def evalObj(self, subj, queue, bindings, proof, query): store.storeQuad((store._experience, store.semanticsWithImportsClosure, doc, F)) return F -import httplib +import http.client class BI_semanticsOrError(BI_semantics): """ Either get and parse to semantics or return an error message on any error """ def evalObj(self, subj, queue, bindings, proof, query): @@ -1065,15 +1065,15 @@ def evalObj(self, subj, queue, bindings, proof, query): store = subj.store x = store.any((store._experience, store.semanticsOrError, subj, None)) if x != None: - if diag.chatty_flag > 10: progress(`store._experience`+`store.semanticsOrError`+": Already found error for "+`subj`+" was: "+ `x`) + if diag.chatty_flag > 10: progress(repr(store._experience)+repr(store.semanticsOrError)+": Already found error for "+repr(subj)+" was: "+ repr(x)) return x try: return BI_semantics.evalObj(self, subj, queue, bindings, proof, query) except (IOError, SyntaxError, DocumentAccessError, - xml.sax._exceptions.SAXParseException, httplib.BadStatusLine): + xml.sax._exceptions.SAXParseException, http.client.BadStatusLine): message = sys.exc_info()[1].__str__() result = store.intern((LITERAL, message)) - if diag.chatty_flag > 0: progress(`store.semanticsOrError`+": Error trying to access <" + `subj` + ">: "+ message) + if diag.chatty_flag > 0: progress(repr(store.semanticsOrError)+": Error trying to access <" + repr(subj) + ">: "+ message) store.storeQuad((store._experience, store.semanticsOrError, subj, @@ -1098,10 +1098,10 @@ def loadToStore(term, types): store = term.store #hmm... separate store from term? C = store.any((store._experience, store.content, doc, None)) if C != None: - if diag.chatty_flag > 10: progress("already read " + `doc`) + if diag.chatty_flag > 10: progress("already read " + repr(doc)) return C - if diag.chatty_flag > 10: progress("Reading " + `doc`) + if diag.chatty_flag > 10: progress("Reading " + repr(doc)) inputURI = doc.uriref() netStream = webget(inputURI, types) @@ -1158,7 +1158,7 @@ def evalObj(self, subj, queue, bindings, proof, query): assert subj.canonical != None F = self.store.any((store._experience, store.cufi, subj, None)) # Cached value? if F != None: - if diag.chatty_flag > 10: progress("Bultin: " + `subj`+ " cached log:conclusion " + `F`) + if diag.chatty_flag > 10: progress("Bultin: " + repr(subj)+ " cached log:conclusion " + repr(F)) return F F = self.store.newFormula() @@ -1169,7 +1169,7 @@ def evalObj(self, subj, queue, bindings, proof, query): # F.collector = reason # proof.append(reason) else: reason = None - if diag.chatty_flag > 10: progress("Bultin: " + `subj`+ " log:conclusion " + `F`) + if diag.chatty_flag > 10: progress("Bultin: " + repr(subj)+ " log:conclusion " + repr(F)) self.store.copyFormula(subj, F, why=reason) # leave open think(F) F = F.close() @@ -1198,7 +1198,7 @@ def evalObj(self, subj, queue, bindings, proof, query): if len(list) != 2: raise ValueError('I need a list of TWO formulae') if diag.chatty_flag > 30: - progress("=== begin filter of:" + `list`) + progress("=== begin filter of:" + repr(list)) # list = [bindings.get(a,a) for a in list] base, filter = list F = self.store.newFormula() @@ -1208,7 +1208,7 @@ def evalObj(self, subj, queue, bindings, proof, query): applyRules(base, filter, F) F = F.close() if diag.chatty_flag > 30: - progress("=== end filter of:" + `list` + "we got: " + `F`) + progress("=== end filter of:" + repr(list) + "we got: " + repr(F)) return F class BI_vars(LightBuiltIn, Function): @@ -1231,7 +1231,7 @@ def eval(self, subj, obj, queue, bindings, proof, query): if not isinstance(subj, Formula): return None s = str(obj) if diag.chatty_flag > 180: - progress(`subj.universals()`) + progress(repr(subj.universals())) return obj in subj.universals() for v in subj.universals(): if v.uriref() == s: return 1 @@ -1280,7 +1280,7 @@ class BI_conjunction(LightBuiltIn, Function): # Light? well, I suppose so. def evalObj(self, subj, queue, bindings, proof, query): subj_py = subj.value() if diag.chatty_flag > 50: - progress("Conjunction input:"+`subj_py`) + progress("Conjunction input:"+repr(subj_py)) for x in subj_py: progress(" conjunction input formula %s has %i statements" % (x, x.size())) @@ -1293,10 +1293,10 @@ def evalObj(self, subj, queue, bindings, proof, query): if not isinstance(x, Formula): return None # Can't if (x.canonical == None): # Not closed! !! F.canonical != None - progress("Conjunction input NOT CLOSED:"+`x`) #@@@ + progress("Conjunction input NOT CLOSED:"+repr(x)) #@@@ self.store.copyFormula(x, F, why=reason) # No, that is if diag.chatty_flag > 74: - progress(" Formula %s now has %i" % (`F`,len(F.statements))) + progress(" Formula %s now has %i" % (repr(F),len(F.statements))) return F.canonicalize() class BI_n3String(LightBuiltIn, Function): # Light? well, I suppose so. @@ -1309,7 +1309,7 @@ class BI_n3String(LightBuiltIn, Function): # Light? well, I suppose so. A canonical form is possisble but not simple.""" def evalObj(self, subj, queue, bindings, proof, query): if diag.chatty_flag > 50: - progress("Generating N3 string for:"+`subj`) + progress("Generating N3 string for:"+repr(subj)) if isinstance(subj, Formula): return self.store.intern((LITERAL, subj.n3String())) @@ -1393,7 +1393,7 @@ def __init__(self, genPrefix=None, metaURI=None, argv=None, crypto=0): self.clear() self.argv = argv # List of command line arguments for N3 scripts - run = uripath.join(uripath.base(), ".RUN/") + `time.time()` # Reserrved URI @@ + run = uripath.join(uripath.base(), ".RUN/") + repr(time.time()) # Reserrved URI @@ if metaURI != None: meta = metaURI else: meta = run + "meta#formula" @@ -1491,7 +1491,7 @@ def __init__(self, genPrefix=None, metaURI=None, argv=None, crypto=0): # List stuff - beware of namespace changes! :-( - from cwm_list import BI_first, BI_rest + from .cwm_list import BI_first, BI_rest rdf = self.symbol(List_NS[:-1]) self.first = rdf.internFrag("first", BI_first) self.rest = rdf.internFrag("rest", BI_rest) @@ -1500,17 +1500,17 @@ def __init__(self, genPrefix=None, metaURI=None, argv=None, crypto=0): self.li = self.intern(N3_li) self.List = self.intern(N3_List) - import cwm_string # String builtins - import cwm_os # OS builtins - import cwm_time # time and date builtins - import cwm_math # Mathematics - import cwm_trigo # Trignometry - import cwm_times # time and date builtins - import cwm_maths # Mathematics, perl/string style - import cwm_list # List handling operations - import cwm_set # Set operations - import cwm_sparql # builtins for sparql - import cwm_xml # XML Document Object Model operations + from . import cwm_string # String builtins + from . import cwm_os # OS builtins + from . import cwm_time # time and date builtins + from . import cwm_math # Mathematics + from . import cwm_trigo # Trignometry + from . import cwm_times # time and date builtins + from . import cwm_maths # Mathematics, perl/string style + from . import cwm_list # List handling operations + from . import cwm_set # Set operations + from . import cwm_sparql # builtins for sparql + from . import cwm_xml # XML Document Object Model operations cwm_string.register(self) cwm_math.register(self) cwm_trigo.register(self) @@ -1522,7 +1522,7 @@ def __init__(self, genPrefix=None, metaURI=None, argv=None, crypto=0): cwm_set.register(self) cwm_sparql.register(self) cwm_xml.register(self) - import cwm_crypto # Cryptography -- register this for the hash functions even if no pub key + from . import cwm_crypto # Cryptography -- register this for the hash functions even if no pub key if crypto and cwm_crypto.USE_PKC == 0: raise RuntimeError("Public Key Crypto unavailable. Try installing pycrypto, and make sure it is in you PYTHONPATH") cwm_crypto.register(self) # would like to anyway to catch bug if used but not available @@ -1612,7 +1612,7 @@ def load(store, uri=None, openFormula=None, asIfFrom=None, contentType=None, rem store._experience.add(source, store.semantics, F, why=BecauseOfExperience("load document")) return F - if diag.chatty_flag > 40: progress("NOT caching semantics for",uri, "remember=%s, openFormula=%s" % (`remember`,`openFormula`)) + if diag.chatty_flag > 40: progress("NOT caching semantics for",uri, "remember=%s, openFormula=%s" % (repr(remember),repr(openFormula))) return webAccess.load(store, uri, openFormula, asIfFrom, contentType, flags, \ referer=referer, why=why) @@ -1686,9 +1686,9 @@ def symbol(self, str, why=None): def _fromPython(self, x, queue=None): """Takem a python string, seq etc and represent as a llyn object""" - if isinstance(x, tuple(types.StringTypes)): + if isinstance(x, tuple((str,))): return self.newLiteral(x) - elif type(x) is types.LongType or type(x) is types.IntType: + elif type(x) is int or type(x) is int: return self.newLiteral(str(x), self.integer) elif isinstance(x, Decimal): return self.newLiteral(str(x), self.decimal) @@ -1696,10 +1696,10 @@ def _fromPython(self, x, queue=None): return self.newLiteral(x and 'true' or 'false', self.boolean) elif isinstance(x, xml.dom.minidom.Document): return self.newXMLLiteral(x) - elif type(x) is types.FloatType: - if `x`.lower() == "nan": # We can get these form eg 2.math:asin + elif type(x) is float: + if repr(x).lower() == "nan": # We can get these form eg 2.math:asin return None - return self.newLiteral(`x`, self.float) + return self.newLiteral(repr(x), self.float) elif isinstance(x, Set) or isinstance(x, ImmutableSet): return self.newSet([self._fromPython(y) for y in x]) elif isinstance(x, Term): @@ -1718,23 +1718,23 @@ def intern(self, what, dt=None, lang=None, why=None, ): """ if isinstance(what, Term): return what # Already interned. @@Could mask bugs - if type(what) is not types.TupleType: - if isinstance(what, tuple(types.StringTypes)): + if type(what) is not tuple: + if isinstance(what, tuple((str,))): return self.newLiteral(what, dt, lang) # progress("llyn1450 @@@ interning non-string", `what`) - if type(what) is types.LongType: + if type(what) is int: return self.newLiteral(str(what), self.integer) - if type(what) is types.IntType: - return self.newLiteral(`what`, self.integer) - if type(what) is types.FloatType: + if type(what) is int: + return self.newLiteral(repr(what), self.integer) + if type(what) is float: return self.newLiteral(repr(what), self.float) if isinstance(what,Decimal): return self.newLiteral(str(what), self.decimal) if isinstance(what, bool): return self.newLiteral(what and 'true' or 'false', self.boolean) - if type(what) is types.ListType: #types.SequenceType: + if type(what) is list: #types.SequenceType: return self.newList(what) - raise RuntimeError("Eh? can't intern "+`what`+" of type: "+`what.__class__`) + raise RuntimeError("Eh? can't intern "+repr(what)+" of type: "+repr(what.__class__)) typ, urirefString = what @@ -1772,7 +1772,7 @@ def intern(self, what, dt=None, lang=None, why=None, ): elif typ == FORMULA: raise RuntimeError("obsolete") result = r.internFrag(urirefString[hash+1:], IndexedFormula) - else: raise RuntimeError, "did not expect other type:"+`typ` + else: raise RuntimeError("did not expect other type:"+repr(typ)) return result def newList(self, value, context=None): @@ -1788,10 +1788,10 @@ def newList(self, value, context=None): def reopen(self, F): if F.canonical is None: if diag.chatty_flag > 50: - progress("reopen formula -- @@ already open: "+`F`) + progress("reopen formula -- @@ already open: "+repr(F)) return F # was open if diag.chatty_flag > 00: - progress("warning - reopen formula:"+`F`) + progress("warning - reopen formula:"+repr(F)) key = len(F.statements), len(F.universals()), len(F.existentials()) try: self._formulaeOfLength[key].remove(F) # Formulae of same length @@ -1843,7 +1843,7 @@ def storeQuad(self, q, why=None): """ context, pred, subj, obj = q - assert isinstance(context, Formula), "Should be a Formula: "+`context` + assert isinstance(context, Formula), "Should be a Formula: "+repr(context) return context.add(subj=subj, pred=pred, obj=obj, why=why) @@ -1933,7 +1933,7 @@ def purgeSymbol(self, context, subj): context.removeStatement(t) # SLOW total = total + 1 if diag.chatty_flag > 30: - progress("Purged %i statements with %s" % (total,`subj`)) + progress("Purged %i statements with %s" % (total,repr(subj))) return total @@ -1963,11 +1963,11 @@ class URISyntaxError(ValueError): def isString(x): # in 2.2, evidently we can test for isinstance(types.StringTypes) # --- but on some releases, we need to say tuple(types.StringTypes) - return type(x) is type('') or type(x) is type(u'') + return type(x) is type('') or type(x) is type('') ##################### Register this module -from myStore import setStoreClass +from .myStore import setStoreClass setStoreClass(RDFStore) #ends diff --git a/local_decimal.py b/local_decimal.py index dbfbabc0..00dd5788 100644 --- a/local_decimal.py +++ b/local_decimal.py @@ -7,7 +7,9 @@ $Id$ """ -from types import IntType, FloatType, LongType, StringTypes +# timbl +# from types import IntType, FloatType, LongType, StringTypes + from math import log10 class Decimal: @@ -18,7 +20,7 @@ class Decimal: """ _limit = 16 - + def normalize(self): """convert this Decimal into some sort of canonical form @@ -33,9 +35,9 @@ def normalize(self): ## while self.magnitude > 2 * self.__class__._limit: ## self.value = self.value / 10 ## self.magnitude = self.magnitude - 1 - - - + + + def __init__(self, other=0): """How to get a new Decimal @@ -46,7 +48,7 @@ def __init__(self, other=0): self.magnitude = other.magnitude return elif isinstance(other, IntType): - self.value = long(other) + self.value = int(other) self.magnitude = 0 self.normalize() return @@ -62,15 +64,15 @@ def __init__(self, other=0): self.normalize() return elif isinstance(other,FloatType): - other = `other` + other = repr(other) try: other[0] except TypeError: - other = `other` + other = repr(other) other = other + 'q' i = 0 - value = long(0) - magnitude = long(0) + value = int(0) + magnitude = int(0) sign = 1 newsign = 1 base = 10 @@ -104,7 +106,7 @@ def __init__(self, other=0): self.value = value*sign self.normalize() - + def __abs__(self): """x.__abs__() <==> abs(x) """ @@ -188,7 +190,7 @@ def __floordiv__(self, other): a.value = self.value // other.value a.normalize() return a - + def __hash__(self): """x.__hash__() <==> hash(x) """ @@ -210,7 +212,7 @@ def __long__(self): while power > 0: value = value // 10 power = power - 1 - return long(value * 10**(-power)) + return int(value * 10**(-power)) def __mod__(self, other): """x.__mod__(y) <==> x%y """ @@ -279,7 +281,7 @@ def __pow__(self, other, mod=0): ## c.normalize() a = self.__class__(pow(float(self),float(other),mod)) return a - + def __radd__(self, other): """x.__radd__(y) <==> y+x """ @@ -396,7 +398,7 @@ def __truediv__(self, other): def n_root(base, power): """Find the nth root of a Decimal """ - print 'trying to compute ', base, ' ** 1/ ', power + print('trying to compute ', base, ' ** 1/ ', power) accuracy = Decimal(1) n = 10 #Decimal._limit while n > 0: @@ -409,7 +411,7 @@ def n_root(base, power): oldguess = guess counter = counter + 1 if counter == 100: - print guess + print(guess) counter = 0 h = 1 - base * (guess ** power) guess = guess + guess * h / power @@ -418,5 +420,5 @@ def n_root(base, power): break # print guess answer = Decimal(1) / Decimal(guess) - print answer + print(answer) return answer diff --git a/mixin.py b/mixin.py index 01c8493c..186c9f26 100644 --- a/mixin.py +++ b/mixin.py @@ -58,12 +58,12 @@ def __new__(metacls, name, bases, dict): ## operations[func][base] = dict[func] if func in base.__dict__: raise ValueError('''I can't let you override an existing method. -Use real inheritance: %s.%s''' % (`base`, func)) +Use real inheritance: %s.%s''' % (repr(base), func)) setattr(base, func, dict[func]) return object.__new__(metacls) -class Mixin: - __metaclass__ = mixinClass +class Mixin(metaclass=mixinClass): + pass diff --git a/myStore.py b/myStore.py index 461ffae7..04ea3d2d 100755 --- a/myStore.py +++ b/myStore.py @@ -74,7 +74,7 @@ """ -import uripath +from . import uripath # Allow a strore provdier to register: @@ -97,7 +97,7 @@ def _checkStore(s=None): if s != None: return s if store != None: return store if storeClass == None: - import llyn # default + from . import llyn # default assert storeClass!= None, "Some storage module must register with myStore.py before you can use it" store = storeClass() # Make new one return store @@ -202,7 +202,7 @@ def __getattr__(self, lname): I hope this is only called *after* the ones defines above have been checked """ if lname.startswith("__"): # python internal - raise AttributeError, lname + raise AttributeError(lname) return _checkStore(self.store).symbol(self._name+lname) @@ -215,7 +215,7 @@ def sym(self, lname): def _test(): - import llyn + from . import llyn store = llyn.RDFStore() setStore(store) diff --git a/my_profiler.py b/my_profiler.py index 031c6eee..b2230f9a 100644 --- a/my_profiler.py +++ b/my_profiler.py @@ -6,7 +6,7 @@ """ import hotshot, hotshot.stats -from cwm import doCommand +from .cwm import doCommand import os, sys diff --git a/n3absyn.py b/n3absyn.py index 7b7450a3..7779670b 100644 --- a/n3absyn.py +++ b/n3absyn.py @@ -137,9 +137,9 @@ def json_formula(fmla, vars={}): i = 0 - while vn in varnames.values(): + while vn in list(varnames.values()): i += 1 - vn = vn + `i` + vn = vn + repr(i) varnames[v] = vn parts = [] # conjuncts @@ -203,7 +203,7 @@ def json_term(t, varmap): elif isinstance(t, formula.Formula): return {'op': 'n3-quote', 'parts': [json_formula(t, varmap)]} else: - raise RuntimeError, "huh? + %s %s" % (t, t.__class__) + raise RuntimeError("huh? + %s %s" % (t, t.__class__)) def lisp_form(f): """generate an s-expression from a formula JSON structure. @@ -217,9 +217,9 @@ def lisp_form(f): yield "%f " % f # string - elif type(f) in (type(''), type(u'')): + elif type(f) in (type(''), type('')): if "\\" in f or '"' in f: - raise RuntimeError, 'commonlisp string quoting TODO: %s' % f + raise RuntimeError('commonlisp string quoting TODO: %s' % f) # @@ hmm... non-ascii chars? yield '"%s" ' % f @@ -245,8 +245,7 @@ def lisp_form(f): # URI, i.e. a 0-ary function symbol if ':' in head: if '|' in head: - raise RuntimeError, \ - "quoting | in symbols not yet implemented" + raise RuntimeError("quoting | in symbols not yet implemented") yield '(URI::|%s|' % head rest = f.get('parts', []) assert(len(rest) == 0) @@ -279,7 +278,7 @@ def lisp_form(f): rest = [f['f']] else: - raise RuntimeError, 'unimplemented list head: %s' % head + raise RuntimeError('unimplemented list head: %s' % head) for expr in rest: for s in lisp_form(expr): @@ -287,7 +286,7 @@ def lisp_form(f): yield ')\n' else: - raise RuntimeError, 'unimplemented syntactic type: %s %s' % (f, type(f)) + raise RuntimeError('unimplemented syntactic type: %s %s' % (f, type(f))) def ikl_sentence(f, subscripts): @@ -334,7 +333,7 @@ def ikl_sentence(f, subscripts): rest = [f['f']] else: - raise RuntimeError, 'unimplemented IKL sentence head: %s' % head + raise RuntimeError('unimplemented IKL sentence head: %s' % head) for expr in rest: for s in ikl_sentence(expr, subscripts): @@ -356,9 +355,9 @@ def ikl_term(f, subscripts): yield "(xsd:double %f)" % f #@@long uri form # string - elif type(f) in (type(''), type(u'')): + elif type(f) in (type(''), type('')): if "\\" in f or '"' in f: - raise RuntimeError, 'string quoting TODO: %s' % f + raise RuntimeError('string quoting TODO: %s' % f) if subscripts: yield "('%s' " % f # string sub = subscripts[0] @@ -394,8 +393,7 @@ def ikl_term(f, subscripts): # URI, i.e. a 0-ary function symbol if ':' in head: if '"' in head: - raise RuntimeError, \ - "quoting \" in IKL names not yet implemented" + raise RuntimeError("quoting \" in IKL names not yet implemented") if subscripts: for s in ikl_term(head, subscripts): yield s @@ -425,7 +423,7 @@ def ikl_term(f, subscripts): return else: - raise RuntimeError, 'unimplemented IKL term head: %s' % head + raise RuntimeError('unimplemented IKL term head: %s' % head) for expr in rest: for s in ikl_term(expr): @@ -433,7 +431,7 @@ def ikl_term(f, subscripts): yield ')\n' else: - raise RuntimeError, 'unimplemented syntactic type: %s %s' % (f, type(f)) + raise RuntimeError('unimplemented syntactic type: %s %s' % (f, type(f))) from xml.sax.saxutils import escape @@ -454,7 +452,7 @@ def xml_form(f): yield '%f\n' % (DT.double, f) # string - elif type(f) in (type(''), type(u'')): + elif type(f) in (type(''), type('')): yield '' yield escape(f) yield '\n' @@ -491,7 +489,7 @@ def xml_form(f): yield "" elif head == 'n3-quote': - raise RuntimeError, 'n3-quote not yet implemented' + raise RuntimeError('n3-quote not yet implemented') # Atomic formula elif head == 'holds': @@ -532,10 +530,10 @@ def xml_form(f): yield "\n" % tagname else: - raise RuntimeError, 'unimplemented list head: %s' % head + raise RuntimeError('unimplemented list head: %s' % head) else: - raise RuntimeError, 'unimplemented syntactic type: %s %s' % (f, type(f)) + raise RuntimeError('unimplemented syntactic type: %s %s' % (f, type(f))) def mathml_top(): @@ -626,14 +624,14 @@ def mathml_fmla(f): yield '%f\n' % f # string - elif type(f) in (type(''), type(u'')): + elif type(f) in (type(''), type('')): yield '' yield escape(f) yield '\n' else: - raise RuntimeError, 'unimplemented syntactic type: %s %s' % (f, type(f)) + raise RuntimeError('unimplemented syntactic type: %s %s' % (f, type(f))) class Namespace(object): def __init__(self, nsname): diff --git a/n3p_tm.py b/n3p_tm.py index cbabeb31..888f17a5 100644 --- a/n3p_tm.py +++ b/n3p_tm.py @@ -7,9 +7,9 @@ from n3p import n3p import os -from uripath import join +from .uripath import join -class absolutizer(unicode): +class absolutizer(str): def __getitem__(self, other): return join(self, other) @@ -226,7 +226,7 @@ def unEscape(string): else: real_str = string[1:-1] triple = False - ret = u'' + ret = '' n = 0 while n < len(real_str): ch = real_str[n] @@ -243,12 +243,12 @@ def unEscape(string): elif a == 'u': m = real_str[n+2:n+6] assert len(m) == 4 - ret += unichr(int(m, 16)) + ret += chr(int(m, 16)) n += 5 elif a == 'U': m = real_str[n+2:n+10] assert len(m) == 8 - ret += unichr(int(m, 16)) + ret += chr(int(m, 16)) n += 9 else: raise ValueError('Bad Escape') diff --git a/n3spark.py b/n3spark.py index 193bc8e7..8e1d00bb 100644 --- a/n3spark.py +++ b/n3spark.py @@ -18,7 +18,7 @@ # cf # Compiling Little Languages in Python # at the 7th International Python Conference. -import spark +from . import spark # http://www.w3.org/DesignIssues/Notation3 @@ -101,11 +101,11 @@ def typestring(self, token): return token.type def error(self, token): - print "@@parse error at line", token.lineno, ":", token.type, token.attr + print("@@parse error at line", token.lineno, ":", token.type, token.attr) raise SystemExit def resolve(self, list): - print "@@resolving ambiguity among:", list + print("@@resolving ambiguity among:", list) return list[0] def terminal(self, token): @@ -153,8 +153,8 @@ def __cmp__(self, o): return cmp(self.type, o) def dump(self, level=0): - print level * " ", - print self.type + print(level * " ", end=' ') + print(self.type) for k in self._kids: k.dump(level+1) @@ -221,7 +221,7 @@ def tokenize(self, input): return self.rv def error(self, s): - print "syntax error: bad token at line", self._ln + print("syntax error: bad token at line", self._ln) class Scanner(Scanner0): @@ -276,9 +276,9 @@ def test(): import sys f = open(sys.argv[1]) tokens = scan(f) - print "TOKENS:", tokens + print("TOKENS:", tokens) t = parse(tokens) - print "AST:" + print("AST:") t.dump() diff --git a/notation3.py b/notation3.py index 3fe3873f..1f5c43f6 100755 --- a/notation3.py +++ b/notation3.py @@ -34,24 +34,24 @@ import types, sys import string import codecs # python 2-ism; for writing utf-8 in RDF/xml output -import urllib +import urllib.request, urllib.parse, urllib.error import re from warnings import warn -from sax2rdf import XMLtoDOM # Incestuous.. would be nice to separate N3 and XML +from .sax2rdf import XMLtoDOM # Incestuous.. would be nice to separate N3 and XML # SWAP http://www.w3.org/2000/10/swap -from diag import verbosity, setVerbosity, progress -from uripath import refTo, join -import uripath -import RDFSink -from RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 -from RDFSink import LITERAL, XMLLITERAL, LITERAL_DT, LITERAL_LANG, ANONYMOUS, SYMBOL -from RDFSink import Logic_NS -import diag -from xmlC14n import Canonicalize - -from why import BecauseOfData, becauseSubexpression +from .diag import verbosity, setVerbosity, progress +from .uripath import refTo, join +from . import uripath +from . import RDFSink +from .RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 +from .RDFSink import LITERAL, XMLLITERAL, LITERAL_DT, LITERAL_LANG, ANONYMOUS, SYMBOL +from .RDFSink import Logic_NS +from . import diag +from .xmlC14n import Canonicalize + +from .why import BecauseOfData, becauseSubexpression N3_forSome_URI = RDFSink.forSomeSym N3_forAll_URI = RDFSink.forAllSym @@ -59,9 +59,9 @@ # Magic resources we know about -from RDFSink import RDF_type_URI, RDF_NS_URI, DAML_sameAs_URI, parsesTo_URI -from RDFSink import RDF_spec, List_NS, uniqueURI -from local_decimal import Decimal +from .RDFSink import RDF_type_URI, RDF_NS_URI, DAML_sameAs_URI, parsesTo_URI +from .RDFSink import RDF_spec, List_NS, uniqueURI +from .local_decimal import Decimal ADDED_HASH = "#" # Stop where we use this in case we want to remove it! # This is the hash on namespace URIs @@ -69,7 +69,7 @@ RDF_type = ( SYMBOL , RDF_type_URI ) DAML_sameAs = ( SYMBOL, DAML_sameAs_URI ) -from RDFSink import N3_first, N3_rest, N3_nil, N3_li, N3_List, N3_Empty +from .RDFSink import N3_first, N3_rest, N3_nil, N3_li, N3_List, N3_Empty LOG_implies_URI = "http://www.w3.org/2000/10/swap/log#implies" @@ -343,7 +343,7 @@ def directive(self, str, i): def bind(self, qn, uri): assert isinstance(uri, - types.StringType), "Any unicode must be %x-encoded already" + bytes), "Any unicode must be %x-encoded already" if qn == "": self._store.setDefaultNamespace(uri) else: @@ -798,7 +798,7 @@ def uri_ref2(self, str, i, res): if pfx == "_": # Magic prefix 2001/05/30, can be overridden res.append(self.anonymousNode(ln)) return j - print('@@@@ pfx ' + pfx) + print(('@@@@ pfx ' + pfx)) raise BadSyntax(self._thisDoc, self.lines, str, i, "Prefix \"%s:\" not bound" % (pfx)) symb = self._store.newSymbol(ns + ln) @@ -990,73 +990,75 @@ def nodeOrLiteral(self, str, i, res): ch = str[i] if ch in "-+0987654321": - m = datetime_syntax.match(str, i); - if m != None: - j = m.end(); - # print "date time "+str[i:j] - if 'T' in str[i:j]: - res.append(self._store.newLiteral(str[i:j], - self._store.newSymbol(DATETIME_DATATYPE))) - return j - else: - res.append(self._store.newLiteral(str[i:j], - self._store.newSymbol(DATE_DATATYPE))) - return j - - m = number_syntax.match(str, i) - if m != None: - j = m.end() - if m.group('exponent') != None: # includes decimal exponent - res.append(float(str[i:j])) + m = datetime_syntax.match(str, i); + if m != None: + j = m.end(); + # print "date time "+str[i:j] + if 'T' in str[i:j]: + res.append(self._store.newLiteral(str[i:j], + self._store.newSymbol(DATETIME_DATATYPE))) + return j + else: + res.append(self._store.newLiteral(str[i:j], + self._store.newSymbol(DATE_DATATYPE))) + return j + + m = number_syntax.match(str, i) + if m != None: + j = m.end() + if m.group('exponent') != None: # includes decimal exponent + res.append(float(str[i:j])) # res.append(self._store.newLiteral(str[i:j], # self._store.newSymbol(FLOAT_DATATYPE))) - elif m.group('decimal') != None: - res.append(Decimal(str[i:j])) - else: - res.append(long(str[i:j])) + elif m.group('decimal') != None: + res.append(Decimal(str[i:j])) + else: + res.append(int(str[i:j])) # res.append(self._store.newLiteral(str[i:j], # self._store.newSymbol(INTEGER_DATATYPE))) - return j - raise BadSyntax(self._thisDoc, self.lines, str, i, - "Bad number or datetime syntax") - + return j + raise BadSyntax(self._thisDoc, self.lines, str, i, + "Bad number or datetime syntax") - if str[i]=='"': - if str[i:i+3] == '"""': delim = '"""' - else: delim = '"' - i = i + len(delim) - dt = None - j, s = self.strconst(str, i, delim) - lang = None - if str[j:j+1] == "@": # Language? - m = langcode.match(str, j+1) - if m == None: - raise BadSyntax(self._thisDoc, startline, str, i, - "Bad language code syntax on string literal, after @") - i = m.end() - lang = str[j+1:i] - j = i - if str[j:j+2] == "^^": - res2 = [] - j = self.uri_ref2(str, j+2, res2) # Read datatype URI - dt = res2[0] - if dt.uriref() == "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral": - try: - dom = XMLtoDOM('' - + s - + '').firstChild - except: - raise ValueError('s="%s"' % s) - res.append(self._store.newXMLLiteral(dom)) - return j - res.append(self._store.newLiteral(s, dt, lang)) - return j + if str[i]=='"': + if str[i:i+3] == '"""': + delim = '"""' else: - return -1 + delim = '"' + i = i + len(delim) + + dt = None + j, s = self.strconst(str, i, delim) + lang = None + if str[j:j+1] == "@": # Language? + m = langcode.match(str, j+1) + if m == None: + raise BadSyntax(self._thisDoc, startline, str, i, + "Bad language code syntax on string literal, after @") + i = m.end() + lang = str[j+1:i] + j = i + if str[j:j+2] == "^^": + res2 = [] + j = self.uri_ref2(str, j+2, res2) # Read datatype URI + dt = res2[0] + if dt.uriref() == "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral": + try: + dom = XMLtoDOM('' + + s + + '').firstChild + except: + raise ValueError('s="%s"' % s) + res.append(self._store.newXMLLiteral(dom)) + return j + res.append(self._store.newLiteral(s, dt, lang)) + return j + else: + return -1 def uriOf(self, sym): - if isinstance(sym, types.TupleType): + if isinstance(sym, tuple): return sym[1] # old system for --pipe return sym.uriref() # cwm api @@ -1068,7 +1070,7 @@ def strconst(self, str, i, delim): j = i - ustr = u"" # Empty unicode string + ustr = "" # Empty unicode string startline = self.lines # Remember where for error messages while j shothand for log:implies i Use identifiers from store - don't regen on output l List syntax suppression. Don't use (..) @@ -1271,7 +1273,7 @@ class ToN3(RDFSink.RDFSink): r Relative URI suppression. Always use absolute URIs. s Subject must be explicit for every statement. Don't use ";" shorthand. t "=" and "()" special syntax should be suppresed. -u Use \u for unicode escaping in URIs instead of utf-8 %XX +u Use \\u for unicode escaping in URIs instead of utf-8 %XX v Use "this log:forAll" for @forAll, and "this log:forAll" for "@forSome". / If namespace has no # in it, assume it ends at the last slash if outputting. @@ -1351,9 +1353,9 @@ def bind(self, prefixString, uri): self._endStatement() self.prefixes[uri] = prefixString if 'r' in self._flags: - self._write(u"@prefix %s: <%s> ."%(prefixString, uri)) + self._write("@prefix %s: <%s> ."%(prefixString, uri)) else: - self._write(u"@prefix %s: <%s> ."%(prefixString, refTo(self.base, uri))) + self._write("@prefix %s: <%s> ."%(prefixString, refTo(self.base, uri))) self._newline() def setDefaultNamespace(self, uri): @@ -1365,38 +1367,38 @@ def setDefaultNamespace(self, uri): x = refTo(self.base, uri) else: x = uri - self._write(u" @prefix : <%s> ." % x ) + self._write(" @prefix : <%s> ." % x ) self._newline() def startDoc(self): if not self._quiet: # Suppress stuff which will confuse test diffs - self._write(u"\n# Notation3 generation by\n") - idstr = u"$Id$" + self._write("\n# Notation3 generation by\n") + idstr = "$Id$" # CVS CHANGES THE ABOVE LINE - self._write(u"# " + idstr[5:-2] + u"\n\n") + self._write("# " + idstr[5:-2] + "\n\n") # Strip "$" in case the N3 file is checked in to CVS - if self.base: self._write(u"# Base was: " + self.base + u"\n") - self._write(u" " * self.indent) + if self.base: self._write("# Base was: " + self.base + "\n") + self._write(" " * self.indent) self._subj = None # self._nextId = 0 def endDoc(self, rootFormulaPair=None): self._endStatement() - self._write(u"\n") + self._write("\n") if self.stayOpen: return # fo concatenation - if not self._quiet: self._write(u"#ENDS\n") + if not self._quiet: self._write("#ENDS\n") return # No formula returned - this is not a store def makeComment(self, str): for line in string.split(str, "\n"): - self._write(u"#" + line + "\n") # Newline order??@@ - self._write(u" " * self.indent + " ") + self._write("#" + line + "\n") # Newline order??@@ + self._write(" " * self.indent + " ") def _newline(self, extra=0): - self._write(u"\n"+ u" " * (self.indent+extra)) + self._write("\n"+ " " * (self.indent+extra)) def makeStatement(self, triple, why=None, aIsPossible=1): # triple = tuple([a.asPair() for a in triple2]) @@ -1416,7 +1418,7 @@ def makeStatement(self, triple, why=None, aIsPossible=1): if self._anodeName.get(str2, None) != None: j = 1 while 1: - str3 = str2 + `j` + str3 = str2 + repr(j) if self._anodeName.get(str3, None) == None: break j = j +1 str2 = str3 @@ -1437,14 +1439,14 @@ def makeStatement(self, triple, why=None, aIsPossible=1): def startAnonymous(self, triple): self._makeSubjPred(triple[CONTEXT], triple[SUBJ], triple[PRED]) - self._write(u" [") + self._write(" [") self.indent = self.indent + 1 self._pred = None self._newline() self._subj = triple[OBJ] # The object is now the current subject def endAnonymous(self, subject, verb): # Remind me where we are - self._write(u" ]") + self._write(" ]") self.indent = self.indent - 1 self._subj = subject self._pred = verb @@ -1453,17 +1455,17 @@ def endAnonymous(self, subject, verb): # Remind me where we are def startAnonymousNode(self, subj): if self._subj: - self._write(u" .") + self._write(" .") self._newline() self.indent = self.indent + 1 - self._write(u" [ ") + self._write(" [ ") self._subj = subj # The object is not the subject context self._pred = None def endAnonymousNode(self, subj=None): # Remove default subject - self._write(u" ]") - if not subj: self._write(u".") + self._write(" ]") + if not subj: self._write(".") self.indent = self.indent - 1 self._newline() self._subj = subj @@ -1475,18 +1477,18 @@ def endAnonymousNode(self, subj=None): # Remove default subject def startListSubject(self, subj): if self._subj: - self._write(u" .") + self._write(" .") self._newline() self.indent = self.indent + 1 - self._write(u" ( ") + self._write(" ( ") self._needNL = 0 self._subj = subj # The object is not the subject context self._pred = N3_li # expect these until list ends def endListSubject(self, subj=None): # Remove default subject - self._write(u" )") - if not subj: self._write(u".") + self._write(" )") + if not subj: self._write(".") self.indent = self.indent - 1 self._newline() self._subj = subj @@ -1498,14 +1500,14 @@ def endListSubject(self, subj=None): # Remove default subject def startListObject(self, triple): self._makeSubjPred(triple[CONTEXT], triple[SUBJ], triple[PRED]) self._subj = triple[OBJ] - self._write(u" (") + self._write(" (") self._needNL = 1 # Choice here of compactness self.indent = self.indent + 1 self._pred = N3_li # expect these until list ends self._subj = triple[OBJ] # The object is now the current subject def endListObject(self, subject, verb): # Remind me where we are - self._write(u" )") + self._write(" )") self.indent = self.indent - 1 self._subj = subject self._pred = verb @@ -1518,7 +1520,7 @@ def startFormulaSubject(self, context): if self._subj != context: self._endStatement() self.indent = self.indent + 1 - self._write(u"{") + self._write("{") self._newline() self._subj = None self._pred = None @@ -1527,21 +1529,21 @@ def endFormulaSubject(self, subj): # Remove context self._endStatement() # @@@@@@@@ remove in syntax change to implicit self._newline() self.indent = self.indent - 1 - self._write(u"}") + self._write("}") self._subj = subj self._pred = None def startFormulaObject(self, triple): self._makeSubjPred(triple[CONTEXT], triple[SUBJ], triple[PRED]) self.indent = self.indent + 1 - self._write(u"{") + self._write("{") self._subj = None self._pred = None def endFormulaObject(self, pred, subj): # Remove context self._endStatement() # @@@@@@@@ remove in syntax change to implicit self.indent = self.indent - 1 - self._write(u"}") + self._write("}") # self._newline() self._subj = subj self._pred = pred @@ -1576,34 +1578,34 @@ def _makeSubjPred(self, context, subj, pred): if "v" not in self._flags and ( self._pred== (SYMBOL, N3_forAll_URI) or self._pred == (SYMBOL, N3_forSome_URI)): - self._write(u".") + self._write(".") else: - self._write(u";") + self._write(";") self._newline(1) # Indent predicate from subject - elif not varDecl: self._write(u" ") + elif not varDecl: self._write(" ") if varDecl: if pred == (SYMBOL, N3_forAll_URI): - self._write( u" @forAll ") + self._write( " @forAll ") else: - self._write( u" @forSome ") + self._write( " @forSome ") elif pred == (SYMBOL, DAML_sameAs_URI) and "t" not in self._flags: - self._write(u" = ") + self._write(" = ") elif pred == (SYMBOL, LOG_implies_URI) and "g" not in self._flags: - self._write(u" => ") + self._write(" => ") elif pred == (SYMBOL, RDF_type_URI) and "t" not in self._flags: - self._write(u" a ") + self._write(" a ") else : - self._write( u" %s " % self.representationOf(context, pred)) + self._write( " %s " % self.representationOf(context, pred)) self._pred = pred else: - self._write(u",") + self._write(",") self._newline(3) # Same subject and pred => object list def _endStatement(self): if self._subj: - self._write(u" .") + self._write(" .") self._newline() self._subj = None @@ -1616,9 +1618,9 @@ def representationOf(self, context, pair): if "t" not in self._flags: if pair == context: - return u"this" + return "this" if pair == N3_nil and not self.noLists: - return u"()" + return "()" ty, value = pair @@ -1627,41 +1629,41 @@ def representationOf(self, context, pair): return stringToN3(value, singleLine=singleLine, flags = self._flags) if ty == XMLLITERAL: - st = u''.join([Canonicalize(x, None, unsuppressedPrefixes=['foo']) for x in value.childNodes]) + st = ''.join([Canonicalize(x, None, unsuppressedPrefixes=['foo']) for x in value.childNodes]) st = stringToN3(st, singleLine=singleLine, flags=self._flags) - return st + u"^^" + self.representationOf(context, (SYMBOL, - u"http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")) + return st + "^^" + self.representationOf(context, (SYMBOL, + "http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral")) if ty == LITERAL_DT: s, dt = value if "b" not in self._flags: if (dt == BOOLEAN_DATATYPE): - return toBool(s) and u"true" or u"false" + return toBool(s) and "true" or "false" if "n" not in self._flags: dt_uri = dt if (dt_uri == INTEGER_DATATYPE): - return unicode(long(s)) + return str(int(s)) if (dt_uri == FLOAT_DATATYPE): - retVal = unicode(float(s)) # numeric value python-normalized + retVal = str(float(s)) # numeric value python-normalized if 'e' not in retVal: retVal += 'e+00' return retVal if (dt_uri == DECIMAL_DATATYPE): - retVal = unicode(Decimal(s)) + retVal = str(Decimal(s)) if '.' not in retVal: retVal += '.0' return retVal st = stringToN3(s, singleLine= singleLine, flags=self._flags) - return st + u"^^" + self.representationOf(context, (SYMBOL, dt)) + return st + "^^" + self.representationOf(context, (SYMBOL, dt)) if ty == LITERAL_LANG: s, lang = value return stringToN3(s, singleLine= singleLine, - flags=self._flags)+ u"@" + lang + flags=self._flags)+ "@" + lang aid = self._anodeId.get(pair[1], None) if aid != None: # "a" flag only - return u"_:" + aid # Must start with alpha as per NTriples spec. + return "_:" + aid # Must start with alpha as per NTriples spec. if ((ty == ANONYMOUS) and not option_noregen and "i" not in self._flags ): @@ -1683,32 +1685,32 @@ def representationOf(self, context, pair): if ch in _notNameChars: if verbosity() > 20: progress("Cannot have character %i in local name for %s" - % (ord(ch), `value`)) + % (ord(ch), repr(value))) break else: namesp = value[:j+1] if (self.defaultNamespace and self.defaultNamespace == namesp and "d" not in self._flags): - return u":"+value[j+1:] + return ":"+value[j+1:] self.countNamespace(namesp) prefix = self.prefixes.get(namesp, None) # @@ #CONVENTION - if prefix != None : return prefix + u":" + value[j+1:] + if prefix != None : return prefix + ":" + value[j+1:] if value[:j] == self.base: # If local to output stream, - return u"<#" + value[j+1:] + u">" # use local frag id + return "<#" + value[j+1:] + ">" # use local frag id if "r" not in self._flags and self.base != None: value = hexify(refTo(self.base, value)) elif "u" in self._flags: value = backslashUify(value) else: value = hexify(value) - return u"<" + value + u">" # Everything else + return "<" + value + ">" # Everything else def nothing(): pass -import triple_maker as tm +from . import triple_maker as tm LIST = 10000 QUESTION = 10001 @@ -1799,7 +1801,7 @@ def addNode(self, node): if dt != None and "n" not in self._flags: dt_uri = dt if (dt_uri == INTEGER_DATATYPE): - self._write(str(long(lit))) + self._write(str(int(lit))) return if (dt_uri == FLOAT_DATATYPE): self._write(str(float(lit))) # numeric python-normalized @@ -1960,20 +1962,20 @@ def addQuestionMarkedSymbol(self, sym): '\\': '\\', '"': '"'} -forbidden1 = re.compile(ur'[\\\"\a\b\f\r\v\u0080-\U0000ffff]') -forbidden2 = re.compile(ur'[\\\"\a\b\f\r\v\t\n\u0080-\U0000ffff]') +forbidden1 = re.compile(r'[\\\"\a\b\f\r\v\u0080-\U0000ffff]') +forbidden2 = re.compile(r'[\\\"\a\b\f\r\v\t\n\u0080-\U0000ffff]') #" def stringToN3(str, singleLine=0, flags=""): - res = u'' + res = '' if (len(str) > 20 and - str[-1] <> u'"' and + str[-1] != '"' and not singleLine and - (string.find(str, u"\n") >=0 - or string.find(str, u'"') >=0)): - delim= u'"""' + (string.find(str, "\n") >=0 + or string.find(str, '"') >=0)): + delim= '"""' forbidden = forbidden1 # (allow tabs too now) else: - delim = u'"' + delim = '"' forbidden = forbidden2 i = 0 @@ -1986,25 +1988,25 @@ def stringToN3(str, singleLine=0, flags=""): j = m.start() res = res + str[i:j] ch = m.group(0) - if ch == u'"' and delim == u'"""' and str[j:j+3] != u'"""': #" + if ch == '"' and delim == '"""' and str[j:j+3] != '"""': #" res = res + ch else: - k = string.find(u'\a\b\f\r\t\v\n\\"', ch) - if k >= 0: res = res + u"\\" + u'abfrtvn\\"'[k] + k = string.find('\a\b\f\r\t\v\n\\"', ch) + if k >= 0: res = res + "\\" + 'abfrtvn\\"'[k] else: if 'e' in flags: # res = res + ('\\u%04x' % ord(ch)) - res = res + (u'\\u%04X' % ord(ch)) + res = res + ('\\u%04X' % ord(ch)) # http://www.w3.org/TR/rdf-testcases/#ntriples else: res = res + ch i = j + 1 # The following code fixes things for really high range Unicode - newstr = u"" + newstr = "" for ch in res + str[i:]: if ord(ch)>65535: - newstr = newstr + (u'\\U%08X' % ord(ch)) + newstr = newstr + ('\\U%08X' % ord(ch)) # http://www.w3.org/TR/rdf-testcases/#ntriples else: newstr = newstr + ch @@ -2017,14 +2019,14 @@ def backslashUify(ustr): to the given unicode""" # progress("String is "+`ustr`) # s1=ustr.encode('utf-8') - str = u"" + str = "" for ch in ustr: # .encode('utf-8'): if ord(ch) > 65535: - ch = u"\\U%08X" % ord(ch) + ch = "\\U%08X" % ord(ch) elif ord(ch) > 126: - ch = u"\\u%04X" % ord(ch) + ch = "\\u%04X" % ord(ch) else: - ch = u"%c" % ord(ch) + ch = "%c" % ord(ch) str = str + ch return str @@ -2064,7 +2066,7 @@ def dummy(): j=-1 # Single quotes don't need escaping in long format if j>=0: ch = "\\" + '\\"abfrvtn'[j] elif ch not in "\n\t" and (ch < " " or ch > "}"): - ch = "[[" + `ch` + "]]" #[2:-1] # Use python + ch = "[[" + repr(ch) + "]]" #[2:-1] # Use python res = res + ch return delim + res + delim diff --git a/pim/,salesTax.py b/pim/,salesTax.py index d1545f47..29f6f06a 100755 --- a/pim/,salesTax.py +++ b/pim/,salesTax.py @@ -253,7 +253,7 @@ def doCommand(year, inputURI="/dev/stdin"): for c in classes: if c in categories: mycat = kb.the(subj=c, pred=rdfs.label) - progress( "# %s %40s %10s" %(date, payee, `amount`)) + progress( "# %s %40s %10s" %(date, payee, repr(amount))) rows = rows + """ %s%s%s %7.2f0\n""" %( date[:10], payee, mycat, amount) @@ -280,7 +280,7 @@ def doCommand(year, inputURI="/dev/stdin"): "rows": rows } - print substitute(form, values) + print(substitute(form, values)) ############################################################ Main program @@ -299,7 +299,7 @@ def doCommand(year, inputURI="/dev/stdin"): ["help", "verbose", "year=", "input="]) except getopt.GetoptError: # print help information and exit: - print __doc__ + print(__doc__) sys.exit(2) output = None for o, a in opts: diff --git a/pim/day.py b/pim/day.py index 506823fe..1feb6e69 100755 --- a/pim/day.py +++ b/pim/day.py @@ -19,7 +19,7 @@ # Regular python library import os, sys, time from math import sin, cos, tan, sqrt, log -from urllib import urlopen +from urllib.request import urlopen import requests # SWAP https://github.com/linkeddata/swap @@ -176,7 +176,7 @@ def smooth(self, n, alpha): # eg 2, 0.7 ele += a.ele ne += 1; - b = b.next + b = b.__next__ if b is None: return lat += b.lat lon += b.lon @@ -211,7 +211,7 @@ def smoothAttribute(self, at, n, alpha): # eg 4, 0.7 total += getattr(a, at) ne += 1 if b is not None: - b = b.next + b = b.__next__ if b is not None and getattr(b, at) is not None: total += getattr(b, at) ne += 1 @@ -272,7 +272,7 @@ def getSize(s, atr): b = 6.3567523e6 #m r_earth = sqrt( ((a*a*cos(phi))**2 + ((a*a*sin(phi)))**2)/ ((a*cos(phi))**2 + ((a*sin(phi)))**2)) - print "Local radius of earth = ", r_earth + print("Local radius of earth = ", r_earth) self.y_m_per_degree = r_earth * pi /180 self.x_m_per_degree = self.y_m_per_degree * cos(self.midla*degree) @@ -516,12 +516,12 @@ def close(self): ["help", "verbose", "gpsData=", "timeline=", "speedClimb=", "outputMap=", "smooth="]) except getopt.GetoptError: # print help information and exit: - print __doc__ + print(__doc__) sys.exit(2) output = None for o, a in opts: if o in ("-h", "--help"): - print __doc__ + print(__doc__) sys.exit() if o in ("-v", "--verbose"): verbose = 1 @@ -553,13 +553,13 @@ def close(self): f = load(gpsData) if verbose: progress( "Loaded.") records = f.each(pred=rdf_type, obj=GPS.Record) - progress( `len(records)`, "records") + progress( repr(len(records)), "records") firstPoint = None; doTime = True; for record in records: tracks = f.each(subj=record, pred=GPS.track) - progress (" ", `len(tracks)`, "tracks") + progress (" ", repr(len(tracks)), "tracks") for track in tracks: points = f.each(subj=track, pred=GPS.trackpoint) for point in points: @@ -586,31 +586,31 @@ def close(self): point = firstPoint while (point is not None): point.smooth(6, 0.9); - point = point.next + point = point.__next__ totalDistance = 0; point = firstPoint while (point is not None): if point.ds is not None: totalDistance += point.ds - point = point.next + point = point.__next__ if doTime: point = firstPoint while (point is not None): point.difference(); point.generateClimb(); - point = point.next + point = point.__next__ point = firstPoint while (point is not None): point.smoothAttribute('kph', 6, 0.9); - point = point.next + point = point.__next__ point = firstPoint while (point is not None): point.smoothAttribute('climb', 6, 0.9); - point = point.next + point = point.__next__ point = firstPoint climb_vs_kph = []; @@ -622,13 +622,13 @@ def close(self): if point.climb is not None and point.climb > 0: totalClimb += point.climb if point.ds is not None: totalDistance += point.ds - point = point.next + point = point.__next__ elapsed = lastPoint.t - firstPoint.t - print "Total climb (m)", totalClimb - print "Total disance (m)", totalDistance - print "Total time (s)", elapsed, elapsed/60 - print "Average speed", totalDistance/elapsed, "m/s", totalDistance/elapsed * 3.6, "kph" + print("Total climb (m)", totalClimb) + print("Total disance (m)", totalDistance) + print("Total time (s)", elapsed, elapsed/60) + print("Average speed", totalDistance/elapsed, "m/s", totalDistance/elapsed * 3.6, "kph") @@ -639,7 +639,7 @@ def close(self): last = None n = len(events) - if verbose: progress( "First event:" , `events[0]`, "Last event:" , `events[n-1]`) + if verbose: progress( "First event:" , repr(events[0]), "Last event:" , repr(events[n-1])) minla, maxla = 90.0, -90.0 minlo, maxlo = 400.0, -400.0 @@ -680,13 +680,13 @@ def close(self): lat = lat1 + a * (lat2-lat1) long = long1 + a * (long2-long1) progress( "%s: Before (%f, %f)" % (dt1, lat1, long1)) - progress( "%s: Guess (%f, %f)" % (dt, lat, long)) + progress( "%s: Guess (%f, %f)" % (dt, lat, int)) progress( "%s: After (%f, %f)" % (dt2, lat2, long2)) where = conclusions.newBlankNode() conclusions.add(ph, GPS.approxLocation, where) conclusions.add(where, WGS.lat, lat) - conclusions.add(where, WGS.long, long) + conclusions.add(where, WGS.long, int) # guess = isodate.fullString(...) @@ -720,8 +720,8 @@ def close(self): loc = st.object() long = conclusions.the(subj=loc, pred=WGS.long) lat = conclusions.the(subj=loc, pred=WGS.lat) - progress("Photo %s at lat=%s, long=%s" %(photo.uriref(), lat, long)) - la, lo = float(lat), float(long) + progress("Photo %s at lat=%s, long=%s" %(photo.uriref(), lat, int)) + la, lo = float(lat), float(int) map.photo(photo.uriref(), lo, la) map.close() svgStream.close() diff --git a/pim/de-space-uris.py b/pim/de-space-uris.py index ab2bf116..3693605b 100755 --- a/pim/de-space-uris.py +++ b/pim/de-space-uris.py @@ -31,7 +31,7 @@ def do(path): do(thisPath) else: # Not directory if (path.endswith('.ttl')): - print "Looking at " + path + print("Looking at " + path) buf = file.open(path, 'r').read() lines = bug.split('\n') for i in range(len(lines)): @@ -41,7 +41,7 @@ def do(path): if start >= 0: end = line.indexOf('>', start) result = line[0:start+5] + line[start+5:end].replace(' ', '') + line[end:] - print " fixed to: " + result + print(" fixed to: " + result) URIsFixed += 1 line[i] = result buf2 = lines.join('\n') @@ -65,7 +65,7 @@ def do(path): elif arg == "-v": verbose = 1 # Tell me even about files which were ok else: - print """Bad option argument. + print("""Bad option argument. -f fix files instead of just looking: remove redundant files This program rmeoves (with -f) or counts (without -f) @@ -74,7 +74,7 @@ def do(path): Runs recusively through the directories. Command line argument is a directory (or more than one). -""" +""") sys.exit(-1) else: files.append(arg) diff --git a/pim/dedup-tunes.py b/pim/dedup-tunes.py index 6395b041..84d531d5 100755 --- a/pim/dedup-tunes.py +++ b/pim/dedup-tunes.py @@ -79,7 +79,7 @@ def do(path): elif arg == "-v": verbose = 1 # Tell me even about files which were ok else: - print """Bad option argument. + print("""Bad option argument. -f fix files instead of just looking: remove redundant files This program rmeoves (with -f) or counts (without -f) @@ -88,7 +88,7 @@ def do(path): Runs recusively through the directories. Command line argument is a directory (or more than one). -""" +""") sys.exit(-1) else: files.append(arg) diff --git a/pim/fileByDateInFilename.py b/pim/fileByDateInFilename.py index 03dce1e1..84403f1d 100644 --- a/pim/fileByDateInFilename.py +++ b/pim/fileByDateInFilename.py @@ -2,7 +2,7 @@ # import sys, os for fn in sys.argv[1:]: - print "Filename ", fn + print("Filename ", fn) if 0: # Old 2014 moves format space = fn.rfind(' ') year = fn[space+1 : space+5] @@ -22,7 +22,7 @@ assert fn[underline+9:underline+14] == '.json' date = year + '-' + month + '-' + day - print "Date = %s-%s-%s" % (year, month, day) + print("Date = %s-%s-%s" % (year, month, day)) assert len(year) == 4 assert len(month) == 2 assert len(day) == 2 @@ -30,5 +30,5 @@ dir = "/Users/timbl/Documents/%s/%s/%s" % (year, month, day); dest = "/Users/timbl/Documents/%s/%s/%s/%s.json" % (year, month, day, type); command = 'mkdir -p "%s"; mv "%s" "%s" ' % (dir,fn, dest); - print command + print(command) os.system(command) \ No newline at end of file diff --git a/pim/fin.py b/pim/fin.py index 02a1de9c..6e345c12 100755 --- a/pim/fin.py +++ b/pim/fin.py @@ -58,29 +58,29 @@ def printTransactionDetails(t): for st in kb.statementsMatching(subj=t): - print " %-15s "%`st.predicate()`, str(st.object()) + print(" %-15s "%repr(st.predicate()), str(st.object())) def printCategoryTotalsOnly(cats, totals, count): trans = 0 inc, out = 0,0 global kb - print "" + print("
") for c in cats: - label = `c` + label = repr(c) tot = totals.get(c, 0) if tot != 0: - print "" %(tot), - print + print(" (%s)"% repr(s), end=' ') + print("" %(tot), end=' ') + print() if tot > 0: inc = inc + tot else: out = out + tot trans = trans + count.get(c,0) - print "
%s" %(label), + print("
%s" %(label), end=' ') for s in kb.each(subj=c, pred=rdfs.subClassOf): - print " (%s)"% `s`, - print "%9.2f
%9.2f
" - print """

In %i transactions, Total income %9.2f, - outgoings %9.2f, balance %92f

""" % (trans, inc, out, inc + out) + print("") + print("""

In %i transactions, Total income %9.2f, + outgoings %9.2f, balance %92f

""" % (trans, inc, out, inc + out)) @@ -110,7 +110,7 @@ def transactionRow(x): payee = kb.any(subj=x, pred=qu.payee) toAccount = kb.the(subj=x, pred=qu.toAccount) accLabel = kb.any(subj = toAccount, pred = rdfs.label) - if not accLabel: accLabel = `toAccount`[-4:] + if not accLabel: accLabel = repr(toAccount)[-4:] return " %s%s%s%7.2f\n" % (accLabel, date, baseRel(x.uriref()), payee, accLabel, amount) @@ -150,7 +150,7 @@ def internalCheck(): usds = kb.each(subj=y, pred=qu.in_USD) if len(usds) == 0:continue # No amount => must be not in this period. if len(usds) != 1: - progress("Error: Ignoring: %i != 1 USD amounts for Internal transaction %s" % (len(usds), `y`+': '+ `usds`)) + progress("Error: Ignoring: %i != 1 USD amounts for Internal transaction %s" % (len(usds), repr(y)+': '+ repr(usds))) transactions.remove(y) continue if abs(amount + @@ -160,8 +160,8 @@ def internalCheck(): else: unbalanced.append(x) if unbalanced: - print "

Unbalanced internal transactions

" - print transactionTable(unbalanced); + print("

Unbalanced internal transactions

") + print(transactionTable(unbalanced)); return @@ -183,8 +183,8 @@ def writeChart(filename, categories, totals, income, outgoings, shortTerm=0): label = kb.the(subj=c, pred=rdfs.label) if label == None: - label = `c` - sys.stderr.write("Warning: No label for category"+`c` +"\n") + label = repr(c) + sys.stderr.write("Warning: No label for category"+repr(c) +"\n") else: label = str(label) @@ -354,7 +354,7 @@ def noteError(e): qu_Unclassified = qu.Unclassified taxCategories = kb.each(pred=rdf_type, obj=tax.Category) if verbose: - progress("Tax categories" + `taxCategories`) + progress("Tax categories" + repr(taxCategories)) specialCategories = taxCategories + [qu.Classified, qu.Unclassified, qu.Transaction] ####### Analyse the data: @@ -388,11 +388,11 @@ def noteError(e): # progress( "Categories: "+`cats`) month = monthNumber(s) - if month not in range(numberOfMonths) : continue + if month not in list(range(numberOfMonths)) : continue payees = kb.each(subj=s, pred=qu_payee) if not payees: - progress("@@@ Error: No payee for "+`uri`) + progress("@@@ Error: No payee for "+repr(uri)) payee = "@@@@@@ Unknown"; fatalErrors += 1; @@ -405,11 +405,11 @@ def noteError(e): if len(amounts) == 0: amounts = kb.each(subj=s, pred=qu_amount) if len(amounts) == 0: - progress("@@@ Error: No USD amount for "+`uri`) + progress("@@@ Error: No USD amount for "+repr(uri)) fatalErrors += 1; else: - progress("Warning: No USD amount for "+`uri`+", assuming USD") + progress("Warning: No USD amount for "+repr(uri)+", assuming USD") if len(amounts) >1: if (cat_ns.Internal not in cats or len(amounts) != 2 ): @@ -417,7 +417,7 @@ def noteError(e): progress( "Error: More than one USD amount %s for transaction %s -- ignoring!\n" - % (`amounts`,uri)) + % (repr(amounts),uri)) else: sum = float(amounts[0]) + float(amounts[1]) if sum != 0: @@ -427,10 +427,10 @@ def noteError(e): continue if len(amounts) != 1: - progress("@@@ Error: No amount for "+`uri`); + progress("@@@ Error: No amount for "+repr(uri)); fatalErrors += 1; ss = kb.statementsMatching(subj=s) - progress(`ss`+'; KB='+`kb.n3String()`) + progress(repr(ss)+'; KB='+repr(kb.n3String())) continue amount = float(amounts[0].__str__()) # print "%s %40s %10s month %i" %(date, payee, `amount`, month) @@ -460,15 +460,15 @@ def noteError(e): bottomCats.remove(sup) if len(bottomCats) == 0: noteError("No categoriy: for <%s>" # all cats: %s, raw cats:%s" - %(`s`)) # ,`cats`, `kb.each(subj=s, pred=rdf.type)`) + %(repr(s))) # ,`cats`, `kb.each(subj=s, pred=rdf.type)`) elif bottomCats[0] not in bottomCategories and (bottomCats[0] not in [ qu.UnclassifiedIncome, qu.UnclassifiedOutgoing]): - noteError("Be more specific: %s for <%s>" %(`bottomCats[0]`, `s`)) # Won't get shown e.g. in year-cat.html + noteError("Be more specific: %s for <%s>" %(repr(bottomCats[0]), repr(s))) # Won't get shown e.g. in year-cat.html if len(bottomCats) > 1: noteError("Inconsistent categories: %s" # all cats: %s, raw cats:%s" - %(`bottomCats`)) # ,`cats`, `kb.each(subj=s, pred=rdf.type)`) + %(repr(bottomCats))) # ,`cats`, `kb.each(subj=s, pred=rdf.type)`) - print '' + print('') if '--summary' in sys.argv: title = "Monthly summary" @@ -477,13 +477,13 @@ def noteError(e): else: title = "Report" - print """ + print(""" %s - """ % (title) + """ % (title)) # dollar @@ -494,17 +494,17 @@ def noteError(e): # SUMMARY TABLE OF CATEGORY BY MONTH if '--summary' in sys.argv: - print "

Personal categories and months %s - %s

" % (startDate, endDate) - print "" + print("

Personal categories and months %s - %s

" % (startDate, endDate)) + print("
") - print "" + print("") for month in range(numberOfMonths): m = month + int(startDate[5:7]) - 1 while m > 11: m -= 12 # Modulo in python? - print "" %(("0"+`m+1`)[-2:], monthName[m]), - print "" + print("" %(("0"+repr(m+1))[-2:], monthName[m]), end=' ') + print("") def listFor(c, depth=0): # Any, because there could be 2 copies of same list :-( @@ -513,7 +513,7 @@ def listFor(c, depth=0): # Any, because there could be 2 copies of same list : if subs == None: subs = kb.each(pred = rdfs.subClassOf, obj = c); if len(subs) > 0: - sys.stderr.write( "Warning: for %s: no disjointUnionOf but subclasses %s\n" %(`c`, `subs`)) + sys.stderr.write( "Warning: for %s: no disjointUnionOf but subclasses %s\n" %(repr(c), repr(subs))) for sub in subs: res += listFor(sub, depth+1) else: for sub in subs: res += listFor(sub, depth+1) @@ -524,27 +524,27 @@ def listFor(c, depth=0): # Any, because there could be 2 copies of same list : for cat, depth in printOrder: label = kb.the(subj=cat, pred=rdfs.label) if label == None: - label = `cat` - sys.stderr.write("@@ No label for "+`cat` +"\n") + label = repr(cat) + sys.stderr.write("@@ No label for "+repr(cat) +"\n") else: label = str(label) anchor = cat.fragid if totals.get(cat, None) != None: - print monthGridRow(anchor, anchor, totals[cat], byMonth.get(cat, [0] * numberOfMonths), - numberOfMonths, indent = depth) + print(monthGridRow(anchor, anchor, totals[cat], byMonth.get(cat, [0] * numberOfMonths), + numberOfMonths, indent = depth)) - print "" - print monthGridRow("Income", None, income, incomeByMonth, numberOfMonths) - print monthGridRow("Outgoings", None, outgoings, outgoingsByMonth, numberOfMonths) - print monthGridRow("Balance", None, income + outgoings, monthTotals, numberOfMonths) + print("") + print(monthGridRow("Income", None, income, incomeByMonth, numberOfMonths)) + print(monthGridRow("Outgoings", None, outgoings, outgoingsByMonth, numberOfMonths)) + print(monthGridRow("Balance", None, income + outgoings, monthTotals, numberOfMonths)) - print "
Total
Total %s
%s
___
___
" + print("") # Chart of income stacked up against expenses if '--charts' in sys.argv: - print "

Chart of day-day income vs expense

" - print "

Chart of all income vs expense

" + print("

Chart of day-day income vs expense

") + print("

Chart of all income vs expense

") writeChart(filename = "chart.svg", categories = bottomCategories + [ qu.UnclassifiedIncome, qu.UnclassifiedOutgoing], @@ -573,11 +573,11 @@ def listFor(c, depth=0): # Any, because there could be 2 copies of same list : # Generate a list of errors found errstr = "" - for x, list in errors.items(): + for x, list in list(errors.items()): errstr += transactionRow(x) - for e in list: errstr += ""+`e`+"\n" # @@@ encode error string + for e in list: errstr += ""+repr(e)+"\n" # @@@ encode error string if errstr: - print "

Inconsistencies

\n" + errstr + "
\n" + print("

Inconsistencies

\n" + errstr + "
\n") # List Unclassified Income and Spending @@ -589,35 +589,35 @@ def transactionList(cat): return st + transactionTable(ts) for cat in [ qu.UnclassifiedIncome, qu.UnclassifiedOutgoing]: - print transactionList(cat) + print(transactionList(cat)) - print reimbursablesCheck(); + print(reimbursablesCheck()); internalCheck() if 0: - print "

Tax Categories

" + print("

Tax Categories

") taxCategories = kb.each(pred=rdf_type, obj=tax.Category) printCategoryTotalsOnly(taxCategories + [ qu.Unclassified], totals, count) - print "

Tax stuff

" - print "" - print "" - print "
-Form lineamount
" + print("

Tax stuff

") + print("") + print("") + print("
-Form lineamount
") # print "

Personal Category total

" # printCategoryTotalsOnly(quCategories + [ qu.Unclassified], totals, count) - print + print() - print "Note totals for tax and personal breakdowns must match." + print("Note totals for tax and personal breakdowns must match.") dates = kb.statementsMatching(pred=qu.date) - print "There should be a total of %i transactions in each." % len(dates) + print("There should be a total of %i transactions in each." % len(dates)) if 0: - print "
(consistency check)"
+        print("
(consistency check)")
         problems = 0
         for s in dates:
             tra = s.subject()
@@ -626,12 +626,12 @@ def transactionList(cat):
                 if typ is qu.Unclassified or typ is qu.Classified:
                     break # ok
             else:
-                print "@@@@ problem transcation with no classified or unclassified, with types", types
+                print("@@@@ problem transcation with no classified or unclassified, with types", types)
                 printTransactionDetails(tra)
                 problems = problems + 1
-        print problems, "problems.
" + print(problems, "problems.
") - print "" + print("") return fatalErrors @@ -642,7 +642,7 @@ def transactionList(cat): global yearInQuestion, startDate, endDate def usage(): - print __doc__ + print(__doc__) startDate = None endDate = None diff --git a/pim/fromGarmin.py b/pim/fromGarmin.py index bf37f4b8..89e003ab 100755 --- a/pim/fromGarmin.py +++ b/pim/fromGarmin.py @@ -65,8 +65,8 @@ def doCommand(serialDevice=None, outputURI=None, doTracks=1, doWaypoints=1, verb gps = Garmin(phys) - print "GPS Product ID: %d Descriptions: %s Software version: %2.2f" % \ - (gps.prod_id, gps.prod_descs, gps.soft_ver) + print("GPS Product ID: %d Descriptions: %s Software version: %2.2f" % \ + (gps.prod_id, gps.prod_descs, gps.soft_ver)) f = formula() # Empty store of RDF data @@ -77,41 +77,41 @@ def doCommand(serialDevice=None, outputURI=None, doTracks=1, doWaypoints=1, verb if doWaypoints: # show waypoints - if verbose: print "Getting waypoints" + if verbose: print("Getting waypoints") wpts = gps.getWaypoints() for w in wpts: - if verbose: progress(`w`) + if verbose: progress(repr(w)) wpt = symbol(uripath.join(base, w.ident)) f.add(record, GPS.waypoint, wpt) - f.add(wpt, WGS.lat, obj=intern(degrees(w.slat))) - f.add(wpt, WGS.long, obj=intern(degrees(w.slon))) + f.add(wpt, WGS.lat, obj=sys.intern(degrees(w.slat))) + f.add(wpt, WGS.long, obj=sys.intern(degrees(w.slon))) if doTracks: # show track - if verbose: print "Getting tracks" + if verbose: print("Getting tracks") tracks = gps.getTracks() for t in tracks: track = f.newBlankNode() f.add(record, GPS.track, track) for p in t: if isinstance(p, TrackHdr): - if verbose: progress(`p`) - f.add(track, GPS.disp, intern(p.dspl)) - f.add(track, GPS.color, intern(p.color)) - f.add(track, GPS.trk_ident, intern(p.trk_ident)) + if verbose: progress(repr(p)) + f.add(track, GPS.disp, sys.intern(p.dspl)) + f.add(track, GPS.color, sys.intern(p.color)) + f.add(track, GPS.trk_ident, sys.intern(p.trk_ident)) else: - if verbose: progress(`p`) + if verbose: progress(repr(p)) point = f.newBlankNode() f.add(track, GPS.trackpoint, point) - f.add(point, WGS.lat, obj=intern(degrees(p.slat))) - f.add(point, WGS.long, obj=intern(degrees(p.slon))) + f.add(point, WGS.lat, obj=sys.intern(degrees(p.slat))) + f.add(point, WGS.long, obj=sys.intern(degrees(p.slon))) # if verbose: progress(" time=", p.time) # progress('p.time='+`p.time`) # @@ - if p.time == 0 or p.time == 0xffffffffL: + if p.time == 0 or p.time == 0xffffffff: if verbose: progress("time=%8x, ignoring" % p.time) else: - f.add(point, WGS.time, obj=intern(isodate.fullString(TimeEpoch+p.time))) + f.add(point, WGS.time, obj=sys.intern(isodate.fullString(TimeEpoch+p.time))) phys.f.close() # Should really be done by the del() below, but isn't del(phys) # close serial link (?) @@ -124,14 +124,14 @@ def doCommand(serialDevice=None, outputURI=None, doTracks=1, doWaypoints=1, verb op.write(s) op.close() else: - print s + print(s) ############################################################ Main program def usage(): - print __doc__ + print(__doc__) if __name__ == '__main__': import getopt @@ -144,7 +144,7 @@ def usage(): ["help", "verbose", "tracks", "waypoints", "device=", "output="]) except getopt.GetoptError: # print help information and exit: - print __doc__ + print(__doc__) sys.exit(2) output = None for o, a in opts: diff --git a/pim/ics2txt.py b/pim/ics2txt.py index fcd63a2b..3d3c7181 100755 --- a/pim/ics2txt.py +++ b/pim/ics2txt.py @@ -137,7 +137,7 @@ def extract(path): b = input.read() input.close() - wr("# Length: " + `len(b)`+ "starts ") + wr("# Length: " + repr(len(b))+ "starts ") wr(" ".join(["%2x"%ord(ch) for ch in b[:8]])) wr("\n") if ord(b[0])==0 and ord(b[1]) == ord('B'): # UTF16 with MSB byte order unmarked @@ -197,7 +197,7 @@ def endGroup(g): return "%s %s;\n" % (dp, do) if len(kludges) != 0: - raise ValueError("Unknown Group pattern:"+`pos`) + raise ValueError("Unknown Group pattern:"+repr(pos)) res = "" for p,o in pos: @@ -247,7 +247,7 @@ def predicateObject(n, props, value): pass elif val in typeFields.get(n, []): if relationshipModifiers.get(val, 0): - if modifiers: print "# @@ multiple modifiers in: "+line + if modifiers: print("# @@ multiple modifiers in: "+line) modifiers = val + '-' + modifiers else: classes.append('vc:'+val[0].upper()+val[1:]) else: @@ -282,7 +282,7 @@ def predicateObject(n, props, value): return '', orderedFields(value, map) # Naked fields - see notes return pred, '[' + orderedFields(value, map) + classSpec + ']' if n == 'version': - assert value == "3.0", "value found: "+`value` + assert value == "3.0", "value found: "+repr(value) return "", "" if n == 'x.ablabel': return "", "" # used elsewhere @@ -299,7 +299,7 @@ def predicateObject(n, props, value): obj = None if n == 'tel': if value[0] != '+': - print "# @@ Warning: not international form tel: "+value + print("# @@ Warning: not international form tel: "+value) obj = '' % (value.replace(' ','-')) elif n == 'url': obj = '<%s>' % (value) @@ -312,7 +312,7 @@ def predicateObject(n, props, value): elif n in singleTextField : # Single text if classSpec: - raiseValueError("Unexpected class on %s: %s"%(n,`classSpec`)) + raiseValueError("Unexpected class on %s: %s"%(n,repr(classSpec))) return pred, stringToN3(unesc, singleLine=0) # @@@ N3 escaping raise ValueError('Unknown tag:'+n) @@ -412,12 +412,12 @@ def _test(): _test() sys.exit(0) else: - print """Bad option argument. + print("""Bad option argument. -v verbose -t self-test -f fix files instead of just looking -""" +""") sys.exit(-1) else: files.append(arg) diff --git a/pim/json2n3.py b/pim/json2n3.py index 97c3ba04..6bd82f44 100644 --- a/pim/json2n3.py +++ b/pim/json2n3.py @@ -49,21 +49,21 @@ def toTurtle(self, x, level = 0): self.writeln(indent + ')'); elif type(x) == type({}): self.writeln(indent + '['); - for key, value in x.items(): + for key, value in list(x.items()): self.writeln(indent + ':'+key + ' '); self.toTurtle(value, level + 1); self.writeln(indent + ';') self.writeln(indent + ']'); - elif type(x) == type(u''): + elif type(x) == type(''): self.writeln('"""' + encodeString(x) + '"""') elif x == None: - self.writeln(indent + `()`); - elif `x` == 'True': + self.writeln(indent + repr(())); + elif repr(x) == 'True': self.writeln(indent + 'true'); # Python to n3 - elif `x` == 'False': + elif repr(x) == 'False': self.writeln(indent + 'false'); else: - self.writeln(indent + `x`); + self.writeln(indent + repr(x)); if level == 0: diff --git a/pim/ldif2n3.py b/pim/ldif2n3.py index 27f88136..fb1be9fe 100755 --- a/pim/ldif2n3.py +++ b/pim/ldif2n3.py @@ -38,13 +38,13 @@ def convert(path): dict = {} - print "# http://www.w3.org/DesignIssues/Notation3" - print "# Generated from", path - print "# Generated by ", version - print - print "@prefix foaf: ." - print "@prefix ldif: ." - print + print("# http://www.w3.org/DesignIssues/Notation3") + print("# Generated from", path) + print("# Generated by ", version) + print() + print("@prefix foaf: .") + print("@prefix ldif: .") + print() input = open(path, "r") buf = input.read() # Read the file @@ -84,7 +84,7 @@ def convert(path): m = blank.match(l) if m: - print " ]." + print(" ].") inPerson = 0 continue valtype = 'LITERAL' @@ -105,22 +105,22 @@ def convert(path): valtype = 'SYMBOL' if m: if not inPerson: - print " [" + print(" [") inPerson = 1 if field == "objectclass": if value == "top": continue # Zero content info - print '\ta ldif:%s; '% (value[0:1].upper() + value[1:]) + print('\ta ldif:%s; '% (value[0:1].upper() + value[1:])) elif field in ["mail", "email", "mozillaSecondEmail"]: ## @@ distinguish? mboxUri = "mailto:" + value hash = binascii.hexlify(sha.new(mboxUri).digest()) - print '\tfoaf:mbox_sha1sum %s;' % (stringToN3(hash, singleLine=1)) + print('\tfoaf:mbox_sha1sum %s;' % (stringToN3(hash, singleLine=1))) if not hideMailbox: - print '\tfoaf:mbox <%s>;' % (mboxUri) + print('\tfoaf:mbox <%s>;' % (mboxUri)) elif field in ["telephoneNumber", "homePhone", 'fax', 'pager', 'mobile']: - print '\tldif:%s ;' % (field, value.replace(' ','-')) + print('\tldif:%s ;' % (field, value.replace(' ','-'))) else: @@ -130,12 +130,12 @@ def convert(path): obj = stringToN3(value, singleLine=0) pred = asFoaf.get(field, '\tldif:'+field) if not (hideMailbox and field == "dn"): - print '\t%s %s; '% (pred, obj) + print('\t%s %s; '% (pred, obj)) continue - print "# ERROR: Unknown line format:", l - print "]." + print("# ERROR: Unknown line format:", l) + print("].") def do(path): @@ -154,12 +154,12 @@ def do(path): for arg in sys.argv[1:]: if arg[0:1] == "-": if arg == "-?" or arg == "--help": - print + print() elif arg == "-v": verbose = 1 elif arg == "-m": hideMailbox = 1 elif arg == "-l": pureLDIF = 1 else: - print """Bad option argument.""" + __doc__ + print("""Bad option argument.""" + __doc__) sys.exit(-1) else: files.append(arg) diff --git a/pim/lookout.py b/pim/lookout.py index 0a572f4f..cf6a53bd 100644 --- a/pim/lookout.py +++ b/pim/lookout.py @@ -31,10 +31,10 @@ # # http://www.w3.org/People/Connolly/ -# Copyright © 1998 World Wide Web Consortium, +# Copyright 1998 World Wide Web Consortium, # (Massachusetts Institute of Technology, Institut National de # Recherche en Informatique et en Automatique, Keio University). All -# Rights Reserved. +# Rights Reserved. # # Permission to use, copy, modify, and distribute this software # and its documentation for any purpose and without fee or @@ -42,7 +42,7 @@ # # W3C IPR SOFTWARE NOTICE # http://www.w3.org/COPYRIGHT -# September 1997 +# September 1997 # This module depends on standard and built-in python modules, per # Python Library Reference @@ -91,7 +91,7 @@ boring = [ # Example value: # Fields from Contacts: - + "LastFirstSpaceOnly", # "Layman Andrew" ; # r "Email2EntryID", # "x\000x\000" ; # r "Email3EntryID", # "0" ; # r @@ -150,11 +150,11 @@ # ms:BusyStatus "0" ; # rw "ConversationIndex", # "^V^@\ubd01\u5ea4\ub781\u1e50\uc7c8\u2510\ud211\uee8d\u # ms:ResponseRequested "1" ; # rw -# ms:Start "2001-02-14T00:00:00Eastern Daylight Time" . +# ms:Start "2001-02-14T00:00:00Eastern Daylight Time" . + - ] - + OTHERS = ["Size", "OutlookInternalVersion", "NetMeetingAutoStart", "OutlookVersion", "FormDescription", "StoreID", "EntryID", # UUIDs mess up the file but might be useful @@ -244,7 +244,7 @@ def main(): ) print "# ENDS" - + # DWC: outlook.Application is the Application class, per @@ -259,7 +259,7 @@ def main(): # the translation to python is straightforward. # # The Microsoft Outlook 97 Automation Server Programming Model -# Last Updated: June 26, 1998 +# Last Updated: June 26, 1998 # http://www.microsoft.com/OutlookDev/Articles/Outprog.htm # @@ -320,7 +320,7 @@ def __init__(self): self.internal = [] self._nextId = 0 self.details = [] # Set of interesting subfields for this operations - + def findContact(self, filter): _mapi = self.GetNamespace(MAPI) @@ -339,7 +339,7 @@ def getFolder(self, what, details): cal = _mapi.GetDefaultFolder(what) print "\n# Folder %i:" % what folderId = self._getItem(cal) - + list = cal.Items n = len(list) for i in range(n): @@ -360,7 +360,7 @@ def _getEntryId(self, item, key="EntryId"): - + def _getItem(self, item, indent=0, noun=0): global verbose try: @@ -369,8 +369,8 @@ def _getItem(self, item, indent=0, noun=0): # pkeys = [] except: print "[ a ms:weirdThing ] #", `self` - return - print "\n", " "*indent, + return + print "\n", " "*indent, need_semicolon = 0 key = "EntryID" @@ -412,7 +412,7 @@ def _getItem(self, item, indent=0, noun=0): else: msid = "???" print "@@@@@@@@ No EntryID in", `atts` - + for ms, f in self.internal: if ms is x: @@ -422,9 +422,9 @@ def _getItem(self, item, indent=0, noun=0): else: self._nextId = self._nextId + 1 frag = "#_g" + `self._nextId` - self.internal.append((x, frag)) + self.internal.append((x, frag)) already = 0 - + print "%-32s %s" % (PREFIX+key, str), if (not already) and indent < 3 and key in self.details: print " = ", @@ -434,7 +434,7 @@ def _getItem(self, item, indent=0, noun=0): latest[key] = str tail = "# rw" if not _w : tail = "# r" - + if latest.get("IsRecurring", '"0"') != '"0"': rec = item.GetRecurrencePattern() print ";\n ms:recurrencePattern # isrec=",latest.get("IsRecurring", 0) @@ -448,7 +448,7 @@ def _getItem(self, item, indent=0, noun=0): return id - + def iso2vb(isotime): @@ -469,8 +469,7 @@ def iso2vb(isotime): return ret - - - -if __name__ == '__main__': main() + + +if __name__ == '__main__': main() diff --git a/plwm.py b/plwm.py index c3e5084f..209830bb 100755 --- a/plwm.py +++ b/plwm.py @@ -15,10 +15,10 @@ revision = '$Id$' import sys -import ArgHandler -import hotswap -import pluggable -import urllib +from . import ArgHandler +from . import hotswap +from . import pluggable +import urllib.request, urllib.parse, urllib.error class NotImplented(RuntimeError): pass @@ -59,7 +59,7 @@ def trace(self, message, subsystem=None): def open(self, url, readwrite="read"): # readwrite part not implemented - stream = urllib.urlopen(url) + stream = urllib.request.urlopen(url) stream.info().uri = url # @@ absoluteize? sanitize? return stream @@ -87,7 +87,7 @@ class MyArgHandler(ArgHandler.ArgHandler): def __init__(self, host=None, *args, **kwargs): #apply(super(MyArgHandler, self).__init__, args, kwargs) - apply(ArgHandler.ArgHandler.__init__, [self]+list(args), kwargs) + ArgHandler.ArgHandler.__init__(*[self]+list(args), **kwargs) self.host = host def handle__pipe(self): @@ -258,9 +258,9 @@ def handleNoArgs(self): hotswap.prepend("wrap_llyn") try: a.run() - except hotswap.NoMatchFound, e: - print e - print "Try --help preplug for more information\n" + except hotswap.NoMatchFound as e: + print(e) + print("Try --help preplug for more information\n") sys.exit(1) # who should do this, really? diff --git a/pretty.py b/pretty.py index f166a0f8..83523bed 100755 --- a/pretty.py +++ b/pretty.py @@ -14,26 +14,26 @@ import types import string -import diag -from diag import progress, verbosity, tracking -from term import Literal, XMLLiteral, Symbol, Fragment, AnonymousNode, \ +from . import diag +from .diag import progress, verbosity, tracking +from .term import Literal, XMLLiteral, Symbol, Fragment, AnonymousNode, \ AnonymousVariable, FragmentNil, AnonymousUniversal, \ Term, CompoundTerm, List, EmptyList, NonEmptyList, N3Set -from formula import Formula, StoredStatement +from .formula import Formula, StoredStatement -from RDFSink import Logic_NS, RDFSink, forSomeSym, forAllSym -from RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4, \ +from .RDFSink import Logic_NS, RDFSink, forSomeSym, forAllSym +from .RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4, \ ANONYMOUS, SYMBOL, LITERAL, LITERAL_DT, LITERAL_LANG, XMLLITERAL -from RDFSink import N3_nil, N3_first, N3_rest, OWL_NS, N3_Empty, N3_List, \ +from .RDFSink import N3_nil, N3_first, N3_rest, OWL_NS, N3_Empty, N3_List, \ List_NS -from RDFSink import RDF_NS_URI -from RDFSink import RDF_type_URI +from .RDFSink import RDF_NS_URI +from .RDFSink import RDF_type_URI cvsRevision = "$Revision$" # Magic resources we know about -from RDFSink import RDF_type_URI, DAML_sameAs_URI +from .RDFSink import RDF_type_URI, DAML_sameAs_URI STRING_NS_URI = "http://www.w3.org/2000/10/swap/string#" META_NS_URI = "http://www.w3.org/2000/10/swap/meta#" @@ -117,7 +117,7 @@ def selectDefaultPrefix(self, printFunction): best = 0 mp = None counts = dummySink.namespaceCounts() - for r, count in counts.items(): + for r, count in list(counts.items()): if verbosity() > 25: progress(" Count is %3i for %s" %(count, r)) if (r != RDF_NS_URI and count > 0 @@ -133,7 +133,7 @@ def selectDefaultPrefix(self, printFunction): # Make up prefixes for things which don't have them: - for r, count in counts.items(): + for r, count in list(counts.items()): if count > 1 and r != mp: if self.store.prefixes.get(r, None) is None: p = r @@ -158,7 +158,7 @@ def selectDefaultPrefix(self, printFunction): else: n = 2 while 1: - pref = p[:3]+`n` + pref = p[:3]+repr(n) if self.store.namespaces.get(pref, None) is None: break n = n + 1 @@ -170,7 +170,7 @@ def selectDefaultPrefix(self, printFunction): self.sink.setDefaultNamespace(self.defaultNamespace) # progress("&&&& Counts: ", counts) - prefixes = self.store.namespaces.keys() # bind in same way as input did FYI + prefixes = list(self.store.namespaces.keys()) # bind in same way as input did FYI prefixes.sort() # For repeatability of test results for pfx in prefixes: r = self.store.namespaces[pfx] @@ -185,7 +185,7 @@ def selectDefaultPrefix(self, printFunction): def dumpPrefixes(self): if self.defaultNamespace is not None: sink.setDefaultNamespace(self.defaultNamespace) - prefixes = self.store.namespaces.keys() # bind in same way as input did FYI + prefixes = list(self.store.namespaces.keys()) # bind in same way as input did FYI prefixes.sort() for pfx in prefixes: uri = self.store.namespaces[pfx] @@ -274,7 +274,7 @@ def fixSet(x): def _outputStatement(self, sink, quad, aWorks = 1): if isinstance(quad[1], Literal): - raise ValueError("Cannot have a literal as a predicate. This makes no sense, %s" % `quad[1]`) + raise ValueError("Cannot have a literal as a predicate. This makes no sense, %s" % repr(quad[1])) if isinstance(quad[1], Formula): raise ValueError("Cannot have a formula as a predicate. This makes no sense") sink.makeStatement(auxPairs(quad), aIsPossible=aWorks) @@ -330,7 +330,7 @@ def fixSet(x): self._outputStatement(sink, [fixSet(x) for x in s.quad]) if 0: # Doesn't work as ther ei snow no list of bnodes - rs = self.store.resources.values() + rs = list(self.store.resources.values()) if sorting: rs.sort(Term.compareAnyTerm) for r in rs : # First the bare resource statements = context.statementsMatching(subj=r) @@ -338,7 +338,7 @@ def fixSet(x): for s in statements : self._outputStatement(sink, s.quad) if not isinstance(r, Literal): - fs = r.fragments.values() + fs = list(r.fragments.values()) if sorting: fs.sort for f in fs : # then anything in its namespace statements = context.statementsMatching(subj=f) @@ -391,7 +391,7 @@ def _scanObj(self, context, x): def _scan(self, x, context=None): # progress("Scanning ", x, " &&&&&&&&") # assert self.context._redirections.get(x, None) is None, "Should not be redirected: "+`x` - if verbosity() > 98: progress("scanning %s a %s in context %s" %(`x`, `x.__class__`,`context`), + if verbosity() > 98: progress("scanning %s a %s in context %s" %(repr(x), repr(x.__class__),repr(context)), x.generated(), self._inContext.get(x, "--")) if isinstance(x, NonEmptyList) or isinstance(x, N3Set): for y in x: @@ -502,12 +502,12 @@ def _topology(self, x, context): _isExistential) if verbosity() > 97: progress( "Topology %s in %s is: ctx=%s,anon=%i obj=%i, pred=%i loop=%s ex=%i "%( - `x`, `context`, `ctx`, _anon, _asObj, _asPred, _loop, _isExistential)) + repr(x), repr(context), repr(ctx), _anon, _asObj, _asPred, _loop, _isExistential)) return ( _anon, _asObj+_asPred ) if verbosity() > 98: progress( "Topology %s in %s is: anon=%i obj=%i, pred=%i loop=%s ex=%i "%( - `x`, `context`, _anon, _asObj, _asPred, _loop, _isExistential)) + repr(x), repr(context), _anon, _asObj, _asPred, _loop, _isExistential)) ## self._topology_returns[x] = ( _anon, _asObj+_asPred ) return ( _anon, _asObj+_asPred ) @@ -576,7 +576,7 @@ def dumpFormulaContents(self, context, sink, sorting, equals=0): allStatements = context.statements[:] if equals: - for x, y in context._redirections.items(): + for x, y in list(context._redirections.items()): if not x.generated() and x not in context.variables(): allStatements.append(StoredStatement( (context, context.store.sameAs, x, y))) @@ -655,7 +655,7 @@ def _dumpSubject(self, subj, context, sink, sorting, statements=[]): for s in statements: p = s.quad[PRED] if p is not self.store.first and p is not self.store.rest: - if verbosity() > 90: progress("Is list, has values for", `p`) + if verbosity() > 90: progress("Is list, has values for", repr(p)) break # Something to print (later) else: if subj.generated(): return # Nothing. @@ -681,7 +681,7 @@ def _dumpSubject(self, subj, context, sink, sorting, statements=[]): sink.endAnonymousNode() return else: - if verbosity() > 90: progress("%s Not list, has property values." % `subj`) + if verbosity() > 90: progress("%s Not list, has property values." % repr(subj)) sink.startAnonymousNode(auPair(subj)) for s in statements: # "[] color blue." might be nicer. @@@ Try it? try: @@ -716,7 +716,7 @@ def dumpStatement(self, sink, triple, sorting): if isinstance(obj, NonEmptyList): if verbosity()>99: - progress("List found as object of dumpStatement " + `obj` + progress("List found as object of dumpStatement " + repr(obj) + context.debugString()) collectionSyntaxOK = ("l" not in self.flags) diff --git a/pycwmko.py b/pycwmko.py index a5bce394..eae30f1f 100644 --- a/pycwmko.py +++ b/pycwmko.py @@ -3,9 +3,9 @@ """ -from pychinko import terms, interpreter +from .pychinko import terms, interpreter #from pychinko import N3Loader -from pychinko.helpers import removedups, convertBNodeToFact +from .pychinko.helpers import removedups, convertBNodeToFact from swap import term, formula from swap.set_importer import Set import time @@ -24,7 +24,7 @@ def reversed(l): yield ll[b] -from pychinko import nodes, rete +from .pychinko import nodes, rete class fullSet(object): def __contains__(self, other): @@ -49,7 +49,7 @@ def __init__(self, workingContext, rulesFormula=None, target=None): #print self.rules self.facts = self.buildFacts(rulesFormula) - print "converting and adding time:", time.time() - t + print("converting and adding time:", time.time() - t) t = time.time() @@ -60,9 +60,9 @@ def __init__(self, workingContext, rulesFormula=None, target=None): #print "add facts time:", time.time() - t t = time.time() self.interp.run() - print "interp.run() time:", time.time() - t + print("interp.run() time:", time.time() - t) - print len(self.interp.inferredFacts), ' inferred fact(s)' + print(len(self.interp.inferredFacts), ' inferred fact(s)') #print "size of inferred facts:", len(self.interp.inferredFacts) # print self.interp.inferredFacts # add the inferred facts back to cwm store @@ -84,7 +84,7 @@ def __init__(self, workingContext, rulesFormula=None, target=None): # self.workingContext.add(*newTriple) # else: # print "contains!" - print "add facts time to cwm:", time.time() - t + print("add facts time to cwm:", time.time() - t) """ print "facts" @@ -101,7 +101,7 @@ def convFromRete(self, t): if not t: return None # print "cnv:", t, type(t) - if isinstance(t,unicode): + if isinstance(t,str): return self.workingContext.newSymbol(t) elif isinstance(t,str): return self.workingContext.newLiteral(t) @@ -266,7 +266,7 @@ def buildFacts(self, indexedFormula): if isinstance(subj, formula.Formula) or \ isinstance(obj, formula.Formula): - print "The RETE cannot process nested formulas at the time - use it for ntriples only" + print("The RETE cannot process nested formulas at the time - use it for ntriples only") # raise NotImplementedError continue diff --git a/query.py b/query.py index 7888a20d..ba1bcee9 100644 --- a/query.py +++ b/query.py @@ -4,26 +4,26 @@ """ QL_NS = "http://www.w3.org/2004/ql#" -from sparql2cwm import SPARQL_NS +from .sparql2cwm import SPARQL_NS -from set_importer import Set, ImmutableSet, sorted +from .set_importer import Set, ImmutableSet, sorted -from RDFSink import Logic_NS, RDFSink, forSomeSym, forAllSym -from RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 -from RDFSink import N3_nil, N3_first, N3_rest, OWL_NS, N3_Empty, N3_List, List_NS -from RDFSink import RDF_NS_URI +from .RDFSink import Logic_NS, RDFSink, forSomeSym, forAllSym +from .RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 +from .RDFSink import N3_nil, N3_first, N3_rest, OWL_NS, N3_Empty, N3_List, List_NS +from .RDFSink import RDF_NS_URI -from OrderedSequence import intersection, minus, indentString +from .OrderedSequence import intersection, minus, indentString -import diag -from diag import chatty_flag, tracking, progress -from term import BuiltIn, LightBuiltIn, RDFBuiltIn, ArgumentNotLiteral, \ +from . import diag +from .diag import chatty_flag, tracking, progress +from .term import BuiltIn, LightBuiltIn, RDFBuiltIn, ArgumentNotLiteral, \ HeavyBuiltIn, Function, ReverseFunction, MultipleFunction, \ MultipleReverseFunction, UnknownType, Env, unify, \ Literal, Symbol, Fragment, FragmentNil, Term, \ CompoundTerm, List, EmptyList, NonEmptyList, ErrorFlag -from formula import StoredStatement, Formula -from why import Because, BecauseBuiltIn, BecauseOfRule, \ +from .formula import StoredStatement, Formula +from .why import Because, BecauseBuiltIn, BecauseOfRule, \ BecauseOfExperience, becauseSubexpression, Reason, \ BecauseSupports, BecauseMerge ,report, Premise, newTopLevelFormula, isTopLevel @@ -82,7 +82,7 @@ def think(knowledgeBase, ruleFormula=None, mode="", why=None): """ if ruleFormula == None: ruleFormula = knowledgeBase - assert knowledgeBase.canonical == None , "Must be open to add stuff:"+ `knowledgeBase ` + assert knowledgeBase.canonical == None , "Must be open to add stuff:"+ repr(knowledgeBase) if diag.chatty_flag > 45: progress("think: rules from %s added to %s" %( knowledgeBase, ruleFormula)) @@ -440,10 +440,10 @@ def replaceWithNil(x): if diag.chatty_flag >20: - progress("New Rule %s ============ looking for:" % `self` ) - for s in self.template.statements: progress(" ", `s`) + progress("New Rule %s ============ looking for:" % repr(self) ) + for s in self.template.statements: progress(" ", repr(s)) progress("=>") - for s in self.conclusion.statements: progress(" ", `s`) + for s in self.conclusion.statements: progress(" ", repr(s)) progress("Universals declared in outer " + seqToString(variables)) progress(" mentioned in template " + seqToString(variablesMentioned)) progress(" also used in conclusion " + seqToString(self.variablesUsed)) @@ -521,8 +521,8 @@ def addTriple(self, key, triple): return total def __repr__(self): - if self in self.affects: return "R"+`self.number`+ "*" - return "R"+`self.number` + if self in self.affects: return "R"+repr(self.number)+ "*" + return "R"+repr(self.number) def compareByAffects(other): if other in self.indirectlyAffects: return -1 # Do me earlier @@ -570,10 +570,10 @@ def testIncludes(f, g, _variables=Set(), bindings={}, interpretBuiltins = 0): if diag.chatty_flag > 20: progress( "# testIncludes BUILTIN, %i terms in template %s, %i unmatched, %i template variables" % ( len(g.statements), - `g`[-8:], len(unmatched), len(templateExistentials))) + repr(g)[-8:], len(unmatched), len(templateExistentials))) if diag.chatty_flag > 80: for v in _variables: - progress( " Variable: " + `v`[-8:]) + progress( " Variable: " + repr(v)[-8:]) result = Query(f.store, unmatched=unmatched, @@ -583,7 +583,7 @@ def testIncludes(f, g, _variables=Set(), bindings={}, interpretBuiltins = 0): existentials=_variables | templateExistentials | more_variables, justOne=1, mode="").resolve() - if diag.chatty_flag >30: progress("=================== end testIncludes =" + `result`) + if diag.chatty_flag >30: progress("=================== end testIncludes =" + repr(result)) return result @@ -620,10 +620,10 @@ def n3Equivalent(g, f, env1, env2, vars=Set([]), if diag.chatty_flag > 20: progress( "# testEqual BUILTIN, %i terms in template %s, %i unmatched, %i template variables" % ( len(g.statements), - `g`[-8:], len(unmatched), len(templateExistentials))) + repr(g)[-8:], len(unmatched), len(templateExistentials))) if diag.chatty_flag > 80: for v in vars: - progress( " Variable: " + `v`[-8:]) + progress( " Variable: " + repr(v)[-8:]) result = Query(f.store, unmatched=unmatched, @@ -634,10 +634,10 @@ def n3Equivalent(g, f, env1, env2, vars=Set([]), existentials= templateExistentials , justReturn=1, mode="").resolve() - if diag.chatty_flag >30: progress("=================== end n3Equivalent =" + `result`) + if diag.chatty_flag >30: progress("=================== end n3Equivalent =" + repr(result)) if not result: result = [] for x in result: - for k, (v, source) in x.items(): + for k, (v, source) in list(x.items()): env1 = env1.bind(k, (v, env2.id)) yield env1, env2 ## return [(x, None) for x in result] @@ -726,7 +726,7 @@ def __repr__(self): def returnWrapper(f): def g(*args, **keywords): retVal = f(*args, **keywords) - progress('%s() returns %s' % (f.func_name, retVal)) + progress('%s() returns %s' % (f.__name__, retVal)) return retVal return g @@ -784,7 +784,7 @@ def __init__(self, if not item.setup(allvars=variables|existentials, unmatched=unmatched, interpretBuiltins=interpretBuiltins, mode=mode): if diag.chatty_flag > 80: progress( - "match: abandoned, no way for "+`item`) + "match: abandoned, no way for "+repr(item)) self.noWay = 1 return # save time if not item.builtIn: @@ -815,7 +815,7 @@ def checkRedirectsInAlready(self): def redirect(self, redirections): for bindings in self.already: - for var, value in bindings.items(): + for var, value in list(bindings.items()): try: x = redirections[value] except: @@ -854,7 +854,7 @@ def conclude(self, bindings, evidence = [], extraBNodes = Set(), allBindings=Non if diag.tracking: if allBindings is None: allBindings = bindings - for loc in xrange(len(evidence)): + for loc in range(len(evidence)): r = evidence[loc] if isinstance(r, BecauseSupportsWill): @@ -863,7 +863,7 @@ def conclude(self, bindings, evidence = [], extraBNodes = Set(), allBindings=Non [[k for k in evidence if isinstance(k, (StoredStatement, Reason))]])) if isinstance(r, BecauseBuiltInWill): evidence[loc] = BecauseBuiltIn(*[smarterSubstitution(k, allBindings, - r.args[0], why=Because("I include it: " + k.debugString() + `allBindings`)) for k in r.args[1:]]) + r.args[0], why=Because("I include it: " + k.debugString() + repr(allBindings))) for k in r.args[1:]]) reason = BecauseOfRule(self.rule, bindings=allBindings, knownExistentials = extraBNodes, evidence=evidence, kb=self.workingContext) # progress("We have a reason for %s of %s with bindings %s" % (self.rule, reason, alBindings)) @@ -871,7 +871,7 @@ def conclude(self, bindings, evidence = [], extraBNodes = Set(), allBindings=Non reason = None es, exout = (self.workingContext.existentials() | extraBNodes), Set() #self.workingContext.existentials() | - for var, (val, source) in bindings.items(): + for var, (val, source) in list(bindings.items()): if isinstance(val, Exception): if "q" in self.mode: # How nice are we? raise ValueError(val) @@ -916,8 +916,8 @@ def conclude(self, bindings, evidence = [], extraBNodes = Set(), allBindings=Non else: if diag.chatty_flag > 25: s = s + (", (%s is existential in kb)"%v) if diag.chatty_flag > 25: - progress("Variables regenerated: universal " + `poss` - + " existential: " +`self.conclusion.existentials()` + s) + progress("Variables regenerated: universal " + repr(poss) + + " existential: " +repr(self.conclusion.existentials()) + s) if diag.chatty_flag>19: @@ -999,11 +999,11 @@ def matchFormula(query, queue, variables, existentials, env=Env(), evidence=[]): if diag.chatty_flag > 20 : progress("@@@@ Warning: query can't find term which will work.") progress( " state is %s, queue length %i" % (state, len(queue)+1)) - progress("@@ Current item: %s" % `item`) + progress("@@ Current item: %s" % repr(item)) progress(queueToString(queue)) continue # Forget it else: - raise RuntimeError, "Unknown state " + `state` + raise RuntimeError("Unknown state " + repr(state)) stack_extent = [] @@ -1021,7 +1021,7 @@ def matchFormula(query, queue, variables, existentials, env=Env(), evidence=[]): continue if isinstance(reason, StoredStatement): if True or reason[CONTEXT] is not query.workingContext: - for m in nb.values(): + for m in list(nb.values()): if isinstance(m, tuple): m = m[0] if m in reason[CONTEXT].existentials(): @@ -1087,18 +1087,18 @@ def matchFormula2(query, assert isinstance(newBindings, Env), 'env is an %s, not an %s' % (newBindings.__class__, Env) if diag.chatty_flag > 59: progress( "QUERY2: called %i terms, %i bindings %s, (new: %s)" % - (len(queue), len(bindings), `bindings`, - `newBindings`)) + (len(queue), len(bindings), repr(bindings), + repr(newBindings))) if diag.chatty_flag > 90: progress( queueToString(queue)) - newBindingItems = newBindings.items() + newBindingItems = list(newBindings.items()) while newBindingItems: # Take care of business left over from recursive call pair = newBindingItems.pop(0) if isinstance(pair[1], tuple): pair = (pair[0], pair[1][0]) else: raise RuntimeError - if diag.chatty_flag>95: progress(" new binding: %s -> %s" % (`pair[0]`, `pair[1]`)) + if diag.chatty_flag>95: progress(" new binding: %s -> %s" % (repr(pair[0]), repr(pair[1]))) if pair[0] in variables: variables.remove(pair[0]) bindings = bindings.newBinding(pair[0], (pair[1], None)) @@ -1123,19 +1123,19 @@ def matchFormula2(query, reallyNewBindings = reallyNewBindingsList[0][0] #We don't deal # with multiple ways to bind except: - print 'we lost' - print pair[0], pair[1] + print('we lost') + print(pair[0], pair[1]) a = pair[0].unify( pair[1], variables, existentials, bindings) - print a - print a[0] - print a[0][0] + print(a) + print(a[0]) + print(a[0][0]) raise - newBindingItems.extend(reallyNewBindings.items()) + newBindingItems.extend(list(reallyNewBindings.items())) newBindings = newBindings.update2(reallyNewBindings) else: if diag.chatty_flag > 40: # Reasonable - progress("Not in existentials or variables but now bound:", `pair[0]`) + progress("Not in existentials or variables but now bound:", repr(pair[0])) elif diag.tracking: bindings = bindings.newBinding(pair[0], (pair[1], None)) if not isinstance(pair[0], CompoundTerm) and ( # Hack - else rules13.n3 fails @@ pair[0] in existentials): # Hack ... could be bnding from nested expression @@ -1163,7 +1163,7 @@ def matchFormula2(query, item = queue.popBest() if diag.chatty_flag>49: - progress( "Looking at " + `item`) + progress( "Looking at " + repr(item)) progress( "...with vars("+seqToString(variables)+")" + " ExQuVars:("+seqToString(existentials)+")") con, pred, subj, obj = item.quad @@ -1206,13 +1206,13 @@ def matchFormula2(query, if diag.chatty_flag > 20 : progress("@@@@ Warning: query can't find term which will work.") progress( " state is %s, queue length %i" % (state, len(queue)+1)) - progress("@@ Current item: %s" % `item`) + progress("@@ Current item: %s" % repr(item)) progress(queueToString(queue)) return total # Forget it else: - raise RuntimeError, "Unknown state " + `state` + raise RuntimeError("Unknown state " + repr(state)) - if diag.chatty_flag > 90: progress("nbs=" + `nbs`) + if diag.chatty_flag > 90: progress("nbs=" + repr(nbs)) # query.py Removed as I added this and wasn't sure whether it works with justReturn change below -tbl # # Optimization when sucess but no bindings @@ -1222,7 +1222,7 @@ def matchFormula2(query, # continue # Loop around and do the next one. optimization. for nb, reason in nbs: - assert isinstance(nb,types.DictType), nb + assert isinstance(nb,dict), nb q2 = Queue([], queue) if query.justReturn: ### What does the following do? @@ -1235,7 +1235,7 @@ def matchFormula2(query, continue if isinstance(reason, StoredStatement): if True or reason[CONTEXT] is not query.workingContext: - for m in nb.values(): + for m in list(nb.values()): if isinstance(m, tuple): m = m[0] if m in reason[CONTEXT].existentials(): @@ -1264,14 +1264,14 @@ def matchFormula2(query, queue.append(item) # And loop back to take the next item - if diag.chatty_flag>50: progress("QUERY MATCH COMPLETE with bindings: " + `bindings`) + if diag.chatty_flag>50: progress("QUERY MATCH COMPLETE with bindings: " + repr(bindings)) if query.justReturn: try: len(queue.statements) len(query.workingContext) except: - print type(queue.statements) - print type(query.workingContext) + print(type(queue.statements)) + print(type(query.workingContext)) raise if len(queue.statements) != len(query.workingContext): return total @@ -1290,13 +1290,13 @@ def remoteQuery(query, items): progress(" Remote service %s" % (items)) serviceURI = items[0].service.uri if serviceURI.startswith("http:"): - from sparql.sparqlClient import SparqlQuery + from .sparql.sparqlClient import SparqlQuery return SparqlQuery(query, items, serviceURI) elif not serviceURI.startswith("mysql:"): raise ValueError("Unknown URI scheme for remote query service: %s" % serviceURI) - import dbork.SqlDB - from dbork.SqlDB import ResultSet, SqlDBAlgae, ShowStatement + from . import dbork.SqlDB + from .dbork.SqlDB import ResultSet, SqlDBAlgae, ShowStatement # SqlDB stores results in a ResultSet. rs = ResultSet() @@ -1310,7 +1310,7 @@ def remoteQuery(query, items): items[0].service.uri).groups() # Look for one of a set of pre-compiled rdb schemas. HostDB2SchemeMapping = { "mysql://root@localhost/w3c" : "AclSqlObjects" } - if (HostDB2SchemeMapping.has_key(items[0].service.uri)): + if (items[0].service.uri in HostDB2SchemeMapping): cachedSchema = HostDB2SchemeMapping.get(items[0].service.uri) else: cachedSchema = None @@ -1338,7 +1338,7 @@ def remoteQuery(query, items): boundRow[v] = interned # bindings nbs.append((boundRow, reason)) - if diag.chatty_flag > 10: progress("====> bindings from remote query:"+`nbs`) + if diag.chatty_flag > 10: progress("====> bindings from remote query:"+repr(nbs)) return nbs # No bindings for testing class BetterNone(object): @@ -1410,7 +1410,7 @@ def setup(self, allvars, unmatched, interpretBuiltins=[], mode=""): self.interpretBuiltins = interpretBuiltins self.service = None - if diag.chatty_flag > 800: progress("setup:" + `allvars`) + if diag.chatty_flag > 800: progress("setup:" + repr(allvars)) if "r" in mode: schema = None if "s" in mode: @@ -1461,7 +1461,7 @@ def setup(self, allvars, unmatched, interpretBuiltins=[], mode=""): hasUnboundCoumpundTerm = 1 # Can't search directly self.searchPattern[p] = None # can bind this if we recurse - if diag.chatty_flag > 98: progress(" %s needs to run: %s"%(`x`, `self.neededToRun[p]`)) + if diag.chatty_flag > 98: progress(" %s needs to run: %s"%(repr(x), repr(self.neededToRun[p]))) self.updateMyIndex(con) if isinstance(pred, RDFBuiltIn) or ( @@ -1477,7 +1477,7 @@ def setup(self, allvars, unmatched, interpretBuiltins=[], mode=""): self.state = S_NEED_DEEP # Put off till later than non-deep ones else: self.state = S_NOT_LIGHT # Not a light built in, not searched. - if diag.chatty_flag > 80: progress("setup:" + `self`) + if diag.chatty_flag > 80: progress("setup:" + repr(self)) if self.state == S_DONE: return False return True @@ -1516,7 +1516,7 @@ def doIncludes(item, queue, existentials, variables, bindings): # if isinstance(obj, Formula): obj = obj.n3String() #raise RuntimeError("Cannot do {%s} log:includes {%s} " % (subj, obj)) progress("""Warning: Type error ignored on builtin: - log:includes only on formulae """+`item`) + log:includes only on formulae """+repr(item)) #@@ was RuntimeError exception item.state = S_DONE return nbs @@ -1537,7 +1537,7 @@ def doSupports(item, queue, existentials, variables, bindings): assert subj.canonical != None F = store.any((store._experience, store.cufi, subj, None)) # Cached value? if F != None: - if diag.chatty_flag > 10: progress("Bultin: " + `subj`+ " cached log:conclusion " + `F`) + if diag.chatty_flag > 10: progress("Bultin: " + repr(subj)+ " cached log:conclusion " + repr(F)) else: oldSubj = subj newSubj = subj.renameVars() @@ -1550,7 +1550,7 @@ def doSupports(item, queue, existentials, variables, bindings): # F.collector = reason # proof.append(reason) else: reason = None - if diag.chatty_flag > 10: progress("Bultin: " + `subj`+ " log:conclusion " + `F`) + if diag.chatty_flag > 10: progress("Bultin: " + repr(subj)+ " log:conclusion " + repr(F)) F.loadFormulaWithSubstitution(subj, why=reason, cannon=True) # leave open think(F) F4 = F.close() @@ -1591,7 +1591,7 @@ def doSupports(item, queue, existentials, variables, bindings): if isinstance(obj, Formula): obj = obj.n3String() #raise RuntimeError("Cannot do {%s} log:includes {%s} " % (subj, obj)) progress("""Warning: Type error ignored on builtin: - log:include only on formulae """+`item`) + log:include only on formulae """+repr(item)) #@@ was RuntimeError exception item.state = S_DONE return nbs @@ -1627,7 +1627,7 @@ def tryBuiltin(self, queue, bindings, evidence): except caughtErrors: progress( "Warning: Built-in %s %s %s failed because:\n %s: %s" - % (`subj`, `pred`, `obj`, sys.exc_info()[0].__name__ , + % (repr(subj), repr(pred), repr(obj), sys.exc_info()[0].__name__ , sys.exc_info()[1].__str__() )) if "h" in self.query.mode: raise @@ -1644,7 +1644,7 @@ def tryBuiltin(self, queue, bindings, evidence): except caughtErrors: errVal = ( "Warning: Built-in %s!%s failed because:\n %s: %s" - % (`pred`, `subj`, sys.exc_info()[0].__name__ , + % (repr(pred), repr(subj), sys.exc_info()[0].__name__ , sys.exc_info()[1].__str__() )) progress(errVal) if "h" in self.query.mode: @@ -1690,7 +1690,7 @@ def tryBuiltin(self, queue, bindings, evidence): except caughtErrors: errVal = ( "Warning: Built-in %s^%s failed because:\n %s: %s" - % (`pred`, `obj`, sys.exc_info()[0].__name__ , + % (repr(pred), repr(obj), sys.exc_info()[0].__name__ , sys.exc_info()[1].__str__() )) progress(errVal) if "h" in self.query.mode: @@ -1727,11 +1727,11 @@ def tryBuiltin(self, queue, bindings, evidence): else: return [] if diag.chatty_flag > 30: - progress("Builtin could not give result"+`self`) + progress("Builtin could not give result"+repr(self)) return [] # no solution # @@@ remove dependency on 'heavy' above and remove heavy as param except (IOError, SyntaxError): - raise BuiltInFailed(sys.exc_info(), self, pred ),None + raise BuiltInFailed(sys.exc_info(), self, pred )(None) def tryDeepSearch(self, queue, oldBindings=Env()): """Search the store, unifying nested compound structures @@ -1746,15 +1746,15 @@ def tryDeepSearch(self, queue, oldBindings=Env()): nbs = [] # Assume failure if self.short == INFINITY: if diag.chatty_flag > 36: - progress( "Can't deep search for %s" % `self`) + progress( "Can't deep search for %s" % repr(self)) else: if diag.chatty_flag > 36: - progress( "Searching (S=%i) %i for %s" %(self.state, self.short, `self`)) + progress( "Searching (S=%i) %i for %s" %(self.state, self.short, repr(self))) try: for s in self.myIndex: pass except: - print self.myIndex, self + print(self.myIndex, self) raise for s in self.myIndex : # for everything matching what we know, if self.query.justReturn and s in queue.statements: @@ -1775,8 +1775,8 @@ def tryDeepSearch(self, queue, oldBindings=Env()): else: # Deep case if diag.chatty_flag > 70: progress( "Deep: Unify %s with %s vars=%s; ee=%s" % - (x, s.quad[p], `self.query.variables`[4:-1], - `self.query._existentialVariables`[4:-1])) + (x, s.quad[p], repr(self.query.variables)[4:-1], + repr(self.query._existentialVariables)[4:-1])) for nb1, env3 in x.unify(s.quad[p], nb, env2, self.neededToRun[p]): env_queue.append((nb1, env3)) ## nbs1 = x.unify(s.quad[p], self.neededToRun[p] & self.query.variables, @@ -1835,11 +1835,11 @@ def bindNew(self, newBindings): """ con, pred, subj, obj = self.quad if diag.chatty_flag > 90: - progress(" binding ", `self` + " with "+ `newBindings`) + progress(" binding ", repr(self) + " with "+ repr(newBindings)) q=[con, pred, subj, obj] for p in ALL4: changed = 0 - for var, val in newBindings.items(): + for var, val in list(newBindings.items()): if var in self.neededToRun[p]: self.neededToRun[p].remove(var) changed = 1 @@ -1885,7 +1885,7 @@ def bindNew(self, newBindings): elif self.state == S_LIGHT_WAIT: self.state = S_LIGHT_GO elif self.state == S_HEAVY_WAIT: self.state = S_HEAVY_READY if diag.chatty_flag > 90: - progress("...bound becomes ", `self`) + progress("...bound becomes ", repr(self)) if self.state == S_DONE: return [] return [({}, None)] # continue @@ -1952,7 +1952,7 @@ def __len__(self): return 0 return 1 - def next(self): + def __next__(self): retVal = self.current self.current = self.current.child return retVal.thunk() @@ -1961,9 +1961,9 @@ def run(self, func=None): retVal = None while self: if func is None or retVal is None: - retVal = self.next() + retVal = next(self) else: - retVal = func(retVal, self.next()) + retVal = func(retVal, next(self)) return retVal # This should probably be properties and methods of IndexedFormula @@ -2032,7 +2032,7 @@ def lookupQuadRecursive(bindings, q, why=None): def queueToString(queue): str = "" for item in queue: - str = str + `item` + "\n" + str = str + repr(item) + "\n" return str @@ -2047,29 +2047,29 @@ def quadToString(q, neededToRun=[[],[],[],[]], pattern=[1,1,1,1]): for p in ALL4: n = neededToRun[p] if n == []: qm[p]="" - else: qm[p] = "(" + `n`[5:-2] + ")" # Set([...]) -> (...) + else: qm[p] = "(" + repr(n)[5:-2] + ")" # Set([...]) -> (...) if pattern[p]==None: qm[p]=qm[p]+"?" - return "%s%s :: %8s%s %8s%s %8s%s." %(`q[CONTEXT]`, qm[CONTEXT], - `q[SUBJ]`,qm[SUBJ], - `q[PRED]`,qm[PRED], - `q[OBJ]`,qm[OBJ]) + return "%s%s :: %8s%s %8s%s %8s%s." %(repr(q[CONTEXT]), qm[CONTEXT], + repr(q[SUBJ]),qm[SUBJ], + repr(q[PRED]),qm[PRED], + repr(q[OBJ]),qm[OBJ]) def seqToString(set): # return `set` set = list(set) str = "" for x in set[:-1]: - str = str + `x` + "," + str = str + repr(x) + "," for x in set[-1:]: - str = str+ `x` + str = str+ repr(x) return str def bindingsToString(bindings): # return `bindings` str = "" - for x, y in bindings.items(): - str = str + (" %s->%s " % ( `x`, `y`)) + for x, y in list(bindings.items()): + str = str + (" %s->%s " % ( repr(x), repr(y))) return str class BecauseBuiltInWill(object): @@ -2082,7 +2082,7 @@ def __init__(self, *args): class BuiltInFailed(Exception): def __init__(self, info, item, pred): - progress("BuiltIn %s FAILED" % pred, `info`) + progress("BuiltIn %s FAILED" % pred, repr(info)) self._item = item self._info = info self._pred = pred @@ -2091,9 +2091,9 @@ def __str__(self): reason = indentString(self._info[1].__str__()) # return "reason=" + reason return ("Error during built-in operation\n%s\nbecause:\n%s" % ( - `self._item`, + repr(self._item), # `self._info`)) - `reason`)) + repr(reason))) def hasFormula(l): if not isinstance(l, (List, Formula)): @@ -2105,7 +2105,7 @@ def hasFormula(l): return True return False -from term import AnonymousNode, CompoundTerm +from .term import AnonymousNode, CompoundTerm def smarterSubstitution(f, bindings, source, why=None, exception=[]): bindings = bindings.asDict() @@ -2115,7 +2115,7 @@ def smarterSubstitution(f, bindings, source, why=None, exception=[]): f2 = f.newFormula() newBindings, _ = f2.loadFormulaWithSubstitution(f, bindings, why=Because("I said so #2", why)) if f is not source: - newExistentials = f2.occurringIn(source.existentials().intersection(Set(bindings.values()))) + newExistentials = f2.occurringIn(source.existentials().intersection(Set(list(bindings.values())))) for n in newExistentials: f2.declareExistential(n) ## for k in bindings.values(): diff --git a/rdf2dot.py b/rdf2dot.py index 3abedd89..699ab66a 100644 --- a/rdf2dot.py +++ b/rdf2dot.py @@ -99,7 +99,7 @@ def eachGraph(text, store, it, props, cluster=''): eachNode(text, store, n, props) #@@hmm... node props = graph props? for sub in store.each(subj=it, pred=GV.subgraph): - raise RuntimeError, "subgraph not yet implemented@@" + raise RuntimeError("subgraph not yet implemented@@") text("}\n") def eachNode(text, store, gnode, props): @@ -141,5 +141,5 @@ def main(argv): if __name__ == '__main__': try: main(sys.argv) - except Usage, e: - print >>sys.stderr, e + except Usage as e: + print(e, file=sys.stderr) diff --git a/rdflib2rdf.py b/rdflib2rdf.py index 1f77064a..1a2d3ac0 100644 --- a/rdflib2rdf.py +++ b/rdflib2rdf.py @@ -53,9 +53,9 @@ """ -from RDFSink import FORMULA -import diag -from diag import progress +from .RDFSink import FORMULA +from . import diag +from .diag import progress class ContextSink(object): def __init__(self, sink, openFormula, @@ -82,14 +82,15 @@ def newXMLLiteral(self, s): def newBlankNode(self): return self.sink.newBlankNode(self._context) - def makeStatement(self, (predicate, subject, object)): + def makeStatement(self, xxx_todo_changeme): + (predicate, subject, object) = xxx_todo_changeme self.sink.makeStatement((self._context, predicate, subject, object), why=self._reason2) -import uripath +from . import uripath from rdflib.syntax.parser import ParserDispatcher from rdflib.URIRef import URIRef @@ -136,9 +137,10 @@ def __convert(self, t): else: raise Exception("Unexpected type") - def add(self, (s, p, o)): + def add(self, xxx_todo_changeme1): """Add triple to the sink (rdflib.syntax.parser.Parser calls this method).""" + (s, p, o) = xxx_todo_changeme1 subject = self.__convert(s) predicate = self.__convert(p) object = self.__convert(o) diff --git a/rdflib_user.py b/rdflib_user.py index e8954a9c..a2727ad4 100644 --- a/rdflib_user.py +++ b/rdflib_user.py @@ -22,9 +22,9 @@ def __call__(self, source, format): from rdflib.URIRef import URIRef from rdflib.BNode import BNode from rdflib.Literal import Literal -from RDFSink import FORMULA, ANONYMOUS, SYMBOL -import diag -from diag import progress +from .RDFSink import FORMULA, ANONYMOUS, SYMBOL +from . import diag +from .diag import progress class rdflib_handoff: @@ -52,10 +52,11 @@ def prefix_mapping(self, prefix, uri, override=False): def feed(self, buffer): self.parser(StringInputSource(buffer), self.format) - def add(self, (subject, predicate, object)): + def add(self, xxx_todo_changeme): # print subject, ", a ", type(subject) # print '---- has the property of ', predicate, ', of type ', type(predicate) # print '---- with the value of ', object, ', of type ', type(object), '.' + (subject, predicate, object) = xxx_todo_changeme self.store.makeStatement((self.formula, self.convertRDFlibTypes(predicate), self.convertRDFlibTypes(subject), @@ -63,7 +64,7 @@ def add(self, (subject, predicate, object)): return self def close(self): - for prefix, uri in self.prefix_ns_map.items(): + for prefix, uri in list(self.prefix_ns_map.items()): if prefix == None: prefix = "" if ':' not in uri: uri = self.asIfFrom + uri diff --git a/rdfn3_yapps.py b/rdfn3_yapps.py index 9f25373c..6c5d4841 100644 --- a/rdfn3_yapps.py +++ b/rdfn3_yapps.py @@ -17,8 +17,8 @@ import string -import uripath -from ConstTerm import Symbol, StringLiteral, Namespace +from . import uripath +from .ConstTerm import Symbol, StringLiteral, Namespace RDF = Namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#') LIST = Namespace("http://www.daml.org/2001/03/daml+oil#") @@ -28,7 +28,7 @@ from string import * import re -from yappsrt import * +from .yappsrt import * class _ParserScanner(Scanner): def __init__(self, str): @@ -264,7 +264,7 @@ def qname(self, str): try: ns = self._prefixes[pfx] except: - raise BadSyntax, "prefix %s not bound" % pfx + raise BadSyntax("prefix %s not bound" % pfx) else: return Symbol(ns + ln) @@ -299,7 +299,7 @@ def intLit(self, str): try: v = int(str) except ValueError: - v = long(str) + v = int(str) return IntegerLiteral(v) #@@ def bindListPrefix(self): diff --git a/rdfn3_yappstest.py b/rdfn3_yappstest.py index 052eea23..adace7b5 100644 --- a/rdfn3_yappstest.py +++ b/rdfn3_yappstest.py @@ -1,5 +1,5 @@ -import rdfn3_yapps -import notation3 +from . import rdfn3_yapps +from . import notation3 import sys @@ -23,7 +23,7 @@ def test(text): gen.endDoc() def testKIF(text, addr): - import KIFSink + from . import KIFSink gen = KIFSink.Sink(sys.stdout.write) p = Parser(gen, addr) gen.startDoc() diff --git a/rdfpath.py b/rdfpath.py index 51ec0440..90ca3502 100644 --- a/rdfpath.py +++ b/rdfpath.py @@ -43,8 +43,8 @@ wrap rdfstore22.Store()'s node() function """ -from __future__ import generators -from cStringIO import StringIO + +from io import StringIO from ntriples import serialize class NameMapper: @@ -231,7 +231,7 @@ def node(self, **kwargs): myURI = None todo = [] - for (key, value) in kwargs.iteritems(): + for (key, value) in kwargs.items(): predicateURI = self.nameMapper.externalize(key) if predicateURI == uri: myURI = value @@ -429,10 +429,10 @@ def getValues(self): ## return longer multi-path if __name__ =='__main__': - print "Performing doctest..." + print("Performing doctest...") import doctest, sys doctest.testmod(sys.modules[__name__]) - print "Done." + print("Done.") # $Log$ # Revision 1.2 2003-08-01 15:45:10 sandro diff --git a/rdfx2kif.py b/rdfx2kif.py index 18c712d0..c63df371 100644 --- a/rdfx2kif.py +++ b/rdfx2kif.py @@ -20,9 +20,9 @@ import os, sys -import KIFSink -import sax2rdf -import xml2rdf # if you want the older RDF parser that doesn't require python2 +from . import KIFSink +from . import sax2rdf +from . import xml2rdf # if you want the older RDF parser that doesn't require python2 def convert(text, addr, outFp): gen = KIFSink.Sink(outFp) diff --git a/rdfxml.py b/rdfxml.py index 7a47b011..7aa9e190 100644 --- a/rdfxml.py +++ b/rdfxml.py @@ -11,9 +11,9 @@ def rdfxmlparser(store, openFormula, thisDoc=None, flags="", why=None, parser='sax2rdf'): if parser == 'rdflib': - import rdflib_user + from . import rdflib_user return rdflib_user.rdflib_handoff(store, openFormula,thisDoc, why=why) else: # parser == sax2xml - import sax2rdf + from . import sax2rdf return sax2rdf.RDFXMLParser(store, openFormula, thisDoc=thisDoc, flags=flags, why=why) diff --git a/reify.py b/reify.py index a4a3f2a5..cadbfb0b 100644 --- a/reify.py +++ b/reify.py @@ -9,16 +9,17 @@ or a new one, depending on the function called. $Id$ """ -from term import BuiltIn, LightBuiltIn, LabelledNode, \ +from .term import BuiltIn, LightBuiltIn, LabelledNode, \ HeavyBuiltIn, Function, ReverseFunction, AnonymousNode, \ Literal, Symbol, Fragment, FragmentNil, Term,\ CompoundTerm, List, EmptyList, NonEmptyList, N3Set -from formula import Formula, StoredStatement -from RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4, RDF_type_URI -import uripath -import diag -from mixin import Mixin -from set_importer import Set +from .formula import Formula, StoredStatement +from .RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4, RDF_type_URI +from . import uripath +from . import diag +from .mixin import Mixin +from .set_importer import Set +from functools import reduce reifyNS = 'http://www.w3.org/2004/06/rei#' REIFY_NS = reifyNS @@ -314,7 +315,7 @@ def dereification(x, f, sink, bnodes={}, xList=[]): xList.append(x) if x == None: - raise ValueError, "Can't dereify nothing. Suspect missing information in reified form." + raise ValueError("Can't dereify nothing. Suspect missing information in reified form.") y = f.the(subj=x, pred=rei["uri"]) if y != None: return sink.newSymbol(y.value()) @@ -338,7 +339,7 @@ def dereification(x, f, sink, bnodes={}, xList=[]): xList.append(uset) ulist = uset #f.the(subj=uset, pred=f.newSymbol(owlOneOf)) xList.append(ulist) - from diag import progress + from .diag import progress if diag.chatty_flag > 54: progress("universals = ",ulist) for v in ulist: @@ -371,4 +372,4 @@ def dereification(x, f, sink, bnodes={}, xList=[]): bnodes[x] = z return z - raise ValueError, "Can't dereify %s - no clues I understand in %s" % (x, f) + raise ValueError("Can't dereify %s - no clues I understand in %s" % (x, f)) diff --git a/sax2rdf.py b/sax2rdf.py index db02532c..b2f87e71 100644 --- a/sax2rdf.py +++ b/sax2rdf.py @@ -16,7 +16,7 @@ Python/XML HOWTO The Python/XML Special Interest Group - xml-sig@python.org + xml-sig@python.org (edited by amk1@bigfoot.com) http://py-howto.sourceforge.net/xml-howto/xml-howto.html @@ -28,7 +28,7 @@ RDF grammar http://www.w3.org/TR/rdf-syntax-grammar/ esp sections 6 and 7 - + ##################################### SAX pointers First hit on Python SAX parser http://www.gca.org/papers/xmleurope2000/papers/s28-04.html#N84395 @@ -73,10 +73,10 @@ import string import sys -import uripath -from why import BecauseOfData -import isXML -import diag +from . import uripath +from .why import BecauseOfData +from . import isXML +from . import diag #from webAccess import urlopenForRDF # http://www.w3.org/2000/10/swap/ import xml.sax # PyXML stuff @@ -94,11 +94,11 @@ XMLLiteralsAsDomTrees = 1 -if XMLLiteralsAsDomTrees: +if XMLLiteralsAsDomTrees: import xml.dom # for XMLLiterals -import RDFSink -from RDFSink import FORMULA, ANONYMOUS, NODE_MERGE_URI +from . import RDFSink +from .RDFSink import FORMULA, ANONYMOUS, NODE_MERGE_URI # States: @@ -116,22 +116,22 @@ DAML_ONT_NS = "http://www.daml.org/2000/10/daml-ont#" # DAML early version DPO_NS = "http://www.daml.org/2001/03/daml+oil#" # DAML plus oil -OWL_NS = "http://www.w3.org/2002/07/owl#" # OWL +OWL_NS = "http://www.w3.org/2002/07/owl#" # OWL List_NS = RDF_NS_URI # Changed 200308 XML_NS_URI = "http://www.w3.org/XML/1998/namespace" -from diag import verbosity, progress, tracking -from set_importer import Set +from .diag import verbosity, progress, tracking +from .set_importer import Set _nextId = 0 # For generation of arbitrary names for anonymous nodes -coreSyntaxTerms = ['RDF', 'ID', 'about', 'parseType', 'resource', 'nodeID', 'datatype'] +coreSyntaxTerms = ['RDF', 'ID', 'about', 'parseType', 'resource', 'nodeID', 'datatype'] syntaxTerms = coreSyntaxTerms + ['Description', 'li'] -oldTerms = ['aboutEach', 'aboutEachPrefix', 'bagID'] +oldTerms = ['aboutEach', 'aboutEachPrefix', 'bagID'] nodeElementExceptions = {} for a in coreSyntaxTerms + oldTerms + ['li',]: @@ -190,7 +190,7 @@ def __init__(self, sink, openFormula, thisDoc, baseURI=None, flags="", why=None) self._items = [] # for containers self._litDepth = 0 self._usedIDs = Set() - + version = "$Id$" # self.sink.makeComment("RDF parsed by "+version[1:-1]) @@ -207,7 +207,7 @@ def characters(self, data): if self._state == STATE_VALUE or \ self._state == STATE_LITERAL: self.testdata += data - + def flush(self): data = self.testdata @@ -240,10 +240,10 @@ def idAboutAttr(self, attrs): #MS1.0 6.5 also proprAttr 6.10 self._state = STATE_DESCRIPTION self._items.append(0) properties = [] - - for name, value in attrs.items(): + + for name, value in list(attrs.items()): ns, ln = name - + # The following section was a kludge to work with presumably old bad RDF # files while RDF was being defined way back when. # if ns: # Removed 2010 as this is a kludge which creaks with sioc:about - timbl 2010-07-19 @@ -261,18 +261,18 @@ def idAboutAttr(self, attrs): #MS1.0 6.5 also proprAttr 6.10 raise BadSyntax(sys.exc_info(), "Two elements cannot have the same ID, %s" % value) self._usedIDs.add((self._base, value)) if self._subject: - print "# oops - subject already", self._subject + print("# oops - subject already", self._subject) raise BadSyntax(sys.exc_info(), ">1 subject") self._subject = self.sink.newSymbol(self.uriref("#" + value)) elif ln == "about": if self._subject: raise BadSyntax(sys.exc_info(), "Subject already defined to be %s, can't have attribute about='%s'" % - (`self._subject`, value)) + (repr(self._subject), value)) self._subject = self.sink.newSymbol(self.uriref(value)) elif ln == "nodeID": if self._subject: raise BadSyntax(sys.exc_info(), "Subject already defined to be %s, can't have attribute nodeID='%s'" % - (`self._subject`, value)) + (repr(self._subject), value)) if not isXML.isNCName(value): raise BadSyntax(sys.exc_info(), 'A nodeID must be a NCName %s' % value) s = self._nodeIDs.get(value, None) @@ -310,7 +310,7 @@ def idAboutAttr(self, attrs): #MS1.0 6.5 also proprAttr 6.10 else: # Property attribute propAttr #MS1.0 6.10 uri = (ns + ln); - properties.append((uri, value)) + properties.append((uri, value)) # print "@@@@@@ <%s> <%s>" % properties[-1] if self._subject == None: @@ -330,14 +330,14 @@ def idAboutAttr(self, attrs): #MS1.0 6.5 also proprAttr 6.10 self._subject, self.sink.newLiteral(obj, dt, lang) ), why=self._reason2) - + def _nodeElement(self, tagURI, attrs): #MS1.0 6.2 if tagURI == RDF_NS_URI + "Description": - self.idAboutAttr(attrs) # Set up subject and context + self.idAboutAttr(attrs) # Set up subject and context elif tagURI in nodeElementExceptions: - raise ValueError, "%s as typednode not implemented" % tagURI + raise ValueError("%s as typednode not implemented" % tagURI) else: # Unknown tag within STATE_NO_SUBJECT: typedNode #MS1.0 6.13 c = self._context # (Might be changed in idAboutAttr) #@@DC: huh? self.idAboutAttr(attrs) @@ -378,8 +378,8 @@ def _propertyAttr(self, ns, name, value): obj), why=self._reason2) self._state = STATE_NOVALUE # NOT looking for value return - - + + def startPrefixMapping(self, prefix, uri): """Performance note: @@ -417,23 +417,23 @@ def startElementNS(self, name, qname, attrs): if self._state != STATE_LITERAL: self.flush() self.bnode = None - + tagURI = ((name[0] or "") + name[1]) if verbosity() > 80: - indent = ". " * len(self._stack) + indent = ". " * len(self._stack) if not attrs: progress(indent+'# State was', self._state, ', start tag: <' + tagURI + '>') else: str = '# State =%s, start tag= <%s ' %( self._state, tagURI) - for name, value in attrs.items(): - str = str + " " + `name` + '=' + '"' + `value` + '"' + for name, value in list(attrs.items()): + str = str + " " + repr(name) + '=' + '"' + repr(value) + '"' progress(indent + str + '>') self._stack.append([self._state, self._context, self._predicate, self._subject, self._delayedStatement, self._base]) - + self._delayedStatement = None self._base = uripath.join(self._base, attrs.get((XML_NS_URI, "base"), self._base)) @@ -444,8 +444,8 @@ def startElementNS(self, name, qname, attrs): tagURI = uripath.join(self._base, tagURI) # If relative, make absolute. Not needed for standard. except ValueError: pass - # Needed for portable RDF generated with --rdf=z - + # Needed for portable RDF generated with --rdf=z + self._language = attrs.get((XML_NS_URI, "lang"), None) value = attrs.get((RDF_NS_URI, "datatype"), None) @@ -461,7 +461,7 @@ def startElementNS(self, name, qname, attrs): else: self._nodeElement(tagURI, attrs) # Parse it as RDF. # http://www.w3.org/2000/10/rdf-tests/rdfcore/rdf-element-not-mandatory/test001.rdf - + elif self._state == STATE_NOT_RDF: if tagURI == RDF_NS_URI + "RDF" and "T" in self.flags: self._state = STATE_NO_SUBJECT @@ -470,7 +470,7 @@ def startElementNS(self, name, qname, attrs): elif self._state == STATE_NO_SUBJECT: #MS1.0 6.2 obj :: desription | container self._nodeElement(tagURI, attrs) - + elif self._state == STATE_DESCRIPTION: # Expect predicate (property) PropertyElt # propertyElt #MS1.0 6.12 # http://www.w3.org/2000/03/rdf-tracking/#rdf-containers-syntax-ambiguity @@ -480,24 +480,24 @@ def startElementNS(self, name, qname, attrs): self._items[-1] = item else: if tagURI in propertyElementExceptions: - raise BadSyntax(sys.exc_info(), 'Invalid predicate URI: %s' % tagURI) + raise BadSyntax(sys.exc_info(), 'Invalid predicate URI: %s' % tagURI) self._predicate = self.sink.newSymbol(tagURI) self._state = STATE_VALUE # May be looking for value but see parse type # self._datatype = None # self._language = None self.testdata = "" # Flush value data - + # print "\n attributes:", `attrs` properties = [] gotSubject = 0 haveResource = 0 haveParseType = 0 haveExtras = 0 - for name, value in attrs.items(): + for name, value in list(attrs.items()): ns, name = name if name == "ID": - print "# Warning: ID=%s on statement ignored" % (value) # I consider these a bug + print("# Warning: ID=%s on statement ignored" % (value)) # I consider these a bug raise ValueError("ID attribute? Reification not supported.") elif name == "parseType": haveParseType = 1 @@ -512,7 +512,7 @@ def startElementNS(self, name, qname, attrs): self.idAboutAttr(attrs) #@@ not according to current syntax @@@@@@@@@@@ self.sink.makeStatement(( c, self._predicate, s, self._subject), why=self._reason2) self._state = STATE_DESCRIPTION # Nest description - + elif value == "Quote": c = self._context s = self._subject @@ -526,7 +526,7 @@ def startElementNS(self, name, qname, attrs): self._context = self._subject self._subject = None self._state = STATE_NO_SUBJECT # Inside quote, there is no subject - + elif (value=="Collection" or value[-11:] == ":collection"): # Is this a daml:collection qname? @@ -561,7 +561,7 @@ def startElementNS(self, name, qname, attrs): elif name == "resource": haveResource = 1 assert not gotSubject - x = self.sink.newSymbol(self.uriref(value)) + x = self.sink.newSymbol(self.uriref(value)) self.sink.makeStatement((self._context, self._predicate, self._subject, @@ -586,12 +586,12 @@ def startElementNS(self, name, qname, attrs): obj ), why=self._reason2) self._state = STATE_NOVALUE # NOT looking for value self._subject = obj - + for ns, name, value in properties: self._propertyAttr(ns, name, value) - + elif self._state == STATE_LIST: # damlCollection :: objs - make list - # Subject and predicate are set and dangling. + # Subject and predicate are set and dangling. c = self._context s = self._subject # The tail of the list so far p = self._predicate @@ -599,8 +599,8 @@ def startElementNS(self, name, qname, attrs): self.sink.makeStatement(( c, # Link in new pair p, s, - pair ), why=self._reason2) - self.idAboutAttr(attrs) # set subject (the next item) and context + pair ), why=self._reason2) + self.idAboutAttr(attrs) # set subject (the next item) and context if tagURI != RDF_NS_URI + "Description": self.sink.makeStatement((c, self.sink.newSymbol(RDF_NS_URI @@ -618,23 +618,23 @@ def startElementNS(self, name, qname, attrs): self.sink.newSymbol(RDF_NS_URI + "type"), self.sink.newSymbol(List_NS + "List"), self._subject), why=self._reason2) # new item - + self._stack[-1][2] = self.sink.newSymbol(List_NS + "rest") # Leave dangling link #@check self._stack[-1][3] = pair # Underlying state tracks tail of growing list - + elif self._state == STATE_VALUE: # Value :: Obj in this case #MS1.0 6.17 6.2 c = self._context p = self._predicate s = self._subject self._nodeElement(tagURI, attrs) # Parse the object thing's attributes self.sink.makeStatement((c, p, s, self._subject), why=self._reason2) - + self._stack[-1][0] = STATE_NOVALUE # When we return, cannot have literal now elif self._state == STATE_NOVALUE: str = "" - for e in self._stack: str = str + `e`+"\n" + for e in self._stack: str = str + repr(e)+"\n" raise BadSyntax(sys.exc_info(), """Expected no value, found name=%s; qname=%s, attrs=%s in nested context:\n%s""" %(name, qname, attrs, str)) @@ -647,9 +647,9 @@ def startElementNS(self, name, qname, attrs): self.literal_element_start(name, qname, attrs) #@@ need to capture the literal else: - raise RuntimeError, ("Unknown state in RDF parser", self._stack) # Unknown state + raise RuntimeError("Unknown state in RDF parser", self._stack) # Unknown state -# aboutEachprefix { <#> forall r . { r startsWith ppp } l:implies ( zzz } ) +# aboutEachprefix { <#> forall r . { r startsWith ppp } l:implies ( zzz } ) # aboutEach { <#> forall r . { ppp rdf:li r } l:implies ( zzz } ) @@ -657,47 +657,47 @@ def endElementNS(self, name, qname): """Handle end element event """ if verbosity() > 80: - indent = "- " * len(self._stack) - progress(indent+'# End %s, State was'%name[1], self._state, ", delayed was ", `self._delayedStatement`) + indent = "- " * len(self._stack) + progress(indent+'# End %s, State was'%name[1], self._state, ", delayed was ", repr(self._delayedStatement)) if self._delayedStatement == 1: - if verbosity() > 80: progress("Delayed subject "+`self._subject`) + if verbosity() > 80: progress("Delayed subject "+repr(self._subject)) self._stack[-1][3] = self._stack[-1][3].close() if self._state == STATE_LITERAL: self._litDepth = self._litDepth - 1 if self._litDepth == 0: buf = self.testdata - if XMLLiteralsAsDomTrees: - e = self.domDocument.documentElement.firstChild - if e is None: - raise ValueError("Weird: " + `self.domDocument.documentElement`) - # progress('e is '+`e`) - while e.nodeType == e.TEXT_NODE: - e = e.nextSibling - #progress("@@@ e=", e, e.nodeName) - self.domElement = e # Leave for literal parser to pick up - if self.sink: - self.sink.makeStatement(( self._context, - self._predicate, - self._subject, - self.sink.newXMLLiteral(e) ), - why=self._reason2) - else: - self._datatype = self.sink.newSymbol("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral") - self.sink.makeStatement(( self._context, - self._predicate, - self._subject, - self.sink.newLiteral(buf, self._datatype) ), why=self._reason2) - self.testdata = "" + if XMLLiteralsAsDomTrees: + e = self.domDocument.documentElement.firstChild + if e is None: + raise ValueError("Weird: " + repr(self.domDocument.documentElement)) + # progress('e is '+`e`) + while e.nodeType == e.TEXT_NODE: + e = e.nextSibling + #progress("@@@ e=", e, e.nodeName) + self.domElement = e # Leave for literal parser to pick up + if self.sink: + self.sink.makeStatement(( self._context, + self._predicate, + self._subject, + self.sink.newXMLLiteral(e) ), + why=self._reason2) + else: + self._datatype = self.sink.newSymbol("http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral") + self.sink.makeStatement(( self._context, + self._predicate, + self._subject, + self.sink.newLiteral(buf, self._datatype) ), why=self._reason2) + self.testdata = "" + +# eh? else: + if XMLLiteralsAsDomTrees: + self.literal_element_end_DOM(name, qname) + else: + self.literal_element_end(name, qname) + self._stack.pop() + return # don't pop state - else: - if XMLLiteralsAsDomTrees: - self.literal_element_end_DOM(name, qname) - else: - self.literal_element_end(name, qname) - self._stack.pop() - return # don't pop state - elif self._state == STATE_VALUE: buf = self.testdata if self._datatype == None: # RDFCore changes 2003 - can't have dt and lang @@ -711,7 +711,7 @@ def endElementNS(self, name, qname): self._subject, obj), why=self._reason2) self.testdata = "" - + elif self._state == STATE_LIST: self.sink.makeStatement(( self._context, self.sink.newSymbol(List_NS + "rest"), @@ -725,8 +725,8 @@ def endElementNS(self, name, qname): self._state == STATE_NOT_RDF: # akuchlin@mems-exchange.org 2002-09-11 pass else: - raise RuntimeError, ("Unknown RDF parser state '%s' in end tag" % self._state, self._stack) - + raise RuntimeError("Unknown RDF parser state '%s' in end tag" % self._state, self._stack) + # c1 = self._context # if self._subject is c1 and self_context is not c1: # self._subject = self._subject.close() # close before use @@ -769,7 +769,7 @@ def literal_element_start(self, name, qname, attrs): self.testdata += "<%s:%s" % (prefix, name[1]) else: self.testdata += "<%s" % name[1] - for ns in [name] + attrs.keys(): + for ns in [name] + list(attrs.keys()): ns = ns[0] if not ns in declared: prefix = nsMap.get(ns, None) @@ -788,18 +788,18 @@ def literal_element_start(self, name, qname, attrs): else: self.testdata += "<%s" % name[1] - for (name, value) in attrs.items(): + for (name, value) in list(attrs.items()): if name[0]: name = declared[name[0]] + ":" + name[1] else: name = name[1] self.testdata += (' %s=%s' % (name, quoteattr(value))) self.testdata += ">" - + def literal_element_start_DOM(self, name, qname, attrs): declared = self.LiteralNS[-1].copy() - declared[u'http://www.w3.org/XML/1998/namespace'] = 'xml' + declared['http://www.w3.org/XML/1998/namespace'] = 'xml' self.LiteralNS.append(declared) nsMap = self._prefixMap[-1] if name[0]: @@ -808,7 +808,7 @@ def literal_element_start_DOM(self, name, qname, attrs): e = self.domDocument.createElementNS(name[0], prefix + ':' + name[1]) else: e = self.domDocument.createElementNS(name[0], name[1]) - for ns in [name] + attrs.keys(): + for ns in [name] + list(attrs.keys()): ns = ns[0] if ns and not ns in declared: prefix = nsMap.get(ns, None) @@ -825,7 +825,7 @@ def literal_element_start_DOM(self, name, qname, attrs): e.setAttribute('xmlns:'+prefix, ns) # use setAttributeNS? What NS? else: # self.testdata += (' xmlns="%s"' % ns) - e.setAttribute('xmlns', ns) + e.setAttribute('xmlns', ns) else: e = self.domDocument.createElement(name[1]) @@ -833,12 +833,12 @@ def literal_element_start_DOM(self, name, qname, attrs): self.domElement = e # progress("@@@ self.domElement.namespaceURI=", self.domElement.namespaceURI) - for (name, value) in attrs.items(): + for (name, value) in list(attrs.items()): if name[0]: e.setAttributeNS(name[0], declared[name[0]] + ":" + name[1], value) else: e.setAttributeNS(None, name[1], value) - + def literal_element_start_DOM_OLD(self, name, qname, attrs): declared = self.LiteralNS[-1].copy() @@ -853,22 +853,22 @@ def literal_element_start_DOM_OLD(self, name, qname, attrs): self.domElement = e # progress("@@@ self.domElement.namespaceURI=", self.domElement.namespaceURI) - for (name, value) in attrs.items(): + for (name, value) in list(attrs.items()): if name[0]: e.setAttributeNS(name[0],name[1], value) else: e.setAttribute(name[1], value) #@@@ Missing prefix on qname #@@@ may need calculating as in the non-dom case, alas. - - def literal_element_end(self, name, qname): + + def literal_element_end(selsf, name, qname): if name[0]: prefix = self._prefixMap[-1][name[0]] if prefix: - end = u"" % (prefix, name[1]) + end = "" % (prefix, name[1]) else: - end = u"" % name[1] + end = "" % name[1] else: - end = u"" % name[1] + end = "" % name[1] self.testdata += end self.LiteralNS.pop() @@ -914,7 +914,7 @@ def loadStream(self, stream): s.setByteStream(stream) try: self._p.parse(s) - except xml.sax._exceptions.SAXException, e: + except xml.sax._exceptions.SAXException as e: # was: raise SyntaxError() which left no info as to what had happened columnNumber = self._p.getColumnNumber() lineNumber = self._p.getLineNumber() @@ -940,7 +940,7 @@ def __init__(self, thisDoc=None, flags="", why=None): why=why) RDFXMLParser.__init__(self, None, None, thisDoc=thisDoc, flags=flags, why=why) - + self._state = STATE_LITERAL self._litDepth = 0 self.LiteralNS = [{}] @@ -955,7 +955,7 @@ def __init__(self, thisDoc=None, flags="", why=None): # self.sink.newSymbol(pred), # self._subject, # self.sink.newSymbol(self.uriref(obj)) ), why=self._reason2) - + @@ -972,11 +972,11 @@ def XMLtoDOM(str): p = XMLDOMParser("foobar:") # Shouldn't need Doc URI etc p.feed(str) return p.domElement - + def test(args = None): import sys, getopt - import notation3 - + from . import notation3 + from time import time if not args: @@ -1001,8 +1001,8 @@ def test(args = None): else: try: f = open(file, 'r') - except IOError, msg: - print file, ":", msg + except IOError as msg: + print(file, ":", msg) sys.exit(1) x = klass(notation3.ToN3(sys.stdout.write), "file:/test.rdf") # test only! @@ -1026,17 +1026,16 @@ def test(args = None): for c in data: p.feed(c, 1) p.close() - except RuntimeError, msg: + except RuntimeError as msg: t1 = time() - print msg + print(msg) if do_time: - print 'total time: %g' % (t1-t0) + print('total time: %g' % (t1-t0)) sys.exit(1) t1 = time() if do_time: - print 'total time: %g' % (t1-t0) + print('total time: %g' % (t1-t0)) if __name__ == '__main__': test() - diff --git a/set_importer.py b/set_importer.py index 5eaa786d..f200212a 100644 --- a/set_importer.py +++ b/set_importer.py @@ -3,26 +3,26 @@ """ -realcmp = cmp -try: - my_sorted = sorted -except NameError: - def my_sorted(iterable, cmp=None, key=None, reverse=False): - m = list(iterable) - if cmp is None and (key is not None or reverse is not None): - cmp = realcmp - if key is not None: - cmp2 = cmp - def cmp(x,y): - return cmp2(key(x), key(y)) - if reverse is not None: - cmp3 = cmp - def cmp(x,y): - return cmp3(y,x) - m.sort(cmp) - return m - -sorted = my_sorted +#realcmp = cmp +#try: +# my_sorted = sorted +#except NameError: +# def my_sorted(iterable, cmp=None, key=None, reverse=False): +# m = list(iterable) +# if cmp is None and (key is not None or reverse is not None): +# cmp = realcmp +# if key is not None: +# cmp2 = cmp +# def cmp(x,y): +# return cmp2(key(x), key(y)) +# if reverse is not None: +# cmp3 = cmp +# def cmp(x,y): +# return cmp3(y,x) +# m.sort(cmp) +# return m +# +# sorted = my_sorted try: @@ -41,7 +41,7 @@ def copy(self): def union(self, other): ret = self._data.copy() - if isinstance(other, BaseSet): + if isinstance(other, BaseSet): ret.update(other._data) return self.__class__(ret) @@ -73,7 +73,7 @@ def union(self, other): ret[transform()] = value return self.__class__(ret) - + def symmetric_difference(self, other): """Return the symmetric difference of two sets as a new set. @@ -86,9 +86,9 @@ def symmetric_difference(self, other): otherdata = other._data except AttributeError: otherdata = Set(other)._data - for elt in ifilterfalse(otherdata.has_key, selfdata): + for elt in filterfalse(otherdata.has_key, selfdata): data[elt] = value - for elt in ifilterfalse(selfdata.has_key, otherdata): + for elt in filterfalse(selfdata.has_key, otherdata): data[elt] = value return self.__class__(data) @@ -97,14 +97,13 @@ def difference(self, other): (I.e. all elements that are in this set and not in the other.) """ - + data = {} try: otherdata = other._data except AttributeError: otherdata = Set(other)._data value = True - for elt in ifilterfalse(otherdata.has_key, self): + for elt in filterfalse(otherdata.has_key, self): data[elt] = value return self.__class__(data) - diff --git a/setup.py b/setup.py index bd7e116b..4849c0fa 100755 --- a/setup.py +++ b/setup.py @@ -21,7 +21,7 @@ lib_dir = get_python_lib() swap_dir = join(lib_dir, "swap") -print "swap dir: "+swap_dir +print("swap dir: "+swap_dir) ##if exists(swap_dir): diff --git a/spark.py b/spark.py index ffe9b4bf..ec655803 100644 --- a/spark.py +++ b/spark.py @@ -1,5 +1,5 @@ # Copyright (c) 1998-2000 John Aycock -# +# # Permission is hereby granted, free of charge, to any person obtaining # a copy of this software and associated documentation files (the # "Software"), to deal in the Software without restriction, including @@ -7,10 +7,10 @@ # distribute, sublicense, and/or sell copies of the Software, and to # permit persons to whom the Software is furnished to do so, subject to # the following conditions: -# +# # The above copyright notice and this permission notice shall be # included in all copies or substantial portions of the Software. -# +# # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. @@ -26,378 +26,378 @@ import string def _namelist(instance): - namelist, namedict, classlist = [], {}, [instance.__class__] - for c in classlist: - for b in c.__bases__: - classlist.append(b) - for name in dir(c): - if not namedict.has_key(name): - namelist.append(name) - namedict[name] = 1 - return namelist + namelist, namedict, classlist = [], {}, [instance.__class__] + for c in classlist: + for b in c.__bases__: + classlist.append(b) + for name in dir(c): + if name not in namedict: + namelist.append(name) + namedict[name] = 1 + return namelist class GenericScanner: - def __init__(self): - pattern = self.reflect() - self.re = re.compile(pattern, re.VERBOSE) - - self.index2func = {} - for name, number in self.re.groupindex.items(): - self.index2func[number-1] = getattr(self, 't_' + name) - - def makeRE(self, name): - doc = getattr(self, name).__doc__ - rv = '(?P<%s>%s)' % (name[2:], doc) - return rv - - def reflect(self): - rv = [] - for name in _namelist(self): - if name[:2] == 't_' and name != 't_default': - rv.append(self.makeRE(name)) - - rv.append(self.makeRE('t_default')) - return string.join(rv, '|') - - def error(self, s, pos): - print "Lexical error at position %s" % pos - raise SystemExit - - def tokenize(self, s): - pos = 0 - n = len(s) - while pos < n: - m = self.re.match(s, pos) - if m is None: - self.error(s, pos) - - groups = m.groups() - for i in range(len(groups)): - if groups[i] and self.index2func.has_key(i): - self.index2func[i](groups[i]) - pos = m.end() - - def t_default(self, s): - r'( . | \n )+' - pass + def __init__(self): + pattern = self.reflect() + self.re = re.compile(pattern, re.VERBOSE) + + self.index2func = {} + for name, number in list(self.re.groupindex.items()): + self.index2func[number-1] = getattr(self, 't_' + name) + + def makeRE(self, name): + doc = getattr(self, name).__doc__ + rv = '(?P<%s>%s)' % (name[2:], doc) + return rv + + def reflect(self): + rv = [] + for name in _namelist(self): + if name[:2] == 't_' and name != 't_default': + rv.append(self.makeRE(name)) + + rv.append(self.makeRE('t_default')) + return string.join(rv, '|') + + def error(self, s, pos): + print("Lexical error at position %s" % pos) + raise SystemExit + + def tokenize(self, s): + pos = 0 + n = len(s) + while pos < n: + m = self.re.match(s, pos) + if m is None: + self.error(s, pos) + + groups = m.groups() + for i in range(len(groups)): + if groups[i] and i in self.index2func: + self.index2func[i](groups[i]) + pos = m.end() + + def t_default(self, s): + r'( . | \n )+' + pass class GenericParser: - def __init__(self, start): - self.rules = {} - self.rule2func = {} - self.rule2name = {} - self.collectRules() - self.startRule = self.augment(start) - self.ruleschanged = 1 - - _START = 'START' - _EOF = 'EOF' - - # - # A hook for GenericASTBuilder and GenericASTMatcher. - # - def preprocess(self, rule, func): return rule, func - - def addRule(self, doc, func): - rules = string.split(doc) - - index = [] - for i in range(len(rules)): - if rules[i] == '::=': - index.append(i-1) - index.append(len(rules)) - - for i in range(len(index)-1): - lhs = rules[index[i]] - rhs = rules[index[i]+2:index[i+1]] - rule = (lhs, tuple(rhs)) - - rule, fn = self.preprocess(rule, func) - - if self.rules.has_key(lhs): - self.rules[lhs].append(rule) - else: - self.rules[lhs] = [ rule ] - self.rule2func[rule] = fn - self.rule2name[rule] = func.__name__[2:] - self.ruleschanged = 1 - - def collectRules(self): - for name in _namelist(self): - if name[:2] == 'p_': - func = getattr(self, name) - doc = func.__doc__ - self.addRule(doc, func) - - def augment(self, start): - # - # Tempting though it is, this isn't made into a call - # to self.addRule() because the start rule shouldn't - # be subject to preprocessing. - # - startRule = (self._START, ( start, self._EOF )) - self.rule2func[startRule] = lambda args: args[0] - self.rules[self._START] = [ startRule ] - self.rule2name[startRule] = '' - return startRule - - def makeFIRST(self): - union = {} - self.first = {} - - for rulelist in self.rules.values(): - for lhs, rhs in rulelist: - if not self.first.has_key(lhs): - self.first[lhs] = {} - - if len(rhs) == 0: - self.first[lhs][None] = 1 - continue - - sym = rhs[0] - if not self.rules.has_key(sym): - self.first[lhs][sym] = 1 - else: - union[(sym, lhs)] = 1 - changes = 1 - while changes: - changes = 0 - for src, dest in union.keys(): - destlen = len(self.first[dest]) - self.first[dest].update(self.first[src]) - if len(self.first[dest]) != destlen: - changes = 1 - - # - # An Earley parser, as per J. Earley, "An Efficient Context-Free - # Parsing Algorithm", CACM 13(2), pp. 94-102. Also J. C. Earley, - # "An Efficient Context-Free Parsing Algorithm", Ph.D. thesis, - # Carnegie-Mellon University, August 1968, p. 27. - # - - def typestring(self, token): - return None - - def error(self, token): - print "Syntax error at or near `%s' token" % token - raise SystemExit - - def parse(self, tokens): - tree = {} - tokens.append(self._EOF) - states = { 0: [ (self.startRule, 0, 0) ] } - - if self.ruleschanged: - self.makeFIRST() - - for i in xrange(len(tokens)): - states[i+1] = [] - - if states[i] == []: - break - self.buildState(tokens[i], states, i, tree) - - #_dump(tokens, states) - - if i < len(tokens)-1 or states[i+1] != [(self.startRule, 2, 0)]: - del tokens[-1] - self.error(tokens[i-1]) - rv = self.buildTree(tokens, tree, ((self.startRule, 2, 0), i+1)) - del tokens[-1] - return rv - - def buildState(self, token, states, i, tree): - needsCompletion = {} - state = states[i] - predicted = {} - - for item in state: - rule, pos, parent = item - lhs, rhs = rule - - # - # A -> a . (completer) - # - if pos == len(rhs): - if len(rhs) == 0: - needsCompletion[lhs] = (item, i) - - for pitem in states[parent]: - if pitem is item: - break - - prule, ppos, pparent = pitem - plhs, prhs = prule - - if prhs[ppos:ppos+1] == (lhs,): - new = (prule, - ppos+1, - pparent) - if new not in state: - state.append(new) - tree[(new, i)] = [(item, i)] - else: - tree[(new, i)].append((item, i)) - continue - - nextSym = rhs[pos] - - # - # A -> a . B (predictor) - # - if self.rules.has_key(nextSym): - # - # Work on completer step some more; for rules - # with empty RHS, the "parent state" is the - # current state we're adding Earley items to, - # so the Earley items the completer step needs - # may not all be present when it runs. - # - if needsCompletion.has_key(nextSym): - new = (rule, pos+1, parent) - olditem_i = needsCompletion[nextSym] - if new not in state: - state.append(new) - tree[(new, i)] = [olditem_i] - else: - tree[(new, i)].append(olditem_i) - - # - # Has this been predicted already? - # - if predicted.has_key(nextSym): - continue - predicted[nextSym] = 1 - - ttype = token is not self._EOF and \ - self.typestring(token) or \ - None - if ttype is not None: - # - # Even smarter predictor, when the - # token's type is known. The code is - # grungy, but runs pretty fast. Three - # cases are looked for: rules with - # empty RHS; first symbol on RHS is a - # terminal; first symbol on RHS is a - # nonterminal (and isn't nullable). - # - for prule in self.rules[nextSym]: - new = (prule, 0, i) - prhs = prule[1] - if len(prhs) == 0: - state.append(new) - continue - prhs0 = prhs[0] - if not self.rules.has_key(prhs0): - if prhs0 != ttype: - continue - else: - state.append(new) - continue - first = self.first[prhs0] - if not first.has_key(None) and \ - not first.has_key(ttype): - continue - state.append(new) - continue - - for prule in self.rules[nextSym]: - # - # Smarter predictor, as per Grune & - # Jacobs' _Parsing Techniques_. Not - # as good as FIRST sets though. - # - prhs = prule[1] - if len(prhs) > 0 and \ - not self.rules.has_key(prhs[0]) and \ - token != prhs[0]: - continue - state.append((prule, 0, i)) - - # - # A -> a . c (scanner) - # - elif token == nextSym: - #assert new not in states[i+1] - states[i+1].append((rule, pos+1, parent)) - - def buildTree(self, tokens, tree, root): - stack = [] - self.buildTree_r(stack, tokens, -1, tree, root) - return stack[0] - - def buildTree_r(self, stack, tokens, tokpos, tree, root): - (rule, pos, parent), state = root - - while pos > 0: - want = ((rule, pos, parent), state) - if not tree.has_key(want): - # - # Since pos > 0, it didn't come from closure, - # and if it isn't in tree[], then there must - # be a terminal symbol to the left of the dot. - # (It must be from a "scanner" step.) - # - pos = pos - 1 - state = state - 1 - stack.insert(0, tokens[tokpos]) - tokpos = tokpos - 1 - else: - # - # There's a NT to the left of the dot. - # Follow the tree pointer recursively (>1 - # tree pointers from it indicates ambiguity). - # Since the item must have come about from a - # "completer" step, the state where the item - # came from must be the parent state of the - # item the tree pointer points to. - # - children = tree[want] - if len(children) > 1: - child = self.ambiguity(children) - else: - child = children[0] - - tokpos = self.buildTree_r(stack, - tokens, tokpos, - tree, child) - pos = pos - 1 - (crule, cpos, cparent), cstate = child - state = cparent - - lhs, rhs = rule - result = self.rule2func[rule](stack[:len(rhs)]) - stack[:len(rhs)] = [result] - return tokpos - - def ambiguity(self, children): - # - # XXX - problem here and in collectRules() if the same - # rule appears in >1 method. But in that case the - # user probably gets what they deserve :-) Also - # undefined results if rules causing the ambiguity - # appear in the same method. - # - sortlist = [] - name2index = {} - for i in range(len(children)): - ((rule, pos, parent), index) = children[i] - lhs, rhs = rule - name = self.rule2name[rule] - sortlist.append((len(rhs), name)) - name2index[name] = i - sortlist.sort() - list = map(lambda (a,b): b, sortlist) - return children[name2index[self.resolve(list)]] - - def resolve(self, list): - # - # Resolve ambiguity in favor of the shortest RHS. - # Since we walk the tree from the top down, this - # should effectively resolve in favor of a "shift". - # - return list[0] + def __init__(self, start): + self.rules = {} + self.rule2func = {} + self.rule2name = {} + self.collectRules() + self.startRule = self.augment(start) + self.ruleschanged = 1 + + _START = 'START' + _EOF = 'EOF' + + # + # A hook for GenericASTBuilder and GenericASTMatcher. + # + def preprocess(self, rule, func): return rule, func + + def addRule(self, doc, func): + rules = string.split(doc) + + index = [] + for i in range(len(rules)): + if rules[i] == '::=': + index.append(i-1) + index.append(len(rules)) + + for i in range(len(index)-1): + lhs = rules[index[i]] + rhs = rules[index[i]+2:index[i+1]] + rule = (lhs, tuple(rhs)) + + rule, fn = self.preprocess(rule, func) + + if lhs in self.rules: + self.rules[lhs].append(rule) + else: + self.rules[lhs] = [ rule ] + self.rule2func[rule] = fn + self.rule2name[rule] = func.__name__[2:] + self.ruleschanged = 1 + + def collectRules(self): + for name in _namelist(self): + if name[:2] == 'p_': + func = getattr(self, name) + doc = func.__doc__ + self.addRule(doc, func) + + def augment(self, start): + # + # Tempting though it is, this isn't made into a call + # to self.addRule() because the start rule shouldn't + # be subject to preprocessing. + # + startRule = (self._START, ( start, self._EOF )) + self.rule2func[startRule] = lambda args: args[0] + self.rules[self._START] = [ startRule ] + self.rule2name[startRule] = '' + return startRule + + def makeFIRST(self): + union = {} + self.first = {} + + for rulelist in list(self.rules.values()): + for lhs, rhs in rulelist: + if lhs not in self.first: + self.first[lhs] = {} + + if len(rhs) == 0: + self.first[lhs][None] = 1 + continue + + sym = rhs[0] + if sym not in self.rules: + self.first[lhs][sym] = 1 + else: + union[(sym, lhs)] = 1 + changes = 1 + while changes: + changes = 0 + for src, dest in list(union.keys()): + destlen = len(self.first[dest]) + self.first[dest].update(self.first[src]) + if len(self.first[dest]) != destlen: + changes = 1 + + # + # An Earley parser, as per J. Earley, "An Efficient Context-Free + # Parsing Algorithm", CACM 13(2), pp. 94-102. Also J. C. Earley, + # "An Efficient Context-Free Parsing Algorithm", Ph.D. thesis, + # Carnegie-Mellon University, August 1968, p. 27. + # + + def typestring(self, token): + return None + + def error(self, token): + print("Syntax error at or near `%s' token" % token) + raise SystemExit + + def parse(self, tokens): + tree = {} + tokens.append(self._EOF) + states = { 0: [ (self.startRule, 0, 0) ] } + + if self.ruleschanged: + self.makeFIRST() + + for i in range(len(tokens)): + states[i+1] = [] + + if states[i] == []: + break + self.buildState(tokens[i], states, i, tree) + + #_dump(tokens, states) + + if i < len(tokens)-1 or states[i+1] != [(self.startRule, 2, 0)]: + del tokens[-1] + self.error(tokens[i-1]) + rv = self.buildTree(tokens, tree, ((self.startRule, 2, 0), i+1)) + del tokens[-1] + return rv + + def buildState(self, token, states, i, tree): + needsCompletion = {} + state = states[i] + predicted = {} + + for item in state: + rule, pos, parent = item + lhs, rhs = rule + + # + # A -> a . (completer) + # + if pos == len(rhs): + if len(rhs) == 0: + needsCompletion[lhs] = (item, i) + + for pitem in states[parent]: + if pitem is item: + break + + prule, ppos, pparent = pitem + plhs, prhs = prule + + if prhs[ppos:ppos+1] == (lhs,): + new = (prule, + ppos+1, + pparent) + if new not in state: + state.append(new) + tree[(new, i)] = [(item, i)] + else: + tree[(new, i)].append((item, i)) + continue + + nextSym = rhs[pos] + + # + # A -> a . B (predictor) + # + if nextSym in self.rules: + # + # Work on completer step some more; for rules + # with empty RHS, the "parent state" is the + # current state we're adding Earley items to, + # so the Earley items the completer step needs + # may not all be present when it runs. + # + if nextSym in needsCompletion: + new = (rule, pos+1, parent) + olditem_i = needsCompletion[nextSym] + if new not in state: + state.append(new) + tree[(new, i)] = [olditem_i] + else: + tree[(new, i)].append(olditem_i) + + # + # Has this been predicted already? + # + if nextSym in predicted: + continue + predicted[nextSym] = 1 + + ttype = token is not self._EOF and \ + self.typestring(token) or \ + None + if ttype is not None: + # + # Even smarter predictor, when the + # token's type is known. The code is + # grungy, but runs pretty fast. Three + # cases are looked for: rules with + # empty RHS; first symbol on RHS is a + # terminal; first symbol on RHS is a + # nonterminal (and isn't nullable). + # + for prule in self.rules[nextSym]: + new = (prule, 0, i) + prhs = prule[1] + if len(prhs) == 0: + state.append(new) + continue + prhs0 = prhs[0] + if prhs0 not in self.rules: + if prhs0 != ttype: + continue + else: + state.append(new) + continue + first = self.first[prhs0] + if None not in first and \ + ttype not in first: + continue + state.append(new) + continue + + for prule in self.rules[nextSym]: + # + # Smarter predictor, as per Grune & + # Jacobs' _Parsing Techniques_. Not + # as good as FIRST sets though. + # + prhs = prule[1] + if len(prhs) > 0 and \ + prhs[0] not in self.rules and \ + token != prhs[0]: + continue + state.append((prule, 0, i)) + + # + # A -> a . c (scanner) + # + elif token == nextSym: + #assert new not in states[i+1] + states[i+1].append((rule, pos+1, parent)) + + def buildTree(self, tokens, tree, root): + stack = [] + self.buildTree_r(stack, tokens, -1, tree, root) + return stack[0] + + def buildTree_r(self, stack, tokens, tokpos, tree, root): + (rule, pos, parent), state = root + + while pos > 0: + want = ((rule, pos, parent), state) + if want not in tree: + # + # Since pos > 0, it didn't come from closure, + # and if it isn't in tree[], then there must + # be a terminal symbol to the left of the dot. + # (It must be from a "scanner" step.) + # + pos = pos - 1 + state = state - 1 + stack.insert(0, tokens[tokpos]) + tokpos = tokpos - 1 + else: + # + # There's a NT to the left of the dot. + # Follow the tree pointer recursively (>1 + # tree pointers from it indicates ambiguity). + # Since the item must have come about from a + # "completer" step, the state where the item + # came from must be the parent state of the + # item the tree pointer points to. + # + children = tree[want] + if len(children) > 1: + child = self.ambiguity(children) + else: + child = children[0] + + tokpos = self.buildTree_r(stack, + tokens, tokpos, + tree, child) + pos = pos - 1 + (crule, cpos, cparent), cstate = child + state = cparent + + lhs, rhs = rule + result = self.rule2func[rule](stack[:len(rhs)]) + stack[:len(rhs)] = [result] + return tokpos + + def ambiguity(self, children): + # + # XXX - problem here and in collectRules() if the same + # rule appears in >1 method. But in that case the + # user probably gets what they deserve :-) Also + # undefined results if rules causing the ambiguity + # appear in the same method. + # + sortlist = [] + name2index = {} + for i in range(len(children)): + ((rule, pos, parent), index) = children[i] + lhs, rhs = rule + name = self.rule2name[rule] + sortlist.append((len(rhs), name)) + name2index[name] = i + sortlist.sort() + list = [a_b[1] for a_b in sortlist] + return children[name2index[self.resolve(list)]] + + def resolve(self, list): + # + # Resolve ambiguity in favor of the shortest RHS. + # Since we walk the tree from the top down, this + # should effectively resolve in favor of a "shift". + # + return list[0] # # GenericASTBuilder automagically constructs a concrete/abstract syntax tree @@ -408,32 +408,32 @@ def resolve(self, list): # class GenericASTBuilder(GenericParser): - def __init__(self, AST, start): - GenericParser.__init__(self, start) - self.AST = AST - - def preprocess(self, rule, func): - rebind = lambda lhs, self=self: \ - lambda args, lhs=lhs, self=self: \ - self.buildASTNode(args, lhs) - lhs, rhs = rule - return rule, rebind(lhs) - - def buildASTNode(self, args, lhs): - children = [] - for arg in args: - if isinstance(arg, self.AST): - children.append(arg) - else: - children.append(self.terminal(arg)) - return self.nonterminal(lhs, children) - - def terminal(self, token): return token - - def nonterminal(self, type, args): - rv = self.AST(type) - rv[:len(args)] = args - return rv + def __init__(self, AST, start): + GenericParser.__init__(self, start) + self.AST = AST + + def preprocess(self, rule, func): + rebind = lambda lhs, self=self: \ + lambda args, lhs=lhs, self=self: \ + self.buildASTNode(args, lhs) + lhs, rhs = rule + return rule, rebind(lhs) + + def buildASTNode(self, args, lhs): + children = [] + for arg in args: + if isinstance(arg, self.AST): + children.append(arg) + else: + children.append(self.terminal(arg)) + return self.nonterminal(lhs, children) + + def terminal(self, token): return token + + def nonterminal(self, type, args): + rv = self.AST(type) + rv[:len(args)] = args + return rv # # GenericASTTraversal is a Visitor pattern according to Design Patterns. For @@ -446,57 +446,57 @@ def nonterminal(self, type, args): # class GenericASTTraversalPruningException: - pass + pass class GenericASTTraversal: - def __init__(self, ast): - self.ast = ast + def __init__(self, ast): + self.ast = ast - def typestring(self, node): - return node.type + def typestring(self, node): + return node.type - def prune(self): - raise GenericASTTraversalPruningException + def prune(self): + raise GenericASTTraversalPruningException - def preorder(self, node=None): - if node is None: - node = self.ast + def preorder(self, node=None): + if node is None: + node = self.ast - try: - name = 'n_' + self.typestring(node) - if hasattr(self, name): - func = getattr(self, name) - func(node) - else: - self.default(node) - except GenericASTTraversalPruningException: - return + try: + name = 'n_' + self.typestring(node) + if hasattr(self, name): + func = getattr(self, name) + func(node) + else: + self.default(node) + except GenericASTTraversalPruningException: + return - for kid in node: - self.preorder(kid) + for kid in node: + self.preorder(kid) - name = name + '_exit' - if hasattr(self, name): - func = getattr(self, name) - func(node) + name = name + '_exit' + if hasattr(self, name): + func = getattr(self, name) + func(node) - def postorder(self, node=None): - if node is None: - node = self.ast + def postorder(self, node=None): + if node is None: + node = self.ast - for kid in node: - self.postorder(kid) + for kid in node: + self.postorder(kid) - name = 'n_' + self.typestring(node) - if hasattr(self, name): - func = getattr(self, name) - func(node) - else: - self.default(node) + name = 'n_' + self.typestring(node) + if hasattr(self, name): + func = getattr(self, name) + func(node) + else: + self.default(node) - def default(self, node): - pass + def default(self, node): + pass # # GenericASTMatcher. AST nodes must have "__getitem__" and "__cmp__" @@ -506,61 +506,61 @@ def default(self, node): # class GenericASTMatcher(GenericParser): - def __init__(self, start, ast): - GenericParser.__init__(self, start) - self.ast = ast - - def preprocess(self, rule, func): - rebind = lambda func, self=self: \ - lambda args, func=func, self=self: \ - self.foundMatch(args, func) - lhs, rhs = rule - rhslist = list(rhs) - rhslist.reverse() - - return (lhs, tuple(rhslist)), rebind(func) - - def foundMatch(self, args, func): - func(args[-1]) - return args[-1] - - def match_r(self, node): - self.input.insert(0, node) - children = 0 - - for child in node: - if children == 0: - self.input.insert(0, '(') - children = children + 1 - self.match_r(child) - - if children > 0: - self.input.insert(0, ')') - - def match(self, ast=None): - if ast is None: - ast = self.ast - self.input = [] - - self.match_r(ast) - self.parse(self.input) - - def resolve(self, list): - # - # Resolve ambiguity in favor of the longest RHS. - # - return list[-1] + def __init__(self, start, ast): + GenericParser.__init__(self, start) + self.ast = ast + + def preprocess(self, rule, func): + rebind = lambda func, self=self: \ + lambda args, func=func, self=self: \ + self.foundMatch(args, func) + lhs, rhs = rule + rhslist = list(rhs) + rhslist.reverse() + + return (lhs, tuple(rhslist)), rebind(func) + + def foundMatch(self, args, func): + func(args[-1]) + return args[-1] + + def match_r(self, node): + self.input.insert(0, node) + children = 0 + + for child in node: + if children == 0: + self.input.insert(0, '(') + children = children + 1 + self.match_r(child) + + if children > 0: + self.input.insert(0, ')') + + def match(self, ast=None): + if ast is None: + ast = self.ast + self.input = [] + + self.match_r(ast) + self.parse(self.input) + + def resolve(self, list): + # + # Resolve ambiguity in favor of the longest RHS. + # + return list[-1] def _dump(tokens, states): - for i in range(len(states)): - print 'state', i - for (lhs, rhs), pos, parent in states[i]: - print '\t', lhs, '::=', - print string.join(rhs[:pos]), - print '.', - print string.join(rhs[pos:]), - print ',', parent - if i < len(tokens): - print - print 'token', str(tokens[i]) - print + for i in range(len(states)): + print('state', i) + for (lhs, rhs), pos, parent in states[i]: + print('\t', lhs, '::=', end=' ') + print(string.join(rhs[:pos]), end=' ') + print('.', end=' ') + print(string.join(rhs[pos:]), end=' ') + print(',', parent) + if i < len(tokens): + print() + print('token', str(tokens[i])) + print() diff --git a/sparql2cwm.py b/sparql2cwm.py index e2a51fc0..446ef47a 100644 --- a/sparql2cwm.py +++ b/sparql2cwm.py @@ -6,14 +6,14 @@ $Id$ """ -from set_importer import Set -import uripath -from term import Term, CompoundTerm -from formula import Formula -import diag -from why import BecauseOfData +from .set_importer import Set +from . import uripath +from .term import Term, CompoundTerm +from .formula import Formula +from . import diag +from .why import BecauseOfData -from cwm_sparql import SPARQL_NS +from .cwm_sparql import SPARQL_NS knownFunctions = {} @@ -27,16 +27,16 @@ def value(self, newVal=None): def abbr(prodURI): return prodURI.split('#').pop() -class typedThing(unicode): +class typedThing(str): def __new__(cls, val, retType=None, ArgTypes=[], trueOnError=False): - ret = unicode.__new__(cls, val) + ret = str.__new__(cls, val) ret.retType = retType ret.argTypes = ArgTypes ret.trueOnError = trueOnError return ret def __call__(self): - return unicode(self) + '__' + unicode(self.retType) + '__' + {False:'', True:'lenient__'}[self.trueOnError] + '_'.join(self.argTypes) + return str(self) + '__' + str(self.retType) + '__' + {False:'', True:'lenient__'}[self.trueOnError] + '_'.join(self.argTypes) def getType(ex): if isinstance(ex, typedThing): @@ -92,7 +92,7 @@ def __setitem__(self, key, value): def update(self, other={}, **values): if values: self.update(values) - for key, val in other.iteritems(): + for key, val in other.items(): self[key] = val def translate(self, fromkey, tokey): if fromkey not in self: @@ -124,7 +124,8 @@ def makeTriple(subj, pred, obj, safeVersion=False): [(pred, ('objectList', [obj]))]))) -def makeSafeVal(val, (subj, pred, obj), safeVersion=False): +def makeSafeVal(val, xxx_todo_changeme, safeVersion=False): + (subj, pred, obj) = xxx_todo_changeme if safeVersion: store = pred[1].store typeErrorReturner = store.newSymbol(SPARQL_NS + '#typeErrorReturner') @@ -153,7 +154,7 @@ def normalize(expr): After normalize, the last bottom up step to generate the n3 can be done. """ - if verbose(): print expr + if verbose(): print(expr) step1 = Coerce()(expr) return NotNot()(step1) @@ -162,7 +163,7 @@ def __init__(self): self.k = 0 def __call__(self, expr, coerce=True): try: - if verbose(): print ' ' * self.k, expr, coerce + if verbose(): print(' ' * self.k, expr, coerce) self.k = self.k + 1 if expr[0] in ('Var', 'Literal', 'Number', 'String', 'symbol'): ww = self.atom(expr, coerce) @@ -173,10 +174,10 @@ def __call__(self, expr, coerce=True): else: ww = getattr(self, 'on_' + expr[0])(expr, coerce) self.k = self.k - 1 - if verbose(): print ' ' * self.k, '/', ww + if verbose(): print(' ' * self.k, '/', ww) return ww except AttributeError: - raise RuntimeError("COERCE why don't you define a %s function, to call on %s?" % ('on_' + expr[0], `expr`)) + raise RuntimeError("COERCE why don't you define a %s function, to call on %s?" % ('on_' + expr[0], repr(expr))) def on_function(self, p, coerce): if coerce: @@ -257,7 +258,7 @@ def __init__(self): def __call__(self, expr, inv=False, Ored=False): try: - if verbose(): print ' ' * self.k, expr, inv + if verbose(): print(' ' * self.k, expr, inv) self.k = self.k + 1 if not isinstance(expr, (list, tuple)): return expr @@ -269,10 +270,10 @@ def __call__(self, expr, inv=False, Ored=False): else: ww = getattr(self, 'on_' + expr[0])(expr, inv, Ored) self.k = self.k - 1 - if verbose(): print ' ' * self.k, '/', ww + if verbose(): print(' ' * self.k, '/', ww) return ww except AttributeError: - raise RuntimeError("NOTNOT why don't you define a %s function, to call on %s?" % ('on_' + expr[0], `expr`)) + raise RuntimeError("NOTNOT why don't you define a %s function, to call on %s?" % ('on_' + expr[0], repr(expr))) def expr(self, p, inv, ored): if inv: @@ -365,9 +366,9 @@ def run(self): def onStart(self, prod): if verbose(): if callable(prod): - print (' ' * len(self.productions)) + prod() + print((' ' * len(self.productions)) + prod()) else: - print (' ' * len(self.productions)) + `prod` + print((' ' * len(self.productions)) + repr(prod)) #if callable(prod): # prod = prod() self.productions.append([prod]) @@ -378,7 +379,7 @@ def onFinish(self): prod = self.sink.prod(k) if self.productions: self.productions[-1].append(prod) - if verbose(): print (' ' * len(self.productions)) + '/' + prodName + ': ' + `prod` + if verbose(): print((' ' * len(self.productions)) + '/' + prodName + ': ' + repr(prod)) return prod def onToken(self, prod, tok): @@ -387,7 +388,7 @@ def onToken(self, prod, tok): self.productions[-1].append(k) except IndexError: return k - if verbose(): print (' ' * len(self.productions)) + `(prod, tok)` + if verbose(): print((' ' * len(self.productions)) + repr((prod, tok))) class productionHandler(object): @@ -396,10 +397,10 @@ def prod(self, production): try: return getattr(self, 'on_' + abbr(production[0]))(production) except: - print production + print(production) raise if True: # len(production) > 1: - raise RuntimeError("why don't you define a %s function, to call on %s?" % ('on_' + abbr(production[0]), `production`)) + raise RuntimeError("why don't you define a %s function, to call on %s?" % ('on_' + abbr(production[0]), repr(production))) return production @@ -612,8 +613,8 @@ def anonymize(self, formula, uri = None): if uri in self.anNodes: return self.anNodes[uri] except: - print uri - print 'uri = ', uri + print(uri) + print('uri = ', uri) raise self.anNodes[uri] = formula.newBlankNode(why=reason2()) return self.anNodes[uri] @@ -655,7 +656,7 @@ def makePatterns(self, f, node, patterns): includedStuff = pattern[4] notIncludedStuff = pattern[5] - for nodeName, graphIntersection in includedStuff.iteritems(): + for nodeName, graphIntersection in includedStuff.items(): if not graphIntersection: continue graph = f.newFormula() for subGraph in graphIntersection: @@ -668,10 +669,10 @@ def makePatterns(self, f, node, patterns): else: semantics = knowledge_base tail.add(semantics, self.store.includes, graph, why=reason2()) - includedVars = Set(self.vars.values()) + includedVars = Set(list(self.vars.values())) excludedVars = includedVars.difference(tail.occurringIn(includedVars)) - for nodeName, graphIntersection in notIncludedStuff.iteritems(): + for nodeName, graphIntersection in notIncludedStuff.items(): if not graphIntersection: continue ## graph = f.newFormula() for subGraph in graphIntersection: @@ -782,17 +783,17 @@ def on_WhereClause(self, p): for pred, obj in k[4]: if positiveTriples is None: positiveTriples = self.store.newFormula() - for formSet in included.values(): + for formSet in list(included.values()): for form in formSet: positiveTriples.loadFormulaWithSubstitution(form) positiveTriples = positiveTriples.close() freeVariables = Set([x.uriref() for x in positiveTriples.freeVariables()]) if pred is self.sparql['bound']: - variable = unicode(obj) + variable = str(obj) if variable not in freeVariables: append = False elif pred is self.sparql['notBound']: ##@@@ This is broken!! - variable = unicode(obj) + variable = str(obj) if variable in freeVariables: append = False if append: @@ -834,7 +835,7 @@ def on__QDISTINCT_E_Opt(self, p): if len(p) == 1: return None return None - raise NotImplementedError(`p`) + raise NotImplementedError(repr(p)) def on_Var(self, p): uri = self.base + p[1][1][1:] @@ -854,7 +855,7 @@ def on__O_QVar_E_Plus_Or__QTIMES_E__C(self, p): else: class ___(object): def __iter__(s): - return iter(self.vars.values()) + return iter(list(self.vars.values())) varList = ___() return ('SelectVars', varList) @@ -894,10 +895,10 @@ def on_RDFLiteral(self, p): def on_NumericLiteral(self, p): if abbr(p[1][0]) == 'INTEGER': - return ('Literal', self.store.newLiteral(`int(p[1][1])`, dt=self.xsd['integer'], lang=None)) + return ('Literal', self.store.newLiteral(repr(int(p[1][1])), dt=self.xsd['integer'], lang=None)) if abbr(p[1][0]) == 'FLOATING_POINT': - return ('Literal', self.store.newLiteral(`float(p[1][1])`, dt=self.xsd['double'], lang=None)) - raise RuntimeError(`p`) + return ('Literal', self.store.newLiteral(repr(float(p[1][1])), dt=self.xsd['double'], lang=None)) + raise RuntimeError(repr(p)) def on_RDFTerm(self, p): return p[1] @@ -1035,15 +1036,15 @@ def on_GroupGraphPattern(self, p, fromFilter = False): else: f.add(subj, pred, obj, why=reason2()) except: - print '================' - print 'subject= ', subject - print 'predicate= ', predicate - print 'pred= ', pred is self.sparql['OPTIONAL'], id(pred), id(self.sparql['OPTIONAL']) - print 'object= ', object + print('================') + print('subject= ', subject) + print('predicate= ', predicate) + print('pred= ', pred is self.sparql['OPTIONAL'], id(pred), id(self.sparql['OPTIONAL'])) + print('object= ', object) raise except: - print 'triples=',triples - print 'triple=',triple + print('triples=',triples) + print('triple=',triple) raise f = f.close() @@ -1230,7 +1231,7 @@ def on_UnaryExpression(self, p): return p[1] if abbr(p[1][0]) == 'GT_NOT': return (typedThing('Not', 'boolean'), p[2]) - raise RuntimeError(`p`) + raise RuntimeError(repr(p)) def on__Q_O_QTIMES_E____QUnaryExpression_E__Or__QDIVIDE_E____QUnaryExpression_E__C_E_Star(self, p): if len(p) == 1: @@ -1335,16 +1336,16 @@ def on_OffsetClause(self, p): return self.on_NumericLiteral(p[1:])[1] def on_DescribeQuery(self, p): - raise RuntimeError(`p`) + raise RuntimeError(repr(p)) def on__QVarOrIRIref_E_Plus(self, p): - raise RuntimeError(`p`) + raise RuntimeError(repr(p)) def on__O_QVarOrIRIref_E_Plus_Or__QTIMES_E__C(self, p): - raise RuntimeError(`p`) + raise RuntimeError(repr(p)) def on__QWhereClause_E_Opt(self, p): - raise RuntimeError(`p`) + raise RuntimeError(repr(p)) def on_DatasetClause(self, p): return None @@ -1367,13 +1368,13 @@ def on_SourceSelector(self, p): def on_OrderClause(self, p): clauses = [p[3]] + p[4] return clauses - raise RuntimeError(`p`) + raise RuntimeError(repr(p)) def on__QOrderCondition_E_Plus(self, p): if len(p) == 1: return [] return [p[1]] + p[2] - raise RuntimeError(`p`) + raise RuntimeError(repr(p)) def on_OrderCondition(self, p): def listize(thing): @@ -1383,7 +1384,7 @@ def listize(thing): return self.store.newList([self.store.newSymbol(thing[1][1])] + [listize(x) for x in thing[2:]]) return self.store.newList([self.store.newLiteral(thing[0])] + [listize(x) for x in thing[1:]]) return listize(p[1]) - raise RuntimeError(`p`) + raise RuntimeError(repr(p)) def on__O_QASC_E__Or__QDESC_E__C(self, p): return p[1][1] @@ -1426,7 +1427,7 @@ def on__O_QCOMMA_E____QObjectList_E__C(self, p): return (p[2][0], p[2][1][1], p[2][2]) def on_VarOrIRIref(self, p): - raise RuntimeError(`p`) + raise RuntimeError(repr(p)) def on__O_QOR_E____QConditionalAndExpression_E__C(self, p): return p[2] @@ -1469,7 +1470,7 @@ def on_BuiltinCallExpression(self, p): return (typedThing('isBlank', 'boolean'), p[3]) if funcName == 'IT_isLITERAL': return (typedThing('isLiteral', 'boolean'), p[3]) - raise RuntimeError(`p`) + raise RuntimeError(repr(p)) def on_RegexExpression(self, p): return ('Regex', p[3], p[5], p[6]) @@ -1530,11 +1531,11 @@ def on__O_QLANGTAG_E__Or__QDTYPE_E____QIRIref_E__C(self, p): return (None, p[1][1][1:]) if abbr(p[1][0]) == 'GT_DTYPE': return (p[1][1], None) - raise RuntimeError(`p`) + raise RuntimeError(repr(p)) def on_BooleanLiteral(self, p): - return ('Literal', (p[1][1] == u'true' and self.true or self.false)) + return ('Literal', (p[1][1] == 'true' and self.true or self.false)) class RulesMaker(object): @@ -1594,7 +1595,7 @@ def unEscape(string): else: real_str = string[1:-1] triple = False - ret = u'' + ret = '' n = 0 while n < len(real_str): ch = real_str[n] @@ -1611,12 +1612,12 @@ def unEscape(string): elif a == 'u': m = real_str[n+2:n+6] assert len(m) == 4 - ret += unichr(int(m, 16)) + ret += chr(int(m, 16)) n += 5 elif a == 'U': m = real_str[n+2:n+10] assert len(m) == 8 - ret += unichr(int(m, 16)) + ret += chr(int(m, 16)) n += 9 else: raise ValueError('Bad Escape') diff --git a/tab2n3.py b/tab2n3.py index 0e594309..bbedf9d3 100644 --- a/tab2n3.py +++ b/tab2n3.py @@ -74,8 +74,8 @@ def parseSheet(self, infile, stringTableFile): self.doElement(root, self.sheetTags, None) self.cleanup() - print self.diagnosticString() - print 'self:', self + print(self.diagnosticString()) + print('self:', self) return; def parseSharedStringTable(self, infile): @@ -91,7 +91,7 @@ def parseSharedStringTable(self, infile): assert t.tag.split('}')[1] == 't' if t.text: self.shortStrings.append(t.text) - print "Short string %i:" % count, t.text + print("Short string %i:" % count, t.text) else: self.shortStrings.append('') count += 1 @@ -106,7 +106,7 @@ def parseXHTML(self, infile): return; def pokeString(self, s, hide): - print "Pokestring at %i,%i " % (self.row, self.col), s + print("Pokestring at %i,%i " % (self.row, self.col), s) if self.row >= -1 and self.col >= 0: s = s.strip() if self.row > 0: @@ -144,7 +144,7 @@ def newColumn(): self.cellType = tag newColumn() - print " column ", self.col + print(" column ", self.col) elif tag == 'br': self.kludge = 1 # A break moves temporarily to the next stacked column @@ -157,7 +157,7 @@ def newColumn(): self.pokeString('date '+e.text, hide); # print " ssss ", 'date '+e.text, 'at', self.row, self.col else: - print parent.tag, parent.attrib, e.tag, e.attrib, e.text + print(parent.tag, parent.attrib, e.tag, e.attrib, e.text) assert False # Now poke any text content into the array: @@ -183,7 +183,7 @@ def newColumn(): newColumn() def cleanup(self): - r = range(len(self)); + r = list(range(len(self))); r.reverse(); for i in r: if len(self[i]) < 10: @@ -195,9 +195,9 @@ def cleanup(self): def diagnosticString(self): s = "" - s += " %i rows Data: %s\n" % (len(self), `self`); - s += " %i headings:%s\n" % (len(self.headings), `self.headings`); - s+= " %i Tips: %s\n" % (len(self.tips), `self.tips`) + s += " %i rows Data: %s\n" % (len(self), repr(self)); + s += " %i headings:%s\n" % (len(self.headings), repr(self.headings)); + s+= " %i Tips: %s\n" % (len(self.tips), repr(self.tips)) s += "%i columns:\n" % (self.numberOfColumns) for row in range(len(self)): if len(self[row]) != len(self.headings): @@ -283,7 +283,7 @@ def readCSV(self, inFP, delim): for jj in range(len(self.headings)): self.headings[jj] = self.headings[jj].strip() if len(self.headings[jj]) == 0: - self.headings[jj] = unichr(65+jj) # Default to "A", "B", etc + self.headings[jj] = chr(65+jj) # Default to "A", "B", etc while self.headings[-1:] == [""]: self.headings = self.headings[:-1]; # Strip trailing comma on heading line (paypal) info( "# headings found: %i %s" % (len(self.headings), self.headings)) lineNo = 2 diff --git a/term.py b/term.py index a247e3f0..8ef96836 100755 --- a/term.py +++ b/term.py @@ -22,33 +22,34 @@ """ -from __future__ import generators # for yield + # for yield import string, sys, types -from set_importer import Set, ImmutableSet +from .set_importer import Set, ImmutableSet -import uripath # DanC's tested and correct one +from . import uripath # DanC's tested and correct one import hashlib, binascii # for building md5 URIs -from uripath import refTo -from RDFSink import runNamespace -from local_decimal import Decimal # for xsd:decimal +from .uripath import refTo +from .RDFSink import runNamespace +from .local_decimal import Decimal # for xsd:decimal +from functools import reduce LITERAL_URI_prefix = "data:application/rdf+n3-literal;" -from RDFSink import List_NS -from RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 -from RDFSink import FORMULA, LITERAL, LITERAL_LANG, LITERAL_DT, ANONYMOUS, \ +from .RDFSink import List_NS +from .RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 +from .RDFSink import FORMULA, LITERAL, LITERAL_LANG, LITERAL_DT, ANONYMOUS, \ SYMBOL, RDF_type_URI -from RDFSink import Logic_NS +from .RDFSink import Logic_NS -from OrderedSequence import merge, intersection, minus +from .OrderedSequence import merge, intersection, minus -import diag -from diag import progress +from . import diag +from .diag import progress from weakref import WeakValueDictionary @@ -96,7 +97,7 @@ def __init__(self, other=None, keywords={}): else: dict.__init__(self, other, **keywords) self.id = self - for k, (a,b) in self.iteritems(): + for k, (a,b) in self.items(): if isinstance(a, tuple): raise RuntimeError("%s : (%s, %s)" % (k,a,b)) @@ -146,7 +147,7 @@ def flatten(self, other): if diag.chatty_flag > 85: progress(lambda : 'Env.flatten(%s,%s)' % (self, other)) retVal = dict(other) - for key, (val, source) in self.items(): + for key, (val, source) in list(self.items()): if key in other: if source == dict.__getitem__(other, key)[1] and val != other[key]: raise ValueError(self[key], other[key]) @@ -161,7 +162,7 @@ def flatten(self, other): return retVal def filter(self, varSet): - retVal = [(a,b) for (a,b) in self.items() if a in varSet] + retVal = [(a,b) for (a,b) in list(self.items()) if a in varSet] retVal = self.__class__(retVal) retVal.id = self.id return retVal @@ -196,7 +197,7 @@ def __hash__(self): try: return self._hashval except AttributeError: - self._hashval = hash(ImmutableSet(self.items())) + self._hashval = hash(ImmutableSet(list(self.items()))) return self._hashval def pickEnv(choice, *envs): @@ -236,7 +237,7 @@ def __repr__(self): return s.encode('unicode_escape') def debugString(self, already=[]): - return `self` # unless more eleborate in superclass + return repr(self) # unless more eleborate in superclass def representation(self, base=None): """The string represnting this in N3 """ @@ -421,7 +422,7 @@ def __getitem__(self, lname): lname -- an XML name (limited to URI characters) """ if lname.startswith("__"): # python internal - raise AttributeError, lname + raise AttributeError(lname) return self.internFrag(lname, Fragment) @@ -523,11 +524,11 @@ class AnonymousNode(Node): def __init__(self, store, uri=None): global nextId try: - if uri: assert isinstance(uri, tuple(types.StringTypes)) + if uri: assert isinstance(uri, tuple((str,))) except: - print uri - print type(uri) - print '---------=============------------' + print(uri) + print(type(uri)) + print('---------=============------------') raise Term.__init__(self, store) self._diagnosticURI = uri @@ -557,7 +558,7 @@ def classOrder(self): def uriref(self): if self._diagnosticURI: return self._diagnosticURI - return runNamespace() + "_g" + `self.serial` + return runNamespace() + "_g" + repr(self.serial) # return runNamespace() + "_b" + `id(self)` def generated(self): @@ -667,7 +668,7 @@ def uriref(self): def substitution(self, bindings, why=None, cannon=False): "Return this or a version of me with variable substitution made" - if self.occurringIn(bindings.keys()) == Set(): + if self.occurringIn(list(bindings.keys())) == Set(): return self # phew! return self.__class__([x.substitution(bindings, why=why, cannon=cannon) for x in self]) @@ -676,7 +677,7 @@ def substituteEquals(self, bindings, newBindings): "Return this or a version of me with substitution of equals made" if diag.chatty_flag > 100: progress("SubstituteEquals list %s with %s" % (self, bindings)) - if self.occurringIn(bindings.keys()) == Set(): + if self.occurringIn(list(bindings.keys())) == Set(): return self # phew! new = self.__class__([x.substitution(bindings, why=why) for x in self]) @@ -734,7 +735,7 @@ def __init__(self, store, first, rest): # Do not use directly _nextList = _nextList + 1 def uriref(self): - return runNamespace() + "li"+ `self._id` + return runNamespace() + "li"+ repr(self._id) def prepend(self, first): x = self._prec.get(first, None) @@ -771,7 +772,7 @@ def value(self): def substitution(self, bindings, why=None, cannon=False): "Return this or a version of me with variable substitution made" - if self.occurringIn(bindings.keys()) == Set(): + if self.occurringIn(list(bindings.keys())) == Set(): return self # phew! s = self.asSequence() s.reverse() @@ -787,7 +788,7 @@ def substituteEquals(self, bindings, newBindings): "Return this or a version of me with substitution of equals made" if diag.chatty_flag > 100: progress("SubstituteEquals list %s with %s" % (self, bindings)) - if self.occurringIn(bindings.keys()) == Set(): + if self.occurringIn(list(bindings.keys())) == Set(): return self # phew! s = self.asSequence() s.reverse() @@ -838,7 +839,7 @@ def doesNodeAppear(self, symbol): else: pass if val == 1: - print 'I won!' + print('I won!') return 1 return 0 @@ -882,7 +883,7 @@ def unifySecondary(self, other, env1, env2, vars, n1Source, n2Source) def debugString(self, already=[]): - s = `self`+" is (" + s = repr(self)+" is (" for i in self: s = s + i.debugString(already) + " " return s + ")" @@ -897,7 +898,7 @@ def __getitem__(self, i): p = p.rest if not isinstance(p, NonEmptyList): raise ValueError("Index %i exceeds size of list %s" - % (i, `self`)) + % (i, repr(self))) i = i - 1 class EmptyList(List): @@ -957,7 +958,7 @@ def __repr__(self): return "()" def __getitem__(self, i): - raise ValueError("Index %i exceeds size of empty list %s" % (i, `self`)) + raise ValueError("Index %i exceeds size of empty list %s" % (i, repr(self))) class FragmentNil(EmptyList, Fragment): @@ -975,7 +976,7 @@ def unifySequence(self, other, vars=Set([]), existentials=Set([]), bindings={}, """ if diag.chatty_flag > 99: progress("Unifying sequence %s with %s" % - (`self`, `other`)) + (repr(self), repr(other))) i = start if len(self) != len(other): return [] while 1: @@ -988,13 +989,13 @@ def unifySequence(self, other, vars=Set([]), existentials=Set([]), bindings={}, try: for a,b in nbs: pass except TypeError: - progress("Bad nbs, should be seq of pairs: >>>" + `nbs` + "<<<") + progress("Bad nbs, should be seq of pairs: >>>" + repr(nbs) + "<<<") raise res = [] for nb, reason in nbs: b2 = bindings.copy() b2.update(nb) - done = Set(nb.keys()) + done = Set(list(nb.keys())) nbs2 = unifySequence(self, other, vars.difference(done), existentials.difference(done), b2, start=i) @@ -1011,7 +1012,7 @@ def unifySet(self, other, vars=Set([]), existentials=Set([]), bindings={}): implemented with indexing, and built-in functions, in query.py. """ if diag.chatty_flag > 99: progress("Unifying set %s with %s" % - (`self`, `other`)) + (repr(self), repr(other))) if len(self) != len(other): return [] # Match fail if self == Set([]): return [ ({}, None) ] # Perfect match self2 = self.copy() # Don't mess with parameters @@ -1025,7 +1026,7 @@ def unifySet(self, other, vars=Set([]), existentials=Set([]), bindings={}): for nb, reason in nbs: # For each way the object matched, b2 = bindings.copy() b2.update(nb) - done = Set(nb.keys()) + done = Set(list(nb.keys())) nbs2 = unifySet(self2, other2, # Try the rest of the set vars.difference(done), existentials.difference(done), b2) @@ -1049,7 +1050,7 @@ def matchSet(pattern, kb, vars=Set([]), bindings={}): bindings map from pattern to kb. """ if diag.chatty_flag > 99: progress("Matching pattern %s against %s, vars=%s" % - (`pattern`, `kb`, `vars`)) + (repr(pattern), repr(kb), repr(vars))) if len(pattern) > len(kb): return [] # Match fail @@@discuss corner cases if len(pattern) == 0: return [(bindings, None)] # Success @@ -1064,7 +1065,7 @@ def matchSet(pattern, kb, vars=Set([]), bindings={}): for nb, reason in nbs: b2 = bindings.copy() b2.update(nb) - done = Set(nb.keys()) + done = Set(list(nb.keys())) nbs2 = matchSet(pattern2, kb2, vars.difference(done), b2) if nbs2 == []: continue for nb2, reason2 in nbs2: @@ -1215,7 +1216,7 @@ def unify(self, other, bindings=Env(), otherBindings=Env(), for x in unifySequence(self, other, env1, env2, vars, existentials, n1Source=n1Source, n2Source=n2Source): yield x elif hasattr(self, 'unifySecondary') and hasattr(other, 'unifySecondary'): - if self.unifySecondary.im_func is other.unifySecondary.im_func: # A reasonable definition of same type + if self.unifySecondary.__func__ is other.unifySecondary.__func__: # A reasonable definition of same type for x in self.unifySecondary(other, env1, env2, vars, universals, existentials, n1Source=n1Source, n2Source=n2Source): yield x @@ -1287,47 +1288,47 @@ def toBool(val, dt=None): raise TypeError("%s type cannot be converted to boolean" % val) typeMap = { "decimal": Decimal, - "integer": long, - "nonPositiveInteger": long, - "negativeInteger": long, + "integer": int, + "nonPositiveInteger": int, + "negativeInteger": int, "long": int, "int": int, "short": int, "byte": int, - "nonNegativeInteger": long, + "nonNegativeInteger": int, "unsignedLong": int, "unsignedInt": int, "unsignedShort": int, "unsignedByte": int, - "positiveInteger": long, + "positiveInteger": int, "boolean": toBool, "double": float, "float": float, - "duration": unicode, - "dateTime": unicode, - "time": unicode, - "date": unicode, - "gYearMonth": unicode, - "gYear": unicode, - "gMonthDay": unicode, - "gDay": unicode, - "gMonth": unicode, - "anyURI": unicode, - "QName": unicode, - "NOTATION": unicode, - "string": unicode, - "normalizedunicodeing": unicode, - "token": unicode, - "language": unicode, - "Name": unicode, - "NCNAME": unicode, - "ID": unicode, - "IDREF": unicode, - "IDREFS": unicode, - "ENTITY": unicode, - "ENTITIES": unicode, - "NMTOKEN": unicode, - "NMTOKENS": unicode} + "duration": str, + "dateTime": str, + "time": str, + "date": str, + "gYearMonth": str, + "gYear": str, + "gMonthDay": str, + "gDay": str, + "gMonth": str, + "anyURI": str, + "QName": str, + "NOTATION": str, + "string": str, + "normalizedunicodeing": str, + "token": str, + "language": str, + "Name": str, + "NCNAME": str, + "ID": str, + "IDREF": str, + "IDREFS": str, + "ENTITY": str, + "ENTITIES": str, + "NMTOKEN": str, + "NMTOKENS": str} ## ## We don't support base64Binary or hexBinary ## @@ -1368,7 +1369,7 @@ def occurringIn(self, vars): def __repr__(self): if len(self.string) < 8: return '"%s"' % self.string.encode('unicode_escape') - return unicode('"' + self.string[0:4] + '...' + self.string[-4:] + '"').encode('unicode_escape')# return self.string + return str('"' + self.string[0:4] + '...' + self.string[-4:] + '"').encode('unicode_escape')# return self.string def asPair(self): if self.datatype: @@ -1429,10 +1430,10 @@ def value(self): except KeyError: raise UnknownType( "Attempt to run built-in on unsupported XSD datatype %s of value %s." - % (`self.datatype`, self.string)) + % (repr(self.datatype), self.string)) raise UnknownType("Attempt to run built-in on unknown datatype %s of value %s." - % (`self.datatype`, self.string)) + % (repr(self.datatype), self.string)) def uriref(self): # Unused at present but interesting! 2000/10/14 @@ -1440,7 +1441,7 @@ def uriref(self): return self.asHashURI() #something of a kludge? #return LITERAL_URI_prefix + uri_encode(self.representation()) # tbl preferred -from xmlC14n import Canonicalize # http://dev.w3.org/cvsweb/2001/xmlsec-python/c14n.py?rev=1.10 +from .xmlC14n import Canonicalize # http://dev.w3.org/cvsweb/2001/xmlsec-python/c14n.py?rev=1.10 class XMLLiteral(Literal): """ A Literal is a representation of an RDF literal @@ -1492,7 +1493,7 @@ def classOrder(self): def compareTerm(self, other): "Assume is also a literal - see function compareTerm in formula.py" if self.datatype == other.datatype: - diff = cmp(unicode(self), unicode(other)) + diff = cmp(str(self), str(other)) return diff return cmp(self.lang, other.lang) else: @@ -1573,7 +1574,7 @@ def uriref(self): ## else: ## result.append(str[i]) ## return result -from urllib import quote as uri_encode +from urllib.parse import quote as uri_encode @@ -1616,7 +1617,7 @@ def eval(self, subj, obj, queue, bindings, proof, query): return Function.eval(self, subj, obj, queue, bindings, proof, query) elif isinstance(self, ReverseFunction): return ReverseFunction.eval(self, subj, obj, queue, bindings, proof, query) - raise RuntimeError("Instance %s of built-in has no eval() or subsititue for it" %`self`) + raise RuntimeError("Instance %s of built-in has no eval() or subsititue for it" %repr(self)) class GenericBuiltIn(BuiltIn): def __init__(self, resource, fragid): diff --git a/testViaOtter.py b/testViaOtter.py index 3383a33c..cd08f661 100644 --- a/testViaOtter.py +++ b/testViaOtter.py @@ -3,9 +3,9 @@ import sys import getopt -import LX.old.rdf -import LX.old.toShortNames -import LX.engine.otter +from . import LX.old.rdf +from . import LX.old.toShortNames +from . import LX.engine.otter def otterFormulaFromN3Files(files, base=",n32o"): @@ -58,7 +58,7 @@ def main(): base = a keepBase = 1 if o in ("-g", "--goal"): - print "Got a goal:", a + print("Got a goal:", a) goal = otterFormulaFromN3Files([a], base+"-goal") if len(args) == 0: usage() @@ -70,20 +70,20 @@ def main(): try: proof = LX.engine.otter.run(prem, base+".prem") - print "Otter found an inconsistency in the premises" + print("Otter found an inconsistency in the premises") except LX.engine.otter.SOSEmpty: - print "Otter says: system is consistent (hyperres terminated)" + print("Otter says: system is consistent (hyperres terminated)") if goal: while goal.endswith("\n") or goal.endswith(" ") or goal.endswith("."): goal = goal[:-1] kb = prem + "\n" + "-(" + goal + ").\n" proof = LX.engine.otter.run(kb, base+".all") - print "Otter found a proof!" + print("Otter found a proof!") def usage(): - print """ + print(""" usage: testViaOtter [opts] premise... opts: --keep basename Set the tempfile prefix, and leave temp @@ -93,7 +93,7 @@ def usage(): Always checks for satisfiability of the combined premises. -""" +""") sys.exit(1) if __name__ == "__main__": diff --git a/thing.py b/thing.py index 79349d61..26517f70 100755 --- a/thing.py +++ b/thing.py @@ -15,7 +15,7 @@ """ -from __future__ import generators # for yield + # for yield import string #import re @@ -25,20 +25,20 @@ # import notation3 # N3 parsers and generators, and RDF generator # import sax2rdf # RDF1.0 syntax parser to N3 RDF stream -import urllib # for hasContent -import uripath # DanC's tested and correct one +import urllib.request, urllib.parse, urllib.error # for hasContent +from . import uripath # DanC's tested and correct one import md5, binascii # for building md5 URIs -from uripath import refTo -from RDFSink import runNamespace +from .uripath import refTo +from .RDFSink import runNamespace LITERAL_URI_prefix = "data:text/n3;" -from RDFSink import List_NS -from RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 -from RDFSink import FORMULA, LITERAL, ANONYMOUS, SYMBOL -from RDFSink import Logic_NS +from .RDFSink import List_NS +from .RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 +from .RDFSink import FORMULA, LITERAL, ANONYMOUS, SYMBOL +from .RDFSink import Logic_NS PARTS = PRED, SUBJ, OBJ ALL4 = CONTEXT, PRED, SUBJ, OBJ @@ -69,7 +69,7 @@ store = None storeClass = None -from diag import progress +from .diag import progress progress("Warning: $SWAP/thing.py is obsolete: use term/py and myStore.py") def setStoreClass(c): @@ -393,7 +393,7 @@ def asPair(self): # _nextList = 0 -from diag import verbosity, progress +from .diag import verbosity, progress class CompoundTerm(Term): """A compound term has occurrences of terms within it. @@ -411,7 +411,7 @@ def __init__(self, store, first, rest): # Do not use directly _nextList = _nextList + 1 def uriref(self): - return runNamespace() + "li"+ `self._id` + return runNamespace() + "li"+ repr(self._id) def precededBy(self, first): x = self._prec.get(first, None) @@ -453,7 +453,7 @@ def substitution(self, bindings, why=None): def occurringIn(self, vars): "Which variables in the list occur in this list?" set = [] - if verbosity() > 98: progress("----occuringIn: ", `self`) + if verbosity() > 98: progress("----occuringIn: ", repr(self)) x = self while not isinstance(x, EmptyList): y = x.first @@ -502,7 +502,7 @@ def unify(self, other, vars, existentials, bindings): return self.rest.unify(other.rest, vars, existentials, bindings) def __repr__(self): - return "(" + `self.first` + "...)" + return "(" + repr(self.first) + "...)" class EmptyList(List): @@ -611,7 +611,7 @@ def value(self): if self.datatype is self.store.integer: return int(self.string) if self.datatype is self.store.float: return float(self.string) raise ValueError("Attempt to run built-in on unknown datatype %s of value %s." - % (`x.datatype`, x.string)) + % (repr(x.datatype), x.string)) def uriref(self): # Unused at present but interesting! 2000/10/14 @@ -702,7 +702,7 @@ def eval(self, subj, obj, queue, bindings, proof, query): return Function.eval(self, subj, obj, queue, bindings, proof, query) elif isinstance(self, ReverseFunction): return ReverseFunction.eval(self, subj, obj, queue, bindings, proof, query) - raise RuntimeError("Instance %s of built-in has no eval() or subsititue for it" %`self`) + raise RuntimeError("Instance %s of built-in has no eval() or subsititue for it" %repr(self)) class LightBuiltIn(BuiltIn): """A light built-in is fast and is calculated immediately before searching the store. diff --git a/timegm.py b/timegm.py index 0e65b868..adc71a28 100644 --- a/timegm.py +++ b/timegm.py @@ -5,27 +5,26 @@ # def timegm(tmtuple): - ''' returns epoch seconds from a GMT time tuple. ''' - import calendar - EPOCH = 1970 - year, month, day, hour, minute, second = tmtuple[:6] - if year < EPOCH: - if year < 69: - year = year + 2000 - else: - year = year + 1900 - if year < EPOCH: - raise ValueError, 'invalid year' - if not 1 <= month <= 12: - raise TypeError, 'invalid month' - days = 365 * (year-EPOCH) + calendar.leapdays(EPOCH, year) - for i in range(1, month): - days = days + calendar.mdays[i] - if month > 2 and calendar.isleap(year): - days = days + 1 - days = days + day - 1 - hours = days * 24 + hour - minutes = hours * 60 + minute - seconds = minutes * 60 + second - return seconds - + ''' returns epoch seconds from a GMT time tuple. ''' + import calendar + EPOCH = 1970 + year, month, day, hour, minute, second = tmtuple[:6] + if year < EPOCH: + if year < 69: + year = year + 2000 + else: + year = year + 1900 + if year < EPOCH: + raise ValueError('invalid year') + if not 1 <= month <= 12: + raise TypeError('invalid month') + days = 365 * (year-EPOCH) + calendar.leapdays(EPOCH, year) + for i in range(1, month): + days = days + calendar.mdays[i] + if month > 2 and calendar.isleap(year): + days = days + 1 + days = days + day - 1 + hours = days * 24 + hour + minutes = hours * 60 + minute + seconds = minutes * 60 + second + return seconds diff --git a/toXML.py b/toXML.py index b313f509..e370f114 100755 --- a/toXML.py +++ b/toXML.py @@ -23,8 +23,8 @@ Internationlization: - Decode incoming N3 file as unicode - Encode outgoing file -- unicode \u (??) escapes in parse -- unicode \u (??) escapes in string output +- unicode \\u (??) escapes in parse +- unicode \\u (??) escapes in string output Note currently unicode strings work in this code but fail when they are output into the python debugger @@ -42,27 +42,27 @@ import string import codecs # python 2-ism; for writing utf-8 in RDF/xml output -import urlparse -import urllib +import urllib.parse +import urllib.request, urllib.parse, urllib.error import re import sys #import thing -from uripath import refTo -from diag import progress +from .uripath import refTo +from .diag import progress from random import choice, seed seed(23) -from xmlC14n import Canonicalize +from .xmlC14n import Canonicalize -import RDFSink -from set_importer import Set +from . import RDFSink +from .set_importer import Set -from RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 -from RDFSink import FORMULA, LITERAL, XMLLITERAL, LITERAL_DT, LITERAL_LANG, ANONYMOUS, SYMBOL -from RDFSink import Logic_NS, NODE_MERGE_URI +from .RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 +from .RDFSink import FORMULA, LITERAL, XMLLITERAL, LITERAL_DT, LITERAL_LANG, ANONYMOUS, SYMBOL +from .RDFSink import Logic_NS, NODE_MERGE_URI -from isXML import isXMLChar, NCNameChar, NCNameStartChar, setXMLVersion, getXMLVersion +from .isXML import isXMLChar, NCNameChar, NCNameStartChar, setXMLVersion, getXMLVersion N3_forSome_URI = RDFSink.forSomeSym N3_forAll_URI = RDFSink.forAllSym @@ -216,7 +216,7 @@ def flushStart(self): # if self.namespaces.get("log", ":::") ==":::": # self.bind("log", Logic_NS) ats = [] - ps = self.prefixes.values() + ps = list(self.prefixes.values()) ps.sort() # Cannonicalize output somewhat if self.defaultNamespace and "d" not in self._flags: if "z" in self._flags: @@ -251,7 +251,7 @@ def makeStatement(self, tuple, why=None, aIsPossible=0): elif pred == N3_rest: return # Ignore rest else: raise RuntimeError ("Should only see %s and %s in list mode, for tuple %s and stack %s" - %(N3_first, N3_rest, `tuple`, `self.stack`)) + %(N3_first, N3_rest, repr(tuple), repr(self.stack))) if subj == context: # and context == self._formula: if pred == (SYMBOL, N3_forAll_URI): @@ -261,7 +261,7 @@ def makeStatement(self, tuple, why=None, aIsPossible=0): nid = self._nodeID.get(obj, None) if nid == None and not("b" in self._flags): self._nextnodeID += 1 - nid = 'b'+`self._nextnodeID` + nid = 'b'+repr(self._nextnodeID) self._nodeID[obj] = nid #progress("object is now", obj, nid) return @@ -325,7 +325,7 @@ def makeStatement(self, tuple, why=None, aIsPossible=0): self._xwr.data(v) self._xwr.endElement() else: - raise RuntimeError("Unexpected subject", `subj`) + raise RuntimeError("Unexpected subject", repr(subj)) if obj[0] not in (XMLLITERAL, LITERAL, LITERAL_DT, LITERAL_LANG): nid = self._nodeID.get(obj, None) @@ -656,7 +656,7 @@ def figurePrefix(self, uriref, rawAttrs, prefixes): self._encwr(" %s=\"" % (n, )) if type(v) is type((1,1)): # progress("@@@@@@ toXML.py 382: ", `v`) - v = `v` + v = repr(v) xmldata(self._encwr, v, self.attrEsc) self._encwr("\"") needNL = 1 @@ -725,7 +725,7 @@ def endDocument(self): ##NO = 0 ##STALE = 1 ##FRESH = 2 -import triple_maker +from . import triple_maker tm = triple_maker def swap(List, a, b): @@ -734,8 +734,8 @@ def swap(List, a, b): List[b] = q def findLegal(dict, str): - ns = Set(dict.values()) - s = u'' + ns = Set(list(dict.values())) + s = '' k = len(str) while k and str[k - 1] not in string.ascii_letters: k = k - 1 @@ -809,7 +809,7 @@ def start(self): # if self.namespaces.get("log", ":::") ==":::": # self.bind("log", Logic_NS) ats = [] - ps = self.prefixes.values() + ps = list(self.prefixes.values()) ps.sort() # Cannonicalize output somewhat if self.defaultNamespace and "d" not in self._flags: if "z" in self._flags: @@ -847,7 +847,7 @@ def referenceTo(self, uri): def addNode(self, node, nameLess = 0): if self._modes[-1] == tm.ANONYMOUS and node is not None and self._parts[-1] == tm.NOTHING: - raise ValueError('You put a dot in a bNode:' + `node`) + raise ValueError('You put a dot in a bNode:' + repr(node)) if self._modes[-1] == tm.FORMULA or self._modes[-1] == tm.ANONYMOUS: self._parts[-1] = self._parts[-1] + 1 if self._parts[-1] > 3: @@ -886,7 +886,7 @@ def addNode(self, node, nameLess = 0): try: self._triples[-1][self._parts[-1]] = node except: - print self._parts, " - ", self._triples + print(self._parts, " - ", self._triples) raise if self._modes[-1] == tm.ANONYMOUS and self._pathModes[-1] == True: self.endStatement() @@ -983,7 +983,7 @@ def endStatement(self): elif False: if self._parts[-1] != tm.OBJECT: - raise ValueError('try adding more to the statement' + `self._triples`) + raise ValueError('try adding more to the statement' + repr(self._triples)) if self._pathModes[-1]: swap(self._triples[-1], tm.PREDICATE, tm.OBJECT) @@ -1032,20 +1032,20 @@ def endList(self): self._modes[-1] = tm.ANONYMOUS self.endAnonymous() self._modes.pop() - print '_______________', self._modes + print('_______________', self._modes) def addAnonymous(self, Id): #print '\\\\\\\\', Id a = (ANONYMOUS, Id) if Id not in self._nodeID: - self._nodeID[Id] = 'b'+`self._nextNodeID` + self._nodeID[Id] = 'b'+repr(self._nextNodeID) self._nextNodeID += 1 self.addNode(a) def beginAnonymous(self): self.namedAnonID += 1 - a = (ANONYMOUS, `self.namedAnonID`) + a = (ANONYMOUS, repr(self.namedAnonID)) if self._parts[-1] == tm.NOTHING: self.addNode(a, nameLess = 1) elif self._parts[-1] == tm.PREDICATE: diff --git a/triple_maker.py b/triple_maker.py index 6fd206bb..22d69b74 100644 --- a/triple_maker.py +++ b/triple_maker.py @@ -20,14 +20,14 @@ """ -import diag # problems importing the tracking flag, and chatty_flag must be explicit it seems diag.tracking -from diag import progress, verbosity -from term import BuiltIn, LightBuiltIn, \ +from . import diag # problems importing the tracking flag, and chatty_flag must be explicit it seems diag.tracking +from .diag import progress, verbosity +from .term import BuiltIn, LightBuiltIn, \ HeavyBuiltIn, Function, ReverseFunction, \ Literal, Symbol, Fragment, FragmentNil, Term,\ CompoundTerm, List, EmptyList, NonEmptyList, AnonymousNode -import RDFSink +from . import RDFSink N3_forSome_URI = RDFSink.forSomeSym N3_forAll_URI = RDFSink.forAllSym @@ -134,7 +134,7 @@ def endStatement(self): pass else: if self._parts[-1] != OBJECT: - raise ValueError('try adding more to the statement' + `self._triples`) + raise ValueError('try adding more to the statement' + repr(self._triples)) formula = self.formulas[-1] if self._pathModes[-1]: diff --git a/update.py b/update.py index 9b673704..f2991ae8 100644 --- a/update.py +++ b/update.py @@ -8,12 +8,12 @@ """ -from set_importer import Set, sorted +from .set_importer import Set, sorted -import diag -from diag import chatty_flag, tracking, progress -from formula import Formula -from query import Query, Rule, seqToString, _substitute +from . import diag +from .diag import chatty_flag, tracking, progress +from .formula import Formula +from .query import Query, Rule, seqToString, _substitute def patch(workingContext, patchFormula): @@ -65,11 +65,11 @@ def patch(workingContext, patchFormula): templateExistentials.add(x) if diag.chatty_flag >20: progress("New Patch =========== applied to %s" %(workingContext) ) - for s in lhs.statements: progress(" ", `s`) + for s in lhs.statements: progress(" ", repr(s)) progress("+=>") - for s in conclusion.statements: progress(" ", `s`) + for s in conclusion.statements: progress(" ", repr(s)) progress("-=>") - for s in retraction.statements: progress(" ", `s`) + for s in retraction.statements: progress(" ", repr(s)) progress("Universals declared in outer " + seqToString(patchFormula.universals())) progress(" mentioned in template " + seqToString(variablesMentioned)) progress(" also used in conclusion " + seqToString(variablesUsed)) @@ -135,7 +135,7 @@ def conclude(self, bindings, evidence = [], extraBNodes=Set(), allBindings=None) raise RuntimeError( """Error: %i matches removing statement {%s %s %s} bound as {%s %s %s} from %s""" % - (len(ss),s,p,o, `subj`, pred.uriref(), `obj`, self.workingContext)) + (len(ss),s,p,o, repr(subj), pred.uriref(), repr(obj), self.workingContext)) if diag.chatty_flag > 25: progress("Deleting %s" % ss[0]) self.workingContext.removeStatement(ss[0]) self.justOne = 1 # drop out of here when done diff --git a/uripath.py b/uripath.py index c6d62ebb..23497f91 100644 --- a/uripath.py +++ b/uripath.py @@ -22,7 +22,7 @@ __version__ = "$Id$" -from string import find, rfind, index +# from string import find, rfind, index def splitFrag(uriref): @@ -31,7 +31,7 @@ def splitFrag(uriref): Punctuation is thrown away. e.g. - + >>> splitFrag("abc#def") ('abc', 'def') @@ -40,7 +40,7 @@ def splitFrag(uriref): """ - i = rfind(uriref, "#") + i = uriref.rfind("#") if i>= 0: return uriref[:i], uriref[i+1:] else: return uriref, None @@ -48,7 +48,7 @@ def splitFragP(uriref, punct=0): """split a URI reference before the fragment Punctuation is kept. - + e.g. >>> splitFragP("abc#def") @@ -59,7 +59,7 @@ def splitFragP(uriref, punct=0): """ - i = rfind(uriref, "#") + i = uriref.rfind("#") if i>= 0: return uriref[:i], uriref[i:] else: return uriref, '' @@ -85,40 +85,40 @@ def join(here, there): >>> join('http://example/x/y/z', '') 'http://example/x/y/z' - + >>> join('mid:foo@example', '#foo') 'mid:foo@example#foo' - + We grok IRIs >>> len(u'Andr\\xe9') 5 - + >>> join('http://example.org/', u'#Andr\\xe9') u'http://example.org/#Andr\\xe9' """ - assert(find(here, "#") < 0), "Base may not contain hash: '%s'"% here # caller must splitFrag (why?) + assert(here.find("#") < 0), "Base may not contain hash: '%s'"% here # caller must splitFrag (why?) - slashl = find(there, '/') - colonl = find(there, ':') + slashl = there.find('/') + colonl = there.find(':') # join(base, 'foo:/') -- absolute if colonl >= 0 and (slashl < 0 or colonl < slashl): return there - bcolonl = find(here, ':') + bcolonl = here.find(':') assert(bcolonl >= 0), "Base uri '%s' is not absolute" % here # else it's not absolute path, frag = splitFragP(there) if not path: return here + frag - + # join('mid:foo@example', '../foo') bzzt - if here[bcolonl+1:bcolonl+2] <> '/': + if here[bcolonl+1:bcolonl+2] != '/': raise ValueError ("Base <%s> has no slash after colon - with relative '%s'." %(here, there)) if here[bcolonl+1:bcolonl+3] == '//': - bpath = find(here, '/', bcolonl+3) + bpath = here.find('/', bcolonl+3) else: bpath = bcolonl+1 @@ -135,7 +135,7 @@ def join(here, there): if there[:1] == '/': return here[:bpath] + there - slashr = rfind(here, '/') + slashr = here.find('/') while 1: if path[:2] == './': @@ -144,7 +144,7 @@ def join(here, there): path = '' elif path[:3] == '../' or path == '..': path = path[3:] - i = rfind(here, '/', bpath, slashr) + i = here.find('/', bpath, slashr) if i >= 0: here = here[:i+1] slashr = i @@ -154,7 +154,7 @@ def join(here, there): return here[:slashr+1] + path + frag - + import re import string commonHost = re.compile(r'^[-_a-zA-Z0-9.]+:(//[^/]*)?/[^/]*$') @@ -168,7 +168,7 @@ def refTo(base, uri): >>> refTo('file:/ex/x/y', 'file:/ex/x/q/r#s') 'q/r#s' - + >>> refTo(None, 'http://ex/x/y') 'http://ex/x/y' @@ -184,7 +184,7 @@ def refTo(base, uri): So 'http://ex/x/q:r' is not a URI. Use 'http://ex/x/q%3ar' instead: >>> x='http://ex/x/y';y='http://ex/x/q%3ar';join(x, refTo(x, y)) == y 1 - + This one checks that it uses a root-realtive one where that is all they share. Now uses root-relative where no path is shared. This is a matter of taste but tends to give more resilience IMHO @@ -203,7 +203,7 @@ def refTo(base, uri): # assert base # don't mask bugs -danc # not a bug. -tim if not base: return uri if base == uri: return "" - + # Find how many path segments in common i=0 while i0 and uri[i-1] != '/' : i=i-1 # scan for slash if i < 3: return uri # No way. - if string.find(base, "//", i-2)>0 \ - or string.find(uri, "//", i-2)>0: return uri # An unshared "//" - if string.find(base, ":", i)>0: return uri # An unshared ":" + if base.find("//", i-2)>0 \ + or uri.find("//", i-2)>0: return uri # An unshared "//" + if base.find(":", i)>0: return uri # An unshared ":" n = string.count(base, "/", i) if n == 0 and i>> canonical("foo bar") 'foo%20bar' - + >>> canonical(u'http:') 'http:' - + >>> canonical('fran%c3%83%c2%a7ois') 'fran%C3%83%C2%A7ois' - + >>> canonical('a') 'a' - + >>> canonical('%4e') 'N' >>> canonical('%9d') '%9D' - + >>> canonical('%2f') '%2F' @@ -305,7 +305,7 @@ def canonical(str_in): 'aaa%2Fbbb' """ - if type(str_in) == type(u''): + if type(str_in) == type(''): s8 = str_in.encode('utf-8') else: s8 = str_in @@ -330,8 +330,8 @@ def canonical(str_in): s += ch i = i +1 return s - - + + import unittest class Tests(unittest.TestCase): @@ -363,7 +363,7 @@ def testPaths(self): ('file:/ex/x/y/', 'file:/ex/x/y/', ''), ('file:/ex/x/y/pdq', 'file:/ex/x/y/pdq', ''), ('file:/ex/x/y/', 'file:/ex/x/y/z/', 'z/'), - ('file:/devel/WWW/2000/10/swap/test/reluri-1.n3', + ('file:/devel/WWW/2000/10/swap/test/reluri-1.n3', 'file://meetings.example.com/cal#m1', 'file://meetings.example.com/cal#m1'), ('file:/home/connolly/w3ccvs/WWW/2000/10/swap/test/reluri-1.n3', 'file://meetings.example.com/cal#m1', 'file://meetings.example.com/cal#m1'), ('file:/some/dir/foo', 'file:/some/dir/#blort', './#blort'), @@ -379,8 +379,8 @@ def testPaths(self): ) for inp1, inp2, exp in cases: - self.assertEquals(refTo(inp1, inp2), exp) - self.assertEquals(join(inp1, exp), inp2) + self.assertEqual(refTo(inp1, inp2), exp) + self.assertEqual(join(inp1, exp), inp2) def testSplit(self): @@ -394,7 +394,7 @@ def testSplit(self): ("abc#de/f", "abc", "de/f"), ) for inp, exp1, exp2 in cases: - self.assertEquals(splitFrag(inp), (exp1, exp2)) + self.assertEqual(splitFrag(inp), (exp1, exp2)) def testRFCCases(self): @@ -426,7 +426,7 @@ def testRFCCases(self): (base, '../../', 'http://a/'), (base, '../../g', 'http://a/g') ) - + otherExamples = ( (base, '', base), (base, '../../../g', 'http://a/g'), #@@disagree with RFC2396 @@ -437,25 +437,25 @@ def testRFCCases(self): (base, '.g', 'http://a/b/c/.g'), (base, 'g..', 'http://a/b/c/g..'), (base, '..g', 'http://a/b/c/..g'), - + (base, './../g', 'http://a/b/g'), (base, './g/.', 'http://a/b/c/g/.'), #@@hmmm... (base, 'g/./h', 'http://a/b/c/g/./h'), #@@hmm... (base, 'g/../h', 'http://a/b/c/g/../h'), (base, 'g;x=1/./y', 'http://a/b/c/g;x=1/./y'), #@@hmmm... (base, 'g;x=1/../y', 'http://a/b/c/g;x=1/../y'), #@@hmmm... - + (base, 'g?y/./x', 'http://a/b/c/g?y/./x'), (base, 'g?y/../x', 'http://a/b/c/g?y/../x'), (base, 'g#s/./x', 'http://a/b/c/g#s/./x'), (base, 'g#s/../x', 'http://a/b/c/g#s/../x') ) - + for b, inp, exp in normalExamples + otherExamples: if exp is None: self.assertRaises(ValueError, join, b, inp) else: - self.assertEquals(join(b, inp), exp) + self.assertEqual(join(b, inp), exp) def _test(): import doctest, uripath diff --git a/vf2.py b/vf2.py index 78017d24..c266b3bc 100644 --- a/vf2.py +++ b/vf2.py @@ -55,7 +55,7 @@ def progress(*x): pass class Problem(object): def __new__(cls, **keywords): self = object.__new__(cls) - for k, v in keywords.items(): + for k, v in list(keywords.items()): setattr(self, k, v) return self @@ -106,7 +106,7 @@ def P(self): __iter__ = choices def __repr__(self): - return u'%s(%s)' % (self.__class__.__name__, self._b) + return '%s(%s)' % (self.__class__.__name__, self._b) def __len__(self): return len(self._b) @@ -124,7 +124,7 @@ class OrderedNodeSet(set): 1 """ def first(self): - return iter(sorted(self)).next() + return next(iter(sorted(self))) class State(object): @@ -150,18 +150,18 @@ def undo(self): def _update(self): G1, G2 = self.problem.G1, self.problem.G2 self.reverseMap = {} - for k, v in self.map.items(): + for k, v in list(self.map.items()): self.reverseMap[v] = k self.t1_in = OrderedNodeSet() self.t1_out = OrderedNodeSet() self.t2_in = OrderedNodeSet() self.t2_out = OrderedNodeSet() - for node1, node2 in self.map.items(): - self.t1_in.update(x for x in G1.predecessors(node1).keys() if x not in self.map) - self.t1_out.update(x for x in G1.followers(node1).keys() if x not in self.map) - self.t2_in.update(x for x in G2.predecessors(node2).keys() if x not in self.reverseMap) - self.t2_out.update(x for x in G2.followers(node2).keys() if x not in self.reverseMap) + for node1, node2 in list(self.map.items()): + self.t1_in.update(x for x in list(G1.predecessors(node1).keys()) if x not in self.map) + self.t1_out.update(x for x in list(G1.followers(node1).keys()) if x not in self.map) + self.t2_in.update(x for x in list(G2.predecessors(node2).keys()) if x not in self.reverseMap) + self.t2_out.update(x for x in list(G2.followers(node2).keys()) if x not in self.reverseMap) self.G1_not_taken = OrderedNodeSet(G1.nodes() - set(self.map)) self.G2_not_taken = OrderedNodeSet(G2.nodes() - set(self.reverseMap)) @@ -275,7 +275,7 @@ def F(s, n, m, extras): termin1, termout1, termin2, termout2, new1, new2 = 0,0,0,0,0,0 - for obj, preds in G1.followers(n).items(): + for obj, preds in list(G1.followers(n).items()): if obj in s.map: image = s.map[obj] e = G2.edge(m, image) @@ -293,7 +293,7 @@ def F(s, n, m, extras): if obj not in s.t1_in and obj not in s.t1_out: new1 += 1 - for subj, preds in G1.predecessors(n).items(): + for subj, preds in list(G1.predecessors(n).items()): if subj in s.map: image = s.map[subj] e = G2.edge(image, m) @@ -312,7 +312,7 @@ def F(s, n, m, extras): new1 += 1 - for obj, preds in G2.followers(m).items(): + for obj, preds in list(G2.followers(m).items()): progress("checking out %s's follower %s" % (m, obj)) if obj in s.reverseMap: image = s.reverseMap[obj] @@ -331,7 +331,7 @@ def F(s, n, m, extras): if obj not in s.t2_in and obj not in s.t2_out: new2 += 1 - for subj, preds in G2.predecessors(m).items(): + for subj, preds in list(G2.predecessors(m).items()): if subj in s.reverseMap: image = s.reverseMap[subj] e = G1.edge(image, n) @@ -405,7 +405,7 @@ def trueEasyMatches(self, s, n1, n2, newBindings=BindingTree(), boring=True): ## easy cases are easy if len(n1) == 1: progress('Size 1 --- easy') - return self(s, iter(n1).next(), iter(n2).next(), newBindings, False) + return self(s, next(iter(n1)), next(iter(n2)), newBindings, False) ## knock out everything we know for x1 in n1: if x1 in s.map and s.map[x1] not in n2: @@ -451,7 +451,7 @@ def trueEasyMatches(self, s, n1, n2, newBindings=BindingTree(), boring=True): b = [] s1 = list(s1) for k in orderings(list(s2)): - b.append(zip(s1, k)) + b.append(list(zip(s1, k))) newBindings.int_or(b) progress('After that, newBindings=%s' % newBindings) @@ -495,11 +495,11 @@ def representsSelf(x): return isLiteral(x) or isSymbol(x) def isLiteral(x): - return isinstance(x, (str, unicode)) and x[0:1] == '"' + return isinstance(x, str) and x[0:1] == '"' def isSymbol(x): - return isinstance(x, (str, unicode)) and x[0:1] == '<' + return isinstance(x, str) and x[0:1] == '<' def isExistential(x): - return isinstance(x, (str, unicode)) and x[0:1] == '_' + return isinstance(x, str) and x[0:1] == '_' def isUniversal(x): return False def isList(x): @@ -516,13 +516,13 @@ def hardMatches(s, n1, n2, newBindings=BindingTree(), boring=True): matches = unifyFormulae(n1, n2) ###@@@ bindings from higher up need to be taken into account if not matches: return False - newBindings.int_or([x.items() for x in matches]) + newBindings.int_or([list(x.items()) for x in matches]) return True return True ### stolen from cant.py -import re, os, urllib +import re, os, urllib.request, urllib.parse, urllib.error from sys import stderr name = "[A-Za-z][A-Za-z0-9]*" #http://www.w3.org/TR/rdf-testcases/#ntriples @@ -599,7 +599,7 @@ def parseFile(self, name): if verbose: stderr.write("Loading data from %s\n" % name) uri = uripath.join(WD, name) - inStream = urllib.urlopen(uri) + inStream = urllib.request.urlopen(uri) for line in inStream: if line == "" : break # if verbose: stderr.write("%s\n" % line) @@ -648,7 +648,7 @@ def main(): pr = Problem(G1=g1,G2=g2) s = State(pr) for m in match(s): - print m + print(m) if __name__ == '__main__': import sys diff --git a/webAccess.py b/webAccess.py index 2f14ce99..7b4fb134 100755 --- a/webAccess.py +++ b/webAccess.py @@ -15,16 +15,16 @@ import sys, os #import urllib -import urllib2, urllib # Python standard +import urllib.request, urllib.error, urllib.parse, urllib.request, urllib.parse, urllib.error # Python standard -from why import newTopLevelFormula +from .why import newTopLevelFormula -import uripath # http://www.w3.org/2000/10/swap/uripath.py -import diag -from diag import progress -import notation3 # Parser @@@ Registery of parsers vs content types woudl be better. +from . import uripath # http://www.w3.org/2000/10/swap/uripath.py +from . import diag +from .diag import progress +from . import notation3 # Parser @@@ Registery of parsers vs content types woudl be better. -from OrderedSequence import indentString +from .OrderedSequence import indentString HTTP_Content_Type = 'content-type' #@@ belongs elsewhere? @@ -88,9 +88,9 @@ def webget(addr, referer=None, types=[]): # buggy in 2.4.2 with CStringIO if addr[:5] == 'data:': # return open_data(addr) - return urllib.urlopen(addr) + return urllib.request.urlopen(addr) - req = urllib2.Request(addr) + req = urllib.request.Request(addr) if types: req.add_header('Accept', ','.join(types)) @@ -98,7 +98,7 @@ def webget(addr, referer=None, types=[]): if referer: #consistently misspelt req.add_header('Referer', referer) - stream = urllib2.urlopen(req) + stream = urllib.request.urlopen(req) if print_all_file_names: diag.file_list.append(addr) @@ -129,7 +129,7 @@ def load(store, uri=None, openFormula=None, asIfFrom=None, contentType=None, if diag.chatty_flag > 40: progress("Taking input from " + addr) netStream = urlopenForRDF(addr, referer) if diag.chatty_flag > 60: - progress(" Headers for %s: %s\n" %(addr, netStream.headers.items())) + progress(" Headers for %s: %s\n" %(addr, list(netStream.headers.items()))) receivedContentType = netStream.headers.get(HTTP_Content_Type, None) else: if diag.chatty_flag > 40: progress("Taking input from standard input") @@ -144,7 +144,7 @@ def load(store, uri=None, openFormula=None, asIfFrom=None, contentType=None, guess = None if receivedContentType: if diag.chatty_flag > 9: - progress("Recieved Content-type: " + `receivedContentType` + " for "+addr) + progress("Recieved Content-type: " + repr(receivedContentType) + " for "+addr) if receivedContentType.find('xml') >= 0 or ( receivedContentType.find('rdf')>=0 and not (receivedContentType.find('n3')>=0) ): @@ -153,7 +153,7 @@ def load(store, uri=None, openFormula=None, asIfFrom=None, contentType=None, guess = "text/n3" if guess== None and contentType: if diag.chatty_flag > 9: - progress("Given Content-type: " + `contentType` + " for "+addr) + progress("Given Content-type: " + repr(contentType) + " for "+addr) if contentType.find('xml') >= 0 or ( contentType.find('rdf') >= 0 and not (contentType.find('n3') >= 0 )): guess = "application/rdf+xml" @@ -188,11 +188,11 @@ def load(store, uri=None, openFormula=None, asIfFrom=None, contentType=None, import os if guess == "x-application/sparql": if diag.chatty_flag > 49: progress("Parsing as SPARQL") - from sparql import sparql_parser - import sparql2cwm + from .sparql import sparql_parser + from . import sparql2cwm convertor = sparql2cwm.FromSparql(store, F, why=why) - import StringIO - p = sparql_parser.N3Parser(StringIO.StringIO(buffer), sparql_parser.branches, convertor) + import io + p = sparql_parser.N3Parser(io.StringIO(buffer), sparql_parser.branches, convertor) F = p.parse(sparql_parser.start).close() elif guess == 'application/rdf+xml': if diag.chatty_flag > 49: progress("Parsing as RDF") @@ -203,7 +203,7 @@ def load(store, uri=None, openFormula=None, asIfFrom=None, contentType=None, flags = '' else: parser = os.environ.get("CWM_RDF_PARSER", "sax2rdf") - import rdfxml + from . import rdfxml p = rdfxml.rdfxmlparser(store, F, thisDoc=asIfFrom, flags=flags, parser=parser, why=why) @@ -213,8 +213,8 @@ def load(store, uri=None, openFormula=None, asIfFrom=None, contentType=None, assert guess == 'text/n3' if diag.chatty_flag > 49: progress("Parsing as N3") if os.environ.get("CWM_N3_PARSER", 0) == 'n3p': - import n3p_tm - import triple_maker + from . import n3p_tm + from . import triple_maker tm = triple_maker.TripleMaker(formula=F, store=store) p = n3p_tm.n3p_tm(asIfFrom, tm) else: @@ -284,11 +284,11 @@ def open_data(url, data=None): # data := *urlchar # parameter := attribute "=" value import mimetools, time - from StringIO import StringIO + from io import StringIO try: [type, data] = url.split(',', 1) except ValueError: - raise IOError, ('data error', 'bad data URL') + raise IOError('data error', 'bad data URL') if not type: type = 'text/plain;charset=US-ASCII' semi = type.rfind(';') @@ -330,13 +330,13 @@ def getParser(format, inputURI, workingContext, flags): if format == "rdf" : touch(_store) if "l" in flags["rdf"]: - from rdflib2rdf import RDFXMLParser + from .rdflib2rdf import RDFXMLParser else: rdfParserName = os.environ.get("CWM_RDF_PARSER", "sax2rdf") if rdfParserName == "rdflib2rdf": - from rdflib2rdf import RDFXMLParser + from .rdflib2rdf import RDFXMLParser elif rdfParserName == "sax2rdf": - from sax2rdf import RDFXMLParser + from .sax2rdf import RDFXMLParser else: raise RuntimeError("Unknown RDF parser: " + rdfParserName) return RDFXMLParser(_store, workingContext, inputURI, diff --git a/why.py b/why.py index 50503ecf..fecf2012 100755 --- a/why.py +++ b/why.py @@ -11,7 +11,7 @@ """ flagHelp = """ -n use iNdirect formulas when they get too large +n use iNdirect formulas when they get too large g always give the :gives clause, even when not needed """ @@ -20,27 +20,27 @@ #import StringIO import sys import weakref -from set_importer import Set, sorted +# from .set_importer import Set, sorted # import notation3 # N3 parsers and generators, and RDF generator # import sax2rdf # RDF1.0 syntax parser to N3 RDF stream -import urllib # for hasContent -import uripath # DanC's tested and correct one +import urllib.request, urllib.parse, urllib.error # for hasContent +from . import uripath # DanC's tested and correct one -from uripath import refTo -from myStore import Namespace -from term import Literal, CompoundTerm, AnonymousNode, List +from .uripath import refTo +from .myStore import Namespace +from .term import Literal, CompoundTerm, AnonymousNode, List # from formula import Formula -import diag -from diag import verbosity, progress +from . import diag +from .diag import verbosity, progress REIFY_NS = 'http://www.w3.org/2004/06/rei#' #from RDFSink import CONTEXT, PRED, SUBJ, OBJ, PARTS, ALL4 #from RDFSink import FORMULA, LITERAL, ANONYMOUS, SYMBOL -from RDFSink import runNamespace +from .RDFSink import runNamespace rdf=Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") @@ -59,9 +59,9 @@ def smushedFormula(F, G): """The formula F has been replaced by G - + Because this module tracks formula in stores, if ever the address - of a formula is changed, that is (currently) when it is + of a formula is changed, that is (currently) when it is canonicalized, then the fact must be reported here. """ progress("why: Formula %s has been replaced by %s" %(F,G)) @@ -81,14 +81,14 @@ def isTopLevel(f): def report(statement, why): """Report a new statement to the reason tracking software - + This module stores the reasons. The store should call this method to report new data. See the KBReasonTracker class Most formulas don't need collectors. Let's see if we can change that. """ if why is dontAsk: return - + f = statement.context() collectors = proofsOf.get(f, []) @@ -103,11 +103,11 @@ def report(statement, why): raise RuntimeError("""More than one tracker for formula %s. Means formula must already have been closed, so shouldn't be added to.""" % f) - + else: collector = collectors[0] - + return collector.newStatement(statement, why) - + def explainFormula(f, flags=""): "Return the explanation formula for f" tr = proofsOf.get(f, None) @@ -119,7 +119,7 @@ def explainFormula(f, flags=""): try: return tr[0].explanation(flags=flags) except: - print f.debugString() + print(f.debugString()) raise @@ -127,11 +127,11 @@ def explainFormula(f, flags=""): def _giveTerm(x, ko): """Describe a term in a proof - + This reifies symbols and bnodes. Internal utility """ #" - from formula import Formula + from .formula import Formula if isinstance(x, Formula): b = ko.newBlankNode(why=dontAsk) x = x.close() # per timbl Tue, 15 Dec 2009 08:53:04 -0500 @@ -150,7 +150,7 @@ def _giveTerm(x, ko): def _subsetFormula(ss): """Return a subset formula containing the given statements - + The statements are all in the same context.""" #. s = ss.pop() # @@ better way to pick one? f=s.context() @@ -160,16 +160,16 @@ def _subsetFormula(ss): try: g.add(s.subject(), s.predicate(), s.object(), why=dontAsk) except: - print s - print s.__class__ - print s.quad + print(s) + print(s.__class__) + print(s.quad) raise # progress("&&&&&&&&&&&&&&&&&&&&&&& ", g.n3String()) #@@@@@@@@@@ g._existentialVariables = g.occurringIn(f._existentialVariables) g._universalVariables = g.occurringIn(f._universalVariables) g = g.close() return g - + class Reason: """The Reason class holds a reason for having some information. Well, its subclasses actually do hold data. This class should not be used @@ -193,7 +193,7 @@ def explain(self, ko, flags): Returns the value of this object as interned in the store. """ raise RuntimeError("What, no explain method for this class?") - + MAX_INLINE_SIZE = 10 @@ -207,7 +207,7 @@ def formulaStandIn(self, ko,f, flags): #progress('cache hit, we save $$!') return m except KeyError: - from formula import Formula, StoredStatement + from .formula import Formula, StoredStatement if 'l' in flags: standIn = ko.newBlankNode(why= dontAsk) else: @@ -219,25 +219,25 @@ def formulaStandIn(self, ko,f, flags): return standIn formulaStandIn = formulaStandIn.__get__({}) -formulaStandIn.im_self[id(formulaStandIn.im_self)] = 0 +formulaStandIn.__self__[id(formulaStandIn.__self__)] = 0 + - class KBReasonTracker(Reason): """A reason tracker tracks the reasons for the statements in its formula. - + Beware that when a new formula is interned, the proofsOf dict must be informed that its identity has changed. The ForumulaReason is informed of each statement added to the knowlege base. - + A knowledge base (open formula) is made from the addition of forumlae, which result from, for example parsing a document or doing inference. Within such added formulae, there are variables, including bnodes, which have a cetain scope. It is impossible to consider the process as being one of simply adding statements, as the cross-reference of - the vaiables within the add formuls mst be preserved. - Variable renaming may occur as thr formula is added. - + the vaiables within the add formuls mst be preserved. + Variable renaming may occur as thr formula is added. + When we track these operations for generating a proof, a proof reason such as an BecauseOfRule or BecauseOfData corresponds to an added formula. The KBReasonTracker tracks which statements in a formula came from which @@ -257,7 +257,7 @@ def __init__(self, formula=None): def newStatement(self, s, why): if verbosity() > 80 and why is not dontAsk: progress("Believing %s because of %s"%(s, why)) - import formula + from . import formula for x in s.quad[1:]: if isinstance(x, formula.Formula): if x.canonical is not x: @@ -270,7 +270,7 @@ def newStatement(self, s, why): def explanation(self, ko=None, flags=""): """Produce a justification for this formula into the output formula - + Creates an output formula if necessary. returns it. (This is different from reason.explain(ko) which returns the reason)""" @@ -283,7 +283,7 @@ def explanation(self, ko=None, flags=""): me=self.explain(ko, flags) ko.add(me, rdf.type, reason.Proof, why=dontAsk) return ko - + def explain(self, ko, flags): me = self.me.get(ko, None) if me != None: return me # Only do this once @@ -292,12 +292,12 @@ def explain(self, ko, flags): g = self.formula e = g.existentials() if g.occurringIn(e) != e: raise RuntimeError(g.debugString()) - + qed = ko.newBlankNode(why= dontAsk) standIn = formulaStandIn(ko,self.formula, flags) ko.add(subj=me, pred=reason.gives, obj=standIn, why=dontAsk) - - + + statementsForReason = {} # reverse index: group by reason for s, rea in sorted(self.reasonForStatement.items()): x = statementsForReason.get(rea, None) @@ -311,14 +311,14 @@ def explain(self, ko, flags): progress("statementsForReason", statementsForReason) # @@ Should special case (no conjunction) if only one r if len(statementsForReason) != 1: - ko.add(subj=me, pred=rdf.type, obj=reason.Conjunction, why=dontAsk) - - for r, ss in statementsForReason.items(): + ko.add(subj=me, pred=rdf.type, obj=reason.Conjunction, why=dontAsk) + + for r, ss in list(statementsForReason.items()): assert r is not self, ("Loop in reasons!", self, id(self), s) try: r1 = r.explain(ko, flags=flags) except: - print ss + print(ss) raise if diag.chatty_flag > 29: progress( @@ -335,29 +335,29 @@ class BecauseMerge(KBReasonTracker): def __init__(self, f, set): raise RuntimeError(f, set) KBReasonTracker.__init__(self, f) - self.fodder = Set() + self.fodder = set() def newStatement(self, s, why): # Why isn't a reason here, it is the source if verbosity() > 80:progress("Merge: Believing %s because of merge"%(s)) self.fodder.add(why) self.reasonForStatement[s] = why - + def explain(self, ko, flags): me = self.me.get(ko, None) if me != None: return me # Only do this once me = self.meIn(ko) qed = ko.newBlankNode(why= dontAsk) - ko.add(subj=me, pred=rdf.type, obj=reason.Conjunction, why=dontAsk) + ko.add(subj=me, pred=rdf.type, obj=reason.Conjunction, why=dontAsk) if 'g' in flags: standIn = formulaStandIn(ko,self.formula, flags=flags) ko.add(subj=me, pred=reason.gives, obj=standIn, why=dontAsk) for x in self.fodder: - ko.add(subj=me, pred=reason.mergeOf, obj=x.explain(ko, flags), why=dontAsk) + ko.add(subj=me, pred=reason.mergeOf, obj=x.explain(ko, flags), why=dontAsk) return me class BecauseSubexpression(Reason): """This was generated as part of a calculatio of a subexpression. - + It is is not necessarily believed""" def explain(self, ko, flags): @@ -379,7 +379,7 @@ def explain(self, ko, flags): class Because(Reason): """For the reason given on the string. This is a kinda end of the road reason. - + A nested reason can also be given. """ def __init__(self, str, because=None): @@ -409,7 +409,7 @@ class Premise(Reason): """For the reason given on the string. This is a kinda end of the road reason. It contais the info which was literally supplied as a premise. - + A nested reason can also be given. Because a premise has to be taken for granted, the tracker has to tell a Premis what statements it has. @@ -418,7 +418,7 @@ def __init__(self, str, because=None): Reason.__init__(self) self._string = str self._reason = because - self.statements = Set() + self.statements = set() return def explain(self, ko, flags): @@ -441,7 +441,7 @@ def explain(self, ko, flags): standIn = formulaStandIn(ko,prem, flags=flags) ko.add(me, reason.gives, standIn, why=dontAsk) if diag.chatty_flag >59: - progress("Premise (%s) is:\n%s" % + progress("Premise (%s) is:\n%s" % ( self._string, prem.n3String())) return me @@ -458,7 +458,7 @@ def __init__(self, rule, bindings, knownExistentials, evidence, kb, because=None self._kb = kb # The formula the rule was trusting at base self._reason = because self._existentials = knownExistentials - self.statements = Set() + self.statements = set() return @@ -470,8 +470,8 @@ def explain(self, ko, flags): if me != None: return me # Only do this once me = self.meIn(ko) if diag.chatty_flag>49: progress("Inference=%s ko=%s"%(self,me)) - ko.add(subj=me, pred=rdf.type, obj=reason.Inference, why=dontAsk) - for var, (val, source) in self._bindings.items(): + ko.add(subj=me, pred=rdf.type, obj=reason.Inference, why=dontAsk) + for var, (val, source) in list(self._bindings.items()): b = ko.newBlankNode(why= dontAsk) ko.add(subj=me, pred=reason.binding, obj=b, why= dontAsk) ko.add(subj=b, pred=reason.variable, @@ -482,11 +482,11 @@ def explain(self, ko, flags): if val in self._existentials: ko.add(subj=valObject, pred=rdf.type, obj=reason.Existential, why=dontAsk) - + if diag.chatty_flag>49: progress("rule:") ru = explainStatement(self._rule,ko, flags=flags) ko.add(subj=me, pred=reason.rule, obj=ru, why=dontAsk) - + if diag.chatty_flag>49: progress("evidence:") ev = [] # For PML compatability we will store it as a collection realStatements = [] @@ -495,7 +495,7 @@ def explain(self, ko, flags): try: e = s.explain(ko, flags=flags) except: - print s + print(s) raise ev.append(e) else: @@ -506,13 +506,13 @@ def explain(self, ko, flags): if s.predicate() is f.store.includes: for t in self.evidence: if t.context() is s.subject(): - progress("Included statement used:" + `t`) + progress("Included statement used:" + repr(t)) try: ko.add(e, reason.includeEvidence, - explainStatement(t, ko)) + explainStatement(t, ko)) except: - print s - raise + print(s) + raise # else: # progress("Included statement found:" + `s`) ev.extend(explainStatements(realStatements, ko)) @@ -522,7 +522,7 @@ def explain(self, ko, flags): standIn = formulaStandIn(ko,prem, flags=flags) ko.add(me, reason.gives, standIn, why=dontAsk) if diag.chatty_flag >59: - progress("Rule (%s) is:\n%s" % + progress("Rule (%s) is:\n%s" % ( self._string, prem.n3String())) return me @@ -534,22 +534,22 @@ def getStatementReason(s): if KBReasonTrackers is None: raise RuntimeError( - """Ooops, no reason collector for this formula?! + """Ooops, no reason collector for this formula?! No proof for formula: %s Needed for statement: %s Only have proofs for %s. Formula contents as follows: %s - """ % ( f, s, dict(proofsOf), f.debugString())) + """ % ( f, s, dict(proofsOf), f.debugString())) tracker = KBReasonTrackers[0] return tracker.reasonForStatement.get(s, None) - + def subFormulaStandIn(self, ko,f, flags): if 'n' not in flags: return f - from formula import Formula + from .formula import Formula if isinstance(f, List): return ko.store.newList([subFormulaStandIn(ko,x, flags) for x in f]) if not isinstance(f, Formula) or len(f) <= MAX_INLINE_SIZE: @@ -569,7 +569,7 @@ def subFormulaStandIn(self, ko,f, flags): return standIn subFormulaStandIn = subFormulaStandIn.__get__({}) -subFormulaStandIn.im_self[id(subFormulaStandIn.im_self)] = 0 +subFormulaStandIn.__self__[id(subFormulaStandIn.__self__)] = 0 def explainStatement(s, ko, ss=None, flags=""): si = describeStatement([s], ko, flags=flags) @@ -579,7 +579,7 @@ def explainStatement(s, ko, ss=None, flags=""): if statementReason == None: raise RuntimeError( - """Ooops, no reason for this statement?! + """Ooops, no reason for this statement?! Collector: %s Formula: %s No reason for statement: %s @@ -587,7 +587,7 @@ def explainStatement(s, ko, ss=None, flags=""): Formula contents as follows: %s """ % (tracker, f, s, tracker.reasonForStatement, - f.debugString())) + f.debugString())) if diag.chatty_flag >49: progress("explaining statement: %s" % (s)) ri = statementReason.explain(ko, flags=flags) @@ -604,7 +604,7 @@ def explainStatements(s_l, ko, ss=None, flags=""): if statementReason == None: raise RuntimeError( - """Ooops, no reason for this statement?! + """Ooops, no reason for this statement?! Collector: %s Formula: %s No reason for statement: %s @@ -615,17 +615,17 @@ def explainStatements(s_l, ko, ss=None, flags=""): f.debugString())) reasonsForStatement.setdefault(statementReason, []).append(s) si_l = [] - for r, statements in reasonsForStatement.items(): + for r, statements in list(reasonsForStatement.items()): si = describeStatement(statements, ko, flags=flags) si_l.append(si) ri = r.explain(ko, flags=flags) ko.add(subj=si, pred=reason.because, obj=ri, why=dontAsk) return si_l - + def describeStatement(s_l, ko, flags): "Describe the statement into the output formula ko" - from formula import StoredStatement + from .formula import StoredStatement f = s_l[0].context() s_l2 = [] for s in s_l: @@ -634,19 +634,19 @@ def describeStatement(s_l, ko, flags): obj = subFormulaStandIn(ko, obj, flags) s = StoredStatement([con, pred, subj, obj]) s_l2.append(s) - + si = ko.newBlankNode(why=dontAsk) ko.add(si, rdf.type, reason.Extraction, why=dontAsk) standIn = formulaStandIn(ko, f.subSet(s_l2, why=dontAsk), flags=flags) ko.add(si, reason.gives, standIn, why=dontAsk) return si - - + + class BecauseOfData(Because): """Directly from data in the resource whose URI is the string. - + A nested reason can also be given, for why this resource was parsed. """ pass @@ -655,7 +655,7 @@ def __init__(self, source, because=None): Reason.__init__(self) self._source = source self._reason = because - self.statements = Set() + self.statements = set() return def explain(self, ko, flags): @@ -669,7 +669,7 @@ def explain(self, ko, flags): ko.add(subj=me, pred=rdf.type, obj=reason.Parsing, why=dontAsk) ko.add(subj=me, pred=reason.source, obj=self._source, why=dontAsk) if self._reason: - ko.add(subj=me, pred=reason.because, + ko.add(subj=me, pred=reason.because, obj=self._reason.explain(ko, flags=flags), why=dontAsk) if "g" in flags: @@ -677,7 +677,7 @@ def explain(self, ko, flags): standIn = formulaStandIn(ko,prem, flags=flags) ko.add(me, reason.gives, standIn, why=dontAsk) if diag.chatty_flag >59: - progress("Rule (%s) is:\n%s" % + progress("Rule (%s) is:\n%s" % ( self._string, prem.n3String())) return me @@ -697,11 +697,11 @@ def explain(self, ko, flags): ko.add(subj=me, pred=rdf.type, obj=reason.CommandLine, why=dontAsk) ko.add(subj=me, pred=reason.args, obj=self._string, why=dontAsk) return me - + class BecauseOfExperience(Because): """Becase of the experience of this agent, as described in the string""" pass - + class BecauseBuiltIn(Reason): """Because the built-in function given concluded so. A nested reason for running the function must be given""" @@ -711,7 +711,7 @@ def __init__(self, context, subj, pred, obj): self._predicate = pred self._object = obj self._context = context - + def explain(self, ko, flags): "This is just a plain fact - or was at the time." me = self.me.get(ko, None) @@ -784,14 +784,14 @@ def explain(self, ko, flags): ko.add(me, rdf.type, reason.Conclusion, why=dontAsk) standIn = formulaStandIn(ko,fact, flags=flags) ko.add(me, reason.gives, standIn, why=dontAsk) - ko.add(subj=m, pred=rdf.type, obj=reason.Conjunction, why=dontAsk) + ko.add(subj=m, pred=rdf.type, obj=reason.Conjunction, why=dontAsk) ko.add(me, reason.because, m, why=dontAsk) statementsForReason = {} # reverse index: group by reason for s in self.reason: try: rea = getStatementReason(s) except: - print s + print(s) raise x = statementsForReason.get(rea, None) if x is None: statementsForReason[rea] = [s] diff --git a/wrap_llyn.py b/wrap_llyn.py index ec7902d8..dc6ef018 100644 --- a/wrap_llyn.py +++ b/wrap_llyn.py @@ -5,13 +5,13 @@ """ -import pluggable -import llyn +from . import pluggable +from . import llyn class Store(pluggable.Store, llyn.RDFStore): def __init__(self, host=None, copyFrom=None): if copyFrom is not None: - raise RuntimeError, "Not implemented" + raise RuntimeError("Not implemented") if host is not None: self.host = host - apply(llyn.RDFStore.__init__, [self]) + llyn.RDFStore.__init__(*[self]) diff --git a/wrap_n3.py b/wrap_n3.py index 52a8a942..c337f9f2 100644 --- a/wrap_n3.py +++ b/wrap_n3.py @@ -5,8 +5,8 @@ """ -import pluggable -import notation3 +from . import pluggable +from . import notation3 import sys class Parser(pluggable.Parser): @@ -32,7 +32,7 @@ def parse(self, stream, host=None): # host is not used yet # uri = stream.info().uri - print "Using sink", self.sink + print("Using sink", self.sink) self.sink.dumpPrefixes(sys.stdout) p = notation3.SinkParser(self.sink, uri) result = p.loadStream(stream) @@ -42,8 +42,7 @@ def parse(self, stream, host=None): class Serializer(pluggable.Serializer, notation3.ToN3): def __init__(self, stream, flags): - apply(notation3.ToN3.__init__, [self, stream.write], - {"flags": flags}) + notation3.ToN3.__init__(*[self, stream.write], **{"flags": flags}) # r=notation3.SinkParser(notation3.ToN3(sys.stdout.write, diff --git a/xml2rdf.py b/xml2rdf.py index 8445f52f..c8b0112b 100644 --- a/xml2rdf.py +++ b/xml2rdf.py @@ -8,10 +8,10 @@ # # parses DAML_ONT_NS or DPO_NS lists, generates DPO_NS -import xmllib # Comes with python 1.5 and greater -import notation3 # http://www.w3.org/2000/10/swap/notation3.py -import urlparse # Comes with python 1.6, lacks file<->url mapping -import urllib # Opening resources in load() +from . import xmllib # Comes with python 1.5 and greater +from . import notation3 # http://www.w3.org/2000/10/swap/notation3.py +import urllib.parse # Comes with python 1.6, lacks file<->url mapping +import urllib.request, urllib.parse, urllib.error # Opening resources in load() import string # States: @@ -38,7 +38,7 @@ class RDFXMLParser(xmllib.XMLParser): def __init__(self, sink, thisURI, **kw): self.testdata = "" - apply(xmllib.XMLParser.__init__, (self,), kw) + xmllib.XMLParser.__init__(*(self,), **kw) self._stack =[] # Stack of states self.sink = sink @@ -57,12 +57,12 @@ def __init__(self, sink, thisURI, **kw): def load(self, uri, _baseURI=""): if uri: - _inputURI = urlparse.urljoin(_baseURI, uri) # Make abs from relative - netStream = urllib.urlopen(_inputURI) + _inputURI = urllib.parse.urljoin(_baseURI, uri) # Make abs from relative + netStream = urllib.request.urlopen(_inputURI) self.feed(netStream.read()) # @ May be big - buffered in memory! self.close() else: - _inputURI = urlparse.urljoin(_baseURI, "STDIN") # Make abs from relative + _inputURI = urllib.parse.urljoin(_baseURI, "STDIN") # Make abs from relative self.feed(sys.stdin.read()) # May be big - buffered in memory! self.close() @@ -88,18 +88,18 @@ def flush(self): def handle_cdata(self, data): self.flush() - print 'cdata:', `data` + print('cdata:', repr(data)) def handle_proc(self, name, data): self.flush() - print 'processing:',name,`data` + print('processing:',name,repr(data)) def handle_comment(self, data): self.flush() self.sink.makeComment(data) def syntax_error(self, message): - print 'error at line %d:' % self.lineno, message + print('error at line %d:' % self.lineno, message) def tag2uri(self, str): """ Generate URI from tagname @@ -111,7 +111,7 @@ def tag2uri(self, str): def uriref(self, str): """ Generate uri from uriref in this document """ - return urlparse.urljoin(self._thisURI,str) + return urllib.parse.urljoin(self._thisURI,str) def idAboutAttr(self, attrs): #6.5 also proprAttr 6.10 """ set up subject and maybe context from attributes @@ -121,15 +121,15 @@ def idAboutAttr(self, attrs): #6.5 also proprAttr 6.10 self._subject = None properties = [] - for name, value in attrs.items(): + for name, value in list(attrs.items()): x = string.find(name, " ") if x>=0: ns = name[:x] ln = name[x+1:] # Strip any namespace on attributes!!! @@@@ if string.find("ID ambout AboutEachPrefix bagid type", name)>0: if ns != RDF_NS_URI: - print ("# Warning -- %s attribute in %s namespace not RDF NS." % - name, ln) + print(("# Warning -- %s attribute in %s namespace not RDF NS." % + name, ln)) ns = RDF_NS_URI # @@@@@@@@@@@@@@@@ uri = ns + ln else: @@ -140,7 +140,7 @@ def idAboutAttr(self, attrs): #6.5 also proprAttr 6.10 if ln == "ID": if self._subject: - print "# oops - subject already", self._subject + print("# oops - subject already", self._subject) raise syntaxError # ">1 subject" self._subject = self.uriref("#" + value) elif ln == "about": @@ -161,7 +161,7 @@ def idAboutAttr(self, attrs): #6.5 also proprAttr 6.10 pass #later else: if not ns: - print "#@@@@@@@@@@@@ No namespace on property attribute", ln + print("#@@@@@@@@@@@@ No namespace on property attribute", ln) raise self.syntaxError properties.append((uri, value))# If no uri, syntax error @@ # self.sink.makeComment("xml2rdf: Ignored attribute "+uri) @@ -179,7 +179,7 @@ def idAboutAttr(self, attrs): #6.5 also proprAttr 6.10 def _generate(self): - generatedId = self._genPrefix + `self._nextId` # + generatedId = self._genPrefix + repr(self._nextId) # self._nextId = self._nextId + 1 self.sink.makeStatement(( (RESOURCE, self._context), (RESOURCE, notation3.N3_forSome_URI), @@ -216,12 +216,12 @@ def unknown_starttag(self, tag, attrs): if chatty: if not attrs: - print '# State =', self._state, 'start tag: <' + tagURI + '>' + print('# State =', self._state, 'start tag: <' + tagURI + '>') else: - print '# state =', self._state, 'start tag: <' + tagURI, - for name, value in attrs.items(): - print " " + name + '=' + '"' + value + '"', - print '>' + print('# state =', self._state, 'start tag: <' + tagURI, end=' ') + for name, value in list(attrs.items()): + print(" " + name + '=' + '"' + value + '"', end=' ') + print('>') self._stack.append([self._state, self._context, self._predicate, self._subject]) @@ -231,9 +231,9 @@ def unknown_starttag(self, tag, attrs): self._state = STATE_NO_SUBJECT # HACK @@ to grab prefixes - nslist = self._XMLParser__namespaces.items() + nslist = list(self._XMLParser__namespaces.items()) for t, d, nst in self.stack: # Hack - nslist = nslist + d.items() + nslist = nslist + list(d.items()) # print "### Namespaces: ", `nslist` for prefix, nsURI in nslist: if nsURI: @@ -252,15 +252,15 @@ def unknown_starttag(self, tag, attrs): # print "\n attributes:", `attrs` - for name, value in attrs.items(): + for name, value in list(attrs.items()): x = string.find(name, " ") if x>=0: name=name[x+1:] # Strip any namespace on attributes!!! @@@@ if name == "ID": - print "# Warning: ID=%s on statement ignored" % (value) # I consider these a bug + print("# Warning: ID=%s on statement ignored" % (value)) # I consider these a bug elif name == "parseType": - nslist = self._XMLParser__namespaces.items() # Get namespaces (this is a qname) + nslist = list(self._XMLParser__namespaces.items()) # Get namespaces (this is a qname) for t, d, nst in self.stack: # Hack - look inside parser - Yuk@@ - nslist = nslist + d.items() + nslist = nslist + list(d.items()) if value == "Literal": self._state = STATE_LITERAL # That's an XML subtree not a string @@ -282,7 +282,7 @@ def unknown_starttag(self, tag, attrs): self.idAboutAttr(attrs) # set subject and context for nested description if self._predicate == RDF_NS_URI+"is": # magic :-( self._subject = s # Forget anonymous genid - context is subect - print "#@@@@@@@@@@@@@ decided subject is ",`s`[-10:-1] + print("#@@@@@@@@@@@@@ decided subject is ",repr(s)[-10:-1]) else: self.sink.makeStatement(( (RESOURCE, c), (RESOURCE, self._predicate), @@ -350,7 +350,7 @@ def unknown_starttag(self, tag, attrs): self._stack[-1][0] = STATE_NOVALUE # When we return, cannot have literal now elif self._state == STATE_NOVALUE: - print "\n@@ Expected no value, found ", tag, attrs, "\n Stack: ",self._stack + print("\n@@ Expected no value, found ", tag, attrs, "\n Stack: ",self._stack) raise syntaxError # Found tag, expected empty else: raise internalError # Unknown state @@ -387,11 +387,11 @@ def unknown_endtag(self, tag): def unknown_entityref(self, ref): self.flush() - print '#*** unknown entity ref: &' + ref + ';' + print('#*** unknown entity ref: &' + ref + ';') def unknown_charref(self, ref): self.flush() - print '#*** unknown char ref: &#' + ref + ';' + print('#*** unknown char ref: &#' + ref + ';') def close(self): xmllib.XMLParser.close(self) @@ -424,8 +424,8 @@ def test(args = None): else: try: f = open(file, 'r') - except IOError, msg: - print file, ":", msg + except IOError as msg: + print(file, ":", msg) sys.exit(1) data = f.read() @@ -442,15 +442,15 @@ def test(args = None): for c in data: x.feed(c) x.close() - except RuntimeError, msg: + except RuntimeError as msg: t1 = time() - print msg + print(msg) if do_time: - print 'total time: %g' % (t1-t0) + print('total time: %g' % (t1-t0)) sys.exit(1) t1 = time() if do_time: - print 'total time: %g' % (t1-t0) + print('total time: %g' % (t1-t0)) if __name__ == '__main__': diff --git a/xmlC14n.py b/xmlC14n.py index fd681363..46eb76f1 100644 --- a/xmlC14n.py +++ b/xmlC14n.py @@ -52,9 +52,9 @@ class XMLNS: # import cStringIO # StringIO = cStringIO #except ImportError: -import StringIO +import io -_attrs = lambda E: (E.attributes and E.attributes.values()) or [] +_attrs = lambda E: (E.attributes and list(E.attributes.values())) or [] _children = lambda E: E.childNodes or [] _IN_XML_NS = lambda n: n.namespaceURI == XMLNS.XML _inclusive = lambda n: n.unsuppressedPrefixes == None @@ -62,7 +62,7 @@ class XMLNS: # Does a document/PI has lesser/greater document order than the # first element? -_LesserElement, _Element, _GreaterElement = range(3) +_LesserElement, _Element, _GreaterElement = list(range(3)) def _sorter(n1,n2): '''_sorter(n1,n2) -> int @@ -90,7 +90,7 @@ def _utilized(n, node, other_attrs, unsuppressedPrefixes): elif n.startswith('xmlns'): n = n[5:] if (n=="" and node.prefix in ["#default", None]) or \ - n == node.prefix or n in unsuppressedPrefixes: + n == node.prefix or n in unsuppressedPrefixes: return 1 for attr in other_attrs: if n == attr.prefix: return 1 @@ -113,10 +113,10 @@ def __init__(self, node, write, **kw): self.comments = kw.get('comments', 0) self.unsuppressedPrefixes = kw.get('unsuppressedPrefixes') nsdict = kw.get('nsdict', { 'xml': XMLNS.XML, 'xmlns': XMLNS.BASE }) - + # Processing state. self.state = (nsdict, {'xml':''}, {}) #0422 - + if node.nodeType == Node.DOCUMENT_NODE: self._do_document(node) elif node.nodeType == Node.ELEMENT_NODE: @@ -131,7 +131,7 @@ def __init__(self, node, write, **kw): elif node.nodeType == Node.TEXT_NODE: self._do_text(node) else: - raise TypeError, str(node) + raise TypeError(str(node)) def _inherit_context(self, node): @@ -141,7 +141,7 @@ def _inherit_context(self, node): canonicalization.''' # Collect the initial list of xml:foo attributes. - xmlattrs = filter(_IN_XML_NS, _attrs(node)) + xmlattrs = list(filter(_IN_XML_NS, _attrs(node))) # Walk up and get all xml:XXX attributes we inherit. inherited, parent = [], node.parentNode @@ -174,7 +174,7 @@ def _do_document(self, node): elif child.nodeType == Node.DOCUMENT_TYPE_NODE: pass else: - raise TypeError, str(child) + raise TypeError(str(child)) handlers[Node.DOCUMENT_NODE] = _do_document @@ -262,14 +262,14 @@ def _do_element(self, node, initial_other_attrs = []): ns_local = ns_parent.copy() xml_attrs_local = {} - # progress("_do_element node.nodeName=", node.nodeName) - # progress("_do_element node.namespaceURI", node.namespaceURI) - # progress("_do_element node.tocml()", node.toxml()) + # progress("_do_element node.nodeName=", node.nodeName) + # progress("_do_element node.namespaceURI", node.namespaceURI) + # progress("_do_element node.tocml()", node.toxml()) # Divide attributes into NS, XML, and others. other_attrs = initial_other_attrs[:] in_subset = _in_subset(self.subset, node) for a in _attrs(node): - # progress("\t_do_element a.nodeName=", a.nodeName) + # progress("\t_do_element a.nodeName=", a.nodeName) if a.namespaceURI == XMLNS.BASE: n = a.nodeName if n == "xmlns:": n = "xmlns" # DOM bug workaround @@ -284,14 +284,14 @@ def _do_element(self, node, initial_other_attrs = []): # Render the node W, name = self.write, None - if in_subset: + if in_subset: name = node.nodeName W('<') W(name) # Create list of NS attributes to render. ns_to_render = [] - for n,v in ns_local.items(): + for n,v in list(ns_local.items()): # If default namespace is XMLNS.BASE or empty, # and if an ancestor was the same @@ -309,7 +309,7 @@ def _do_element(self, node, initial_other_attrs = []): # If not previously rendered # and it's inclusive or utilized - if (n,v) not in ns_rendered.items() \ + if (n,v) not in list(ns_rendered.items()) \ and (_inclusive(self) or \ _utilized(n, node, other_attrs, self.unsuppressedPrefixes)): ns_to_render.append((n, v)) @@ -324,9 +324,9 @@ def _do_element(self, node, initial_other_attrs = []): # Else, add all local and ancestor xml attributes # Sort and render the attributes. if not _inclusive(self) or _in_subset(self.subset,node.parentNode): #0426 - other_attrs.extend(xml_attrs_local.values()) + other_attrs.extend(list(xml_attrs_local.values())) else: - other_attrs.extend(xml_attrs.values()) + other_attrs.extend(list(xml_attrs.values())) other_attrs.sort(_sorter) for a in other_attrs: self._do_attr(a.nodeName, a.value) @@ -358,8 +358,8 @@ def Canonicalize(node, output=None, **kw): prefixes that should be inherited. ''' if output: - apply(_implementation, (node, output.write), kw) + _implementation(*(node, output.write), **kw) else: - s = StringIO.StringIO() - apply(_implementation, (node, s.write), kw) + s = io.StringIO() + _implementation(*(node, s.write), **kw) return s.getvalue() diff --git a/xmllib.py b/xmllib.py index cfe45297..93f61595 100644 --- a/xmllib.py +++ b/xmllib.py @@ -94,13 +94,13 @@ class XMLParser: # Interface -- initialize and reset this instance def __init__(self, **kw): self.__fixed = 0 - if kw.has_key('accept_unquoted_attributes'): + if 'accept_unquoted_attributes' in kw: self.__accept_unquoted_attributes = kw['accept_unquoted_attributes'] - if kw.has_key('accept_missing_endtag_name'): + if 'accept_missing_endtag_name' in kw: self.__accept_missing_endtag_name = kw['accept_missing_endtag_name'] - if kw.has_key('map_case'): + if 'map_case' in kw: self.__map_case = kw['map_case'] - if kw.has_key('accept_utf8'): + if 'accept_utf8' in kw: self.__accept_utf8 = kw['accept_utf8'] self.reset() @@ -116,7 +116,7 @@ def __fixclass(self, kl): self.__fixclass(k) def __fixdict(self, dict): - for key in dict.keys(): + for key in list(dict.keys()): if key[:6] == 'start_': tag = key[6:] start, end = self.elements.get(tag, (None, None)) @@ -196,7 +196,7 @@ def translate_references(self, data, all = 1): data = pre + str + post i = res.start(0)+len(str) elif all: - if self.entitydefs.has_key(str): + if str in self.entitydefs: data = pre + self.entitydefs[str] + post i = res.start(0) # rescan substituted text else: @@ -288,7 +288,7 @@ def goahead(self, end): 'encoding', 'standalone') if version[1:-1] != '1.0': - raise RuntimeError, 'only XML version 1.0 supported' + raise RuntimeError('only XML version 1.0 supported') if encoding: encoding = encoding[1:-1] if standalone: standalone = standalone[1:-1] self.handle_xml(encoding, standalone) @@ -347,7 +347,7 @@ def goahead(self, end): name = res.group('name') if self.__map_case: name = string.lower(name) - if self.entitydefs.has_key(name): + if name in self.entitydefs: self.rawdata = rawdata = rawdata[:res.start(0)] + self.entitydefs[name] + rawdata[i:] n = len(rawdata) i = res.start(0) @@ -369,7 +369,7 @@ def goahead(self, end): i = i+1 continue else: - raise RuntimeError, 'neither < nor & ??' + raise RuntimeError('neither < nor & ??') # We get here only if incomplete matches but # nothing else break @@ -397,8 +397,8 @@ def goahead(self, end): # Internal -- parse comment, return length or -1 if not terminated def parse_comment(self, i): rawdata = self.rawdata - if rawdata[i:i+4] <> '") - if not self.inline: - s.write("\n") - s.write(prefix[0:-2]) - - -def stylelink(href): - return Element("link", attrs={ - "rel":"stylesheet", - "type":"text/css", - "href":href}) - -def prefixEveryLine(prefix, s): - extra = "" - s = str(s) - if s.endswith("\n"): extra = "\n"+prefix - return prefix+("\n"+prefix).join(s.splitlines())+extra - -class Flow: - """ - >>> import html - >>> h = html.Flow() - >>> print prefixEveryLine("* ", - ... h.span("Spanned Text", class_="authorName")) - * - * Spanned Text - * - """ - def __getattr__(self, name): - def func(*content, **kw): - kw["inline"] = 0 - return Element(*[name]+list(content), **kw) - return func - -class Inline: - """ - >>> import html - >>> h = html.Inline() - >>> print h.span("Spanned Text", class_="authorName") - Spanned Text - """ - def __getattr__(self, name): - def func(*content, **kw): - kw["inline"] = 1 - return Element(*[name]+list(content), **kw) - return func - -"""Foo""" - -def createStdElements(): - from . import html - - flow = Flow() - inline = Inline() - - # These element names come from - # http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd - # - # I added the "+" myself (a flag for non-inline element); it's - # just about the style in which the HTML is generated and doesnt - # HAVE to match inline/flow in HTML's own syntax. - - elements = """a abbr acronym address area b base bdo big - blockquote+ body+ br+ button caption cite code col colgroup dd del - dfn div+ dl dt em fieldset form+ h1+ h2+ h3+ h4+ h5+ h6+ head+ hr+ html+ i - img input+ ins kbd label legend li link map meta noscript object ol+ - optgroup option p+ param pre q samp script+ select+ small span strong - style sub sup table+ tbody+ td textarea+ tfoot th thead title tr+ tt - ul+ var""" - - for x in elements.split(" "): - tag = x.strip() - if tag: - if tag.endswith("+"): - tag = tag[0:-1] - html.__dict__[tag] = getattr(flow, tag) - else: - html.__dict__[tag] = getattr(inline, tag) - - -createStdElements() - -if __name__ =='__main__': - import doctest, html - print("Performing doctest...") - print(doctest.testmod(html)) - -# $Log$ -# Revision 1.6 2003-09-04 05:40:13 sandro -# added XML quoting and Raw elements -# -# Revision 1.5 2003/08/01 15:40:46 sandro -# simpler API; actually changed Apr 29 but not comitted -# -# Revision 1.4 2003/04/29 04:46:57 sandro -# added clever << operator (now where have I seen that before?) -# -# Revision 1.3 2003/02/18 17:02:05 sandro -# added auto-generation of element-creation functions, based on XHTML DTD -# -# Revision 1.2 2003/02/14 00:44:24 sandro -# added some more functions: htable, tr, td, a -# -# Revision 1.1 2003/01/29 18:28:37 sandro -# first version, passes doctests, [warning: excessive tool-building] -# From cfc69ac7587b239138620542c70836d3282e0fd2 Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Tue, 2 Jan 2024 14:05:57 +0000 Subject: [PATCH 06/18] fix fix for rfind oops --- Makefile | 14 ++++++++------ cwm.py | 22 ++++++++++++++++++---- diag.py | 2 +- formula.py | 4 +++- isodate.py | 4 +--- llyn.py | 4 +++- notation3.py | 5 +++-- pretty.py | 24 ++++++++++++------------ term.py | 20 ++++++++++++++++---- uripath.py | 5 +++-- 10 files changed, 68 insertions(+), 36 deletions(-) diff --git a/Makefile b/Makefile index 68b7b369..b8f92665 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ # $Id$ -PYTHON=python +PYTHON=python3 YAPPS=yapps2.py @@ -9,9 +9,9 @@ TESTIN=test/sameDan.n3 HTMLS= term.html formula.html pretty.html myStore.html check.html query.html RDFSink.html cwm.html cwm_crypto.html cwm_list.html cwm_math.html cwm_sparql.html cwm_maths.html cwm_os.html cwm_string.html cwm_time.html cwm_times.html diag.html llyn.html notation3.html reify.html sax2rdf.html rdflib2rdf.html thing.html toXML.html uripath.html xml2infoset.html why.html sparql2cwm.html doc/changes.html -## SOURCES = cwm.py cant.py delta.py notation3.py query.py llyn.py uripath.py diag.py RDFSink.py reify.py why.py myStore.py webAccess.py OrderedSequence.py term.py formula.py pretty.py cwm_list.py cwm_string.py cwm_os.py cwm_time.py isodate.py cwm_math.py cwm_trigo.py cwm_times.py cwm_maths.py cwm_sparql.py cwm_set.py toXML.py update.py sax2rdf.py rdflib_user.py rdfxml.py __init__.py local_decimal.py isXML.py my_profiler.py cwm_crypto.py set_importer.py triple_maker.py mixin.py sparql2cwm.py sparql/sparql_parser.py sparql/sparql_tokens.py sparql/sparqlClient.py sparql/sparql_tokens_table.py sparql/sparql_table.py sparql/table_generator.py sparql/__init__.py sparql/webserver.py +SOURCES = cwm.py cant.py delta.py notation3.py query.py llyn.py uripath.py diag.py RDFSink.py reify.py why.py myStore.py webAccess.py OrderedSequence.py term.py formula.py pretty.py cwm_list.py cwm_string.py cwm_os.py cwm_time.py isodate.py cwm_math.py cwm_trigo.py cwm_times.py cwm_maths.py cwm_sparql.py cwm_set.py toXML.py update.py sax2rdf.py rdflib_user.py rdfxml.py __init__.py local_decimal.py isXML.py my_profiler.py cwm_crypto.py set_importer.py triple_maker.py mixin.py sparql2cwm.py sparql/sparql_parser.py sparql/sparql_tokens.py sparql/sparqlClient.py sparql/sparql_tokens_table.py sparql/sparql_table.py sparql/table_generator.py sparql/__init__.py sparql/webserver.py -SOURCES := $(shell python importList.py my_profiler.py cant.py check.py delta.py cwm.py) sparql/Makefile +## SOURCES := $(shell python3 importList.py my_profiler.py cant.py check.py delta.py cwm.py) sparql/Makefile DOC=doc/CwmHelp.htm @@ -38,6 +38,8 @@ TARBALL_STUFF = README LICENSE LICENSE.rdf LICENSE.n3 .DELETE_ON_ERROR : swap #all: yappstest yappsdoc math.rdf log.rdf db.rdf os.rdf string.rdf crypto.rdf +run: + python3 cwm.py --chatty=100 --n3 --rdf --n3 test/t1.n3 install : setup.py ./setup.py install @@ -104,9 +106,9 @@ setup_tarball: $(SOURCES) $(HTMLS) $(TESTS) $(GRAMMAR) $(TARBALL_STUFF) tested f for A in $(TARBALL_STUFF) $(HTMLS) $(GRAMMAR) $(TESTS); do echo "$$A" >> MANIFEST; done for A in $(SOURCES); do echo swap/"$$A" >> MANIFEST; done cat test/testfilelist | sed -e 's/^/test\//' >> MANIFEST - python setup.py sdist - -python setup.py bdist_rpm - -python setup.py bdist_wininst + python3 setup.py sdist + -python3 setup.py bdist_rpm + -python3 setup.py bdist_wininst rm -rf swap rm cwm rm delta diff --git a/cwm.py b/cwm.py index c7fafc20..9e22fa7e 100755 --- a/cwm.py +++ b/cwm.py @@ -1,4 +1,4 @@ -#!/usr/bin/python + #!/usr/bin/python3 """ $Id$ @@ -64,8 +64,12 @@ cvsRevision = "$Revision$" - + ############## Temp test setup +# @@ this should be able to be set up in the launch.json file in vscode +for arg in ["--chatty=100", "--n3", "test/t1.n3"]: + sys.argv.append(arg) +print('@@ sys.argv', len(sys.argv)) ################################################# Command line @@ -199,6 +203,7 @@ def doCommand(): # The base URI for this process - the Web equiv of cwd _baseURI = uripath.base() + print('Current base: ', _baseURI) option_format = "n3" # set the default format option_first_format = None @@ -216,9 +221,12 @@ def doCommand(): _rhs = "" try: [_lhs,_rhs]=arg.split('=',1) + print('@@@ oop 1 : ', _lhs, _rhs) + try: _uri = join(option_baseURI, _rhs) except ValueError: + print('@@@ 2 oop : ', option_baseURI, _rhs) _uri = _rhs except ValueError: pass if arg == "-ugly": option_outputStyle = arg @@ -439,7 +447,12 @@ def filterize(): except ValueError: _uri =_rhs if arg[0] != "-": + print('cwm pass 2 option_baseURI', option_baseURI) + print('cwm pass 2 splitFrag(arg)[0]', splitFrag(arg)[0]) + _inputURI = join(option_baseURI, splitFrag(arg)[0]) + print('cwm pass 2 _inputURI', _inputURI) + assert ':' in _inputURI ContentType={ "rdf": "application/xml+rdf", "n3": "text/n3", @@ -656,15 +669,16 @@ def _handler(s): elif _lhs == "-prove": # code copied from -filter without really being understood -sdh + _newURI = join(_baseURI, "_w_"+repr(_genid)) # Intermediate + _metaURI = _newURI + "_meta" _tmpstore = llyn.RDFStore( _outURI+"#_g", metaURI=_metaURI, argv=option_with, crypto=option_crypto) tmpContext = _tmpstore.newFormula(_uri+ "#_formula") - _newURI = join(_baseURI, "_w_"+repr(_genid)) # Intermediate _genid = _genid + 1 _newContext = _tmpstore.newFormula(_newURI+ "#_formula") _tmpstore.loadURI(_uri) - print(targetkb) + print(_tmpstore) elif arg == "-flatten": raise NotImplementedError diff --git a/diag.py b/diag.py index badb94c0..41900974 100644 --- a/diag.py +++ b/diag.py @@ -28,7 +28,7 @@ def progress(*args): if i < 0: break a = a[:i+1] + (" "*level) + a[i+1:] i = i+1 - q = utf_8_encode("%s " % (a,))[0] + q = "%s " % (a,) sys.stderr.write(q) ## if lineCount[0] > 20: ## lineCount[0] = 0 diff --git a/formula.py b/formula.py index c65362be..bc3be154 100755 --- a/formula.py +++ b/formula.py @@ -850,7 +850,9 @@ def comparePredObj(self, other): if s is not o: return s.compareAnyTerm(o) return 0 - + # Python3 requires we use keyfunctions instead of CMP + def keyForPredObj(self): + return [self.quad[PRED].sortKey(), self.quad[OBJ].sortKey()] def context(self): """Return the context of the statement""" diff --git a/isodate.py b/isodate.py index 034e7a33..a009f3fe 100644 --- a/isodate.py +++ b/isodate.py @@ -33,8 +33,6 @@ import sys, time, re, operator import calendar # timegm - from python -from types import StringType, UnicodeType, IntType, LongType, FloatType - __version__ = "0.6" date_parser = re.compile(r"""^ (?P\d{4,4}) @@ -75,7 +73,7 @@ def parse(s): """ parse a string and return seconds since the epoch. """ - assert type(s) in [StringType, UnicodeType] + # assert type(s) in [StringType, UnicodeType] r = date_parser.search(s) try: a = r.groupdict('0') diff --git a/llyn.py b/llyn.py index 3da91873..d72e8575 100755 --- a/llyn.py +++ b/llyn.py @@ -358,8 +358,10 @@ def add(self, subj, pred, obj, why=None): obj = obj.substituteEquals(self._redirections, newBindings) if diag.chatty_flag > 90: + print('@@ llyn.py ', type(subj)) + print('@@ llyn.py ', subj) progress("Add statement (size before %i, %i statements) to %s:\n {%s %s %s}" % ( - self.store.size, len(self.statements),repr(self), repr(subj), repr(pred), repr(obj)) ) + self.store.size, len(self.statements), repr(self), repr(subj), repr(pred), repr(obj)) ) if self.statementsMatching(pred, subj, obj): if diag.chatty_flag > 97: progress("Add duplicate SUPPRESSED %s: {%s %s %s}" % ( diff --git a/notation3.py b/notation3.py index cd0e9ff9..376d11c6 100755 --- a/notation3.py +++ b/notation3.py @@ -210,6 +210,7 @@ def feed(self, octets): remainder after any statements have been parsed. So if there is more data to feed to the parser, it should be straightforward to recover.""" + str = octets.decode('utf-8') i = 0 while i >= 0: @@ -342,8 +343,8 @@ def directive(self, str, i): return -1 # Not a directive, could be something else. def bind(self, qn, uri): - assert isinstance(uri, - bytes), "Any unicode must be %x-encoded already" + # print('@@ type of uri', type(uri)) + #assert isinstance(uri, bytes), "Any unicode must be %x-encoded already" if qn == "": self._store.setDefaultNamespace(uri) else: diff --git a/pretty.py b/pretty.py index 83523bed..ef1eb86d 100755 --- a/pretty.py +++ b/pretty.py @@ -215,7 +215,7 @@ def dumpLists(self): if isinstance(l, N3Set): a = context.newBlankNode() ll = [mm for mm in l] #I hate sorting things - ll.sort(Term.compareAnyTerm) + ll.sort(key=Term.sortKey) list = self.store.newList(ll) self._outputStatement(sink, (context, self.store.forSome, context, a)) l._list = list @@ -285,8 +285,8 @@ def dumpVariables(self, context, sink, sorting=1, pretty=0, dataOnly=0): uv = list(context.universals()) ev = list(context.existentials()) if sorting: - uv.sort(Term.compareAnyTerm) - ev.sort(Term.compareAnyTerm) + uv.sort(key=Term.sortKey) + ev.sort(key=Term.sortKey) if not dataOnly: for v in uv: self._outputStatement(sink, (context, self.store.forAll, context, v)) @@ -331,10 +331,10 @@ def fixSet(x): if 0: # Doesn't work as ther ei snow no list of bnodes rs = list(self.store.resources.values()) - if sorting: rs.sort(Term.compareAnyTerm) + if sorting: rs.sort(key=Term.sortKey) for r in rs : # First the bare resource statements = context.statementsMatching(subj=r) - if sorting: statements.sort(StoredStatement.comparePredObj) + if sorting: statements.sort(key=StoredStatement.keyForPredObj) for s in statements : self._outputStatement(sink, s.quad) if not isinstance(r, Literal): @@ -342,7 +342,7 @@ def fixSet(x): if sorting: fs.sort for f in fs : # then anything in its namespace statements = context.statementsMatching(subj=f) - if sorting: statements.sort(StoredStatement.comparePredObj) + if sorting: statements.sort(key=StoredStatement.keyForPredObj) for s in statements: self._outputStatement(sink, s.quad) sink.endDoc() @@ -420,7 +420,7 @@ def _scan(self, x, context=None): def _breakloops(self, context): _done = {} _todo = list(self._occurringAs[SUBJ]) - _todo.sort(Term.compareAnyTerm) + _todo.sort(key=Term.sortKey) for x in _todo: if x in _done: continue @@ -623,7 +623,7 @@ def _dumpSubject(self, subj, context, sink, sorting, statements=[]): if isinstance(subj, N3Set): #I hate having to sort things se = [mm for mm in subj] - se.sort(Term.compareAnyTerm) + se.sort(key=Term.sortKey) li = self.store.newList(se) else: se = None @@ -640,7 +640,7 @@ def _dumpSubject(self, subj, context, sink, sorting, statements=[]): pass else: # Could have alternative syntax here - if sorting: statements.sort(StoredStatement.comparePredObj) # @@ Needed now Fs are canonical? + if sorting: statements.sort(key=StoredStatement.keyForPredObj) # @@ Needed now Fs are canonical? if se is not None: a = self.context.newBlankNode() @@ -692,7 +692,7 @@ def _dumpSubject(self, subj, context, sink, sorting, statements=[]): return # arcs as subject done - if sorting: statements.sort(StoredStatement.comparePredObj) + if sorting: statements.sort(key=StoredStatement.keyForPredObj) for s in statements: self.dumpStatement(sink, s.quad, sorting) @@ -744,7 +744,7 @@ def dumpStatement(self, sink, triple, sorting): if isinstance(obj, N3Set): a = self.context.newBlankNode() tempobj = [mm for mm in obj] #I hate sorting things - yosi - tempobj.sort(Term.compareAnyTerm) + tempobj.sort(key=Term.sortKey) tempList = self.store.newList(tempobj) sink.startAnonymous(auxPairs((triple[CONTEXT], triple[PRED], triple[SUBJ], a))) @@ -758,7 +758,7 @@ def dumpStatement(self, sink, triple, sorting): if _anon and _incoming == 1: # Embedded anonymous node in N3 sink.startAnonymous(auxPairs(triple)) ss = context.statementsMatching(subj=obj) - if sorting: ss.sort(StoredStatement.comparePredObj) + if sorting: ss.sort(key=StoredStatement.keyForPredObj) for t in ss: self.dumpStatement(sink, t.quad, sorting) sink.endAnonymous(sub.asPair(), pre.asPair()) diff --git a/term.py b/term.py index bb259092..6f7f62c4 100755 --- a/term.py +++ b/term.py @@ -233,8 +233,8 @@ def __repr__(self): if prefix != None : return (prefix + ":" + s[p+1:]).encode('unicode_escape') if s.endswith("#_formula"): return "`"+s[-22:-9]+"`" # Hack - debug notation for formula - if p >= 0: return s[p+1:].encode('unicode_escape') - return s.encode('unicode_escape') + if p >= 0: return s[p+1:] # .encode('unicode_escape') # @@ chek what that was for + return s # .encode('unicode_escape') def debugString(self, already=[]): return repr(self) # unless more eleborate in superclass @@ -266,6 +266,13 @@ def compareAnyTerm(self, other): if diff != 0: return diff return self.compareTerm(other) + # Python3 drops the functionaity of sorting with comparison functions + # So instead you have to use a key function. + # The sort keys must all be the same type, so we can't make integers sort as integers + # and strings sort as strings. + + def sortKey(self): + return str(self.classOrder()) + '__' + self.representation() # works for simple terms only def asPair(self): """Representation in an earlier format, being phased out 2002/08 @@ -391,13 +398,16 @@ class Symbol(LabelledNode): def __init__(self, uri, store): Term.__init__(self, store) + assert isinstance(uri, str) assert uri.find("#") < 0, "no fragments allowed: %s" % uri assert ':' in uri, "must be absolute: %s" % uri self.uri = uri self.fragments = WeakValueDictionary() def uriref2(self, base): - assert ':' in base, "base must be absolute: %s" % base + if ':' in self.uri: + return self.uri # is absolute + assert base and ':' in base, "base <%s> must be absolute if URI <%s> is not: %s" % (base, self.uri) return refTo(base, self.uri) def uriref(self): @@ -468,7 +478,9 @@ def __init__(self, resource, fragid): Term.__init__(self, resource.store) self.resource = resource self.fragid = fragid - + assert isinstance(resource, Symbol), "resource is of wrong type %s " % type(resource) + assert isinstance(fragid, str), "fragid is of wrong type %s " % type(fragid) + def compareTerm(self, other): if not isinstance(other, Fragment): return LabelledNode.compareTerm(self, other) diff --git a/uripath.py b/uripath.py index 23497f91..ba841a23 100644 --- a/uripath.py +++ b/uripath.py @@ -99,6 +99,7 @@ def join(here, there): """ assert(here.find("#") < 0), "Base may not contain hash: '%s'"% here # caller must splitFrag (why?) + print('@@ join ', here, there) slashl = there.find('/') colonl = there.find(':') @@ -135,7 +136,7 @@ def join(here, there): if there[:1] == '/': return here[:bpath] + there - slashr = here.find('/') + slashr = here.rfind('/') while 1: if path[:2] == './': @@ -175,7 +176,7 @@ def refTo(base, uri): >>> refTo('http://ex/x/y', 'http://ex/x/y') '' - Note the relationship between refTo and join: + Note the relationship between refTo and join:q join(x, refTo(x, y)) == y which points out certain strings which cannot be URIs. e.g. >>> x='http://ex/x/y';y='http://ex/x/q:r';join(x, refTo(x, y)) == y From 50115dfa9b462cac6c785f7c7b681f68ee111d78 Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Tue, 2 Jan 2024 17:58:45 +0000 Subject: [PATCH 07/18] cwm has run though once, so moving to restest.py and the tests --- Makefile | 2 +- notation3.py | 52 ++++++++++++++-------------- test/Makefile | 4 +-- test/retest.py | 93 ++++++++++++++++++++++++++------------------------ uripath.py | 5 ++- 5 files changed, 79 insertions(+), 77 deletions(-) diff --git a/Makefile b/Makefile index b8f92665..89197972 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ GRAMMAR = grammar/n3.n3 grammar/README.txt grammar/predictiveParser.py grammar/ TESTS = test/Makefile test/rdfcore-tests.n3 test/regression.n3 test/list/detailed.tests test/ql/detailed.tests test/math/detailed.tests test/norm/detailed.tests test/n3parser.tests test/cwm/detailed.tests test/ntriples/detailed.tests test/delta/detailed.tests test/syntax/detailed.tests test/reify/detailed.tests test/testmeta.n3 test/retest.py test/sparql/detailed.tests test/sets/detailed.tests test/reason/detailed.tests test/delta/t3/from.n3 test/delta/t3/to-same.n3 test/delta/t3/to-diff.n3 test/string/detailed.tests test/paw/detailed.tests test/includes/detailed.tests -VERSION = 1.2.0 +VERSION = 3.0.0 TARNAME = cwm-$(VERSION) TARBALL_STUFF = README LICENSE LICENSE.rdf LICENSE.n3 diff --git a/notation3.py b/notation3.py index 376d11c6..bd09c115 100755 --- a/notation3.py +++ b/notation3.py @@ -992,33 +992,33 @@ def nodeOrLiteral(self, str, i, res): ch = str[i] if ch in "-+0987654321": m = datetime_syntax.match(str, i); - if m != None: - j = m.end(); - # print "date time "+str[i:j] - if 'T' in str[i:j]: - res.append(self._store.newLiteral(str[i:j], - self._store.newSymbol(DATETIME_DATATYPE))) - return j - else: - res.append(self._store.newLiteral(str[i:j], - self._store.newSymbol(DATE_DATATYPE))) - return j + if m != None: + j = m.end(); + # print "date time "+str[i:j] + if 'T' in str[i:j]: + res.append(self._store.newLiteral(str[i:j], + self._store.newSymbol(DATETIME_DATATYPE))) + return j + else: + res.append(self._store.newLiteral(str[i:j], + self._store.newSymbol(DATE_DATATYPE))) + return j - m = number_syntax.match(str, i) - if m != None: - j = m.end() - if m.group('exponent') != None: # includes decimal exponent - res.append(float(str[i:j])) - # res.append(self._store.newLiteral(str[i:j], - # self._store.newSymbol(FLOAT_DATATYPE))) - elif m.group('decimal') != None: - res.append(Decimal(str[i:j])) - else: - res.append(int(str[i:j])) - # res.append(self._store.newLiteral(str[i:j], - # self._store.newSymbol(INTEGER_DATATYPE))) - return j - raise BadSyntax(self._thisDoc, self.lines, str, i, + m = number_syntax.match(str, i) + if m != None: + j = m.end() + if m.group('exponent') != None: # includes decimal exponent + res.append(float(str[i:j])) + # res.append(self._store.newLiteral(str[i:j], + # self._store.newSymbol(FLOAT_DATATYPE))) + elif m.group('decimal') != None: + res.append(Decimal(str[i:j])) + else: + res.append(int(str[i:j])) + # res.append(self._store.newLiteral(str[i:j], + # self._store.newSymbol(INTEGER_DATATYPE))) + return j + raise BadSyntax(self._thisDoc, self.lines, str, i, "Bad number or datetime syntax") diff --git a/test/Makefile b/test/Makefile index 6b2fd710..08b13b92 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,6 @@ -# Makefile +# cwm test Makefile # -P=python +P=python3 W=../../../.. C=../cwm.py RC=../../rdf-tests/rdfcore diff --git a/test/retest.py b/test/retest.py index 4df3e085..6dd3dd50 100755 --- a/test/retest.py +++ b/test/retest.py @@ -26,10 +26,10 @@ W3C open source licence . """ -from os import system, popen3 +from os import system import os import sys -import urllib +import urllib.request, urllib.parse, urllib.error # From PYTHONPATH equivalent to http://www.w3.org/2000/10 @@ -38,7 +38,7 @@ from swap.uripath import refTo, base from swap import diag from swap.diag import progress -from swap.term import AnonymousNode +from swap.term import AnonymousNode, Term rdf = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#") @@ -78,13 +78,13 @@ def problem(str): # raise RuntimeError(str) def usage(): - print __doc__ + print(__doc__) -from subprocess import Popen, call, PIPE +from subprocess import call, PIPE def execute(cmd1, noStdErr=False): global verbose, no_action - if verbose: print " "+cmd1 + if verbose: print(" "+cmd1) if no_action: return stderr = None try: @@ -107,18 +107,18 @@ def diff(case, ref=None, prog="diff -Bbwu"): a.write('\n') a.close() diffcmd = """%s %s ,temp/%s >,diffs/%s""" %(prog, ref, case, case) - if verbose: print " ", diffcmd + if verbose: print(" ", diffcmd) if no_action: result = 0 else: result = system(diffcmd) if result < 0: raise problem("Comparison fails: result %i executing %s" %(result, diffcmd)) - if result > 0: print "Files differ, result=", result - d = urllib.urlopen(",diffs/"+case) + if result > 0: print("Files differ, result=", result) + d = urllib.request.urlopen(",diffs/"+case) buf = d.read() if len(buf) > 0: if just_fix_it == 0: - print "# If this is OK, cp ,temp/%s %s" %(case, ref) - print "######### Differences from reference output:\n" + buf + print("# If this is OK, cp ,temp/%s %s" %(case, ref)) + print("######### Differences from reference output:\n" + buf) return 1 else: os.system("cp ,temp/%s %s" %(case, ref)) @@ -131,16 +131,16 @@ def rdfcompare3(case, ref=None): if ref == None: ref = "ref/%s" % case diffcmd = """python ../cant.py -d %s -f ,temp/%s >,diffs/%s""" %(ref, case, case) - if verbose: print " ", diffcmd + if verbose: print(" ", diffcmd) result = system(diffcmd) if result < 0: raise problem("Comparison fails: result %i executing %s" %(result, diffcmd)) - if result > 0: print "Files differ, result=", result - d = urllib.urlopen(",diffs/"+case) + if result > 0: print("Files differ, result=", result) + d = urllib.request.urlopen(",diffs/"+case) buf = d.read() if len(buf) > 0: # print "# If this is OK, cp ,temp/%s %s" %(case, ref) - print "######### Differences from reference output:\n" + buf + print("######### Differences from reference output:\n" + buf) return 1 return result @@ -161,19 +161,22 @@ def rdfcompare(case, ref=None): if ref == None: ref = "ref/%s" % case diffcmd = """java jena.rdfcompare %s ,temp/%s N-TRIPLE N-TRIPLE >,diffs/%s""" %(ref, case, case) - if verbose: print " ", diffcmd + if verbose: print(" ", diffcmd) result = system(diffcmd) if result != 0: raise problem("Comparison fails: result %s executing %s" %(result, diffcmd)) return result - +def byCaseURI (testItem): + # print('@@ sort type ', type(testItem)) + # print('@@ sort item ', testItem) + return testItem[1] def main(): global verbose, proofs, chatty, normal, no_action start = 1 cwm_command='../cwm.py' - python_command='python -tt' + python_command='python3 -tt' global ploughOn # even if error ploughOn = 0 global verbose @@ -283,12 +286,12 @@ def main(): status = kb.the(t, rdft.status).string good = 1 if status != "APPROVED": - if verbose: print "\tNot approved: "+ inputDocument[-40:] + if verbose: print("\tNot approved: "+ inputDocument[-40:]) good = 0 categories = kb.each(t, rdf.type) for cat in categories: if cat is triage.ReificationTest: - if verbose: print "\tNot supported (reification): "+ inputDocument[-40:] + if verbose: print("\tNot supported (reification): "+ inputDocument[-40:]) good = 0 ## if cat is triage.ParseTypeLiteralTest: ## if verbose: print "\tNot supported (Parse type literal): "+ inputDocument[-40:] @@ -311,15 +314,15 @@ def main(): status = kb.the(t, rdft.status).string good = 1 if status != "APPROVED": - if verbose: print "\tNot approved: "+ inputDocument[-40:] + if verbose: print("\tNot approved: "+ inputDocument[-40:]) good = 0 categories = kb.each(t, rdf.type) for cat in categories: if cat is triage.knownError: - if verbose: print "\tknown failure: "+ inputDocument[-40:] + if verbose: print("\tknown failure: "+ inputDocument[-40:]) good = 0 if cat is triage.ReificationTest: - if verbose: print "\tNot supported (reification): "+ inputDocument[-40:] + if verbose: print("\tNot supported (reification): "+ inputDocument[-40:]) good = 0 if good: RDFNegativeTestData.append((t.uriref(), case, description, inputDocument)) @@ -340,7 +343,7 @@ def main(): categories = kb.each(t, rdf.type) for cat in categories: if cat is triage.knownError: - if verbose: print "\tknown failure: "+ inputDocument[-40:] + if verbose: print("\tknown failure: "+ inputDocument[-40:]) good = 0 if good: n3PositiveTestData.append((t.uriref(), case, description, inputDocument)) @@ -382,8 +385,8 @@ def main(): good = 1 status = kb.the(subj=t, pred=dawg_test.approval) if status != dawg_test.Approved: - print status, name - if verbose: print "\tNot approved: "+ inputDocument[-40:] + print(status, name) + if verbose: print("\tNot approved: "+ inputDocument[-40:]) good = 0 if good: sparqlTestData.append((tt.uriref(), case, name, inputDocument, data, outputDocument)) @@ -401,24 +404,24 @@ def main(): else: env = str(environment) + " " perfData.append((x, theTime, description, env, arguments)) - testData.sort() + testData.sort(key=byCaseURI) cwmTests = len(testData) - if verbose: print "Cwm tests: %i" % cwmTests - RDFTestData.sort() - RDFNegativeTestData.sort() + if verbose: print("Cwm tests: %i" % cwmTests) + RDFTestData.sort(key=byCaseURI) + RDFNegativeTestData.sort(key=byCaseURI) rdfTests = len(RDFTestData) rdfNegativeTests = len(RDFNegativeTestData) - perfData.sort() + perfData.sort(key=byCaseURI) perfTests = len(perfData) - n3PositiveTestData.sort() + n3PositiveTestData.sort(key=byCaseURI) n3PositiveTests = len(n3PositiveTestData) - n3NegativeTestData.sort() + n3NegativeTestData.sort(key=byCaseURI) n3NegativeTests = len(n3NegativeTestData) - sparqlTestData.sort() + sparqlTestData.sort(key=byCaseURI) sparqlTests = len(sparqlTestData) totalTests = cwmTests + rdfTests + rdfNegativeTests + sparqlTests \ + perfTests + n3PositiveTests + n3NegativeTests - if verbose: print "RDF parser tests: %i" % rdfTests + if verbose: print("RDF parser tests: %i" % rdfTests) for t, u, case, refFile, description, env, arguments, verboseDebug in testData: tests = tests + 1 @@ -426,7 +429,7 @@ def main(): urel = refTo(base(), u) - print "%3i/%i %-30s %s" %(tests, totalTests, urel, description) + print("%3i/%i %-30s %s" %(tests, totalTests, urel, description)) # print " %scwm %s giving %s" %(arguments, case) assert case and description and arguments cleanup = """sed -e 's/\$[I]d.*\$//g' -e "s;%s;%s;g" -e '/@prefix run/d' -e 's;%s;%s;g'""" % (WD, REFWD, @@ -461,7 +464,7 @@ def main(): if tests < start: continue urel = refTo(base(), u) - print "%3i/%i %-30s %s" %(tests, totalTests, urel, name) + print("%3i/%i %-30s %s" %(tests, totalTests, urel, name)) inNtriples = case + '_1' outNtriples = case + '_2' try: @@ -486,7 +489,7 @@ def main(): if tests < start: continue - print "%3i/%i) %s %s" %(tests, totalTests, case, description) + print("%3i/%i) %s %s" %(tests, totalTests, case, description)) # print " %scwm %s giving %s" %(inputDocument, case) assert case and description and inputDocument and outputDocument # cleanup = """sed -e 's/\$[I]d.*\$//g' -e "s;%s;%s;g" -e '/@prefix run/d' -e '/^#/d' -e '/^ *$/d'""" % ( @@ -503,7 +506,7 @@ def main(): if tests < start: continue - print "%3i/%i) %s %s" %(tests, totalTests, case, description) + print("%3i/%i) %s %s" %(tests, totalTests, case, description)) # print " %scwm %s giving %s" %(inputDocument, case) assert case and description and inputDocument # cleanup = """sed -e 's/\$[I]d.*\$//g' -e "s;%s;%s;g" -e '/@prefix run/d' -e '/^#/d' -e '/^ *$/d'""" % ( @@ -528,7 +531,7 @@ def main(): if tests < start: continue - print "%3i/%i) %s %s" %(tests, totalTests, case, description) + print("%3i/%i) %s %s" %(tests, totalTests, case, description)) # print " %scwm %s giving %s" %(inputDocument, case) assert case and description and inputDocument # cleanup = """sed -e 's/\$[I]d.*\$//g' -e "s;%s;%s;g" -e '/@prefix run/d' -e '/^#/d' -e '/^ *$/d'""" % ( @@ -546,7 +549,7 @@ def main(): if tests < start: continue - print "%3i/%i) %s %s" %(tests, totalTests, case, description) + print("%3i/%i) %s %s" %(tests, totalTests, case, description)) # print " %scwm %s giving %s" %(inputDocument, case) assert case and description and inputDocument # cleanup = """sed -e 's/\$[I]d.*\$//g' -e "s;%s;%s;g" -e '/@prefix run/d' -e '/^#/d' -e '/^ *$/d'""" % ( @@ -573,13 +576,13 @@ def main(): urel = refTo(base(), u) - print "%3i/%i %-30s %s" %(tests, totalTests, urel, description) + print("%3i/%i %-30s %s" %(tests, totalTests, urel, description)) tt = os.times()[-1] a = system("""%s %s %s --quiet %s >,time.out""" % (env, python_command, cwm_command, arguments)) userTime = os.times()[-1] - tt - print """%spython %s --quiet %s 2>,time.out""" % \ - (env, cwm_command, arguments) + print("""%spython %s --quiet %s 2>,time.out""" % \ + (env, cwm_command, arguments)) ## c = file(',time.out', 'r') ## timeOutput = c.read() ## c.close() @@ -588,7 +591,7 @@ def main(): ## userTimeStr = timeList[1] ## userTime = int(userTimeStr[0])*60 + float(userTimeStr[1] + '.' + userTimeStr[2]) pyCount = pyStoneTime * userTime - print pyCount + print(pyCount) if problems != []: sys.stderr.write("\nProblems:\n") diff --git a/uripath.py b/uripath.py index ba841a23..4c4aafef 100644 --- a/uripath.py +++ b/uripath.py @@ -99,7 +99,6 @@ def join(here, there): """ assert(here.find("#") < 0), "Base may not contain hash: '%s'"% here # caller must splitFrag (why?) - print('@@ join ', here, there) slashl = there.find('/') colonl = there.find(':') @@ -229,7 +228,7 @@ def refTo(base, uri): if base.find("//", i-2)>0 \ or uri.find("//", i-2)>0: return uri # An unshared "//" if base.find(":", i)>0: return uri # An unshared ":" - n = string.count(base, "/", i) + n = base.count("/", i) if n == 0 and i Date: Wed, 3 Jan 2024 12:16:54 +0000 Subject: [PATCH 08/18] get rid of circular dependency problem --- cwm.py | 7 ++++--- formula.py | 8 +++++++- myStore.py | 18 +++++++++--------- pretty.py | 10 +++++----- sax2rdf.py | 7 ++++--- term.py | 11 ++++++----- test/retest.py | 12 ++++++------ why.py | 2 -- 8 files changed, 41 insertions(+), 34 deletions(-) diff --git a/cwm.py b/cwm.py index 9e22fa7e..21adc4d0 100755 --- a/cwm.py +++ b/cwm.py @@ -67,9 +67,10 @@ ############## Temp test setup # @@ this should be able to be set up in the launch.json file in vscode -for arg in ["--chatty=100", "--n3", "test/t1.n3"]: - sys.argv.append(arg) -print('@@ sys.argv', len(sys.argv)) +# for arg in ["--chatty=100", "--n3", "test/t1.n3"]: +# sys.argv.append(arg) +# print('@@ sys.argv', len(sys.argv)) + ################################################# Command line diff --git a/formula.py b/formula.py index bc3be154..bd062f61 100755 --- a/formula.py +++ b/formula.py @@ -29,7 +29,6 @@ from .set_importer import Set, ImmutableSet, sorted -from . import notation3 # N3 parsers and generators, and RDF generator from . import diag # problems importing the tracking flag, must be explicit it seems diag.tracking from .diag import progress, verbosity, tracking @@ -850,9 +849,14 @@ def comparePredObj(self, other): if s is not o: return s.compareAnyTerm(o) return 0 + # Python3 requires we use keyfunctions instead of CMP + def keyForPredObj(self): return [self.quad[PRED].sortKey(), self.quad[OBJ].sortKey()] + + def keyForSubjPredObj(self): + return [self.quad[SUBJ].sortKey(), self.quad[PRED].sortKey(), self.quad[OBJ].sortKey()] def context(self): """Return the context of the statement""" @@ -953,5 +957,7 @@ def asFormula(self, why=None): +from . import notation3 # N3 parsers and generators, and RDF generator + #ends diff --git a/myStore.py b/myStore.py index 04ea3d2d..97d97f47 100755 --- a/myStore.py +++ b/myStore.py @@ -96,8 +96,8 @@ def _checkStore(s=None): global store, storeClass if s != None: return s if store != None: return store - if storeClass == None: - from . import llyn # default + #if storeClass == None: + # from . import llyn # default assert storeClass!= None, "Some storage module must register with myStore.py before you can use it" store = storeClass() # Make new one return store @@ -214,13 +214,13 @@ def sym(self, lname): -def _test(): - from . import llyn - store = llyn.RDFStore() - setStore(store) - - import doctest, myStore - return doctest.testmod(myStore) +#def _test(): +# from . import llyn # avoid circular dependency? +# store = llyn.RDFStore() +# setStore(store) +# +# import doctest, myStore + # return doctest.testmod(myStore) if __name__ == "__main__": _test() diff --git a/pretty.py b/pretty.py index ef1eb86d..b769a9e6 100755 --- a/pretty.py +++ b/pretty.py @@ -184,7 +184,7 @@ def selectDefaultPrefix(self, printFunction): def dumpPrefixes(self): if self.defaultNamespace is not None: - sink.setDefaultNamespace(self.defaultNamespace) + self.sink.setDefaultNamespace(self.defaultNamespace) prefixes = list(self.store.namespaces.keys()) # bind in same way as input did FYI prefixes.sort() for pfx in prefixes: @@ -314,7 +314,7 @@ def dumpBySubject(self, sorting=1): self.dumpLists() ss = context.statements[:] - ss.sort() + ss.sort(key = StoredStatement.keyForSubjPredObj) def fixSet(x): try: return x._node @@ -334,7 +334,7 @@ def fixSet(x): if sorting: rs.sort(key=Term.sortKey) for r in rs : # First the bare resource statements = context.statementsMatching(subj=r) - if sorting: statements.sort(key=StoredStatement.keyForPredObj) + if sorting: statements.sort(key = StoredStatement.keyForPredObj) for s in statements : self._outputStatement(sink, s.quad) if not isinstance(r, Literal): @@ -545,7 +545,7 @@ def _dumpNode(self, node): tm.endList() elif isinstance(node, N3Set): pass - elif isinstance(node, formula): + elif isinstance(node, Formula): tm.startFormula() self._dumpFormula(node) tm.endFormula() @@ -580,7 +580,7 @@ def dumpFormulaContents(self, context, sink, sorting, equals=0): if not x.generated() and x not in context.variables(): allStatements.append(StoredStatement( (context, context.store.sameAs, x, y))) - allStatements.sort() + allStatements.sort(key = StoredStatement.keyForSubjPredObj) # context.statements.sort() # @@ necessary? self.dumpVariables(context, sink, sorting, pretty=1) diff --git a/sax2rdf.py b/sax2rdf.py index c1778085..6c734a55 100644 --- a/sax2rdf.py +++ b/sax2rdf.py @@ -74,7 +74,7 @@ import sys from . import uripath -from .why import BecauseOfData +# from .why import BecauseOfData # can't: circular from . import isXML from . import diag #from webAccess import urlopenForRDF # http://www.w3.org/2000/10/swap/ @@ -179,8 +179,9 @@ def __init__(self, sink, openFormula, thisDoc, baseURI=None, flags="", why=None) self._context = None self._reason = why # Why the parser w self._reason2 = None # Why these triples - if diag.tracking: self._reason2 = BecauseOfData( - sink.newSymbol(thisDoc), because=self._reason) + # if diag.tracking: self._reason2 = BecauseOfData( + # sink.newSymbol(thisDoc), because=self._reason) + if diag.tracking: self._reason2 = None # @@ fix self._subject = None self._predicate = None diff --git a/term.py b/term.py index 6f7f62c4..73fae179 100755 --- a/term.py +++ b/term.py @@ -47,6 +47,7 @@ from .RDFSink import Logic_NS from .OrderedSequence import merge, intersection, minus +# from .formula import Formula, StoredStatement no circular from . import diag from .diag import progress @@ -180,8 +181,8 @@ def substitution(self, node, *otherEnvs): return [self.substitution(x, otherEnvs) for x in node] if isinstance(node, tuple): return tuple([self.substitution(x, otherEnvs) for x in node]) - if isinstance(node, (formula.StoredStatement, term.Term)): - return node.substitution(self) ## Wrong! + # if isinstance(node, (StoredStatement, Term)): # @@ circular import + # return node.substitution(self) ## Wrong! return node @@ -239,9 +240,9 @@ def __repr__(self): def debugString(self, already=[]): return repr(self) # unless more eleborate in superclass - def representation(self, base=None): + def representation(self): """The string represnting this in N3 """ - return "<" + self.uriref(base) + ">" + return "<" + self.uriref() + ">" def generated(self): """Boolean Is this thing a genid - is its name arbitrary? """ @@ -463,7 +464,7 @@ def dereference(self, mode="", workingContext=None): self, workingContext)) workingContext.store.copyFormula(F, workingContext) if "x" in mode: # capture experience - workingContext.add(r, self.store.semantics, F) + workingContext.add(self, self.store.semantics, F) if not hasattr(self, "_semantics"): setattr(self, "_semantics", F) if diag.chatty_flag > 25: diff --git a/test/retest.py b/test/retest.py index 6dd3dd50..f81bafb7 100755 --- a/test/retest.py +++ b/test/retest.py @@ -89,7 +89,7 @@ def execute(cmd1, noStdErr=False): stderr = None try: if noStdErr: - stderr = file('/dev/null', 'w') + stderr = open('/dev/null', 'w') result = call(cmd1, shell=True, stderr=stderr) finally: if stderr: @@ -176,7 +176,7 @@ def main(): global verbose, proofs, chatty, normal, no_action start = 1 cwm_command='../cwm.py' - python_command='python3 -tt' + python_command='python3' global ploughOn # even if error ploughOn = 0 global verbose @@ -184,7 +184,7 @@ def main(): global just_fix_it just_fix_it = 0 if diag.print_all_file_names: - a = file('testfilelist','w') + a = open('testfilelist','w') a.write('') a.close() try: @@ -568,8 +568,8 @@ def main(): timeMatcher = re.compile(r'\t([0-9]+)m([0-9]+)\.([0-9]+)s') -## from test.pystone import pystones -## pyStoneTime = pystones()[1] + # from test.pystone import pystones + # pyStoneTime = pystones()[1] for u, theTime, description, env, arguments in perfData: tests = tests + 1 if tests < start: continue @@ -590,7 +590,7 @@ def main(): ## print timeList ## userTimeStr = timeList[1] ## userTime = int(userTimeStr[0])*60 + float(userTimeStr[1] + '.' + userTimeStr[2]) - pyCount = pyStoneTime * userTime + pyCount = userTime ## * pyStoneTime print(pyCount) if problems != []: diff --git a/why.py b/why.py index fecf2012..79606682 100755 --- a/why.py +++ b/why.py @@ -642,8 +642,6 @@ def describeStatement(s_l, ko, flags): return si - - class BecauseOfData(Because): """Directly from data in the resource whose URI is the string. From 4486bc6e05552f7e9231b2b0a2efe0cf11330b2d Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Wed, 3 Jan 2024 20:50:46 +0000 Subject: [PATCH 09/18] Passes first 1 regression test of 94 --- cwm.py | 6 ------ myStore.py | 4 ++-- term.py | 26 ++++++++++++++++++-------- test/retest.py | 16 ++++++++++++---- 4 files changed, 32 insertions(+), 20 deletions(-) diff --git a/cwm.py b/cwm.py index 21adc4d0..fb67a882 100755 --- a/cwm.py +++ b/cwm.py @@ -204,7 +204,6 @@ def doCommand(): # The base URI for this process - the Web equiv of cwd _baseURI = uripath.base() - print('Current base: ', _baseURI) option_format = "n3" # set the default format option_first_format = None @@ -448,12 +447,7 @@ def filterize(): except ValueError: _uri =_rhs if arg[0] != "-": - print('cwm pass 2 option_baseURI', option_baseURI) - print('cwm pass 2 splitFrag(arg)[0]', splitFrag(arg)[0]) - _inputURI = join(option_baseURI, splitFrag(arg)[0]) - print('cwm pass 2 _inputURI', _inputURI) - assert ':' in _inputURI ContentType={ "rdf": "application/xml+rdf", "n3": "text/n3", diff --git a/myStore.py b/myStore.py index 97d97f47..971473f7 100755 --- a/myStore.py +++ b/myStore.py @@ -222,5 +222,5 @@ def sym(self, lname): # import doctest, myStore # return doctest.testmod(myStore) -if __name__ == "__main__": - _test() +# if __name__ == "__main__": +# _test() diff --git a/term.py b/term.py index 73fae179..a584218b 100755 --- a/term.py +++ b/term.py @@ -60,7 +60,7 @@ raise RuntimeError("Sorry, this software requires python2.3 or newer.") - +# RDF_type_URI = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" ######################################## Storage URI Handling # @@ -372,17 +372,22 @@ class Node(Term): class LabelledNode(Node): "The labelled node is one which has a URI." + + def sortKey(self): + if (self.uriref() == RDF_type_URI): + return str(self.classOrder()) + '__' + '< >' # type always comes first in predicates + return str(self.classOrder()) + '__' + self.representation() # works for simple terms only def compareTerm(self, other): "Assume is also a LabelledNode - see function compareTerm in formula.py" - _type = RDF_type_URI + RDF_type_URItype = RDF_type_URI s = self.uriref() - if self is self.store.type: + if s == RDF_type_URI: return -1 o = other.uriref() - if other is self.store.type: + if o == RDF_type_URI: return 1 - retVal = cmp(s, o) + retVal = compareString(s, o) if retVal: return retVal progress( "Error with '%s' being the same as '%s'" %(s,o)) @@ -471,6 +476,10 @@ def dereference(self, mode="", workingContext=None): progress("Web: Dereferencing %s gave %s" %(self, F)) return F +def compareString(self, other): + if self < other: return -1 + if self > other: return 1 + return 0 class Fragment(LabelledNode): """ A Term which DOES have a fragment id in its URI @@ -485,12 +494,13 @@ def __init__(self, resource, fragid): def compareTerm(self, other): if not isinstance(other, Fragment): return LabelledNode.compareTerm(self, other) - if self is self.resource.store.type: + if self.uriref() == RDF_type_URI: + # if self is self.resource.store.type: return -1 - if other is self.resource.store.type: + if other.uriref() == RDF_type_URI: return 1 if self.resource is other.resource: - return cmp(self.fragid, other.fragid) + return compareString(self.fragid, other.fragid) return self.resource.compareTerm(other.resource) def uriref(self): diff --git a/test/retest.py b/test/retest.py index f81bafb7..3be91d1f 100755 --- a/test/retest.py +++ b/test/retest.py @@ -30,12 +30,13 @@ import os import sys import urllib.request, urllib.parse, urllib.error +from swap import uripath # From PYTHONPATH equivalent to http://www.w3.org/2000/10 from swap import llyn from swap.myStore import load, loadMany, Namespace -from swap.uripath import refTo, base +from swap.uripath import refTo, base, join from swap import diag from swap.diag import progress from swap.term import AnonymousNode, Term @@ -62,6 +63,11 @@ verbose = 0 no_action = 0 + +# The base URI for this process - the Web equiv of cwd +processBaseURI = uripath.base() +print('Current base: ', processBaseURI) + def localize(uri): """Get URI relative to where this lives""" from swap import uripath @@ -80,7 +86,7 @@ def problem(str): def usage(): print(__doc__) -from subprocess import call, PIPE +from subprocess import call def execute(cmd1, noStdErr=False): global verbose, no_action @@ -113,12 +119,14 @@ def diff(case, ref=None, prog="diff -Bbwu"): if result < 0: raise problem("Comparison fails: result %i executing %s" %(result, diffcmd)) if result > 0: print("Files differ, result=", result) - d = urllib.request.urlopen(",diffs/"+case) + diffFileURI = join(processBaseURI, ",diffs/"+case) + + d = urllib.request.urlopen(diffFileURI) buf = d.read() if len(buf) > 0: if just_fix_it == 0: print("# If this is OK, cp ,temp/%s %s" %(case, ref)) - print("######### Differences from reference output:\n" + buf) + print("######### Differences from reference output:\n", buf) return 1 else: os.system("cp ,temp/%s %s" %(case, ref)) From da4d672a6aca590b4225fceba785e1b6d980fde5 Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Wed, 3 Jan 2024 21:13:22 +0000 Subject: [PATCH 10/18] Total 67 errors in 94 tests. --- cwm.py | 2 -- pretty.py | 5 +++-- test/retest.py | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cwm.py b/cwm.py index fb67a882..67ca141a 100755 --- a/cwm.py +++ b/cwm.py @@ -221,8 +221,6 @@ def doCommand(): _rhs = "" try: [_lhs,_rhs]=arg.split('=',1) - print('@@@ oop 1 : ', _lhs, _rhs) - try: _uri = join(option_baseURI, _rhs) except ValueError: diff --git a/pretty.py b/pretty.py index b769a9e6..e47ab6b9 100755 --- a/pretty.py +++ b/pretty.py @@ -536,12 +536,13 @@ def tmDumpNested(self): self._dumpFormula(context) self.tm.end() - def _dumpNode(self, node): + # Unsed - delete? + def _dumpNode(self, node, context): tm = self.tm _anon, _incoming = self._topology(node, context) if isinstance(node, List): tm.startList() - [self._dumpNode(x) for x in node] + [self._dumpNode(x, context) for x in node] tm.endList() elif isinstance(node, N3Set): pass diff --git a/test/retest.py b/test/retest.py index 3be91d1f..15ca40a3 100755 --- a/test/retest.py +++ b/test/retest.py @@ -126,7 +126,7 @@ def diff(case, ref=None, prog="diff -Bbwu"): if len(buf) > 0: if just_fix_it == 0: print("# If this is OK, cp ,temp/%s %s" %(case, ref)) - print("######### Differences from reference output:\n", buf) + print("######### Differences from reference output:\n", buf.decode('utf-8')) return 1 else: os.system("cp ,temp/%s %s" %(case, ref)) From 540ad785be1a17391bd4130e2f4695f031ed4f95 Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Wed, 3 Jan 2024 22:04:48 +0000 Subject: [PATCH 11/18] undefines out of pretty formula notation3 sax2rdf --- formula.py | 11 +++++++---- notation3.py | 9 +++++---- sax2rdf.py | 4 ++-- term.py | 35 +++++++++++++++++++++-------------- 4 files changed, 35 insertions(+), 24 deletions(-) diff --git a/formula.py b/formula.py index bd062f61..3e797840 100755 --- a/formula.py +++ b/formula.py @@ -35,7 +35,7 @@ from .term import matchSet, \ AnonymousNode , AnonymousExistential, AnonymousUniversal, \ Term, CompoundTerm, List, \ - unifySequence, unify + unifySequence, unify, compareStrings, compareNumbers from .RDFSink import Logic_NS from .RDFSink import CONTEXT, PRED, SUBJ, OBJ @@ -128,7 +128,7 @@ def compareTerm(self, other): s.sort() # forumulae are all the same o.sort() for i in range(ls): - diff = cmp(s[i],o[i]) + diff = s[i].compareTerm(o[i]) if diff != 0: return diff return 0 from . import why @@ -702,6 +702,9 @@ def outputStrings(self, channel=None, relation=None): strings output. @@ what is this doing here?? + This is a feature for writing reports. It allows rules to crate statements that + say that "foo bar" is an output String then use --strings on the command line to generate the + report. """ if channel == None: channel = sys.stdout @@ -711,7 +714,7 @@ def outputStrings(self, channel=None, relation=None): pairs = [] for s in list: pairs.append((s[SUBJ], s[OBJ])) - pairs.sort(comparePair) + pairs.sort() for key, str in pairs: channel.write(str.string.encode('utf-8')) @@ -815,7 +818,7 @@ def __cmp__(self, other): Avoid loops by spotting reference to containing formula""" if self is other: return 0 if not isinstance(other, StoredStatement): - return cmp(self.__class__, other.__class__) + return compareStrings(self.__class__, other.__class__) sc = self.quad[CONTEXT] oc = other.quad[CONTEXT] for p in [SUBJ, PRED, OBJ]: # Note NOT internal order diff --git a/notation3.py b/notation3.py index bd09c115..3191f904 100755 --- a/notation3.py +++ b/notation3.py @@ -173,7 +173,7 @@ def __init__(self, store, openFormula=None, thisDoc="", baseURI=None, self.makeStatement(((SYMBOL, metaURI), # quantifiers - use inverse? (SYMBOL, N3_forSome_URI), #pred self._context, #subj - subj)) # obj + self._context)) # obj def here(self, i): """String generated from position in file @@ -1741,6 +1741,7 @@ def __init__(self, write, base=None, genPrefix = None, self.defaultNamespace = None self.indent = 1 # Level of nesting of output self.base = base + self.bNodes = {} # Map from bnode to generated ID # self.nextId = 0 # Regenerate Ids on output self.regen = {} # Mapping of regenerated Ids # self.genPrefix = genPrefix # Prefix for generated URIs on output @@ -1918,11 +1919,11 @@ def addAnonymous(self, Id): function to call """ - if Id not in bNodes: + if Id not in self.bNodes: a = self.formulas[-1].newBlankNode() - bNodes[Id] = a + self.bNodes[Id] = a else: - a = bNodes[Id] + a = self.bNodes[Id] self.addNode(a) diff --git a/sax2rdf.py b/sax2rdf.py index 6c734a55..8b7c1ff0 100644 --- a/sax2rdf.py +++ b/sax2rdf.py @@ -275,7 +275,7 @@ def idAboutAttr(self, attrs): #MS1.0 6.5 also proprAttr 6.10 elif ln == "aboutEachPrefix": if value == " ": # OK - a trick to make NO subject self._subject = None - else: raise ooops # can't do about each prefix yet + else: raise NotImplementedError("can't do about each prefix yet") elif ln == "bagID": if not isXML.isName(value): raise BadSyntax(sys.exc_info(), 'A bagID must be a Name %s' % value) @@ -852,7 +852,7 @@ def literal_element_start_DOM_OLD(self, name, qname, attrs): e.setAttribute(name[1], value) #@@@ Missing prefix on qname #@@@ may need calculating as in the non-dom case, alas. - def literal_element_end(selsf, name, qname): + def literal_element_end(self, name, qname): if name[0]: prefix = self._prefixMap[-1][name[0]] if prefix: diff --git a/term.py b/term.py index a584218b..4a0239be 100755 --- a/term.py +++ b/term.py @@ -206,7 +206,19 @@ def pickEnv(choice, *envs): if choice is env.id: return env return [] ## Not here. - + +def compareStrings(self, other): + if self < other: return -1 + if self > other: return 1 + return 0 + +def compareNumbers(self, other): + if self < other: return -1 + if self > other: return 1 + return 0 + +# This class is the core one for the system. + class Term(object): """The Term object represents an RDF term. @@ -263,7 +275,7 @@ def compareAnyTerm(self, other): This is not done yet """ if self is other: return 0 - diff = cmp(self.classOrder(), other.classOrder()) + diff = compareNumber(self.classOrder(), other.classOrder()) if diff != 0: return diff return self.compareTerm(other) @@ -387,7 +399,7 @@ def compareTerm(self, other): o = other.uriref() if o == RDF_type_URI: return 1 - retVal = compareString(s, o) + retVal = compareStrings(s, o) if retVal: return retVal progress( "Error with '%s' being the same as '%s'" %(s,o)) @@ -476,11 +488,6 @@ def dereference(self, mode="", workingContext=None): progress("Web: Dereferencing %s gave %s" %(self, F)) return F -def compareString(self, other): - if self < other: return -1 - if self > other: return 1 - return 0 - class Fragment(LabelledNode): """ A Term which DOES have a fragment id in its URI """ @@ -568,10 +575,10 @@ def compareTerm(self, other): otherSerial = other.uri else: otherSerial = other.serial - retVal = cmp(selfSerial, otherSerial) + retVal = compareStrings(selfSerial, otherSerial) if retVal: return retVal - return cmp(self.serial, other.serial) + return compareStrings(self.serial, other.serial) def classOrder(self): """Anonymous ndoes are higher than symbols as the = smushing @@ -1407,9 +1414,9 @@ def classOrder(self): def compareTerm(self, other): "Assume is also a literal - see function compareTerm in formula.py" if self.datatype == other.datatype: - diff = cmp(self.string, other.string) + diff = compareStrings(self.string, other.string) if diff != 0 : return diff - return cmp(self.lang, other.lang) + return compareStrings(self.lang, other.lang) else: if self.datatype == None: return -1 if other.datatype == None: return 1 @@ -1516,9 +1523,9 @@ def classOrder(self): def compareTerm(self, other): "Assume is also a literal - see function compareTerm in formula.py" if self.datatype == other.datatype: - diff = cmp(str(self), str(other)) + diff = compareStrings(str(self), str(other)) return diff - return cmp(self.lang, other.lang) + return compareStrings(self.lang, other.lang) else: if self.datatype == None: return -1 if other.datatype == None: return 1 From 0f2b461e8f6791a46c06a0eef2d4aefd6a6203f7 Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Thu, 4 Jan 2024 17:03:11 +0000 Subject: [PATCH 12/18] toXML encoding --- notation3.py | 23 ++++++++++++----------- sax2rdf.py | 2 ++ toXML.py | 21 +++++++++++---------- why.py | 2 +- 4 files changed, 26 insertions(+), 22 deletions(-) diff --git a/notation3.py b/notation3.py index 3191f904..89ee94fa 100755 --- a/notation3.py +++ b/notation3.py @@ -55,7 +55,7 @@ N3_forSome_URI = RDFSink.forSomeSym N3_forAll_URI = RDFSink.forAllSym - +FRESH = 2 # Magic resources we know about @@ -126,7 +126,7 @@ def __init__(self, store, openFormula=None, thisDoc="", baseURI=None, if genPrefix: store.setGenPrefix(genPrefix) # pass it on self._thisDoc = thisDoc - self.lines = 0 # for error handling + self.lines = 0 # for error handling count line number self.startOfLine = 0 # For calculating character number self._genPrefix = genPrefix self.keywords = ['a', 'this', 'bind', 'has', 'is', 'of', 'true', 'false' ] @@ -166,10 +166,11 @@ def __init__(self, store, openFormula=None, thisDoc="", baseURI=None, self._parentContext = None if metaURI: - self.makeStatement((SYMBOL, metaURI), # relate doc to parse tree - (SYMBOL, PARSES_TO_URI ), #pred - (SYMBOL, thisDoc), #subj - self._context) # obj + # self.makeStatement((SYMBOL, metaURI), # relate doc to parse tree + # (SYMBOL, PARSES_TO_URI ), #pred + # (SYMBOL, thisDoc), #subj + # self._context) # obj + self.makeStatement(((SYMBOL, metaURI), # quantifiers - use inverse? (SYMBOL, N3_forSome_URI), #pred self._context, #subj @@ -1035,7 +1036,7 @@ def nodeOrLiteral(self, str, i, res): if str[j:j+1] == "@": # Language? m = langcode.match(str, j+1) if m == None: - raise BadSyntax(self._thisDoc, startline, str, i, + raise BadSyntax(self._thisDoc, self.lines, str, i, "Bad language code syntax on string literal, after @") i = m.end() lang = str[j+1:i] @@ -1209,11 +1210,11 @@ def UEscape(self, str, i, startline): class BadSyntax(SyntaxError): - def __init__(self, uri, lines, str, i, why): - self._str = str.encode('utf-8') # Better go back to strings for errors + def __init__(self, uri, lineNumber, str, i, why): + # self._str = str.encode('utf-8') # Better go back to strings for errors - pre python3 self._i = i self._why = why - self.lines = lines + self.lineNumber = lineNumber self._uri = uri def __str__(self): @@ -1228,7 +1229,7 @@ def __str__(self): else: post="" return 'at line %i of <%s>:\nBad syntax (%s) at ^ in:\n"%s%s^%s%s"' \ - % (self.lines +1, self._uri, self._why, pre, + % (self.lineNumber +1, self._uri, self._why, pre, str[st:i], str[i:i+60], post) diff --git a/sax2rdf.py b/sax2rdf.py index 8b7c1ff0..9b6e60a5 100644 --- a/sax2rdf.py +++ b/sax2rdf.py @@ -257,9 +257,11 @@ def idAboutAttr(self, attrs): #MS1.0 6.5 also proprAttr 6.10 raise BadSyntax(sys.exc_info(), ">1 subject") self._subject = self.sink.newSymbol(self.uriref("#" + value)) elif ln == "about": + print('@@ about ') if self._subject: raise BadSyntax(sys.exc_info(), "Subject already defined to be %s, can't have attribute about='%s'" % (repr(self._subject), value)) + print('base: ', self._base) self._subject = self.sink.newSymbol(self.uriref(value)) elif ln == "nodeID": if self._subject: raise BadSyntax(sys.exc_info(), diff --git a/toXML.py b/toXML.py index bdad4f12..2544fb64 100755 --- a/toXML.py +++ b/toXML.py @@ -121,12 +121,13 @@ class ToRDF(RDFSink.RDFStructuredOutput): def __init__(self, outFp, thisURI=None, base=None, flags=""): RDFSink.RDFSink.__init__(self) if outFp == None: + # raise ValueError ('No output file given for XML') self._xwr = XMLWriter(dummyWrite, self) else: - dummyEnc, dummyDec, dummyReader, encWriter = codecs.lookup('utf-8') - z = encWriter(outFp) - zw = z.write - self._xwr = XMLWriter(zw, self) + # dummyEnc, dummyDec, dummyReader, encWriter = codecs.lookup('utf-8') + # z = encWriter(outFp) + # zw = z.write + self._xwr = XMLWriter(outFp.write, self) self._subj = None self._base = base self._formula = None # Where do we get this from? The outermost formula @@ -519,12 +520,12 @@ class XMLWriter: """ taken from Id: tsv2xml.py,v 1.1 2000/10/02 19:41:02 connolly Exp connolly - Takes as argument a writer which does the (eg utf-8) encoding + Takes as argument a writer which does the (eg utf-8) encoding (python2 only) """ def __init__(self, encodingWriter, counter, squeaky=0, version='1.0'): -# self._outFp = outFp - self._encwr = encodingWriter + # self._outFp = outFp + self._encwr = encodingWriter self._elts = [] self.squeaky = squeaky # No, not squeaky clean output self.tab = 4 # Number of spaces to indent per level @@ -768,9 +769,9 @@ def __init__(self, outFp, thisURI=None, base=None, flags=""): if outFp == None: self._xwr = XMLWriter(dummyWrite, self) else: - dummyEnc, dummyDec, dummyReader, encWriter = codecs.lookup('utf-8') - z = encWriter(outFp) - zw = z.write + # dummyEnc, dummyDec, dummyReader, encWriter = codecs.lookup('utf-8') + # z = encWriter(outFp) + zw = outFp.write self._xwr = XMLWriter(zw, self) self._subj = None self._base = base diff --git a/why.py b/why.py index 79606682..c7d44959 100755 --- a/why.py +++ b/why.py @@ -12,7 +12,7 @@ flagHelp = """ n use iNdirect formulas when they get too large -g always give the :gives clause, even when not needed +g always give the :gives clause, even when not needed """ import string From b44528c907c5ec3d0de0b30d4810993089c30b6b Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Fri, 5 Jan 2024 17:26:10 +0000 Subject: [PATCH 13/18] utf8 encoding was not needed in python3 etc etc --- cwm_math.py | 2 +- cwm_maths.py | 2 +- llyn.py | 6 +++--- local_decimal.py | 32 +++++++++++++++++--------------- sax2rdf.py | 5 ++--- term.py | 10 ++++++---- test/ref/xml-redefine.rdf | 10 +++++----- test/ref/xml-redefine2.rdf | 10 +++++----- test/ref/xmllit.nt | 2 +- toXML.py | 5 +++-- xmlC14n.py | 34 ++++++++++++++++++++++------------ 11 files changed, 66 insertions(+), 52 deletions(-) diff --git a/cwm_math.py b/cwm_math.py index 40bf8f1d..a88a266a 100755 --- a/cwm_math.py +++ b/cwm_math.py @@ -248,4 +248,4 @@ def register(store): str.internFrag('memberCount', BI_memberCount) if __name__=="__main__": - print(string.strip(__doc__)) + print(__doc__.strip()) diff --git a/cwm_maths.py b/cwm_maths.py index 36332d73..9567c31e 100755 --- a/cwm_maths.py +++ b/cwm_maths.py @@ -227,4 +227,4 @@ def register(store): str.internFrag('memberCount', BI_memberCount) if __name__=="__main__": - print(string.strip(__doc__)) + print(__doc__.strip()) diff --git a/llyn.py b/llyn.py index d72e8575..b13be677 100755 --- a/llyn.py +++ b/llyn.py @@ -79,7 +79,7 @@ Literal, XMLLiteral, Symbol, Fragment, FragmentNil, Term, LabelledNode, \ CompoundTerm, List, EmptyList, NonEmptyList, AnonymousNode, N3Set, \ UnknownType -from .formula import Formula, StoredStatement +from .formula import Formula, StoredStatement # , keyForSubjPredObj # from . import reify from weakref import WeakValueDictionary @@ -593,7 +593,7 @@ def canonicalize(F, cannon=False): return F if diag.chatty_flag > 70: progress('I got here, possibles = ', possibles) - fl.sort() + fl.sort(key = StoredStatement.keyForSubjPredObj) fe = F.existentials() #fe.sort(Term.compareAnyTerm) fu = F.universals () @@ -610,7 +610,7 @@ def canonicalize(F, cannon=False): if gkey != l: raise RuntimeError("@@Key of %s is %s instead of %s" %(G, repr(gkey), repr(l))) - gl.sort() + gl.sort(key = StoredStatement.keyForSubjPredObj) for se, oe, in ((fe, G.existentials()), (fu, G.universals())): if se != oe: diff --git a/local_decimal.py b/local_decimal.py index 00dd5788..445701f0 100644 --- a/local_decimal.py +++ b/local_decimal.py @@ -7,8 +7,6 @@ $Id$ """ -# timbl -# from types import IntType, FloatType, LongType, StringTypes from math import log10 @@ -24,8 +22,10 @@ class Decimal: def normalize(self): """convert this Decimal into some sort of canonical form - """ + self.value = int(self.value) + self.magnitude = int(self.magnitude) + if self.value == 0: self.magnitude = 0 return @@ -38,32 +38,32 @@ def normalize(self): - def __init__(self, other=0): + def __init__(self, other = 0): """How to get a new Decimal - What happened? - """ + Argument can be string, int, long, or float, or existing Dedimal + """ if isinstance(other, Decimal): self.value = other.value self.magnitude = other.magnitude return - elif isinstance(other, IntType): - self.value = int(other) - self.magnitude = 0 - self.normalize() - return - elif isinstance(other, LongType): + elif isinstance(other, int): self.value = other self.magnitude = 0 self.normalize() return + #elif isinstance(other, LongType): + # self.value = other + # self.magnitude = 0 + # self.normalize() + # return elif hasattr(other,'__Decimal__') and callable(getattr(other, '__Decimal__')): a = other.__Decimal__() self.value = a.value self.magnitude = a.magnitude self.normalize() return - elif isinstance(other,FloatType): + elif isinstance(other,float): other = repr(other) try: other[0] @@ -350,6 +350,7 @@ def __rtruediv__(self, other): return self.__rdiv__(other) # def __setattr__(self, other): # pass + def __str__(self): """x.__str__() <==> str(x) """ @@ -371,8 +372,9 @@ def __str__(self): if magnitude == 0 and magSign == 1: output.append(".") magSign = 0 - digit = value.__mod__(10) - value = value // 10 + # digit = value.__mod__(10) + digit = int(value % 10) + value = int(value) // 10 output.append("0123456789"[digit]) magnitude = magnitude-1 while magnitude > 0: diff --git a/sax2rdf.py b/sax2rdf.py index 9b6e60a5..48c83023 100644 --- a/sax2rdf.py +++ b/sax2rdf.py @@ -257,11 +257,10 @@ def idAboutAttr(self, attrs): #MS1.0 6.5 also proprAttr 6.10 raise BadSyntax(sys.exc_info(), ">1 subject") self._subject = self.sink.newSymbol(self.uriref("#" + value)) elif ln == "about": - print('@@ about ') if self._subject: raise BadSyntax(sys.exc_info(), "Subject already defined to be %s, can't have attribute about='%s'" % (repr(self._subject), value)) - print('base: ', self._base) + # print('base: ', self._base) self._subject = self.sink.newSymbol(self.uriref(value)) elif ln == "nodeID": if self._subject: raise BadSyntax(sys.exc_info(), @@ -382,7 +381,7 @@ def startPrefixMapping(self, prefix, uri): it takes extra space and more time to set up a new binding.""" - #print "startPrefixMapping with prefix=", prefix, "uri=", `uri` + # print "startPrefixMapping with prefix=", prefix, "uri=", `uri` prefix = prefix or "" uri = uri or "" uri = self.uriref(uri) diff --git a/term.py b/term.py index 4a0239be..3a9b5640 100755 --- a/term.py +++ b/term.py @@ -96,7 +96,8 @@ def __init__(self, other=None, keywords={}): if other is None: dict.__init__(self) else: - dict.__init__(self, other, **keywords) + # dict.__init__(self, other, **keywords) + dict.__init__(self, other) self.id = self for k, (a,b) in self.items(): if isinstance(a, tuple): @@ -121,6 +122,7 @@ def asDict(self): def newBinding(self, var, val): retVal = Env(self, {var: val}) + # retVal = Env(self, var=val) retVal.id = self.id return retVal bind = newBinding @@ -243,7 +245,7 @@ def __repr__(self): if (p>=0 and s[p+1:].find(".") <0 ): # Can't use prefix if localname includes "." prefix = self.store.prefixes.get(s[:p+1], None) # @@ #CONVENTION - if prefix != None : return (prefix + ":" + s[p+1:]).encode('unicode_escape') + if prefix != None : return (prefix + ":" + s[p+1:]) if s.endswith("#_formula"): return "`"+s[-22:-9]+"`" # Hack - debug notation for formula if p >= 0: return s[p+1:] # .encode('unicode_escape') # @@ chek what that was for @@ -1398,8 +1400,8 @@ def occurringIn(self, vars): def __repr__(self): if len(self.string) < 8: - return '"%s"' % self.string.encode('unicode_escape') - return str('"' + self.string[0:4] + '...' + self.string[-4:] + '"').encode('unicode_escape')# return self.string + return '"%s"' % self.string # .encode('unicode_escape') + return str('"' + self.string[0:4] + '...' + self.string[-4:] + '"') # was encode def asPair(self): if self.datatype: diff --git a/test/ref/xml-redefine.rdf b/test/ref/xml-redefine.rdf index c36df5f8..1b0ef69b 100644 --- a/test/ref/xml-redefine.rdf +++ b/test/ref/xml-redefine.rdf @@ -2,17 +2,17 @@ + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:xml2="http://example.com/xml#"> - - - + + + diff --git a/test/ref/xml-redefine2.rdf b/test/ref/xml-redefine2.rdf index 563ff949..6acbcae0 100644 --- a/test/ref/xml-redefine2.rdf +++ b/test/ref/xml-redefine2.rdf @@ -2,10 +2,10 @@ + xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" + xmlns:xml2="http://example.com/xml#" + xmlns:xmlns2="http://example.com/xmlns#"> @@ -13,8 +13,8 @@

- - + +

diff --git a/test/ref/xmllit.nt b/test/ref/xmllit.nt index 93a77372..f80c8ffc 100644 --- a/test/ref/xmllit.nt +++ b/test/ref/xmllit.nt @@ -1,3 +1,3 @@ - "\n \n John\n \n "^^ . + " John "^^ . diff --git a/toXML.py b/toXML.py index 2544fb64..f6313848 100755 --- a/toXML.py +++ b/toXML.py @@ -156,8 +156,9 @@ def bind(self, prefix, namespace): prefix = self.namespace_redirections[prefix] else: realPrefix = prefix - while prefix in self.illegals or prefix[:3] == 'xml': - prefix = choice(ASCII_LETTERS) + prefix + while prefix in self.illegals or prefix in [ 'xml', 'xmlns' ]: + # prefix = choice(ASCII_LETTERS) + prefix # random choice bad for testing + prefix = prefix + '2' if realPrefix is not prefix: self.illegals.add(prefix) self.namespace_redirections[realPrefix] = prefix diff --git a/xmlC14n.py b/xmlC14n.py index 46eb76f1..a81a0df0 100644 --- a/xmlC14n.py +++ b/xmlC14n.py @@ -72,6 +72,12 @@ def _sorter(n1,n2): if i: return i return cmp(n1.localName, n2.localName) +def keyNamespaceLocalname(n): + '''keyNamespaceLocalname(n) -> string + Sorting key for NS attributes.''' + + return [n.namespaceURI, n.localName] # Namespace, localname + def _sorter_ns(n1,n2): '''_sorter_ns((n,v),(n,v)) -> int @@ -81,6 +87,10 @@ def _sorter_ns(n1,n2): if n2[0] == 'xmlns': return 1 return cmp(n1[0], n2[0]) +def keyNSURI(n): + "(an empty namespace URI is lexicographically least)." + return n[0] + def _utilized(n, node, other_attrs, unsuppressedPrefixes): '''_utilized(n, node, other_attrs, unsuppressedPrefixes) -> boolean Return true if that nodespace is utilized within the node''' @@ -183,10 +193,10 @@ def _do_text(self, node): Process a text or CDATA node. Render various special characters as their C14N entity representations.''' if not _in_subset(self.subset, node): return - s = string.replace(node.data, "&", "&") - s = string.replace(s, "<", "<") - s = string.replace(s, ">", ">") - s = string.replace(s, "\015", " ") + s = node.data.replace("&", "&") + s = s.replace("<", "<") + s = s.replace(">", ">") + s = s.replace("\015", " ") if s: self.write(s) handlers[Node.TEXT_NODE] = _do_text handlers[Node.CDATA_SECTION_NODE] = _do_text @@ -237,12 +247,12 @@ def _do_attr(self, n, value): W(' ') W(n) W('="') - s = string.replace(value, "&", "&") - s = string.replace(s, "<", "<") - s = string.replace(s, '"', '"') - s = string.replace(s, '\011', ' ') - s = string.replace(s, '\012', ' ') - s = string.replace(s, '\015', ' ') + s = value.replace("&", "&") + s = s.replace("<", "<") + s = s.replace('"', '"') + s = s.replace('\011', ' ') + s = s.replace('\012', ' ') + s = s.replace('\015', ' ') W(s) W('"') @@ -315,7 +325,7 @@ def _do_element(self, node, initial_other_attrs = []): ns_to_render.append((n, v)) # Sort and render the ns, marking what was rendered. - ns_to_render.sort(_sorter_ns) + ns_to_render.sort(key = keyNSURI) for n,v in ns_to_render: self._do_attr(n, v) ns_rendered[n]=v #0417 @@ -327,7 +337,7 @@ def _do_element(self, node, initial_other_attrs = []): other_attrs.extend(list(xml_attrs_local.values())) else: other_attrs.extend(list(xml_attrs.values())) - other_attrs.sort(_sorter) + other_attrs.sort(key = keyNamespaceLocalname) for a in other_attrs: self._do_attr(a.nodeName, a.value) W('>') From 00d3b2bc85b1dfb317fc99e71dbe07fa49f31dfc Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Sun, 7 Jan 2024 23:43:23 +0000 Subject: [PATCH 14/18] doc itself is firs subject in serialization --- compare.py | 16 ++++++++++++++++ cwm.py | 4 ++-- formula.py | 6 ++++-- isodate.py | 4 ++-- llyn.py | 10 +++++----- local_decimal.py | 7 ++++++- pretty.py | 23 +++++++++++++++++------ query.py | 4 ++-- serial.py | 9 +++++++++ term.py | 15 +++------------ test/retest.py | 14 +++++++++----- toXML.py | 20 ++++++++++---------- xmlC14n.py | 26 +++++++++++++++++--------- 13 files changed, 102 insertions(+), 56 deletions(-) create mode 100644 compare.py diff --git a/compare.py b/compare.py new file mode 100644 index 00000000..2d8bc503 --- /dev/null +++ b/compare.py @@ -0,0 +1,16 @@ +# Python2 had a `cmp` function +# Python3 removed it and replaced it with `__lt__`, `__eq__`, `__gt__` + +# for sorting, key functions are used instread of compaison functions in p3. +# but sometimes it useful to have a comparison function. + +def compareStrings(self, other): + if self < other: return -1 + if self > other: return 1 + return 0 + +def compareNumbers(self, other): + if self < other: return -1 + if self > other: return 1 + return 0 + diff --git a/cwm.py b/cwm.py index 67ca141a..4bb6f895 100755 --- a/cwm.py +++ b/cwm.py @@ -67,9 +67,9 @@ ############## Temp test setup # @@ this should be able to be set up in the launch.json file in vscode -# for arg in ["--chatty=100", "--n3", "test/t1.n3"]: +# for arg in "-n3 test/equiv-syntax.n3 -rdf".split(' '): # sys.argv.append(arg) -# print('@@ sys.argv', len(sys.argv)) +# print('@@ sys.argv', len(sys.argv)) ################################################# Command line diff --git a/formula.py b/formula.py index 3e797840..3e9d494c 100755 --- a/formula.py +++ b/formula.py @@ -26,6 +26,8 @@ import types import io import sys # for outputstrings. shouldn't be here - DWC +from .compare import compareStrings + from .set_importer import Set, ImmutableSet, sorted @@ -490,9 +492,9 @@ def renameVars(self): n = {} F1 = self.newFormula() F1.loadFormulaWithSubstitution(self, m2, why=Because("Vars in subexpressions must be renamed")) - for v in sorted(list(F1.existentials()), Term.compareAnyTerm): + for v in sorted(list(F1.existentials()), key = Term.sortKey): m[v] = F1.newBlankNode() - for v in sorted(list(F1.universals()), Term.compareAnyTerm): + for v in sorted(list(F1.universals()), key = Term.sortKey): n[v] = F1.newUniversal(v) e = F1.existentials() u = F1.universals() diff --git a/isodate.py b/isodate.py index a009f3fe..cf7965e4 100644 --- a/isodate.py +++ b/isodate.py @@ -100,14 +100,14 @@ def parse(s): def fullString(i): """ given seconds since the epoch, return a full dateTime string in Z timezone. """ - assert type(i) in [IntType, FloatType, LongType], "Wrong type: "+ repr(type(i)) +repr(i) + assert type(i) in [int, float], "Wrong type: "+ repr(type(i)) +repr(i) year, month, day, hour, minute, second, wday, jday, dst = time.gmtime(i) return str(year) + '-%2.2d-%2.2dT%2.2d:%2.2d:%2.2dZ' % (month, day, hour, minute, second) def asString(i): """ given seconds since the epoch, return a dateTime string. """ - assert type(i) in [IntType, FloatType] + assert type(i) in [int, float] year, month, day, hour, minute, second, wday, jday, dst = time.gmtime(i) o = str(year) if (month, day, hour, minute, second) == (1, 1, 0, 0, 0): return o diff --git a/llyn.py b/llyn.py index b13be677..651aa49a 100755 --- a/llyn.py +++ b/llyn.py @@ -136,12 +136,12 @@ class DataObject: x = F.theObject(pred=rdfType obj=fooCar) for y in x[color][label] """ - def __init__(context, term): + def __init__(self, context, term): self.context = context self.term = term def __getItem__(pred): # Use . or [] ? - values = context.objects(pred=pred, subj=self.term) + values = self.context.objects(pred=pred, subj=self.term) for v in value: yield DataObject(self.context, v) @@ -277,7 +277,7 @@ def any(self, subj=None, pred=None, obj=None): if pred is None: return s[PRED] if subj is None: return s[SUBJ] if obj is None: return s[OBJ] - raise ParameterError("You must give one wildcard") + raise ValueError("You must give one wildcard") def the(self, subj=None, pred=None, obj=None): @@ -297,7 +297,7 @@ def the(self, subj=None, pred=None, obj=None): if pred is None: return s[PRED] if subj is None: return s[SUBJ] if obj is None: return s[OBJ] - raise parameterError("You must give one wildcard using the()") + raise ValueError("You must give one wildcard using the()") def each(self, subj=None, pred=None, obj=None): """Return a list of values value filing the blank in the called parameters @@ -313,7 +313,7 @@ def each(self, subj=None, pred=None, obj=None): if pred is None: wc = PRED elif subj is None: wc = SUBJ elif obj is None: wc = OBJ - else: raise ParameterError("You must give one wildcard None for each()") + else: raise ValueError("You must give one wildcard None for each()") res = [] for s in hits: res.append(s[wc]) # should use yeild @@ when we are ready diff --git a/local_decimal.py b/local_decimal.py index 445701f0..1f0e4b29 100644 --- a/local_decimal.py +++ b/local_decimal.py @@ -10,6 +10,11 @@ from math import log10 +def compareNumbers(self, other): # python3 has no cmp + if self < other: return -1 + if self > other: return 1 + return 0 + class Decimal: """make a new Decimal @@ -140,7 +145,7 @@ def __cmp__(self, other): while other.magnitude > self.magnitude: self.magnitude = self.magnitude+1 self.value = self.value * 10 - a = cmp(self.value, other.value) + a = compareNumbers(self.value, other.value) self.normalize() return a def __coerce__(self, other): diff --git a/pretty.py b/pretty.py index e47ab6b9..93bc9c7f 100755 --- a/pretty.py +++ b/pretty.py @@ -85,6 +85,7 @@ def __init__(self, F, sink, flags="", sorting=0): # assert F.canonical is not None, "Formula printed must be canonical" self.store = F.store self.sink = sink + self.base = self.sink.base self.defaultNamespace = None self.flags = flags self.sorting = sorting @@ -95,6 +96,9 @@ def __init__(self, F, sink, flags="", sorting=0): self._occurringAs = [{}, {}, {}, {}] self._topology_returns = {} + # print('@@ serializer base', self.base) + # print('@@ serializer sink.+base', self.sink.base) + def selectDefaultPrefix(self, printFunction): """ Symbol whose fragments have the most occurrences. @@ -243,8 +247,6 @@ def dumpLists(self): list = list.rest - - def dumpChronological(self): "Fast as possible. Only dumps data. No formulae or universals." context = self.context @@ -302,7 +304,9 @@ def dumpVariables(self, context, sink, sorting=1, pretty=0, dataOnly=0): def dumpBySubject(self, sorting=1): """ Dump one formula only by order of subject except - forSome's first for n3=a mode""" + forSome's first for n3=a mode + In the order of subjects, the document itself (<>) comes first. + """ context = self.context uu = context.universals().copy() @@ -329,7 +333,7 @@ def fixSet(x): else: self._outputStatement(sink, [fixSet(x) for x in s.quad]) - if 0: # Doesn't work as ther ei snow no list of bnodes + if 0: # Doesn't work as there is now no list of bnodes rs = list(self.store.resources.values()) if sorting: rs.sort(key=Term.sortKey) for r in rs : # First the bare resource @@ -574,14 +578,21 @@ def dumpFormulaContents(self, context, sink, sorting, equals=0): """ Iterates over statements in formula, bunching them up into a set for each subject. """ - + def keySPO (st): + con, pred, subj, obj = st.quad + if hasattr(subj, 'uri') and subj.uri == self.base: + s = " " # this document comes first in order + else: + s = repr(subj) + return s + repr(pred) + repr(obj) + allStatements = context.statements[:] if equals: for x, y in list(context._redirections.items()): if not x.generated() and x not in context.variables(): allStatements.append(StoredStatement( (context, context.store.sameAs, x, y))) - allStatements.sort(key = StoredStatement.keyForSubjPredObj) + allStatements.sort(key = keySPO) # context.statements.sort() # @@ necessary? self.dumpVariables(context, sink, sorting, pretty=1) diff --git a/query.py b/query.py index 503c6742..a83f095b 100644 --- a/query.py +++ b/query.py @@ -897,7 +897,7 @@ def conclude(self, bindings, evidence = [], extraBNodes = Set(), allBindings=Non for x in poss.copy(): if x in ok: poss.remove(x) poss_sorted = list(poss) - poss_sorted.sort(Term.compareAnyTerm) + poss_sorted.sort(key = Term.sortKey) #progress(poss) # vars = self.conclusion.existentials() + poss # Terms with arbitrary identifiers @@ -908,7 +908,7 @@ def conclude(self, bindings, evidence = [], extraBNodes = Set(), allBindings=Non v2 = self.targetContext.newUniversal() b2[v] =v2 # Regenerate names to avoid clash if diag.chatty_flag > 25: s = s + ",uni %s -> %s" %(v, v2) - for v in sorted(list(self.conclusion.existentials()), Term.compareAnyTerm): + for v in sorted(list(self.conclusion.existentials()), key = Term.sortKey): if v not in exout: v2 = self.targetContext.newBlankNode() b2[v] =v2 # Regenerate names to avoid clash diff --git a/serial.py b/serial.py index 89e8312d..2fd8a767 100755 --- a/serial.py +++ b/serial.py @@ -78,8 +78,17 @@ def __init__(self, f, write=None, base=None, flags=""): self.namespaces = {} self._flags = flags self.defaultNamespace = None + print('@@ serializer base', self.base') + + def keyStoredStatement(statement): + s = statement.quad[SUBJ].value + if s === self.base: s = ' ' # this document comes first in order + print('@@ serializer key ', self.base, s') + key = s + statement.quad[PRED] + return (key) + def scan(self): for st in self.f.statements: x = st.object() diff --git a/term.py b/term.py index 3a9b5640..5c12b24b 100755 --- a/term.py +++ b/term.py @@ -25,6 +25,7 @@ # for yield import string, sys, types +from .compare import compareStrings, compareNumbers from .set_importer import Set, ImmutableSet @@ -209,16 +210,6 @@ def pickEnv(choice, *envs): return env return [] ## Not here. -def compareStrings(self, other): - if self < other: return -1 - if self > other: return 1 - return 0 - -def compareNumbers(self, other): - if self < other: return -1 - if self > other: return 1 - return 0 - # This class is the core one for the system. class Term(object): @@ -277,7 +268,7 @@ def compareAnyTerm(self, other): This is not done yet """ if self is other: return 0 - diff = compareNumber(self.classOrder(), other.classOrder()) + diff = compareNumbers(self.classOrder(), other.classOrder()) if diff != 0: return diff return self.compareTerm(other) @@ -509,7 +500,7 @@ def compareTerm(self, other): if other.uriref() == RDF_type_URI: return 1 if self.resource is other.resource: - return compareString(self.fragid, other.fragid) + return compareStrings(self.fragid, other.fragid) return self.resource.compareTerm(other.resource) def uriref(self): diff --git a/test/retest.py b/test/retest.py index 15ca40a3..4d92b147 100755 --- a/test/retest.py +++ b/test/retest.py @@ -12,7 +12,8 @@ --start=13 -s 13 Skip the first 12 tests --verbose -v Print what you are doing as you go --ignoreErrors -i Print error message but plough on though more tests if errors found - (Summary error still raised when all tests ahve been tried) +--crashes -x Stop if a test crashes cwm + (Summary error still raised when all tests have been tried) --cwm=../cwm.py Cwm command is ../cwm --help -h Print this message and exit @@ -62,7 +63,7 @@ proofs = 0 verbose = 0 no_action = 0 - +stopOnCrash = False # The base URI for this process - the Web equiv of cwd processBaseURI = uripath.base() @@ -74,7 +75,7 @@ def localize(uri): return uripath.refTo("http://www.w3.org/2000/10/swap/test/retest.py", uri) def problem(str): - global ploughOn + global ploughOn, stopOnCrash global problems sys.stderr.write(str + "\n") problems.append(str) @@ -113,7 +114,7 @@ def diff(case, ref=None, prog="diff -Bbwu"): a.write('\n') a.close() diffcmd = """%s %s ,temp/%s >,diffs/%s""" %(prog, ref, case, case) - if verbose: print(" ", diffcmd) + if verbose: print(" exit status: ", diffcmd) if no_action: result = 0 else: result = system(diffcmd) if result < 0: @@ -186,6 +187,7 @@ def main(): cwm_command='../cwm.py' python_command='python3' global ploughOn # even if error + global stopOnCrash ploughOn = 0 global verbose verbose = 0 @@ -196,7 +198,7 @@ def main(): a.write('') a.close() try: - opts, testFiles = getopt.getopt(sys.argv[1:], "h?s:nNcipf:v", + opts, testFiles = getopt.getopt(sys.argv[1:], "h?s:nNcipf:vx", ["help", "start=", "testsFrom=", "no-action", "No-normal", "chatty", "ignoreErrors", "proofs", "verbose","overwrite","cwm="]) except getopt.GetoptError: @@ -212,6 +214,8 @@ def main(): verbose = 1 if o in ("-i", "--ignoreErrors"): ploughOn = 1 + if o in ("x", "crashes"): + stopOnCrash = True if o in ("-s", "--start"): start = int(a) if o in ("-f", "--testsFrom"): diff --git a/toXML.py b/toXML.py index f6313848..f8d3c248 100755 --- a/toXML.py +++ b/toXML.py @@ -129,9 +129,9 @@ def __init__(self, outFp, thisURI=None, base=None, flags=""): # zw = z.write self._xwr = XMLWriter(outFp.write, self) self._subj = None - self._base = base + self.base = base self._formula = None # Where do we get this from? The outermost formula - if base == None: self._base = thisURI + if base == None: self.base = thisURI self._thisDoc = thisURI self._flags = flags self._nodeID = {} @@ -149,7 +149,7 @@ def doNothing(): def dummyClone(self): "retun a version of myself which will only count occurrences" - return ToRDF(None, self._thisDoc, base=self._base, flags=self._flags ) + return ToRDF(None, self._thisDoc, base=self.base, flags=self._flags ) def bind(self, prefix, namespace): if prefix in self.namespace_redirections: @@ -200,9 +200,9 @@ def toDoMore(): def referenceTo(self, uri): "Conditional relative URI" - if "r" in self._flags or self._base == None: + if "r" in self._flags or self.base == None: return uri - return refTo(self._base, uri) + return refTo(self.base, uri) def flushStart(self): if not self._docOpen: @@ -775,9 +775,9 @@ def __init__(self, outFp, thisURI=None, base=None, flags=""): zw = outFp.write self._xwr = XMLWriter(zw, self) self._subj = None - self._base = base + self.base = base self._formula = None # Where do we get this from? The outermost formula - if base == None: self._base = thisURI + if base == None: self.base = thisURI self._thisDoc = thisURI self._flags = flags self._nodeID = {} @@ -790,7 +790,7 @@ def doNothing(): def dummyClone(self): "retun a version of myself which will only count occurrences" - return tmToRDF(None, self._thisDoc, base=self._base, flags=self._flags ) + return tmToRDF(None, self._thisDoc, base=self.base, flags=self._flags ) def start(self): self._parts = [tm.NOTHING] @@ -843,9 +843,9 @@ def end(self): def referenceTo(self, uri): "Conditional relative URI" - if "r" in self._flags or self._base == None: + if "r" in self._flags or self.base == None: return uri - return refTo(self._base, uri) + return refTo(self.base, uri) def addNode(self, node, nameLess = 0): diff --git a/xmlC14n.py b/xmlC14n.py index a81a0df0..2345a03f 100644 --- a/xmlC14n.py +++ b/xmlC14n.py @@ -42,12 +42,20 @@ import string from xml.dom import Node -try: - from xml.ns import XMLNS -except: - class XMLNS: - BASE = "http://www.w3.org/2000/xmlns/" - XML = "http://www.w3.org/XML/1998/namespace" +from .compare import compareStrings, compareNumbers + +def compareNumbers(self, other): + if self < other: return -1 + if self > other: return 1 + return 0 + + +#try: +# from xml.ns import XMLNS +#except: +class XMLNS: + BASE = "http://www.w3.org/2000/xmlns/" + XML = "http://www.w3.org/XML/1998/namespace" #try: # import cStringIO # StringIO = cStringIO @@ -68,9 +76,9 @@ def _sorter(n1,n2): '''_sorter(n1,n2) -> int Sorting predicate for non-NS attributes.''' - i = cmp(n1.namespaceURI, n2.namespaceURI) + i = compareStrings(n1.namespaceURI, n2.namespaceURI) if i: return i - return cmp(n1.localName, n2.localName) + return compareStrings(n1.localName, n2.localName) def keyNamespaceLocalname(n): '''keyNamespaceLocalname(n) -> string @@ -85,7 +93,7 @@ def _sorter_ns(n1,n2): if n1[0] == 'xmlns': return -1 if n2[0] == 'xmlns': return 1 - return cmp(n1[0], n2[0]) + return compareStrings(n1[0], n2[0]) def keyNSURI(n): "(an empty namespace URI is lexicographically least)." From d351b5dadd021b9c9857947b9dc917f47e8958fe Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Mon, 8 Jan 2024 10:58:46 +0000 Subject: [PATCH 15/18] strings must be encoded for hash --- term.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/term.py b/term.py index 5c12b24b..cb2df479 100755 --- a/term.py +++ b/term.py @@ -1421,7 +1421,7 @@ def asHashURI(self): Hmm... for a class of literals including this one, strictly speaking.""" x=hashlib.md5() - x.update(self.string) + x.update(self.string.encode('utf-8')) d=x.digest() b16=binascii.hexlify(d) return "md5:" + b16 From 402e50e385f49d269de194a9b71c641dcbadaee1 Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Mon, 8 Jan 2024 22:10:17 +0000 Subject: [PATCH 16/18] sort keys etc --- llyn.py | 17 ++++++++++------- query.py | 8 +++----- set_importer.py | 2 +- term.py | 28 ++++++++++++---------------- 4 files changed, 26 insertions(+), 29 deletions(-) diff --git a/llyn.py b/llyn.py index 651aa49a..b0838550 100755 --- a/llyn.py +++ b/llyn.py @@ -595,9 +595,9 @@ def canonicalize(F, cannon=False): progress('I got here, possibles = ', possibles) fl.sort(key = StoredStatement.keyForSubjPredObj) fe = F.existentials() - #fe.sort(Term.compareAnyTerm) + #fe.sort(key = Term.sortKey) fu = F.universals () - #fu.sort(Term.compareAnyTerm) + #fu.sort(key = Term.sortKey) for G in possibles: @@ -725,11 +725,11 @@ def outputStrings(self, channel=None, relation=None): pairs = [] for s in list: pairs.append((s[SUBJ], s[OBJ])) - pairs.sort(comparePair) - for key, str in pairs: - if not hasattr(str, "string"): - print(repr(str)) - channel.write(str.string.encode('utf-8')) + pairs.sort(key = keyForPair) + for key, text in pairs: + # if not hasattr(text, "string"): + # print(repr(text)) + channel.write(str(text)) def debugString(self, already=[]): @@ -851,6 +851,9 @@ def comparePair(self, other): if x != 0: return x +def keyForPair(pair): + return str(pair[0]) + '__' + str(pair[1]) + diff --git a/query.py b/query.py index a83f095b..ed13536a 100644 --- a/query.py +++ b/query.py @@ -186,7 +186,7 @@ def run(self): def runBrilliant(self): """Perform task. Return number of new facts. - Start again if new rule mayhave been generated. + Start again if new rule may have been generated. This should be much faster than even runSmart, despite being much simpler""" if self.repeat and self.targetContext is self.workingContext: @@ -200,15 +200,12 @@ def runBrilliant(self): if "q" not in self.mode: self.gatherRules(self.ruleFormula) - scheduler = self.scheduler if not canLoop: self.scheduler = None total = scheduler.run(int.__add__) - self.scheduler = scheduler - return total @@ -390,7 +387,8 @@ def __init__(self, task, antecedent, consequent, statement, variables): self.store = self.template.store self.statement = statement # original statement self.number = nextRule = nextRule+1 - self.meta = self.conclusion.contains(pred=self.conclusion.store.implies) #generate rules? + # self.meta = self.conclusion.contains(pred=self.conclusion.store.implies) #generate rules? + # Suspect ^ not used. # if task.repeat: self.already = [] # No neat to track dups if not # else: self.already = None self.already = [] diff --git a/set_importer.py b/set_importer.py index 3685e62d..1aed41ae 100644 --- a/set_importer.py +++ b/set_importer.py @@ -27,7 +27,7 @@ try: Set = set - ImmutableSet = set + ImmutableSet = frozenset sorted = sorted except NameError: diff --git a/term.py b/term.py index cb2df479..9cea87b2 100755 --- a/term.py +++ b/term.py @@ -201,7 +201,7 @@ def __hash__(self): try: return self._hashval except AttributeError: - self._hashval = hash(ImmutableSet(list(self.items()))) + self._hashval = hash(frozenset(list(self.items()))) return self._hashval def pickEnv(choice, *envs): @@ -665,26 +665,22 @@ class CompoundTerm(Term): Examples: List, Formula""" pass -class N3Set(ImmutableSet, CompoundTerm): #, +class N3Set(frozenset): # CompoundTerm """There can only be one of every N3Set """ res = {} - def __init__(self, stuff=[]): - """something""" - ImmutableSet.__init__(self, stuff) + # def __init__(self, stuff=[]): + # """something""" + # frozenset.__init__(self, stuff) def __new__(cls, stuff=[]): - new_set = ImmutableSet.__new__(cls, stuff) - new_set.__init__(stuff) - if new_set in cls.res: - return cls.res[new_set] - cls.res[new_set] = new_set - return new_set + return super().__new__(cls, stuff) + ## def __setattr__(self, attr, value): ## print "%s=%s" % (`attr`, `value`) -## ImmutableSet.__setattr__(self, attr, value) +## frozenset.__setattr__(self, attr, value) def uriref(self): raise NotImplementedError @@ -1232,7 +1228,7 @@ def unify(self, other, bindings=Env(), otherBindings=Env(), yield (env11, envWithBinding) else: raise ValueError - elif isinstance(self, (Set, ImmutableSet)): + elif isinstance(self, (Set, frozenset)): for x in unifySet(self, other, env1, env2, vars, existentials, n1Source=n1Source, n2Source=n2Source): yield x elif type(self) is tuple: @@ -1420,10 +1416,10 @@ def asHashURI(self): Hmm... encoding... assuming utf8? @@test this. Hmm... for a class of literals including this one, strictly speaking.""" - x=hashlib.md5() + x = hashlib.md5() x.update(self.string.encode('utf-8')) - d=x.digest() - b16=binascii.hexlify(d) + d = x.digest() + b16 = d.hex() return "md5:" + b16 def substitution(self, bindings, why=None, cannon=False): From bf54697aca1251063f94f99eea7ba5f2bfa237f3 Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Wed, 10 Jan 2024 21:35:23 +0000 Subject: [PATCH 17/18] remve another decode. Add assertions to help understand query.py --- notation3.py | 2 +- query.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/notation3.py b/notation3.py index 89ee94fa..5217e916 100755 --- a/notation3.py +++ b/notation3.py @@ -212,7 +212,7 @@ def feed(self, octets): So if there is more data to feed to the parser, it should be straightforward to recover.""" - str = octets.decode('utf-8') + str = octets # .decode('utf-8') i = 0 while i >= 0: j = self.skipSpace(str, i) diff --git a/query.py b/query.py index ed13536a..d17a8bff 100644 --- a/query.py +++ b/query.py @@ -157,6 +157,8 @@ def scheduleAttachRule(task, statement, formula, variables): def addRule(): if not formula.contains(subj=subj, pred=formula.store.implies, obj=obj): return 0 # The triple is no longer there + assert issubclass(type(subj),Formula) , "A rule links two formulae - subj type is " + str(type(subj)) + assert issubclass(type(obj),Formula) , "A rule links two formulae - obj type is " + str(type(obj)) return Rule(task, subj, obj, statement, variables).once() task.schedule(addRule) @@ -164,8 +166,12 @@ def scheduleAttachQuery(task, subj, obj, statement, variables): formula = statement.context() variables = variables | formula.universals() def addRule(): + assert type(formula) is Formula if not formula.contains(subj=statement.subject(), pred=statement.predicate(), obj=statement.object()): return 0 # The triple is no longer there + assert type(subj) is Formula # A rule links two formulae + assert type(obj) is Formula + r = Rule(task, subj, obj, statement, variables).once() if (diag.chatty_flag >30): progress( "Found rule %r for statement %s " % (r, statement)) @@ -1925,7 +1931,8 @@ def run(self, func=None): retVal = func(retVal, next(self)) return retVal -# This should probably be properties and methods of IndexedFormula +# This should probably be properties and methods of IndexedFormula? - No + class RuleInstaller(object): def __init__(self, task, ruleSource, variables): self.task = task From 86b33dcbd3bfe49d3ae4a766b8d46ed7a7993ce7 Mon Sep 17 00:00:00 2001 From: Tim Berners-Lee Date: Wed, 10 Jan 2024 21:46:00 +0000 Subject: [PATCH 18/18] can pass bytes or str --- notation3.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/notation3.py b/notation3.py index 5217e916..749d8f22 100755 --- a/notation3.py +++ b/notation3.py @@ -212,7 +212,10 @@ def feed(self, octets): So if there is more data to feed to the parser, it should be straightforward to recover.""" - str = octets # .decode('utf-8') + if isinstance(octets, bytes): + str = octets.decode('utf-8') + else: + str = str = octets i = 0 while i >= 0: j = self.skipSpace(str, i)