views:

48

answers:

1

I am trying to write an application using Python 2.7 that will allow the user to open a dialog box, pick a file, have it read into a structure of some sort (ArrayList, List etc...) open to suggestions here and then to find basic statistical measures from the data (things like mean, standard deviation etc...) and to output the original file with a summary of the statistical measurements in XML format. Just wondering how is the best way to accomplish this. I have code for opening a window to allow a user to pick a file (from another website) but not sure how to use it to pass the selected file into the function that will read the xml file selected.

Here is the code for the window:

from Tkinter import *
from tkMessageBox import *
from tkColorChooser import askcolor              
from tkFileDialog   import askopenfilename      

def callback():
    askopenfilename() 
    Button(text='Please Select File', command=callback).pack(fill=X)
    mainloop()
    pass
def quit(event):
    if tkMessageBox.askokcancel('Quit','Do you really want to quit?'):
    root.destroy()

I think there is more to the pass function.

I have a version of the XMLReader in Java (That is run in BlueJ so don't know if it will run out side of it) code:

import java.util.*;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLReader
   {
       public static void main(String argv[]) {
       ArrayList XTimeStamp = new ArrayList();
       ArrayList XY = new ArrayList();
       ArrayList Price = new ArrayList();

       try {
           File file = new File("<PATH>\shares.xml");
           DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
           DocumentBuilder db = dbf.newDocumentBuilder();
           Document doc = db.parse(file);
           doc.getDocumentElement().normalize();
           System.out.println("Root element " + doc.getDocumentElement().getNodeName());
           NodeList nodeLst = doc.getElementsByTagName("shareprice");
           System.out.println("Share Price");

   for (int s = 0; s < nodeLst.getLength(); s++) {

       Node fstNode = nodeLst.item(s);
       if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
       Element fstElmnt = (Element) fstNode;
       NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("timeStamp");
       Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
       NodeList fstNm = fstNmElmnt.getChildNodes();
       String timeStamp = fstNm.item(0).getNodeValue(); 
       XTimeStamp.add(timeStamp); 
       System.out.println("timeStamp : "  + ((Node) fstNm.item(0)).getNodeValue());
       NodeList lstNmElmntLst = fstElmnt.getElementsByTagName("Price");
       Element lstNmElmnt = (Element) lstNmElmntLst.item(0);
       NodeList lstNm = lstNmElmnt.getChildNodes();
       String YValue = lstNm.item(0).getNodeValue(); 
       Price.add(YValue);
       System.out.println("Price : " + ((Node) lstNm.item(0)).getNodeValue());
               }
             }
            } catch (Exception e) {
          e.printStackTrace();
      }
  System.out.println(XTimeStamp);
  System.out.println(Price);
  XY.add (XTimeStamp);
  XY.add (Price);
 System.out.println(XY);
  }
}

The one thing I don't like about the Java code is that I have to include the path of the file. I would like to allow the user to pick the file in the Java version as well. The reason I started with java is I have a little bit more experience (but not much). To create the application what is the best way forward, Python or Java and depending on which one any help on getting started sorting the issues would be greatly appreciated.

+1  A: 

Well, you could do this in either Python or Java without too much difficulty, but I would make up your mind as to which one you want! I'll talk about Python because I much prefer it to Java, but I'm sure you can find people who will help you with Java.

So first, you want a file selection box with Tkinter. Well, that's pretty standard and there's lots of documentation and code boilerplate on Google. Try:

import Tkinter,tkFileDialog

root = Tkinter.Tk()
file = tkFileDialog.askopenfile(parent=root,mode='rb',title='Choose a file')

for starters; you can make it much more complicated if you'd like. Then to parse the file you could use Python's xml.parsers.expat module:

import xml.parsers.expat
xmlparser.ParseFile( file )

You'll have to write functions for what to do when the parser meets each type of node, but that's in the documentation. Then, you want to do some statistics on what you parse; scipy has all the functions you'll ever need to do so. This is the bit where you'll need to roll your own code the most.

Finally, you want to write the statistics you've generated to a new file (I think?).

statsfile = open( "stats.txt", w )
try:
    statsfile.write( stats )
finally:    
    statsfile.close()

Does that help?

katrielalex
I would prefer Python from the little I know it does seem easier than Java in the sense you don't have top worry to much about declaring variable types and the syntax is easier to follow. But like most programs this is not for exactly for my own use (I will point out that I am getting no money for it, its for a friend who wants to add it to a website he is running for a club). As for the output file, if I could somehow append the statistical information into the original xml file would be the ideal solution. As the output of this part is to become the input in what he is working on.
Rebel
Are you sure you want to add the stats data into the old file? I think it would be nicer to make a new file, possibly in XML format, and give your friend that to analyse. Also, are you sure you want to use XML to communicate with your friend? It's a bit, well, clunky for this purpose, especially if you don't have that much data to send over in the first place. You could just write a plain text file, which might be easier to handle.If you're sure you want to use XML, have a look at http://www.postneo.com/projects/pyxml/ for an example of how to use `xml.dom.minidom`.
katrielalex