Hi, I'm doing a sort of symbolic linking app in Windows for my own
enjoyment, and figured I would do it in python for the same reason +
learning the language.
The following functions could obviously do with some refactoring. One
obvious thing would be to make wordsetter and wordsforfolder more
generic, and just pass a few extra parameters. But that seems sort of
stupid.
Any suggestions on refactoring here? other improvements?
def getwordslist(wo rd):
thisword = wordpath + word + ".xml"
if exists(thisword ):
doc = xml.dom.minidom .parse(thisword )
loc = doc.childNodes[0]
for i in range(0, len(loc.childNo des)):
if (loc.childNodes[i].firstChild.dat a == thispath):
break
else:
wordsetter(this word,doc,loc)
else :
doc = xml.dom.minidom .Document()
loc = doc.createEleme ntNS("", "locations" )
doc.appendChild (loc)
wordsetter(this word,doc,loc)
return None
def getfolderwords( word):
if exists(normpath (folderwords)):
doc = xml.dom.minidom .parse(folderwo rds)
loc = doc.childNodes[0]
wordsforfolder( word,doc,loc)
else :
doc = xml.dom.minidom .Document()
loc = doc.createEleme ntNS("", "wordlist")
doc.appendChild (loc)
xml.dom.ext.Pre ttyPrint(doc, open(normpath(f olderwords), "w"))
wordsforfolder( word,doc,loc)
return None
def wordsetter(word ,doc,loc):
thisloc = doc.createEleme ntNS("", "location")
xexpr= "//location[.='" + thispath + "']"
xp = Evaluate(xexpr, doc.documentEle ment)
if len(xp) < 1:
loc.appendChild (thisloc)
text = doc.createTextN ode(thispath)
thisloc.appendC hild(text)
fi = open(word, "w")
fi.write(doc.to xml())
def wordsforfolder( word,doc,loc):
thisloc = doc.createEleme ntNS("", "word")
xexpr= "//word[.='" + word + "']"
xp = Evaluate(xexpr, doc.documentEle ment)
if len(xp) < 1:
loc.appendChild (thisloc)
text = doc.createTextN ode(word)
thisloc.appendC hild(text)
fi = open(folderword s, "w")
fi.write(doc.to xml())
Cheers,
Bryan Rasmussen
enjoyment, and figured I would do it in python for the same reason +
learning the language.
The following functions could obviously do with some refactoring. One
obvious thing would be to make wordsetter and wordsforfolder more
generic, and just pass a few extra parameters. But that seems sort of
stupid.
Any suggestions on refactoring here? other improvements?
def getwordslist(wo rd):
thisword = wordpath + word + ".xml"
if exists(thisword ):
doc = xml.dom.minidom .parse(thisword )
loc = doc.childNodes[0]
for i in range(0, len(loc.childNo des)):
if (loc.childNodes[i].firstChild.dat a == thispath):
break
else:
wordsetter(this word,doc,loc)
else :
doc = xml.dom.minidom .Document()
loc = doc.createEleme ntNS("", "locations" )
doc.appendChild (loc)
wordsetter(this word,doc,loc)
return None
def getfolderwords( word):
if exists(normpath (folderwords)):
doc = xml.dom.minidom .parse(folderwo rds)
loc = doc.childNodes[0]
wordsforfolder( word,doc,loc)
else :
doc = xml.dom.minidom .Document()
loc = doc.createEleme ntNS("", "wordlist")
doc.appendChild (loc)
xml.dom.ext.Pre ttyPrint(doc, open(normpath(f olderwords), "w"))
wordsforfolder( word,doc,loc)
return None
def wordsetter(word ,doc,loc):
thisloc = doc.createEleme ntNS("", "location")
xexpr= "//location[.='" + thispath + "']"
xp = Evaluate(xexpr, doc.documentEle ment)
if len(xp) < 1:
loc.appendChild (thisloc)
text = doc.createTextN ode(thispath)
thisloc.appendC hild(text)
fi = open(word, "w")
fi.write(doc.to xml())
def wordsforfolder( word,doc,loc):
thisloc = doc.createEleme ntNS("", "word")
xexpr= "//word[.='" + word + "']"
xp = Evaluate(xexpr, doc.documentEle ment)
if len(xp) < 1:
loc.appendChild (thisloc)
text = doc.createTextN ode(word)
thisloc.appendC hild(text)
fi = open(folderword s, "w")
fi.write(doc.to xml())
Cheers,
Bryan Rasmussen