Mailing List Archive

force sequenz with only one item?
High,

I think it's easy to solve, but I couldn't get it right now.
Take a look at the command-line source below:

>>> A = 0
>>> B = 1
>>> seq=raw_input("?")
?A,B
>>> print seq
A,B
>>> print eval(seq)
(0, 1)
>>> seq=raw_input("?")
?A
>>> print eval(seq)
0
>>> A in seq
Traceback (innermost last):
File "<stdin>", line 1, in ?
TypeError: string member test needs char left operand
>>> A in eval(seq)
Traceback (innermost last):
File "<stdin>", line 1, in ?
TypeError: 'in' or 'not in' needs sequence right argument
>>>

Ok? Check it out?
That's what I want to do with it:
There's a file I'm parsing over special keywords per line, e.g.:
Action = CREATE, VERBOSE, APPEND
When read the line I put the right term to a variable, e.g:
anAction = eval(rightTerm)
Then I want to test for that keywords (global integer variables in
source, e.g.: CREATE = 0):
if CREATE in anAction:
pass #do something

But how could I force python to accept one-item sequences (see below!)?

Thanx for comin' with me...

Ciao,
Holger
force sequenz with only one item? [ In reply to ]
On Thu, Jun 10, 1999 at 12:03:58PM +0000, Holger Jannsen wrote:
> High,
>
> I think it's easy to solve, but I couldn't get it right now.
> Take a look at the command-line source below:
>
> >>> A = 0
> >>> B = 1
> >>> seq=raw_input("?")
> ?A,B
> >>> print seq
> A,B
> >>> print eval(seq)
> (0, 1)
> >>> seq=raw_input("?")
> ?A
> >>> print eval(seq)
> 0
> >>> A in seq
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> TypeError: string member test needs char left operand
> >>> A in eval(seq)
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> TypeError: 'in' or 'not in' needs sequence right argument
> >>>
>
> Ok? Check it out?
> That's what I want to do with it:
> There's a file I'm parsing over special keywords per line, e.g.:
> Action = CREATE, VERBOSE, APPEND
> When read the line I put the right term to a variable, e.g:
> anAction = eval(rightTerm)
> Then I want to test for that keywords (global integer variables in
> source, e.g.: CREATE = 0):
> if CREATE in anAction:
> pass #do something
>
> But how could I force python to accept one-item sequences (see below!)?
>
> Thanx for comin' with me...
>
> Ciao,
> Holger

One item list is '[A]'
One item tuple is '(A,)'
force sequenz with only one item? [ In reply to ]
Holger Jannsen asks:
>
> I think it's easy to solve, but I couldn't get it right now.
> Take a look at the command-line source below:
>
> >>> A = 0
> >>> B = 1
> >>> seq=raw_input("?")
> ?A,B
> >>> print seq
> A,B
> >>> print eval(seq)
> (0, 1)
> >>> seq=raw_input("?")
> ?A
> >>> print eval(seq)
> 0
> >>> A in seq
> Traceback (innermost last)
:...
> TypeError: 'in' or 'not in' needs sequence right argument
> >>>

The reason "A,B" gets evaluated to the sequence (0,1) is because of
the comma. "(A,)" would get evaluated to (0,), while "[A]" would
evaluate to [0]. If you want to always end up with sequences, you
should probably eval it and test it:

if type(evaledinput) not in (type(()), type([])):
evaledinput = (evaledinput,)

Single element tuples need the extra comma to distinguish them from
a simple grouping, as in 4 * ( 3 + 7 ). It would cause great
wailing and gnashing of teeth if it yeilded (10, 10, 10, 10).

even-worse-than-while-1-ly y'rs

- Gordon
force sequenz with only one item? [ In reply to ]
Thanx again, Gordon!

mg,
Holger