Creating slice notation from string

Bob van der Poel bob at mellowood.ca
Wed Sep 2 19:41:34 EDT 2009


On Sep 2, 4:16 pm, "Rhodri James" <rho... at wildebst.demon.co.uk> wrote:
> On Wed, 02 Sep 2009 23:57:48 +0100, Bob van der Poel <b... at 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 this. Speed is not a big
> > deal in this. As to malicious input, I could pretty easily check to
> > see that all the values are integers.
>
> If you've done that check, you've parsed the input so you might as well
> use the values you've derived rather than waste time and add risk by
> using eval().

Not sure exactly what you mean here? Checking the values is (fairly)
trivial. Probably split on ":" and check the resulting list, etc.

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 lines:

 if len(i) == 1:
    x=x[i(0)]
  else if len(i) == 2:
    x=x[i(0):i(1)]
   ....


But, I'm wondering if just doing a split() on the index list and
seeing if they are all ints or None isn't safe?





More information about the Python-list mailing list