Many thanks mrabarnett for the code critic...<br><br><br><br><div class="gmail_quote">On Mon, Jan 9, 2012 at 5:08 PM, MRAB <span dir="ltr"><<a href="mailto:python@mrabarnett.plus.com">python@mrabarnett.plus.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="im">On 09/01/2012 22:51, <a href="mailto:david.garvey@gmail.com" target="_blank">david.garvey@gmail.com</a> wrote:<br>

<blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
Hello,<br>
<br>
I have a class and i return both a key list and dictionary from the<br>
class. Is it good form to do this? The print helo.__dict__ shows both<br>
the list and dictionary.<br>
<br>
<br>
<br>
 >>> class Parse_Nagios_Header:<br>
...     def __init__(self):<br>
...         self.keylist = []<br>
...         self.d = {}<br>
...         f = open("common.h")<br>
...         lines = f.readlines()<br>
...         lines = filter(lambda x: not x.isspace(), lines)<br>
...         f.close()<br>
...         for line in lines:<br>
...             if re.search("CMD", line):<br>
</blockquote>
<br></div>
You don't need to use a regex for this. It's better to do this instead:<br>
<br>
    if "CMD" in line:<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
...                 line = line.lstrip('#define ')<br>
</blockquote>
<br></div>
The .lstrip, .rstrip and .strip methods treat the argument as a _set_<br>
of characters, so that line will strip the characters '#', 'd', 'e',<br>
'f, 'i', 'n' and ' ' from the left end of the line however many times<br>
they occur.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
...                 line = line.strip()<br>
...                 line.replace('\t', ' ')<br>
...                 line = line.split()<br>
</blockquote>
<br></div>
The .split method, when called without an argument (or an argument of<br>
None), will split on a sequence of whitespace characters, but ignore<br>
leading and trailing whitespace.<br>
<br>
Therefore, there is no need to use the .strip method or the .replace<br>
(which will have no effect anyway because it _returns_ its result,<br>
which is then discarded) before the split.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
...             if len(line) > 2:<br>
...                 pass<br>
</blockquote>
<br></div>
The preceding two lines are pointless. Just turn the next line into an<br>
'if'; it'll have the same effect.<div class="im"><br>
<br>
<blockquote class="gmail_quote" style="margin:0pt 0pt 0pt 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
...             elif len(line) == 2:<br>
...                 line[1] = int(line[1])<br>
...                 self.d[line[1]] = line[0]<br>
...         self.keylist = sorted(self.d.iterkeys())<br>
...     def __iter__(self):<br>
...         return iter(self.keylist, self.d)<br>
...<br>
</blockquote></div>
[snip]<span class="HOEnZb"><font color="#888888"><br>
-- <br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/<u></u>mailman/listinfo/python-list</a><br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br>David Garvey<br>