Python Class

Thread Solved

Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

Python Class

 
0
  #1
Jan 22nd, 2009
I am trying to construct a class and call upon some functions within that class, but nothing is returned, somehting should definitely be returned

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,017
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 286
woooee woooee is offline Offline
Veteran Poster

Re: Python Class

 
0
  #2
Jan 22nd, 2009
You return the string "error". It is an empty string unless the following is true.
  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
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,379
Reputation: evstevemd has a spectacular aura about evstevemd has a spectacular aura about evstevemd has a spectacular aura about 
Solved Threads: 127
evstevemd's Avatar
evstevemd evstevemd is offline Offline
Nearly a Posting Virtuoso

Re: Python Class

 
0
  #3
Jan 23rd, 2009
the best debugging technique is putting print statement/function after few lines to see whether it passes a given statement
Atheist: God is man made imagination, he doesn't exist!
Theist: It's okay, can you imagine anything else that doesn't exist?
Junior MD --- Python, C++ and PHP
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

Re: Python Class

 
0
  #4
Jan 23rd, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 58
Reputation: adam291086 is an unknown quantity at this point 
Solved Threads: 0
adam291086 adam291086 is offline Offline
Junior Poster in Training

Re: Python Class

 
0
  #5
Jan 23rd, 2009
ok i was being really dumb. Solved it
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC