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


Groups > comp.lang.python > #50453

Re: xslice idea | a generator slice

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 <oscar.j.benjamin@gmail.com>
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 <c087829a-8e30-4f50-afb9-d28a779932e3@googlegroups.com>
References <4a81b6a6-023e-4d47-9bd0-bbc0516caf6b@googlegroups.com> <c087829a-8e30-4f50-afb9-d28a779932e3@googlegroups.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Thu, 11 Jul 2013 16:14:46 +0100
Subject Re: xslice idea | a generator slice
To Russel Walker <russ.pobox@gmail.com>
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 <python-list.python.org>
List-Unsubscribe <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4590.1373555714.3114.python-list@python.org> (permalink)
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

Show key headers only | View raw


On 11 July 2013 15:54, Russel Walker <russ.pobox@gmail.com> 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

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


Thread

xslice idea | a generator slice Russel Walker <russ.pobox@gmail.com> - 2013-07-11 07:52 -0700
  Re: xslice idea | a generator slice Russel Walker <russ.pobox@gmail.com> - 2013-07-11 07:54 -0700
    Re: xslice idea | a generator slice Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-11 16:14 +0100
      Re: xslice idea | a generator slice Russel Walker <russ.pobox@gmail.com> - 2013-07-11 09:21 -0700
        Re: xslice idea | a generator slice Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-07-11 17:34 +0100
        Re: xslice idea | a generator slice Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-11 12:00 -0600
        Re: xslice idea | a generator slice Fábio Santos <fabiosantosart@gmail.com> - 2013-07-11 20:58 +0100
        Re: xslice idea | a generator slice Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-11 15:02 -0600
  Re: xslice idea | a generator slice Ian Kelly <ian.g.kelly@gmail.com> - 2013-07-11 10:16 -0600

csiph-web