Python help

Cole Tuininga colet at code-energy.com
Thu Feb 5 11:17:29 EST 2004


First of all, welcome to python!  8)  I do a fair amount of python
coding and am always happy to help out if I can.  

Quick question - what version of python are you running?

>     #!/sw/bin/python
> 
>     import os
>     import sys
> 
>     class gr_struct:             # I found this in the docs and it seems
>           pass                   # to work rather well.

Keep in mind that instantiation of objects is one of python's slower
operations.  If speed matters, you'd do much better to use a dictionary.

>     gr_hash = {}
>     group = gr_struct()
> 
>     fp=open(sys.argv[1], 'r')
> 
>     for i in fp:
>            i=i.strip("\n")
>            line = i.split(':')
>            group.name = line[0]  # what I don't quite get is *why* this works
>            group.gid = line[2]   # but it certainly beats the dealing
>                                  # with nested/anon dictionaries
>                                  # (though I think I like
>                                  # perl's hashes better
> 
>            group.members = line[3].split(',')
>            gr_hash = {group.name: group}


Why this works is that unless you do some work to prevent it, objects will create member variable references on the fly.  You can't do a test against one before you set it, so for instance, doing something like:
	if group.bob == "something":

would throw an exception, unless you explicit define group.bob before
hand.  You're essentially using an instance of an object as a dict,
which again leads me to think you should just use a dict.  8)

Note, the code as I'm reading it right now will not work.  You will end
up with a single entry repeated many times in the output.  The reason
for this is that you are using the same object over and over.  Keep in
mind that python is heavily referential.  When you say gr_hash =
{group.name: group} but not changing the reference to group, each of the
items are going to point to the same object.  

What I *think* you want is to put the "group = gr_struct()" line within
the loop.

>     fp.close()    
>     skeys = gr_hash.keys()
>     string = ''
>     for key in skeys:
>         members = gr_hash[key].members # why can't I do 
>                                        #  gr_hash[key].members.sort()
>         members.sort()

The reason you can't do this in place is that sort does not return the
sorted list - it sorts it in place.  I believe the return value for the
sort() method is a None object.  One neat thing about sort is that you
can pass it your own comparison function.  

>         for i in members:              # I would expect to be able use
>             if string == '':           # i.join() somehow, but that doesn't
>                string = i              # seem to work
>             else:
>                string += ',' + i
>         print key + ":" + gr_hash[key].gid + ":" + string

string is not a great name for a variable, IMNSHO.  8)

I believe the join syntax you want is:
member_names = ','.join( members )

-- 
"I have one plan for linux.  World Domination."
 -Linus Torvalds

Cole Tuininga
Lead Developer
Code Energy, Inc
colet at code-energy.com
PGP Key ID: 0x43E5755D





More information about the gnhlug-discuss mailing list