Shell like syntax for subprocess.Popen # overloading >, <, |

Christos Georgiou tzot at sil-tec.gr
Wed Apr 19 08:23:00 EDT 2006


On 18 Apr 2006 05:00:55 -0700, rumours say that "jelle"
<jelleferinga at gmail.com> might have written:

>Hi Christos,
>
>Thanks for your pointers there, impressive to see
>-that a 12 year old thread still can make an interesting read
>-you being able to remember & trace it... impressive...
>
>Thanks for your pointers.
>I think the
>input > process > output
>Syntax is more powerful , since it would let you build chaining
>commmands in a more readable fashion.
>
>The goal of this class would be to construct command chains such as:
>
>input > processA | processB > ouput
>
>Something which wouldn't be possible if only one operator is
>overloaded.
>I'm curious to see if its doable via overloading, since no __rgt__
>methods exists...

The problem with the operators chaining is that ">" is treated differently
than "|".  Check the following disassembly:

>>> import dis
>>> dis.dis(compile("a<b<c", "", "eval"))
  0           0 LOAD_NAME                0 (a)
              3 LOAD_NAME                1 (b)
              6 DUP_TOP             
              7 ROT_THREE           
              8 COMPARE_OP               0 (<)
             11 JUMP_IF_FALSE           10 (to 24)
             14 POP_TOP             
             15 LOAD_NAME                2 (c)
             18 COMPARE_OP               0 (<)
             21 JUMP_FORWARD             2 (to 26)
        >>   24 ROT_TWO             
             25 POP_TOP             
        >>   26 RETURN_VALUE        
>>> dis.dis(compile("a|b|c", "", "eval"))
  0           0 LOAD_NAME                0 (a)
              3 LOAD_NAME                1 (b)
              6 BINARY_OR           
              7 LOAD_NAME                2 (c)
             10 BINARY_OR           
             11 RETURN_VALUE        


The comparison operators include some logic in order to "do what I mean" (so
that 4<x<10 works like 4<x and x<10, but x<10 will never be evaluated if 4<x
is False), and that is why I suggested you use the "|" operator instead.

Of course, you can use the ">" operator, just don't chain it, in order to
avoid such unexpected behaviour.
-- 
TZOTZIOY, I speak England very best.
"Dear Paul,
please stop spamming us."
The Corinthians



More information about the Python-list mailing list