Would XML be a better approach? I am not very familiar with XML, but
have found scripts on the Internet which will load and unload an XML
file to and from a TreeCtrl.
I like to use XML to load and write data from Tree(List)Ctrls, because you can easily iterate through the data and load it. I use XML for my own convenience in applications, but your users may demand a different standard.
Am I likely to run up against size limits? It would be a shame to
spend a lot of time making this work on a small scale and find that it
would not work on a large scale either because of fixed size limits or
unacceptable time delays in reading and writing data or saving changes
to the structure.
I have never tested the difference in overhead between accessing large MySQL databases and large XML files, but I can safely say that Python + ElementTree work for files that are around 9000 lines (I have never needed to use anything larger).
If you are worried about speed, I hear that PyRXP backs up its claim of being the “fastest validating XML parser available for Python.” I have never used it, so I can’t advise you on that.
Here are two methods that I use in Notalon (which is available on SourceForge if you want to see the whole code) to write and read XML into my TreeCtrl. I only have a root node and children nodes of the root node, but you can extend the idea with recursion.
def WriteState(self, filename):
“”"
Iterate through the nodes, putting the XML’ed text into a specified file.
“”"
current = self.tree.GetRootItem()
root = Element("tree")
root.set("title", self.tree.GetItemText(self.tree.GetRootItem()))
roottext = unicode(self.tree.GetPyData
(self.tree.GetRootItem()))
if roottext == ‘None’:
roottext = “” # We don’t want it to literally write ‘None’
treedata = SubElement(root, “treedata”)
treedata.text
= roottext
# Loop through children now
while current.IsOk():
new = self.tree.GetNextVisible(current)
current = new
if not current.IsOk(): break
# Start translating the data
new = SubElement(root, "node")
new.set("title", unicode(self.tree.GetItemText(current)).encode('utf8'))
data = SubElement(new, "data")
datatext = unicode(self.tree.GetPyData(current)).encode('utf8')
if datatext == 'None':
datatext = "" # Again, we don't want it to literally write 'None'
data.text = datatext
tree = ElementTree(root)
tree.write(unicode(filename))
And the reading one…
def OpenFile(self, filename):
"""
Opens a command-line argument file by parsing XML and changes components accordingly.
"""
# Clear everything before starting
self.tree.DeleteAllItems
()
self.filename = filename
opened = ElementTree(file = filename)
iter = opened.getiterator()
for element in iter:
if element.tag == 'tree':
if element.keys():
for name, value in element.items():
# If the root's not there, we add a new one.
if not self.tree.GetRootItem().IsOk():
self.tree.AddRoot(value)
# If the root's there, we change the title.
else:
self.tree.SetItemText(self.tree.GetRootItem(), value)
if element.getchildren(): # If root has data
for child in element:
if child.tag == 'treedata':
self.tree.SetPyData(self.tree.GetRootItem(), child.text
)
if element.tag == 'node':
if element.keys(): # If the node has attributes
for name, value in element.items():
newnode = self.tree.AppendItem(self.tree.GetRootItem(), value)
if element.getchildren():
for child in element:
# The children should just be one data child
self.tree.SetPyData(newnode, child.text)
self.hist.AddFileToHistory(filename)
if not self.fileclearrecent.IsEnabled():
self.fileclearrecent.Enable(True)
self.tree.Expand
(self.tree.GetRootItem())
self.control.SetValue(unicode(self.tree.GetPyData(self.tree.GetRootItem())))
self.SetModified(False)
This is surprising because I
am still trying to learn both Python and wxPython.
All Python/wxPython programmers learn something new about their programming language and GUI library of choice every day. I learned programming 3 months ago so I could write Notalon, and I am very pleased with the way that Python lets you learn while you are productive.