<br>thanks for the feedback - i will go through your comments line by line adjust and test and will re-post when complete.<br><br>GTXY20<br><br><div class="gmail_quote">On Jan 4, 2008 9:29 PM, Kent Johnson &lt;<a href="mailto:kent37@tds.net">
kent37@tds.net</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Tiger12506 wrote:<br><br>&gt; Ouch. Usually in OOP, one never puts any user interaction into a class.
<br><br>That seems a bit strongly put to me. Generally user interaction should<br>be separated from functional classes but you might have a class to help<br>with command line interaction (e.g. the cmd module in the std lib) and
<br>GUI frameworks are usually built with classes.<br><div class="Ih2E3d"><br><br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;uh, tf = line.split(&#39;,&#39;)<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if uh in self.uhdata:<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f=
self.uhdata[uh]<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if tf not in f:<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;f.append(tf)<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else:<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;self.uhdata[uh]=[tf]<br>&gt;<br></div>&gt; This can read
<br>&gt;<br>&gt; try:<br>&gt; &nbsp; f = self.uhdata[uh]<br>&gt; except KeyError:<br>&gt; &nbsp; self.uhdata[uh] = []<br>&gt; finally:<br>&gt; &nbsp; self.uhdata[uh].append(tf)<br><br>These are not equivalent - the original code avoids duplicates in
<br>self.uhdata[uh].<br><br>Possibly self.uhdata[uh] could be a set instead of a list. Then this<br>could be written very nicely using defaultdict:<br><br>from collections import defaultdict<br> &nbsp; ...<br> &nbsp; self.uhdata = defaultdict(set)
<br> &nbsp; ...<br> &nbsp; self.uhdata[uh].add(tf)<br><div class="Ih2E3d"><br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for uh, Sections in self.uhdata.items():<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Sections.sort()<br>&gt;<br></div>&gt; This will not work. In the documentation, it says that dictionary object
<br>&gt; return a *copy* of the (key,value) pairs.<br>&gt; You are sorting those *copies*.<br><br>No, the original code is fine. The docs say that dict.items() returns &quot;a<br>copy of a&#39;s list of (key, value) pairs&quot;. This is a little misleading,
<br>perhaps. A dict does not actually contain a list of key, value pairs, it<br>is implemented with a hash table. dict.items() returns a new list<br>containing the key, value pairs.<br><br>But the keys and values in the new list are references to the same keys
<br>and values in the dict. So mutating the values in the returned list, via<br>sort(), will also mutate the values in the dict.<br><div class="Ih2E3d"><br><br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;missingpgcounts={}<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fmissingpgcounts=[]
<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for x in self.uhdata:<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for f in self.uhdata[x]:<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if f not in fmissingpgcounts:<br>&gt;&gt; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;fmissingpgcounts.append(f)<br>
&gt;<br></div>&gt; fmissingpgcounts = [f for f in self.uhdata.itervalues() if f not in<br>&gt; fmissingpgcounts]<br>&gt; Ahhh... This looks like a set.<br>&gt; fmissingpgcounts = set(self.uhdata.itervalues())<br><br>Again, this is not quite the same thing. The original code builds a set
<br>of the contents of the values. You are building a set from the values<br>(which are already lists). You still need one loop:<br>fmissingpgcounts = set()<br>for v in self.uhdate.itervalues():<br> &nbsp; fmissingpgcounts.update
(v)<br><br>&gt; self.pgcounts = dict((x,0) for x in fmissingpgcounts)<br><br>or, self.pgcounts = dict.fromkeys(fmissingpgcounts, 0)<br><br>That&#39;s all I have energy for...<br><font color="#888888">Kent<br><br></font></blockquote>
</div><br>