From Dict to Classes yes or no and how

Jerry Rocteur macosx at rocteur.cc
Tue Jun 22 06:05:30 EDT 2010


Hi,

Sorry for the long mail but I've been searching the web for days for how to do this.. I see that possibilities using
shelve or pickle but I don't want to do this (The source of the data changes constantly)

I'm learning Python and very much a beginner with Classes.

I have data like this: (highly simplified)

user is unique!

File1
user;secnum;name
jro;012345;John Rogers
dbt;012346;Debbie Row
dri;012347;Daniel Deridder
etc.

File2
group,user
ace1,jro
ace2,jro
ace1,dri
ace3,dbt
ace3.dbt
ace3.jro
etc.

At the moment I read, split into a dict like this:

Read through File1
users = {}
key   = splits[0]
users[key] = {    'user'        : key,
                  'secnum'      : splits[1],
                  'name'        : splits[2]
	     }

Read through File2
user    = splits[0]
group   = splits[1]
users[user]['groups'].append(group)

This works great ..

But shouldn't I do this using classes instead ?

So I try

class GRPUser(object):
    def __init__(self, user, secnum, name, groups=None):
        self.user          = user
        self.secnum        = secnum
        self.name          = name
        self.groups        = groups

So how do I load the Class, iterate through it and call up individual users ?

I've tried all sorts of things u = GRPUser(user, split[1], split[2]) whilst reading File1, I get no errors but I have
no idea how to read in file2 and worse of all, no idea how to iterate through or call up individual items from the
class, for example:

print users['jro']['name'], users['jro']['secnum'], users['jro']['groups']

or

    for keys in users:
        print users[keys]['name']
        for group in users[keys]['groups']:
            print group

etc.

Thanks in advance,

jerry




More information about the Python-list mailing list