Featured post

c# - Usage of Server Side Controls in MVC Frame work -

i using asp.net 4.0 , mvc 2.0 web application. project requiremrnt have use server side control in application not possibl in noraml case. ideally want use adrotator control , datalist control. i saw few samples , references in codepleax mvc controllib howwver found less useful. can tell how utilize theese controls in asp.net application along mvc. note: please provide functionalities related adrotator , datalist controls not equivalent functionalities thanks in advace. mvc pages not use normal .net solution makes use of normal .net components impossible. a normal .net page use event driven solution call different methods service side mvc use actions , view completly different way handle things. also, mvc not use viewstate normal .net controlls require. found article discussing mixing of normal .net , mvc.

Problem with defining variables in python -


i'm trying write xml piece of code

docs = xmlreportgenerator() docs.addmatchrow('fc barcelona','madryt','5:0') docs.save() 

and wrote own method:

from lxml import etree  class xmlreportgenerator:     """"""     root = etree.element('results')     doc = etree.elementtree(root)      #----------------------------------------------------------------------     def __init__(self):          """"""       def addmatchrow(self,teama,teamb, score):         pageelement = etree.subelement(root,'flight',teama, teamb, score)          """"""      def save(self,path = none):         outfile = open('matches.xml', 'w')         doc.write(outfile)  

nameerror: global name 'root' not defined process terminated exit code of 1 done

nameerror: global name 'doc' not defined process terminated exit code of 1 done

am missing something? i'm newbie in python (i have more experience in c#).

python explicit. instance variables must prepended self.. class variables must prepended name of class.

here's fixed version. original subelement call incorrect well:

from lxml import etree  # derive 'object' if python 2.x (it default in python 3.x) class xmlreportgenerator(object):      def __init__(self):         # clearer init instance variables here.         self.root = etree.element('results')         self.doc = etree.elementtree(self.root)      def addmatchrow(self,teama,teamb, score):         # need self.root here         pageelement = etree.subelement(self.root,'flight')         # added data elements (or did want attributes?)         etree.subelement(pageelement,'teama').text = teama         etree.subelement(pageelement,'teamb').text = teamb         etree.subelement(pageelement,'score').text = score      def save(self,path = none):         outfile = open('matches.xml', 'w')         # need self.doc here         self.doc.write(outfile)  # code run if script executed directly, # skipped if script imported script. if __name__ == '__main__':     docs = xmlreportgenerator()     docs.addmatchrow('fc barcelona','madryt','5:0')     docs.save() 

Comments

Popular posts from this blog

c# - Usage of Server Side Controls in MVC Frame work -

cocoa - Nesting arrays into NSDictionary object (Objective-C) -

ios - Very simple iPhone App crashes on UILabel settext -