Re: Creating slice notation from string

2009-09-03 Thread Falcolas
On Sep 2, 3:55 pm, bvdp b...@mellowood.ca wrote: I'm trying to NOT create a parser to do this and I'm sure that it's easy if I could only see the light! Is it possible to take an arbitrary string in the form 1:2, 1, :-1, etc. and feed it to slice() and then apply the result to an

Re: Creating slice notation from string

2009-09-03 Thread Bob van der Poel
On Sep 2, 8:52 pm, Steven D'Aprano ste...@remove.this.cybersource.com.au wrote: On Wed, 02 Sep 2009 17:32:09 -0700, Bob van der Poel wrote: Actually, nither this or Jan's latest is working properly. I don't know if it's the slice() function or what (I'm using python 2.5). But: x =

Creating slice notation from string

2009-09-02 Thread bvdp
I'm trying to NOT create a parser to do this and I'm sure that it's easy if I could only see the light! Is it possible to take an arbitrary string in the form 1:2, 1, :-1, etc. and feed it to slice() and then apply the result to an existing list? For example, I have a normal python list.

Re: Creating slice notation from string

2009-09-02 Thread MRAB
bvdp wrote: I'm trying to NOT create a parser to do this and I'm sure that it's easy if I could only see the light! Is it possible to take an arbitrary string in the form 1:2, 1, :-1, etc. and feed it to slice() and then apply the result to an existing list? For example, I have a normal

Re: Creating slice notation from string

2009-09-02 Thread Robert Kern
On 2009-09-02 16:55 PM, bvdp wrote: I'm trying to NOT create a parser to do this and I'm sure that it's easy if I could only see the light! Is it possible to take an arbitrary string in the form 1:2, 1, :-1, etc. and feed it to slice() and then apply the result to an existing list? For

Re: Creating slice notation from string

2009-09-02 Thread Jan Kaliszewski
03-09-2009 o 00:11:17 MRAB pyt...@mrabarnett.plus.com wrote: bvdp wrote: I'm trying to NOT create a parser to do this and I'm sure that it's easy if I could only see the light! Is it possible to take an arbitrary string in the form 1:2, 1, :-1, etc. and feed it to slice() and then apply

Re: Creating slice notation from string

2009-09-02 Thread Jan Kaliszewski
Erratum: eval(str(x) + s) -- but it's worse: less secure (e.g. if s could be user-typed) and most probably much more time-consuming (especially the latter). There should be *repr* instead of *str*. *j -- Jan Kaliszewski (zuo) z...@chopin.edu.pl --

Re: Creating slice notation from string

2009-09-02 Thread Bob van der Poel
For a one-liner:    x[slice(*map(int, x[1:-1].split(':')))] Thanks. Almost works :) For s=[2] and s=[1:2] it's fine. But, if I have s = [:2] then I get: x[slice(*[int(i) for i in s.strip([]).split(:)])] Traceback (most recent call last): File stdin, line 1, in module ValueError:

Re: Creating slice notation from string

2009-09-02 Thread Bob van der Poel
Of course, you could also do something like this:      eval('x' + s) or      eval(str(x) + s) Yes, I have user inputed 's'. So, if I can't get the generalized list version from Robert working I'll have to use this. Speed is not a big deal in this. As to malicious input, I could pretty

Re: Creating slice notation from string

2009-09-02 Thread Rhodri James
On Wed, 02 Sep 2009 23:57:48 +0100, Bob van der Poel b...@mellowood.ca wrote: Of course, you could also do something like this:      eval('x' + s) or      eval(str(x) + s) Yes, I have user inputed 's'. So, if I can't get the generalized list version from Robert working I'll have to use

Re: Creating slice notation from string

2009-09-02 Thread Robert Kern
On 2009-09-02 17:55 PM, Bob van der Poel wrote: For a one-liner: x[slice(*map(int, x[1:-1].split(':')))] Thanks. Almost works :) For s=[2] and s=[1:2] it's fine. But, if I have s = [:2] then I get: x[slice(*[int(i) for i in s.strip([]).split(:)])] Traceback (most recent call

Re: Creating slice notation from string

2009-09-02 Thread Steven D'Aprano
On Wed, 02 Sep 2009 16:41:34 -0700, Bob van der Poel wrote: But, translating 1, 2 or 3 ints into a valid splice isn't quit that easy? I could figure each value, and convert them to either int or None (key is the None! From my previous try '' doesn't work!) But, I still need three possible

Re: Creating slice notation from string

2009-09-02 Thread Ethan Furman
Bob van der Poel wrote: For a one-liner: x[slice(*map(int, x[1:-1].split(':')))] Thanks. Almost works :) For s=[2] and s=[1:2] it's fine. But, if I have s = [:2] then I get: x[slice(*[int(i) for i in s.strip([]).split(:)])] Traceback (most recent call last): File stdin, line 1,

Re: Creating slice notation from string

2009-09-02 Thread Bob van der Poel
On Sep 2, 4:16 pm, Rhodri James rho...@wildebst.demon.co.uk wrote: On Wed, 02 Sep 2009 23:57:48 +0100, Bob van der Poel b...@mellowood.ca   wrote: Of course, you could also do something like this:      eval('x' + s) or      eval(str(x) + s) Yes, I have user inputed 's'. So, if I

Re: Creating slice notation from string

2009-09-02 Thread Bob van der Poel
On Sep 2, 4:43 pm, Jan Kaliszewski z...@chopin.edu.pl wrote: 03-09-2009 o 00:55:10 Bob van der Poel b...@mellowood.ca wrote: For a one-liner:    x[slice(*map(int, x[1:-1].split(':')))] Thanks. Almost works :) For s=[2] and s=[1:2] it's fine. But, if I have s = [:2] then I get:

Re: Creating slice notation from string

2009-09-02 Thread Bob van der Poel
On Sep 2, 5:16 pm, Steven D'Aprano st...@remove-this- cybersource.com.au wrote: On Wed, 02 Sep 2009 16:41:34 -0700, Bob van der Poel wrote: But, translating 1, 2 or 3 ints into a valid splice isn't quit that easy? I could figure each value, and convert them to either int or None (key is

Re: Creating slice notation from string

2009-09-02 Thread Jan Kaliszewski
03-09-2009 o 00:55:10 Bob van der Poel b...@mellowood.ca wrote: For a one-liner:    x[slice(*map(int, x[1:-1].split(':')))] Thanks. Almost works :) For s=[2] and s=[1:2] it's fine. But, if I have s = [:2] then I get: x[slice(*[int(i) for i in s.strip([]).split(:)])] Traceback (most

Re: Creating slice notation from string

2009-09-02 Thread Bob van der Poel
On Sep 2, 4:27 pm, Ethan Furman et...@stoneleaf.us wrote: Bob van der Poel wrote: For a one-liner:   x[slice(*map(int, x[1:-1].split(':')))] Thanks. Almost works :) For s=[2] and s=[1:2] it's fine. But, if I have s = [:2] then I get: x[slice(*[int(i) for i in

Re: Creating slice notation from string

2009-09-02 Thread Ethan Furman
On Wed, 02 Sep 2009 17:36:36 -0700, Bob van der Poel b...@mellowood.ca wrote: On Sep 2, 4:27 pm, Ethan Furman et...@stoneleaf.us wrote: Bob van der Poel wrote: For a one-liner:   x[slice(*map(int, x[1:-1].split(':')))] Thanks. Almost works :) For s=[2] and s=[1:2] it's fine. But,

Re: Creating slice notation from string

2009-09-02 Thread Steven D'Aprano
On Wed, 02 Sep 2009 17:32:09 -0700, Bob van der Poel wrote: Actually, nither this or Jan's latest is working properly. I don't know if it's the slice() function or what (I'm using python 2.5). But: x = [1,2,3,4,5] slice_string=2 items = [int(n) if n else None for n in

Re: Creating slice notation from string

2009-09-02 Thread Paul McGuire
On Sep 2, 4:55 pm, bvdp b...@mellowood.ca wrote: I'm trying to NOT create a parser to do this and I'm sure that it's easy if I could only see the light! Well, this is a nice puzzler, better than a sudoku. Maybe a quick parser with pyparsing will give you some guidance on how to do this