Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!1.eu.feeder.erje.net!lightspeed.eweka.nl!lightspeed.eweka.nl!newsfeed.xs4all.nl!newsfeed2a.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.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'indices': 0.07; 'boundaries': 0.09; 'calculating': 0.09; 'cest': 0.09; 'fixme:': 0.09; 'indexes': 0.09; 'negative.': 0.09; 'slices': 0.09; 'subject:skip:f 10': 0.09; 'def': 0.12; 'assume': 0.14; 'cleaner': 0.16; 'disallow': 0.16; 'iterators': 0.16; 'slice,': 0.16; 'wrote:': 0.18; 'value.': 0.19; 'otherwise,': 0.22; 'case.': 0.24; 'question': 0.24; '>': 0.26; 'values': 0.27; 'header:In-Reply- To:1': 0.27; 'am,': 0.29; 'possibility': 0.29; 'skip:g 30': 0.30; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; 'that.': 0.31; 'approve': 0.31; 'end,': 0.31; 'steven': 0.31; 'could': 0.34; 'subject:with': 0.35; 'common': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'really': 0.36; 'positive': 0.37; 'e.g.': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'negative': 0.60; 'skip:n 10': 0.64; 'provide': 0.64; 'reads': 0.68; 'skip:r 40': 0.68; '2015': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=cup2FqH/5aOI5McRRhFxYzdomwYPOLtBXaGzNglMkR4=; b=EsWPNf/FdSb6hyN/gRgN2hML8ynJeorrjOSaz6ur5aYn9AHiuJXgwwCojDXX3r691L w+NW5cUgesSX3Ulb9BWo9E9GuH137+UItXJoDKLtfXG8uNzZ5PALmF305BTLtRaWJh1D GWCF3qZlmwRpgPvebL91TGX7za7ogKREWCbC2KInf2yyikJI3E2EIBZEBXsqPEwPaA81 rZEB9bDz8uh7z74uLqsr3r8EjKOAOT3InJbfPYvGpsSEdtejGqgPYyS3eos8NRWBfNF3 cBrWsfpawUSzIGLIwkka1ki0QV/QmuIQkUMpNas+GXlfPlNew6bGfuzkFlfu28TOLYiE kf8Q== MIME-Version: 1.0 X-Received: by 10.50.85.43 with SMTP id e11mr1848964igz.15.1430834220986; Tue, 05 May 2015 06:57:00 -0700 (PDT) In-Reply-To: <87fv7bgt6t.fsf@Equus.decebal.nl> References: <87oalzh0d5.fsf@Equus.decebal.nl> <55489e80$0$12913$c3e8da3$5496439d@news.astraweb.com> <87fv7bgt6t.fsf@Equus.decebal.nl> Date: Tue, 5 May 2015 07:57:00 -0600 Subject: Re: Step further with filebasedMessages From: Ian Kelly To: Python Content-Type: multipart/alternative; boundary=089e0149bdf2799f070515560d2c 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: 69 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1430834230 news.xs4all.nl 2906 [2001:888:2000:d::a6]:39417 X-Complaints-To: abuse@xs4all.nl X-Received-Bytes: 6517 X-Received-Body-CRC: 2551571663 Xref: csiph.com comp.lang.python:89966 --089e0149bdf2799f070515560d2c Content-Type: text/plain; charset=UTF-8 On May 5, 2015 5:46 AM, "Cecil Westerhof" wrote: > > Op Tuesday 5 May 2015 12:41 CEST schreef Steven D'Aprano: > > > # Untested. > > def get_message_slice(message_filename, start=0, end=None, step=1): > > real_file = expanduser(message_filename) > > messages = [] > > # FIXME: I assume this is expensive. Can we avoid it? > > nr_of_messages = get_nr_of_messages(real_file) > > If I want to give the possibility to use negative values also, I need > the value. You could make this call only if one of the boundaries is actually negative. Then callers that provide positive values don't need to pay the cost of that case. Alternatively, consider that it's common for slices of iterators to disallow negative indices altogether, and question whether you really need that. > > the_slice = slice(start, end, step) > > # Calculate the indexes in the given slice, e.g. > > # start=1, stop=7, step=2 gives [1,3,5]. > > indices = range(*(the_slice.indices(nr_of_messages))) > > with open(real_file, 'r') as f: > > for i, message in enumerate(f): > > if i in indices: > > messages.append(message) > > return messages I approve of using slice.indices instead of calculating the indices manually, but otherwise, the islice approach feels cleaner to me. This reads like a reimplementation of that. --089e0149bdf2799f070515560d2c Content-Type: text/html; charset=UTF-8

On May 5, 2015 5:46 AM, "Cecil Westerhof" <Cecil@decebal.nl> wrote:
>
> Op Tuesday 5 May 2015 12:41 CEST schreef Steven D'Aprano:
>
> > # Untested.
> > def get_message_slice(message_filename, start=0, end=None, step=1):
> > real_file = expanduser(message_filename)
> > messages = []
> > # FIXME: I assume this is expensive. Can we avoid it?
> > nr_of_messages = get_nr_of_messages(real_file)
>
> If I want to give the possibility to use negative values also, I need
> the value.

You could make this call only if one of the boundaries is actually negative. Then callers that provide positive values don't need to pay the cost of that case.

Alternatively, consider that it's common for slices of iterators to disallow negative indices altogether, and question whether you really need that.

> > the_slice = slice(start, end, step)
> > # Calculate the indexes in the given slice, e.g.
> > # start=1, stop=7, step=2 gives [1,3,5].
> > indices = range(*(the_slice.indices(nr_of_messages)))
> > with open(real_file, 'r') as f:
> > for i, message in enumerate(f):
> > if i in indices:
> > messages.append(message)
> > return messages

I approve of using slice.indices instead of calculating the indices manually, but otherwise, the islice approach feels cleaner to me. This reads like a reimplementation of that.

--089e0149bdf2799f070515560d2c--