Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #89627

Re: Let exception fire or return None

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 <python-python-list@m.gmane.org>
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> <mailman.122.1430386245.3680.python-list@python.org> <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 <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.127.1430396946.3680.python-list@python.org> (permalink)
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

Show key headers only | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Let exception fire or return None Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 09:43 +0200
  Re: Let exception fire or return None Dave Angel <davea@davea.name> - 2015-04-30 04:18 -0400
    Re: Let exception fire or return None Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 11:13 +0200
  Re: Let exception fire or return None Peter Otten <__peter__@web.de> - 2015-04-30 11:30 +0200
    Re: Let exception fire or return None Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 13:26 +0200
      Re: Let exception fire or return None Peter Otten <__peter__@web.de> - 2015-04-30 14:28 +0200
        Re: Let exception fire or return None Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 16:53 +0200
      Re: Let exception fire or return None Cecil Westerhof <Cecil@decebal.nl> - 2015-04-30 14:22 +0200
  Re: Let exception fire or return None Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-04-30 14:05 +0200
  Re: Let exception fire or return None Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2015-04-30 14:30 +0200

csiph-web