Class attributes / methods..... full Python script

MRAB python at mrabarnett.plus.com
Mon Mar 1 23:31:02 CET 2010


Gabor Urban wrote:
> Hi guys,
> 
> thanks for the ideas. Here you are the code. Not transcoded from Java
> for I do not know Java enough..
> 
> I am scanning an XML file, and have a large ammount of logging.
> 
> Any ideas are wellcome as before....
> 
> Thnx
> 
> Code:>
> 
> #---------------------------------------------
> ## Generate CSV from OEP History XML
> #---------------------------------------------
> 
> import sys
> import os
> from xml.dom.minidom import parse, parseString
> import xml.dom
> import logging
> 
> versionStr = '0.01'
> 
> class Item:
>     def __init__(self, pId, pChange, pComment):
>         self.ID = pId
>         self.Delta = pChange
>         self.Comment = pComment
> 
>     def PrintItem(self):
>         str = '%s,%s,%s'%(self.ID,self.Delta,self.comment)
>         return str
> 
> class Package:
>     def __init__(self, pSerial, pDate, pOwner, pSchema):
>         self.serial = pSerial
>         self.date = pDate
>         self.owner = pOwner
>         self.schema = pSchema
>         self.items = []
> 
>     def insertItem(self, pItem):
>         self.items.append(pItem)
> 
>     def printPackage(self):
>         lines = []
>         str = '%s,%s,%s,%s'%(self.serial,self.date,self.owner,self.schema)
>         number = self.items.length

If this is meant to get the length of self.items, then it won't work. It
should be:

         number = len(self.items)

>         for idx in xrange(number):
>             line = ''

What's the purpose of this line? You're not using 'line' before you
assign to it again.

>             istr = self.items[idx].PrintItem()
>             line = str + ',' + istr
>             lines.append(line)
>         return lines
> 
> def getItem(tracer, pkgItem, itemName):
>     retval = -1
>     name = ""
>     found = 0
>     str = ""
>     tracer.info('Function getItem entry, name = %s',itemName)
>     nItem = pkgItem.getElementsByTagName(itemName).item(0)
>     for node in pkgItem.childNodes:
>         if node.nodeType != xml.dom.Node.TEXT_NODE:
>             tracer.debug('Scanning node name = %s',node.nodeName)
>             if node.nodeName == itemName:
>                 tracer.debug('Pkg %s found',itemName)
>             else:
>                 tracer.warning('Pkg %s is not found',itemName)
>                 continue
>             for entity in node.childNodes:
>                 if entity.nodeType == xml.dom.Node.TEXT_NODE:
>                     retval = entity.nodeValue
>                     tracer.debug("Node value found %s",retval)
>                     found = 1
>                     break
>         if found == 1:
>             break
>     tracer.debug('Function getItem returns %s',retval)
>     return retval
> 
> logger = logging.getLogger('XmlHistory')
> handler = logging.FileHandler("Xmlhistory.trc",'a')
> ## handler = logging.StreamHandler(sys.stderr)
> logger.addHandler(handler)
> formatter = logging.Formatter("%(name)s %(asctime)s %(filename)s
> %(lineno)d %(levelname)s %(message)s")
> handler.setFormatter(formatter)
> logger.setLevel(2)
> 
> fileName = "history.xml"
> output = 'history.csv'
> logger.info('Xml history generator version %s',versionStr)
> logger.info('Starting history generation on file:%s',fileName)
> 
> packages = []
> data = []
> 
> ## Start XML processing
> 
> dataSource = parse(fileName)
> print dataSource.documentElement.tagName
> listCsomag = dataSource.getElementsByTagName("csomag")
> size = listCsomag.length
> logger.debug('Number of packages = %d',size)
> for idx in xrange(size):
>     attrib = ""
>     package = None
>     serial = 0
>     date = ""
>     owner = ""
>     schema = ""
>     flag = False
>     logger.info('Parsing package id=%d',idx)
>     attrib = getItem(logger, listCsomag.item(idx),'sorszam')
>     if attrib <> "NUM" and attrib <> -1:
>         serial = int(attrib)
>         flag = True
>         logger.debug('Package serial set to %d',serial)
>     else:
>         logger.debug("Template package found.")
>         break
>     attrib = ""

What's the purpose of this line? You're not using 'attrib' before you
assign to it again. You're doing this repeatedly in the code.

>     attrib = getItem(logger,listCsomag.item(idx),"datum")
>     if attrib <> -1:
>         date = attrib
>         flag = flag and True

Whether 'flag' is False or True, this line won't change it. You're also
doing this repeatedly in the code.

>         logger.debug('Package date set to %s',date)
>     attrib = ""
>     attrib = getItem(logger,listCsomag.item(idx),"ki")
>     if attrib <> -1:
>         owner = attrib
>         flag = flag and True
>         logger.debug('Package owner set to %s',owner)
>     attrib = ""
>     attrib = getItem(logger,listCsomag.item(idx),"sema")
>     if attrib <> -1:
>         schema = attrib
>         flag = flag and True
>         logger.debug('Package schema set to %s',schema)
> 
>     if flag <> True:

Clearer as:

     if not flag:

or even clearer if you picked a better name than 'flag'.

>         logger.error('Package id=%d is inconsistent',idx)
>         break
>     else:
>         logger.info('Package id=%d is ok',idx)
> 
>     package = Package(serial,date,owner,schema)
> 
>     listItem = listCsomag.item(idx).getElementsByTagName("item")
>     itemSize = listItem.length
>     logger.debug('Number of items = %d',itemSize)
>     for num in xrange(itemSize):
>         flag = False
>         value = -1
>         listId = 0
>         change = ""
>         comment = ""
>         item = None
>         logger.info('Parsing item id = %d',num)
>         value = getItem(logger,listItem.item(num),"item_id")
>         if value <> -1:
>             listId = int(value)
>             flag = True
>             logger.debug('List id set to %d',listId)
>         value = ""
>         value = getItem(logger,listItem.item(num),"valtozas")
>         if value <> -1:
>             change = value
>             flag = flag and True
>             logger.debug('List change desc. set to "%s"',change)
>         value = ""
>         value = getItem(logger,listItem.item(num),"komment")
>         if value <> -1:
>             comment = value
>             flag = flag and True
>             logger.debug('List comment desc. set to "%s"',comment)
> 
>         if flag <> True:
>             logger.error('Item id = %d inconsistent',num)
>             break
>         else:
>             logger.info('Item id = %d is ok',num)
> 
>         item = Item(listId,change,comment)
>         package.insertItem(item)
> 
>     packages.append(Package)
> 
The instance is 'package', the class is 'Package'. You're using the
wrong one.

> logger.info('Processing Xml file %s is done',fileName)
> logger.info('Generating file %s is started',output)
> 
> size = len(packages)
> package = None
> for index in xrange(size):
>     logger.info('PrettyPrinting package id=%d',index)
>     oplines = []
>     oplines = packages[index].printPackage()
>     for i in xrange(oplines.length):

The length of a list, string, etc, is given by 'len', eg. len(oplines).

>         data.append(oplines[i])
> 
> print data
> 




More information about the Python-list mailing list