From Dict to Classes yes or no and how

J

Jerry Rocteur

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
 

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,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top