From: Jerry Rocteur on
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

From: Andre Alexander Bell on
On 06/22/2010 12:05 PM, Jerry Rocteur wrote:
> 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)

You might be interested in the csv module

http://docs.python.org/library/csv.html

Regards


Andre

From: Jerry Rocteur on
> On 06/22/2010 12:05 PM, Jerry Rocteur wrote:
>> 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)
>
> You might be interested in the csv module
>
> http://docs.python.org/library/csv.html
>
> Regards
>
>
> Andre

My input is NOT CSV, I used this format to try and make the question shorter. Although I could create a CSV file, I'd
like to learn how to code a class to work the way I described in the question.

Jerry

From: Andre Alexander Bell on
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 06/22/2010 01:32 PM, Jerry Rocteur wrote:
> My input is NOT CSV, I used this format to try and make the question shorter. Although I could create a CSV file, I'd
> like to learn how to code a class to work the way I described in the question.

Sorry for misunderstanding. Can you maybe give one example that's not
CSV? Both your demonstrated files actually are CSV like. Can you be more
specific on what actually changes constantly. (Fields given in the
files, or the users in the files, or...)
Can you tell us, why you want to use classes if the dict approach works
great?

Regards


Andre

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkwgo10ACgkQnuHMhboRh6QpCACePUckiiafgAM/h65/THfFQNgZ
RmwAn35of1VvLTNALA/pTme5gKA8g683
=oYjv
-----END PGP SIGNATURE-----
From: James Mills on
On Tue, Jun 22, 2010 at 9:32 PM, Jerry Rocteur <macosx(a)rocteur.cc> wrote:
> My input is NOT CSV, I used this format to try and make the question shorter. Although I could create a CSV file, I'd
> like to learn how to code a class to work the way I described in the question.

Your input certainly looks CSV-ish to me (us).

Even I didn't bother reading your email in full (sorry but it was too long!).

If you're not familiar with using classes and objects in python
perhaps you should read through relevant documentation
and/or tutorials on the subject.

If you were able to ask us perhaps a more specific question
and describe your problem a little more concisely perhaps
I (and we) might have a bit more to offer you.

cheers
James

--
--
-- "Problems are solved by method"