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

G

Gabor Urban

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
for idx in xrange(number):
line = ''
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 = ""
attrib = getItem(logger,listCsomag.item(idx),"datum")
if attrib <> -1:
date = attrib
flag = flag and True
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:
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)

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):
data.append(oplines)

print data
 
P

Peter Otten

Gabor said:
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:>
packages.append(Package)

Replace Package (the class) with package (an instance).
for i in xrange(oplines.length):
data.append(oplines)


Python lists don't have a length attribute. You can determine their length
with len(oplines). To iterate over the items of a list you need not know the
list's length:

for line in oplines:
data.append(line)

which can be simplified to

data.extend(oplines)

Peter

PS: Rrread the tutorial
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top