Second attempt WAS: From Dict to Classes yes or no and how

Andre Alexander Bell post at andre-bell.de
Tue Jun 22 08:45:38 EDT 2010


On 06/22/2010 02:03 PM, Jerry Rocteur wrote:
>> On Tue, Jun 22, 2010 at 9:32 PM, Jerry Rocteur <macosx at rocteur.cc> wrote:
>> 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.
> 
> I have a dictionary:
> 
> users[key] = {    'user'        : key,
>                   'secnum'      : secnum,
>                   'name'        : name
>              }
> 
> Is it possible for me to code a class like this:
> 
> class GRPUser(object):
>     def __init__(self, user, secnum, name, groups=None):
>         self.user          = user
>         self.secnum        = secnum
>         self.name          = name
> 

So far your class is perfectly fine, representing the data in form of a
class having an object for each individual user.

> Which would allow me to iterate through and access specific records ?

That actually is not part of the class nor should it even be part of the
class. You would probably end up with something comparable to your
initial dict to 'lookup' the objects.

users = {}
users['jdoe'] = GRPUser('jdoe', ...)
...

This dict is something like an index from user id to user objects. You
may then setup an index for the secnum (if unique) the same way:

secnums = {}
secnums[12345] = users['jdoe']
...

Note, that I did set the identical object in the other index dict.
Modifying users['jdoe'] would modify secnums[12345]!
This is what you actually want from an index...

> How do I iterate through and access an individual user record!

You iterate over the index...


Andre



More information about the Python-list mailing list