Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'elif': 0.04; 'method,': 0.07; 'effect.': 0.09; 'from:addr:python': 0.09; 'lines:': 0.09; 'trailing': 0.09; 'anyway': 0.09; 'def': 0.13; 'argument': 0.15; 'result,': 0.15; "'n'": 0.16; 'f.close()': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:name:mrab': 0.16; 'line.split()': 0.16; 'line.strip()': 0.16; 'lines)': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'occur.': 0.16; 'received:84.92': 0.16; 'received:84.92.122': 0.16; 'received:84.92.122.60': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'reply-to:addr:python-list': 0.16; 'subject:classes': 0.16; 'whitespace.': 0.16; 'wrote:': 0.18; '>>>': 0.18; '(which': 0.19; 'this?': 0.19; '(or': 0.22; 'header :In-Reply-To:1': 0.22; 'dictionary': 0.23; 'ignore': 0.26; 'effect': 0.28; 'pass': 0.29; 'print': 0.29; 'class': 0.29; 'lines': 0.30; 'characters,': 0.30; 'line:': 0.30; 'preceding': 0.30; 'whitespace': 0.30; 'list': 0.32; 'header:User-Agent:1': 0.33; 'this.': 0.33; 'there': 0.33; 'to:addr:python-list': 0.34; 'received:84': 0.34; 'reply-to:addr:python.org': 0.34; '...': 0.36; 'shows': 0.37; 'class.': 0.37; 'sequence': 0.37; 'two': 0.37; 'but': 0.37; 'hello,': 0.37; 'skip:_ 10': 0.37; 'characters': 0.39; 'called': 0.40; "it's": 0.40; 'to:addr:python.org': 0.40; 'leading': 0.62; 'therefore,': 0.66; 'header:Reply-To:1': 0.71; 'reply-to:no real name:2**0': 0.72; '.replace': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.0 cv=J8QoHXbS c=1 sm=1 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=9jsOeB20M3cA:10 a=OUOv7kDek9cA:10 a=8nJEP1OIZ-IA:10 a=pGLkceISAAAA:8 a=Lh-69r1mqJi2XBpUSKMA:9 a=wPNLvfGTeEIA:10 a=MSl-tDqOz04A:10 a=0nF1XD0wxitMEM03M9B4ZQ==:117 X-AUTH: mrabarnett:2500 Date: Tue, 10 Jan 2012 01:08:03 +0000 From: MRAB User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:8.0) Gecko/20111105 Thunderbird/8.0 MIME-Version: 1.0 To: python-list@python.org Subject: Re: classes and __iter__ References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list Reply-To: python-list@python.org List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 57 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1326157685 news.xs4all.nl 6975 [2001:888:2000:d::a6]:54112 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:18726 On 09/01/2012 22:51, david.garvey@gmail.com wrote: > 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): You don't need to use a regex for this. It's better to do this instead: if "CMD" in line: > ... line = line.lstrip('#define ') The .lstrip, .rstrip and .strip methods treat the argument as a _set_ of characters, so that line will strip the characters '#', 'd', 'e', 'f, 'i', 'n' and ' ' from the left end of the line however many times they occur. > ... line = line.strip() > ... line.replace('\t', ' ') > ... line = line.split() The .split method, when called without an argument (or an argument of None), will split on a sequence of whitespace characters, but ignore leading and trailing whitespace. Therefore, there is no need to use the .strip method or the .replace (which will have no effect anyway because it _returns_ its result, which is then discarded) before the split. > ... if len(line) > 2: > ... pass The preceding two lines are pointless. Just turn the next line into an 'if'; it'll have the same effect. > ... 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) > ... [snip]