A newbie question about some code
Hi everyone. I am reading a python library code and found something i can not understand. Please help! class Envelope(object): def __init__(self,ta_info): self.ta_info = ta_info def writefilelist(self,ta_list,tofile): for filename in ta_list: fromfile = botslib.botsopendata(filename, 'rb',self.ta_info['charset']) shutil.copyfileobj(fromfile,tofile) fromfile.close() def convertfilelist(self,ta_list): return [botslib.botsgetabspathdata(filename) for filename in ta_list] class myxmlenvelop(Envelope): def __init__(self,ta_info,ta_list): ''' class for (test) xml envelope. There is no standardised XML-envelope! writes a new XML-tree; uses places-holders for XML-files to include; real enveloping is done by ElementTree's include''' self.ta_info = ta_info self._openoutenvelope() #~ self.out.put({'BOTSID':'root','xmlns:xi':" http://www.w3.org/2001/XInclude"}) #works, but attribute is not removed bij ETI.include self.out.put({'BOTSID':'root'}) ta_list = self.convertfilelist(ta_list) for filename in ta_list: self.out.put({'BOTSID':'root'},{'BOTSID':'{ http://www.w3.org/2001/XInclude}include','parse':"xml",'href':filename}) self.out.envelopewrite(self.out.root Please check the blue highlighted part, I don't understand how the object get the property? Jim -- http://mail.python.org/mailman/listinfo/python-list
Re: A newbie question about some code
On Mon, May 18, 2009 at 5:30 PM, Rhodri James wrote: > On Mon, 18 May 2009 10:18:52 +0100, Jim Qiu > wrote: > > Please check the blue highlighted part, I don't understand how the object >> get the property? >> > > Colours and highlighting don't come across in Usenet postings. Could > you be a bit more specific. Which object, and what property? > > -- > Rhodri James *-* Wildebeeste Herder to the Masses > -- > http://mail.python.org/mailman/listinfo/python-list It's the out property. and I realized my mistake after reading Chris Rebert's Message. Thanks. Jim -- http://mail.python.org/mailman/listinfo/python-list
Yet another question about class property.
Hi everyone, Following is the code i am reading, i don't see anywhere the declaration of Message.root object, Where is it from? #bots-modules import bots.botslib as botslib import bots.node as node from bots.botsconfig import * class Message(object): ''' abstract class; represents a edi message. is subclassed as outmessage or inmessage object. ''' def __init__(self): self.recordnumber=0#segment counter. Is not used for UNT of SE record; some editypes want sequential recordnumbering @staticmethod def display(records): '''for debugging lexed records.''' for record in records: t = 0 for veld in record: if t==0: print '%s(Record-id)'%(veld[VALUE]) else: if veld[SFIELD]: print '%s(sub)'%(veld[VALUE]) else: print '%s(veld)'%(veld[VALUE]) t += 1 def get(self,*mpaths): ''' query tree (self.root) with mpath; get value (string); get None if not found.''' if self.root.record is None: raise botslib.MpathRootError('get("%s"): "root" of incoming message is empty; either split messages or use inn.getloop'%(str(mpaths))) return self.root.get(*mpaths) def getnozero(self,*mpaths): ''' like get, returns None is value is zero (0) or not numeric. Is sometimes usefull in mapping.''' if self.root.record is None: raise botslib.MpathRootError('get("%s"): "root" of incoming message is empty; either split messages or use inn.getloop'%(str(mpaths))) return self.root.getnozero(*mpaths) def getindicator(self,ind,*mpaths): ''' like get, returns None is value is zero (0) or not numeric. Is sometimes usefull in mapping.''' if self.root.record is None: raise botslib.MpathRootError('get("%s"): "root" of incoming message is empty; either split messages or use inn.getloop'%(str(mpaths))) return self.root.getindicator(ind,*mpaths) def getcount(self): ''' count number of nodes in self.root. Number of nodes is number of records.''' return self.root.getcount() def getcountoccurrences(self,*mpaths): ''' count number of nodes in self.root. Number of nodes is number of records.''' count = 0 for value in self.getloop(*mpaths): count += 1 return count def getcountsum(self,*mpaths): ''' return the sum for all values found in mpath. Eg total number of ordered quantities.''' if self.root.record is None: raise botslib.MpathRootError('get("%s"): "root" of incoming message is empty; either split messages or use inn.getloop'%(str(mpaths))) return self.root.getcountsum(*mpaths) def getloop(self,*mpaths): ''' query tree with mpath; generates all the nodes. Is typically used as: for record in inn.get(mpath): ''' if self.root.record:#self.root is a real root for terug in self.root.getloop(*mpaths): #search recursive for rest of mpaths yield terug else: #self.root is dummy root for childnode in self.root.children: for terug in childnode.getloop(*mpaths): #search recursive for rest of mpaths yield terug def put(self,*mpaths): if self.root.record is None and self.root.children: raise botslib.MpathRootError('put("%s"): "root" of outgoing message is empty; use out.putloop'%(str(mpaths))) return self.root.put(*mpaths) def putloop(self,*mpaths): if not self.root.record:#no input yet, and start with a putloop(): dummy root if len(mpaths) == 1: self.root.append(node.Node(mpaths[0])) return self.root.children[-1] else: #TODO: what if self.root.record is None and len(mpaths) > 1? raise botslib.MpathRootError('putloop("%s"): mpath too long???'%(str(mpaths))) return self.root.putloop(*mpaths) def sort(self,*mpaths): if self.root.record is None: raise botslib.MpathRootError('get("%s"): "root" of message is empty; either split messages or use inn.getloop'%(str(mpaths))) self.root.sort(*mpaths) def normalisetree(self,node): ''' The node tree is check, sorted, fields are formated etc. Always use this method before writing output. ''' #~ node.display() #~ print 'normalisetree' self._checktree(node,self.defmessage.structure[0]) self._canonicaltree(node,self.defmessage.structure[0]) def _checktree(self,tree,structure): ''' checks tree with table: - all records should be in table at the right place in hierarchy - for each record, all fields should be in grammar This
Re: Yet another question about class property.
Thanks for you patience, Dave. Obviously i have not got used to the python philosophy. Following you guide, i implemented the following code, and you are absolutely right. #!/usr/bin/python #coding:utf-8 class Parent(object): def __init__(self): pass def displayAttrOfSubClass(self): print self.attrFromChild class Child(Parent): def __init__(self): self.attrFromChild = 'Hi, Everyone, Java does this support this trick!' childObj = Child() childObj.displayAttrOfSubClass() Python can use in the parent class a variable only instantiated by a sub-class, is this a point of advance of technique of Python? Jim On Wed, May 20, 2009 at 6:00 PM, Dave Angel wrote: > Jim Qiu wrote: > >> Hi everyone, >> >> Following is the code i am reading, i don't see anywhere the declaration >> of >> Message.root object, >> Where is it from? >> >> #bots-modules >> import bots.botslib as botslib >> import bots.node as node >> from bots.botsconfig import * >> >> >> class Message(object): >>''' abstract class; represents a edi message. >>is subclassed as outmessage or inmessage object. >>''' >>def __init__(self): >>self.recordnumber=0#segment counter. Is not used >> for >> UNT of SE record; some editypes want sequential recordnumbering >> >>@staticmethod >>def display(records): >>'''for debugging lexed records.''' >>for record in records: >>t = 0 >>for veld in record: >>if t==0: >>print '%s(Record-id)'%(veld[VALUE]) >>else: >>if veld[SFIELD]: >>print '%s(sub)'%(veld[VALUE]) >>else: >>print '%s(veld)'%(veld[VALUE]) >>t += 1 >> >>def get(self,*mpaths): >>''' query tree (self.root) with mpath; get value (string); get None >> if not found.''' >>if self.root.record is None: >>raise botslib.MpathRootError('get("%s"): "root" of incoming >> message is empty; either split messages or use inn.getloop'%(str(mpaths))) >>return self.root.get(*mpaths) >> >>def getnozero(self,*mpaths): >>''' like get, returns None is value is zero (0) or not numeric. >>Is sometimes usefull in mapping.''' >>if self.root.record is None: >>raise botslib.MpathRootError('get("%s"): "root" of incoming >> message is empty; either split messages or use inn.getloop'%(str(mpaths))) >>return self.root.getnozero(*mpaths) >> >>def getindicator(self,ind,*mpaths): >>''' like get, returns None is value is zero (0) or not numeric. >>Is sometimes usefull in mapping.''' >>if self.root.record is None: >>raise botslib.MpathRootError('get("%s"): "root" of incoming >> message is empty; either split messages or use inn.getloop'%(str(mpaths))) >>return self.root.getindicator(ind,*mpaths) >> >>def getcount(self): >>''' count number of nodes in self.root. Number of nodes is number >> of >> records.''' >>return self.root.getcount() >> >>def getcountoccurrences(self,*mpaths): >>''' count number of nodes in self.root. Number of nodes is number >> of >> records.''' >>count = 0 >>for value in self.getloop(*mpaths): >>count += 1 >>return count >> >>def getcountsum(self,*mpaths): >>''' return the sum for all values found in mpath. Eg total number >> of >> ordered quantities.''' >>if self.root.record is None: >>raise botslib.MpathRootError('get("%s"): "root" of incoming >> message is empty; either split messages or use inn.getloop'%(str(mpaths))) >>return self.root.getcountsum(*mpaths) >> >>def getloop(self,*mpaths): >>''' query tree with mpath; generates all the nodes. Is typically >> used as: for record in inn.get(mpath): >>''' >>if self.root.record:#self.root
Invitation to connect on LinkedIn
LinkedIn Jim Qiu requested to add you as a connection on LinkedIn: -- Jaime, I'd like to add you to my professional network on LinkedIn. - Jim Accept invitation from Jim Qiu http://www.linkedin.com/e/I2LlXdLlWUhFABKmxVOlgGLlWUhFAfhMPPF/blk/I431163609_3/6lColZJrmZznQNdhjRQnOpBtn9QfmhBt71BoSd1p65Lr6lOfPdvej0ScPoNcjcQiiZNnSFSd55EjiYTcP4NejgPdPwLrCBxbOYWrSlI/EML_comm_afe/ View invitation from Jim Qiu http://www.linkedin.com/e/I2LlXdLlWUhFABKmxVOlgGLlWUhFAfhMPPF/blk/I431163609_3/0PnPAMdzcScj4Pd4ALqnpPbOYWrSlI/svi/ -- Why might connecting with Jim Qiu be a good idea? Jim Qiu's connections could be useful to you: After accepting Jim Qiu's invitation, check Jim Qiu's connections to see who else you may know and who you might want an introduction to. Building these connections can create opportunities in the future. -- (c) 2009, LinkedIn Corporation -- http://mail.python.org/mailman/listinfo/python-list
source install of python2.7 and rpm install of cx_Oracle collision
Hi all, I make installed python 2.7 from source, and also installed the RPM version of cx_Oracle for python 2.7. But ldd tells me : #ldd cx_Oracle.so libpython2.7.so.1.0 => not found I find out that only libpython2.7.a generated when I install python2.7, who can tell me what I need to do ? I want a libpython2.7.so.1.0 generated when I install python. I am not familiar with GCC and .so .a stuff. Best regards, Jim -- http://mail.python.org/mailman/listinfo/python-list
How to output a complex List object to a file.
Hi all, I have a object list list this: from bots.botsconfig import * from D96Arecords import recorddefs from edifactsyntax3 import syntax structure=[ {ID:'UNH',MIN:1,MAX:1,LEVEL:[ {ID:'BGM',MIN:1,MAX:1}, {ID:'DTM',MIN:1,MAX:5}, {ID:'NAD',MIN:1,MAX:5,LEVEL:[ {ID:'CTA',MIN:0,MAX:5,LEVEL:[ {ID:'COM',MIN:0,MAX:5}, ]}, ]}, {ID:'RFF',MIN:0,MAX:5,LEVEL:[ {ID:'DTM',MIN:0,MAX:5}, ]}, {ID:'CUX',MIN:0,MAX:5,LEVEL:[ {ID:'DTM',MIN:0,MAX:5}, ]}, {ID:'LOC',MIN:1,MAX:20,LEVEL:[ {ID:'DTM',MIN:0,MAX:5}, {ID:'LIN',MIN:0,MAX:20,LEVEL:[ {ID:'PIA',MIN:0,MAX:5}, {ID:'IMD',MIN:0,MAX:5}, {ID:'RFF',MIN:0,MAX:5}, {ID:'ALI',MIN:0,MAX:5}, {ID:'MOA',MIN:0,MAX:5}, {ID:'PRI',MIN:0,MAX:5}, {ID:'QTY',MIN:0,MAX:999,LEVEL:[ {ID:'NAD',MIN:0,MAX:1}, ]}, ]}, ]}, {ID:'UNT',MIN:1,MAX:1}, ] } ] I need to output this "structure" object into a file, how to do that ? Jim -- http://mail.python.org/mailman/listinfo/python-list