object aware of others

2012-01-28 Thread Lee Chaplin
Hi all,

I am trying to create an object that is aware of other objects created
before itself, and when found, then copy some attributes from them,
something like:

class A:
def __init__(self):
self.myname = "IamA"
print 'This is A'
def foo(self):
print "foo"
def update(self):
i = ''
obj = self
for i in globals():
obj = globals()[i]
if hasattr(obj, 'myname'):
print "The only friends I've got are ", i, obj.myname
else:
print "Oops, not my friend."


class B:
def __init__(self):
print 'This is B'
def foo(self):
print "bar"

# a = A()
# b = B()
# c = A()
# c.update()

The last four lines work if they are in the same module as the class
definitions (a000), but it doesn't work if they are called from a
different module, say:

import a000

a = a000.A()
b = a000.B()
c = a000.A()
c.update()

I presume there is something that need to replace the globals() call,
but I cannot find what.
Any help is greatly appreciated.

Thanks,
Lee
-- 
http://mail.python.org/mailman/listinfo/python-list


sort by column a csv file case insensitive

2012-04-15 Thread Lee Chaplin
Hi all,

I am trying to sort, in place, by column, a csv file AND sort it case
insensitive.
I was trying something like this, with no success:

import csv
import operator

def sortcsvbyfield(csvfilename, columnnumber):
  with open(csvfilename, 'rb') as f:
readit = csv.reader(f)
thedata = list(readit)

  thedata = sorted(thedata, key = lambda x:
(operator.itemgetter(columnnumber) ,x[0].lower()))  #!!!
  with open(csvfilename, 'wb') as f:
writeit = csv.writer(f)
writeit.writerows(thedata)

The line marked is the culprit.
Any help is greatly appreciated.

Thanks,
Lee
-- 
http://mail.python.org/mailman/listinfo/python-list


sort by column a csv file case insensitive

2012-04-16 Thread Lee Chaplin
Well, if I have a file like this one:

EWIENER,
edit,
edgard,
evan,
erick,
elliott,
enquiries,
Elliott,

I would like to get something like this (sorted by column 4 and case
insensitive):

edgard,
edit,
elliott,
Elliott,
enquiries,
erick,
evan,
EWIENER,

(Obviously, there are more data in the other columns, I edited the
file for clarity.)

>From the previous email I would like to call:
sortcsvbyfield('e.txt', 4)

I am on python 2.6 on Win.

Thanks,
Lee
-- 
http://mail.python.org/mailman/listinfo/python-list