refactoring question

  • Thread starter bryan rasmussen
  • Start date
B

bryan rasmussen

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(word):
thisword = wordpath + word + ".xml"
if exists(thisword):
doc = xml.dom.minidom.parse(thisword)
loc = doc.childNodes[0]
for i in range(0, len(loc.childNodes)):
if (loc.childNodes.firstChild.data == thispath):
break
else:
wordsetter(thisword,doc,loc)


else :
doc = xml.dom.minidom.Document()
loc = doc.createElementNS("", "locations")
doc.appendChild(loc)
wordsetter(thisword,doc,loc)

return None

def getfolderwords(word):

if exists(normpath(folderwords)):
doc = xml.dom.minidom.parse(folderwords)
loc = doc.childNodes[0]
wordsforfolder(word,doc,loc)


else :

doc = xml.dom.minidom.Document()
loc = doc.createElementNS("", "wordlist")
doc.appendChild(loc)
xml.dom.ext.PrettyPrint(doc, open(normpath(folderwords), "w"))
wordsforfolder(word,doc,loc)

return None


def wordsetter(word,doc,loc):
thisloc = doc.createElementNS("", "location")
xexpr= "//location[.='" + thispath + "']"

xp = Evaluate(xexpr,doc.documentElement)
if len(xp) < 1:

loc.appendChild(thisloc)
text = doc.createTextNode(thispath)
thisloc.appendChild(text)
fi = open(word, "w")
fi.write(doc.toxml())


def wordsforfolder(word,doc,loc):
thisloc = doc.createElementNS("", "word")
xexpr= "//word[.='" + word + "']"

xp = Evaluate(xexpr,doc.documentElement)
if len(xp) < 1:

loc.appendChild(thisloc)
text = doc.createTextNode(word)
thisloc.appendChild(text)
fi = open(folderwords, "w")
fi.write(doc.toxml())


Cheers,

Bryan Rasmussen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top