Python and XML Help

2009-04-11 Thread ookrin
I'm in the process of learning python and PyQt4. I had decided to make
myself a simple app and soon discovered that I needed to crash into
xml to use some of the data I was going to be getting off of the
server.

I picked up enough xml to use the sax parser to get the data out of
the xml. I can get as far as printing it to the screen, but there is
where I get stuck I can't seem to send the data to anywhere else,
into another variable, to another function. The parser stops after the
first line it finds.

class offlineLoad():
def loadXmlFile(self):
print "Loading from File"
xf = open('CharacterID.xml','r')
xml = xmlHandler()
saxparser = make_parser()
print "parser created"
saxparser.setContentHandler(xml)
print "parser runn"
try:
saxparser.parse(xf)
except:
print "Error Reading xml"

class xmlHandler(ContentHandler):
def startElement(self, name, attrs):
self.charList = []
charName = []
charID = []
if name == "row":
charName = attrs.get("name", "")
charID = attrs.get("characterID", "")
print charName, '\t', charID
self.buildlist(self,charName,charID)
#self.test()

def test(self):
print "TeST"
def buildlist(self,charName,charID):
print charName, '\t',charID

so here... test will print, but buildlist makes the parser stop and
print "Error reading XML" after the first row it finds.  Appending
charName and charID to another variable makes it stop as well. I'm
confused at this point
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python and XML Help

2009-04-14 Thread ookrin
On Apr 12, 12:51 am, "Diez B. Roggisch"  wrote:
> ookrin schrieb:
>
>
>
> > I'm in the process of learning python and PyQt4. I had decided to make
> > myself a simple app and soon discovered that I needed to crash into
> > xml to use some of the data I was going to be getting off of the
> > server.
>
> > I picked up enough xml to use the sax parser to get the data out of
> > the xml. I can get as far as printing it to the screen, but there is
> > where I get stuck I can't seem to send the data to anywhere else,
> > into another variable, to another function. The parser stops after the
> > first line it finds.
>
> > class offlineLoad():
> >     def loadXmlFile(self):
> >         print "Loading from File"
> >         xf = open('CharacterID.xml','r')
> >         xml = xmlHandler()
> >         saxparser = make_parser()
> >         print "parser created"
> >         saxparser.setContentHandler(xml)
> >         print "parser runn"
> >         try:
> >             saxparser.parse(xf)
> >         except:
> >             print "Error Reading xml"
>
> This is a very bad thing to do - there are only very few justified cases
> of catch-all for exceptions. This certainly isn't one, as it suppresses
> the real cause for what is happening.
>
> Which makes it harder for you & for us to diagnose the problem, because
> you don't show (and currently can't) the stacktrace to us.
>
> > class xmlHandler(ContentHandler):
> >     def startElement(self, name, attrs):
> >         self.charList = []
>
> If this shall gather some state over the coures of time, this is the
> wrong place to initialize it, as it will get overwritten with each element.
>
> >         charName = []
> >         charID = []
>
> There is no need to "prefill" variables. And it's confusing you do it
> with a different type than what you assign to them below.
>
>
>
> >         if name == "row":
> >             charName = attrs.get("name", "")
> >             charID = attrs.get("characterID", "")
> >             print charName, '\t', charID
> >             self.buildlist(self,charName,charID)
> >             #self.test()
>
> >     def test(self):
> >         print "TeST"
> >     def buildlist(self,charName,charID):
> >         print charName, '\t',charID
>
> > so here... test will print, but buildlist makes the parser stop and
> > print "Error reading XML" after the first row it finds.  Appending
> > charName and charID to another variable makes it stop as well. I'm
> > confused at this point
>
> Please show us the stacktrace you suppress. Then help might be possible.
>
> Diez

Sorry for the delay, easter weekend:

I know you don't need to prefill the variables with python, and they
weren't there originally, I was just getting frustrated and trying
various things to see if they worked. There is actually no stacktrace
error. It will just print me the error message that I have when I try
to send the variables outside of the if loop. (from the try - except
statement.) I can still run click the button and it'll do it again.

There are three rows that I have to get out of my xml all the same
except with different values:
< row name="CharName" charID="IDNumber" >

So it prints out the first of the three lines and then just says
"Error Reading XML" if I'm running the buildlist() and trying to send
the variables out. As long as I don't try to do anything, it works
fine.

I can get these out and print them to the cmd line. But I want to
collect these in a variable and send it back off to my gui in pyqt4 to
list them. which is where i'm getting stuck.

--
http://mail.python.org/mailman/listinfo/python-list


Re: Python and XML Help

2009-04-14 Thread ookrin
On Apr 14, 8:15 pm, John Machin  wrote:
> On Apr 15, 12:29 pm, ookrin  wrote:
>
>
>
> > On Apr 12, 12:51 am, "Diez B. Roggisch"  wrote:
>
> > > ookrin schrieb:
>
> > > > I'm in the process of learning python and PyQt4. I had decided to make
> > > > myself a simple app and soon discovered that I needed to crash into
> > > > xml to use some of the data I was going to be getting off of the
> > > > server.
>
> > > > I picked up enough xml to use the sax parser to get the data out of
> > > > the xml. I can get as far as printing it to the screen, but there is
> > > > where I get stuck I can't seem to send the data to anywhere else,
> > > > into another variable, to another function. The parser stops after the
> > > > first line it finds.
>
> > > > class offlineLoad():
> > > >     def loadXmlFile(self):
> > > >         print "Loading from File"
> > > >         xf = open('CharacterID.xml','r')
> > > >         xml = xmlHandler()
> > > >         saxparser = make_parser()
> > > >         print "parser created"
> > > >         saxparser.setContentHandler(xml)
> > > >         print "parser runn"
> > > >         try:
> > > >             saxparser.parse(xf)
> > > >         except:
> > > >             print "Error Reading xml"
>
> > > This is a very bad thing to do - there are only very few justified cases
> > > of catch-all for exceptions. This certainly isn't one, as it suppresses
> > > the real cause for what is happening.
>
> > > Which makes it harder for you & for us to diagnose the problem, because
> > > you don't show (and currently can't) the stacktrace to us.
>
> > > Please show us the stacktrace you suppress. Then help might be possible.
>
> > There is actually no stacktrace
> > error. It will just print me the error message that I have when I try
> > to send the variables outside of the if loop. (from the try - except
> > statement.)
>
> That's because (as Diez has already pointed out, and told you to stop
> doing) you are *suppressing* the error message and stack trace that
> will help diagnose what is going wrong in your callback method.
>
> Instead of this:
>         try:
>             saxparser.parse(xf)
>         except:
>             print "Error Reading xml"
> do just this:
>         saxparser.parse(xf)
> or if you really want to print something at the time, do this:
>         try:
>             saxparser.parse(xf)
>         except:
>             print "something meaningful"
>             raise # throw the exception back for normal handling
>
> And just in case you're not taking the advice of Scott D. D. either,
> let me tell you again: use ElementTree, it's easier (no callbacks to
> complicate things) and the ratio of helpful-user-count to problem-
> likelihood is likely to be much higher than with sax.
>
> Cheers,
> John

Ok, I didn't know that was what I was actively doing by using the try-
except. I am still learning. And it's not that I won't take the advice
for using ElementTree, I just currently don't know anything about it.
I just didn't want to say, "I have no idea what you're talking about!"
to Scott cause I figured that would be rude, but I guess so is not
saying anything, sorry. So I'll need to read up on it before I
actually try to do anything with that approach.


Seeing the errors - I changed the two classes to this:

class offlineLoad():
def loadXmlFile(self):
print "Loading from File"
xf = open('CharacterID.xml','r')
xml = xmlHandler()
saxparser = make_parser()
saxparser.setContentHandler(xml)

saxparser.parse(xf)

class xmlHandler(ContentHandler):
def __init__(self):
print "---"
self.idList = []

def startElement(self, name, attrs):
if name == "row":
charName = attrs.get("name", "")
charID = attrs.get("characterID", "")
self.buildlist(charName,charID)

def buildlist(self,charName,charID):
temp = charName,charID
self.idList.append(temp)
print ""
print self.idList

I know calling the self.idList = [] in the init probably isn't
correct, but I couldn't think of any other way currently so it doesn't
get rebuilt every time the definition gets called. This works and at
least I think it puts everything into an array for me. I'm going to
look at the element tree now, but I probably won't figure it out
tonight.

I'm using python 2.6

Thanks so far,
Andrew
--
http://mail.python.org/mailman/listinfo/python-list


Re: Python and XML Help

2009-04-17 Thread ookrin
On Apr 15, 9:43 am, Scott David Daniels  wrote:
> ookrin wrote:
> >  I am still learning. And it's not that I won't take the advice
> > for using ElementTree, I just currently don't know anything about it.
> > I just didn't want to say, "I have no idea what you're talking about!"
> > to Scott cause I figured that would be rude, but I guess so is not
> > saying anything, sorry. So I'll need to read up on it before I
> > actually try to do anything with that approach.
>
> > Seeing the errors - I changed the two classes to this:
> >   ... [some code that actually says a _little_ bit about his target] 
>
> If you had done a better job of asking your question, you'd have found a
> quick answer.  That is why everyone points people at smartquestions, not
> to pick on them.
>
> Presuming you have an xml structure like:
>      txt = ''' something 
>              Some contents. 
>             Other contents.
>             '''
>      import xml.etree.ElementTree as ET
>      structure = ET.fromstring(xmls)
>      for node in structure.findall('def/name'):
>          print '%s: %s / %s: %s' % (node.tag, node.attrib['name'],
>                                node.attrib['characterID'], node.text)
>
> or a file named 'my.xml' with the same contents:
>      import xml.etree.ElementTree as ET
>      structure = ET.parse('my.xml')
>      for node in structure.findall('def/name'):
>          print '%s: %s / %s: %s' % (node.tag, node.attrib['name'],
>                                node.attrib['characterID'], node.text)
>
> --Scott David Daniels
> scott.dani...@acm.org

Thanks very much for your patience with me and thanks for the help
everyone.
This is my end result:

self.api = apiConnection(self.userID,self.apiKey)
xmlData = self.api.returnXml()
if(xmlData != "None"):
struct = ET.fromstring(xmlData)
self.charIDs =[]
i=0
for node in struct.findall('result/rowset/row'):
char = node.attrib['name']
id = node.attrib['characterID']
temp = char, id
#append them to the variable for later
self.charIDs.append(temp)
i+=1
#Setup the select window and run it
#selects which character I want
self.selectCharUi = selectChar(self,self.charIDs)
self.selectCharUi.selectWindow.exec_()

#Get the name that was selected
self.selectedCharName = self.selectCharUi.returnValue
()
#Set the text widget with the name
self.lE_charID.setText(self.selectedCharName)
#enable the Ok button
self.pbOk.setEnabled(True)

It collects the names and IDs, sends them to a pyqt4 window with a
list widget to list the names, a name gets selected and then I get the
name that was selected. Though now that I think about it, maybe using
a dictionary would be better. Elsewhere I gather up the userID, apiID,
characterName and characterID and write it t a file. I hope it doesn't
look too horrible.

John - my programming experience was two semesters of c++ about 6
years ago. Everything else I've learned from the internet.  =/

I'll try to make my questions better next time.

Andrew
--
http://mail.python.org/mailman/listinfo/python-list


pyqt4 qTableWidget add items help

2009-04-18 Thread ookrin
I've been searching around the internet for an example of how to add a
list of items to the qTableWidget for the last few hours with little
success.

I have a list orders [[34,940,30,50,67], [50,56,35,30,57]] as my
example here

I built the qTableWidget in designer, so it already has the header
columns filled out.

Date | time | Number | Price | Buyer

ui.tb1_tblOrders.setRowCount(len(orders))

gives me the correct number of rows I want, but how do I fill the
rows?

I've been trying

while(len(orders)> i):
ui.tb1_tblOrders.setCurrentCell(i,0,orders[i][1])
i+=1

which to me, says go add in the first column row with the first order,
and it makes sense to me

 It just says "Error: argument 3 of QTableWidget.setCurrenCell() has
invalid type, I know it's the orders, but I can't figure out what the
proper way of giving it what it wants is.
--
http://mail.python.org/mailman/listinfo/python-list


Re: pyqt4 qTableWidget add items help

2009-04-18 Thread ookrin
On Apr 18, 3:46 am, Sebastian Wiesner  wrote:
> -BEGIN PGP SIGNED MESSAGE-
> Hash: SHA1
>
> 
>
> [...]> I've been trying
>
> >         while(len(orders)> i):
> >             ui.tb1_tblOrders.setCurrentCell(i,0,orders[i][1])
> >             i+=1
>
> > which to me, says go add in the first column row with the first order,
> > and it makes sense to me
>
> Read the documentation [1] to learn, what ".setCurrentCell()" actually does
> and what its arguments are!  And please stop this wild guessing ...
>
> The method you're searching for is ".setItem()" [2], which adds a new
> QTableWidgetItem [3] to a QTableWidget.
>
> [1]http://doc.trolltech.com/4.5/qtablewidget.html#setCurrentCell
> [2]http://doc.trolltech.com/4.5/qtablewidget.html#setItem
> [3]http://doc.trolltech.com/4.5/qtablewidgetitem.html
>
> - --
> Freedom is always the freedom of dissenters.
>                                       (Rosa Luxemburg)
> -BEGIN PGP SIGNATURE-
> Version: GnuPG v2.0.11 (GNU/Linux)
>
> iEYEARECAAYFAknpr4sACgkQGV4vxEMMOxdnawCfTXO55EffBJMQ7h91RGtMIpZ/
> hcYAoLQ9yF5u/hBgNRvqxGRlIy5lPDgb
> =Q6ef
> -END PGP SIGNATURE-

I forgot to include what I tried first.
setCurrentItem was just the last thing I was on at the time.

First I filled out a row in Designer to see how it loads the items. It
loads like this:
self.tb1_tblOrders.item(0, 0).setText(QtGui.QApplication.translate
("MainWin", "Date", None, QtGui.QApplication.UnicodeUTF8))

Ok following that example:

ui.tb1_tblOrders.item(0,0).setText(QtGui.QApplication.translate
("MainWindow",order[0][1], None,QtGui.QApplication.UnicodeUTF8))

AttributeError: 'NoneType' object has no attribute 'setText'

So reading through the QTableWidget Doc I see the setItem

setItem(int,int, QTableWidgetItem * item)
QTableWidgetItem*item ???

The only example on the entire page:

 QTableWidgetItem *newItem = new QTableWidgetItem(tr("%1").arg(
 pow(row, column+1)));

is that setup as c++ ? Ok, it's a class.

twi = QTableWidgetItem()
 newItem = twi(order[i][0])
ui.tb1_tb1Orders.setItem(i,0,newItem)

NameError: global name 'QTableWidgetItem' is not defined

At the top of the page
include 
from PyQt4 import  ?? QtGui? QtCore? Those are already loaded   other
options are pyqtconfig and uic and those don't sound correct...

And that's what I did first. Then I started guessing in the next
logical order, not wildly. editItem, setCurrentItem, setCurrentCell.
Then I came here. So I guess I actually stuck at the QTableWidgetItem
thing, I should have made that a little more clearer first post.

Andrew
--
http://mail.python.org/mailman/listinfo/python-list