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


Groups > comp.lang.python > #89617

Re: Let exception fire or return None

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!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.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '"""': 0.07; 'suppose': 0.07; 'default.': 0.09; 'exception,': 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; 'way:': 0.09; 'python': 0.11; 'def': 0.12; 'big,': 0.16; 'fetch': 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; 'subject:exception': 0.16; 'usage,': 0.16; 'exception': 0.16; 'index': 0.16; 'wrote:': 0.18; 'file,': 0.19; "skip:' 30": 0.19; 'memory': 0.22; 'header:User-Agent:1': 0.23; 'gets': 0.27; 'header:X-Complaints-To:1': 0.27; 'function': 0.29; 'raise': 0.29; 'skip:g 30': 0.30; 'code': 0.31; 'assert': 0.31; 'catching': 0.31; 'controlled': 0.31; 'file:': 0.31; 'index,': 0.31; 'ok.': 0.31; 'file': 0.32; 'except': 0.35; '(2)': 0.35; 'but': 0.35; 'returning': 0.36; "didn't": 0.36; 'to:addr:python-list': 0.38; 'that,': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:o 30': 0.61; 'first': 0.61; 'provide': 0.64; 'more': 0.64; 'fire': 0.65; 'limit': 0.70; 'closes': 0.84; '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 11:30:21 +0200
Organization None
References <87bni6awol.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.122.1430386245.3680.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1430386245 news.xs4all.nl 2932 [2001:888:2000:d::a6]:39542
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:89617

Show key headers only | View raw


Cecil Westerhof wrote:

> I have a function to fetch a message from a file:
>     def get_indexed_message(message_filename, index):
>         """
>         Get index message from a file, where 0 gets the first message
>         """
> 
>         return open(expanduser(message_filename),
>         'r').readlines()[index].rstrip()
> 
> What is more the Python way: let the exception fire like this code
> when index is to big, or catching it and returning None?

Fire an exception, but you may also allow the user to provide a default.

> I suppose working zero based is OK.

Not just OK, it's de rigueur. 


You didn't ask for that, but

(1)

with open(...) as f:
    return f.readlines()[index].rstrip()

is preferrable because it closes the file in a controlled way and 

(2) you may want to take measures to limit memory usage, e. g. 

assert index >= 0
try:
    [line] = itertools.islice(f, index, index+1)
except ValueError:
    raise IndexError
return line.rstrip()

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