943,696 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 575
  • Python RSS
Jan 22nd, 2009
0

Python Class

Expand Post »
I am trying to construct a class and call upon some functions within that class, but nothing is returned, somehting should definitely be returned

Python Syntax (Toggle Plain Text)
  1. #!/usr/bin/python
  2. import cElementTree as ET
  3. import urllib2
  4. import MySQLdb
  5. print "Content-type: text/html\n"
  6. class xml:
  7. @staticmethod
  8.  
  9. def find_text(element):
  10. if element.text:
  11. yield element
  12. for subelement in element:
  13. for txt in find_text(subelement):
  14. yield txt
  15.  
  16. def read_xml():
  17. # connect
  18. db = MySQLdb.connect(host = "localhost", user = "adamplo1", passwd = "U7RJM5HQ", db = "adamplo1_UniProject")
  19.  
  20. # create a database cursor
  21. cursor = db.cursor(MySQLdb.cursors.DictCursor)
  22.  
  23. # execute SQL select statement
  24. cursor.execute("SELECT * FROM Threshold")
  25.  
  26. Results = cursor.fetchall()
  27. error = " "
  28. feed = urllib2.urlopen("http://server-up.theatticnetwork.net/demo/")
  29. try:
  30. tree = ET.parse(feed)
  31.  
  32. except Exception, inst:
  33. print "Unexpected error opening %s: %s" % (tree, inst)
  34.  
  35. root= tree.getroot()
  36. text = root.getchildren()
  37.  
  38. for item in text:
  39. if item.tag =="Memory":
  40. extra = "Memory"
  41. elif item.tag == "Network":
  42. extra = "Network"
  43. elif item.tag == "Vitals":
  44. extra = "Vitals"
  45. elif item.tag == "Hardware":
  46. extra = "Hardware"
  47. elif item.tag == "Swap":
  48. extra = "Swap"
  49. elif item.tag == "Swapdevices":
  50. extra = "Swapdevices"
  51. elif item.tag == "FileSystem":
  52. extra = "FileSystem"
  53. else:
  54. extra = "Other"
  55. for element in find_text(item):
  56. if extra+element.tag.replace(' ','') in Results[0].keys():
  57. if element.text >= Results[0][extra+element.tag.replace(' ','')]:
  58. error = error + "Error with "+item.tag+" "+element.tag+" value = "+element.text +" \n"
  59.  
  60. else:
  61. continue
  62. return error
  63.  
  64.  
  65. a = xml()
  66. problem = a.read_xml()
  67. print problem
Last edited by adam291086; Jan 22nd, 2009 at 12:57 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
adam291086 is offline Offline
61 posts
since Nov 2008
Jan 22nd, 2009
0

Re: Python Class

You return the string "error". It is an empty string unless the following is true.
Python Syntax (Toggle Plain Text)
  1. for element in find_text(item):
  2. if extra+element.tag.replace(' ','') in Results[0].keys():
  3. if element.text >= Results[0][extra+element.tag.replace(' ','')]:
  4. error = error + "Error with "+item.tag+" "+element.tag+" value = "+element.text +" \n"
  5.  
  6. else:
  7. continue
Reputation Points: 741
Solved Threads: 691
Nearly a Posting Maven
woooee is online now Online
2,305 posts
since Dec 2006
Jan 23rd, 2009
0

Re: Python Class

the best debugging technique is putting print statement/function after few lines to see whether it passes a given statement
Reputation Points: 462
Solved Threads: 392
Senior Poster
evstevemd is offline Offline
3,681 posts
since Jun 2007
Jan 23rd, 2009
0

Re: Python Class

ok i have partially solved the problem and added static method bit above each function. But i am not getting the error message find_text is not defined.

How can i have class with two function in it. With on function calling on the other function within the class?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
adam291086 is offline Offline
61 posts
since Nov 2008
Jan 23rd, 2009
0

Re: Python Class

ok i was being really dumb. Solved it
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
adam291086 is offline Offline
61 posts
since Nov 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Web Spider?
Next Thread in Python Forum Timeline: tkinter





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC