classes and __iter__
david.garvey at gmail.com
david.garvey at gmail.com
Mon Jan 9 17:51:24 EST 2012
Hello,
I have a class and i return both a key list and dictionary from the class.
Is it good form to do this? The print helo.__dict__ shows both the list and
dictionary.
>>> class Parse_Nagios_Header:
... def __init__(self):
... self.keylist = []
... self.d = {}
... f = open("common.h")
... lines = f.readlines()
... lines = filter(lambda x: not x.isspace(), lines)
... f.close()
... for line in lines:
... if re.search("CMD", line):
... line = line.lstrip('#define ')
... line = line.strip()
... line.replace('\t', ' ')
... line = line.split()
... if len(line) > 2:
... pass
... elif len(line) == 2:
... line[1] = int(line[1])
... self.d[line[1]] = line[0]
... self.keylist = sorted(self.d.iterkeys())
... def __iter__(self):
... return iter(self.keylist, self.d)
...
>>> helo = Parse_Nagios_Header()
>>> print helo
<__main__.Parse_Nagios_Header instance at 0x7fbf5b09b488>
>>> for key in helo.keylist:
... print "Key:%s Value:%s" %(key,helo.d[key])
...
Key:0 Value:CMD_NONE
Key:1 Value:CMD_ADD_HOST_COMMENT
Key:2 Value:CMD_DEL_HOST_COMMENT
Key:3 Value:CMD_ADD_SVC_COMMENT
Key:4 Value:CMD_DEL_SVC_COMMENT
Key:5 Value:CMD_ENABLE_SVC_CHECK
--
David Garvey
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120109/0ed655e5/attachment.html>
More information about the Python-list
mailing list