Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed4.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.027 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'else:': 0.03; 'elif': 0.05; 'indices': 0.07; 'cc:addr:python-list': 0.11; 'def': 0.12; "wouldn't": 0.14; "'''": 0.16; '*args):': 0.16; 'subject:generator': 0.16; 'wrote:': 0.18; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'cc:no real name:2**0': 0.24; 'header:In- Reply-To:1': 0.27; 'raise': 0.29; 'start,': 0.30; 'message- id:@mail.gmail.com': 0.30; 'class': 0.32; 'probably': 0.32; 'skip:_ 10': 0.34; 'received:google.com': 0.35; 'step': 0.37; 'skip:x 10': 0.40; 'how': 0.40; 'most': 0.60; 'simple': 0.61; 'july': 0.63; 'to:addr:gmail.com': 0.65; 'here': 0.66; 'oscar': 0.84; 'stop,': 0.84; '2013': 0.98 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=L9XWBtjbz+BID0alRLDU4gz2ct73OCh/Or5SoNYLtUU=; b=VqrPCkmPuUTBab83O4pEJ3Ne5aLmlJWREUR4OSUAT0YE5J9DmJdvBBpKwM3zun0mM1 QXQrPLykc5qKSDl49BuWZ8WCgNMgA5o+sD5UT2ffI9gWHO1hkmJCrf6VBzWsops5TMWa bL01x/baokibPPHLZT8F03U89/LBtlrDg61iuV3FbAmVpvDFYXE2+vlos7lgaRMk2X0/ D1hXIfrKY797MNqgzBqFWUO5kYPrznpWzC7xW5qIj9km3IMxdu3FVWz745SO2N1/lr/9 2e8WC76UUl6on93M7owStPOo0JHJmIaX6Mj7TO5ZAbBmux8dN9S4Uz8AGay60HCY2xAu wWxw== X-Received: by 10.52.185.130 with SMTP id fc2mr18445656vdc.45.1373555706479; Thu, 11 Jul 2013 08:15:06 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: References: <4a81b6a6-023e-4d47-9bd0-bbc0516caf6b@googlegroups.com> From: Oscar Benjamin Date: Thu, 11 Jul 2013 16:14:46 +0100 Subject: Re: xslice idea | a generator slice To: Russel Walker Content-Type: text/plain; charset=ISO-8859-1 Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1373555714 news.xs4all.nl 15919 [2001:888:2000:d::a6]:59080 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:50453 On 11 July 2013 15:54, Russel Walker wrote: > ...oh and here is the class I made for it. > > class xslice(object): > ''' > xslice(seq, start, stop, step) -> generator slice > ''' > > def __init__(self, seq, *stop): Wouldn't it be better if it has the same signature(s) as itertools.islice? > if len(stop) > 3: > raise TypeError("xslice takes at most 4 arguments") > elif len(stop) < 0: How would len(stop) be negative? > raise TypeError("xslice requires atleast 2 arguments") > else: > start, stop, step = (((0,) + stop[:2])[-2:] + # start, stop > (stop[2:] + (1,))[:1]) # step > stop = min(stop, len(seq)) > self._ind = iter(xrange(start, stop, step)) > self._seq = seq > > def __iter__(self): > return self > > def next(self): > return self._seq[self._ind.next()] > > > > Although now that I think about it, it probably should've just been a simple generator function. Or you can use itertools.imap: def xslice(sequence, start_or_stop, *args): indices = xrange(*slice(start_or_stop, *args).indices(len(sequence))) return imap(sequence.__getitem__, indices) Oscar