Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!ecngs!feeder2.ecngs.de!novso.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!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.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'messages.': 0.05; '-1)': 0.09; 'check,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:None': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'anyway': 0.14; 'fetches': 0.16; 'lines),': 0.16; 'measures': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'record,': 0.16; 'statement.': 0.16; 'subject:exception': 0.16; 'usage,': 0.16; 'index': 0.16; 'wrote:': 0.18; 'unlike': 0.19; '(the': 0.22; 'memory': 0.22; 'header:User-Agent:1': 0.23; 'header:X-Complaints- To:1': 0.27; 'raise': 0.29; "doesn't": 0.30; 'important.': 0.30; 'skip:g 30': 0.30; 'code': 0.31; 'assert': 0.31; 'index,': 0.31; 'file': 0.32; 'reader': 0.33; 'except': 0.35; 'possible.': 0.35; '(2)': 0.35; 'but': 0.35; 'should': 0.36; 'list': 0.37; 'to:addr :python-list': 0.38; 'rather': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'negative': 0.60; 'first': 0.61; 'between': 0.67; 'biggest': 0.67; 'limit': 0.70; '100': 0.79; 'lean': 0.84; 'yours': 0.88; 'subject:Let': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Let exception fire or return None Date: Thu, 30 Apr 2015 14:28:49 +0200 Organization: None References: <87bni6awol.fsf@Equus.decebal.nl> <87h9rxx3g6.fsf@Equus.decebal.nl> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd82bc.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430396946 news.xs4all.nl 2955 [2001:888:2000:d::a6]:55950 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:89627 Cecil Westerhof wrote: >> (2) you may want to take measures to limit memory usage, e. g. >> >> assert index >= 0 > > I put that in, but as first statement. For the record, this was not a recommended check, but rather a way to communicate to the reader of my code that unlike yours it doesn't support negative indices. >> try: >> [line] = itertools.islice(f, index, index+1) >> except ValueError: >> raise IndexError >> return line.rstrip() > > In my case it is not important. (The biggest file I use has between > 100 and 200 lines), but I publish it, so I should do my best to make > it so lean as possible. If you keep your return f.readlines()[index] anyway you might allow for negative indices, and then get_indexed_message(message_filename, -1) fetches the last message in the file and get_indexed_message(message_filename, slice(None, None)) fetches a list with all messages.