newbie evaluation

I

Ismael Herrera

Hi , i am new to python , and this is my first wanna-be program, it would
help me a lot if you point me my mistakes or bad design, bad practice or an
easier , faster ,cleaner or better way to program since dont want to stick
with bad practices.

The point of the mini program is to store the debian package database(wich
is in a file) in a list of objects instead of just text , for easy
manipulation.

Later i will transform it to xml then to database schema,to make advances
queries against the package manager,but thats later.(not in this app)

The database file is a serie of entries(packages) that looks like this:

Package: tcpd
Priority: important
Section: base
Installed-Size: 196
Maintainer: Anthony Towns <[email protected]>
Architecture: i386
Source: tcp-wrappers
Version: 7.6.dbs-5
Replaces: libwrap0 (<< 7.6-8)
Depends: libc6 (>= 2.3.2.ds1-4), libwrap0 (>= 7.6-1.1), debconf (>= 0.5) |
debconf-2.0
Conflicts: netbase (<< 3.16-1)
Size: 71974
Description: Wietse Venema's TCP wrapper utilities
Wietse Venema's network logger, also known as TCPD or LOG_TCP.
.
These programs log the client host name of incoming telnet,
ftp, rsh, rlogin, finger etc. requests. Security options are:
access control per host, domain and/or service; detection of
host name spoofing or host address spoofing; booby traps to
implement an early-warning system.

each package is separated by a \n\n.
Dont forget to copy/paste it in your text editor for easy reading

#!/usr/bin/python
import re
#each package in the file
class dpackage(object):

PKG = 'Package'
PRI = 'Priority'
SEC = 'Section'
INS = 'Installed-Size'
MAI = 'Maintainer'
ARQ = 'Architecture'
VER = 'Version'
DEP = 'Depends'
REQ = 'Recommends'
SIZ = 'Size'
DES = 'Description'

def __init__(self):

self.package = None
self.priority = None
self.section = None
self.installedSize = None
self.mainteiner = None
self.arquitecture = None
self.version = None
self.depends = []
self.recoments = []
self.size = None
self.description = None

def __str__(self):
return '''
Package = %s
Priority = %s
Section = %s
Installed Size = %s
Mainteiner = %s
Arquitecture = %s
Version = %s
Depends = %s
Recoments = %s
Size = %s
Description = %s
''' % \
(self.package,self.priority,self.section,self.installedSize,
self.mainteiner,self.arquitecture,self.version,self.depends,
self.recoments,self.size,self.description)


class dpackagedb(object):

def _init__(self):

self.db = []

def load(self):

try :

#open the database file
f = open('/var/lib/dpkg/available','r')

except : raise IOError

#temporary list to store package entries
tmpls = f.read().split('\n\n')
#make package objects for each package
self.db = map(self.makeobject,tmpls)

del self.db[len(self.db)-1]

f.close()

def makeobject(self,item):

p = dpackage()

entry = self._processitem(item)

p.package = entry.get(dpackage.PKG)

p.priority = entry.get(dpackage.PRI)

p.section = entry.get(dpackage.SEC)

p.installedSize = entry.get(dpackage.INS)

p.mainteiner = entry.get(dpackage.MAI)

p.arquitecture = entry.get(dpackage.ARQ)

p.version = entry.get(dpackage.VER)

p.depends = entry.get(dpackage.DEP)

p.recoments = entry.get(dpackage.REQ)

p.size = entry.get(dpackage.SIZ)

p.description = entry.get(dpackage.DES)

return p


def _processitem(self,item):

result = []

iter = re.finditer('.*\n',item)
for x in iter :
xstr = x.group()
if xstr.startswith('Description'):
# get from description to the end(exceptional case)
result.append(item[x.start():])
break
else :
result.append(xstr.strip()) # normal field
# get field : value pairs
result = dict([ item.split(':',1) for item in result])

# exceptional case
recommends = result.get('Recommends')
if recommends:result['Recommends'] = recommends.split(',')

# exceptional case
depends = result.get('Depends')
if depends : result['Depends'] = depends.split(',')

return result




#if __name__ is '__main__':
#test
d = dpackagedb()
d.load()
for x in d.db:
if x:
print x
#print d.db
 

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

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,128
Latest member
ElwoodPhil
Top