Changeset 3530:90b34c166214 in livinglogic.python.xist

Show
Ignore:
Timestamp:
07/22/08 11:06:57 (11 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Add a zip() function and tuple unpacking in for loops with three variables.

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • NEWS.rst

    r3529 r3530  
    3838    an XIST node as a unicode string. :class:`XISTPublishAction` has been renamed 
    3939    to :class:`XISTBytesAction`. 
     40 
     41*   UL4 has gained a new function ``zip``. It can be called with two or three 
     42    arguments and does what :func:`itertools.zip` does. 
     43 
     44*   UL4 now supports tuple unpacking in ``<?for?>`` tag with three variables. 
    4045 
    4146 
  • src/ll/ul4c.py

    r3528 r3530  
    781781            _code("def %s(templates={}, **variables):" % function) 
    782782            indent += 1 
    783         _code("import sys, marshal, datetime") 
     783        _code("import sys, marshal, datetime, itertools") 
    784784        _code("from ll.misc import xmlescape") 
    785785        _code("from ll import ul4c") 
     
    963963                    elif opcode.arg == "get": 
    964964                        _code("reg%d = variables.get(reg%d, reg%d)" % (opcode.r1, opcode.r2, opcode.r3)) 
     965                    elif opcode.arg == "zip": 
     966                        _code("reg%d = itertools.izip(reg%d, reg%d)" % (opcode.r1, opcode.r2, opcode.r3)) 
    965967                    else: 
    966968                        raise UnknownFunctionError(opcode.arg) 
     
    968970                    if opcode.arg == "range": 
    969971                        _code("reg%d = xrange(reg%d, reg%d, reg%d)" % (opcode.r1, opcode.r2, opcode.r3, opcode.r4)) 
     972                    elif opcode.arg == "zip": 
     973                        _code("reg%d = itertools.izip(reg%d, reg%d, reg%d)" % (opcode.r1, opcode.r2, opcode.r3, opcode.r4)) 
    970974                    else: 
    971975                        raise UnknownFunctionError(opcode.arg) 
     
    21612165        return For(_0.start, cont.end, [iter1, iter2], cont) 
    21622166 
     2167    @spark.production('for ::= ( name , name , name ) in expr0') 
     2168    def for3a(self, _0, iter1, _1, iter2, _2, iter3, _3, _4, cont): 
     2169        return For(_0.start, cont.end, [iter1, iter2, iter3], cont) 
     2170 
     2171    @spark.production('for ::= ( name , name , name , ) in expr0') 
     2172    def for3b(self, _0, iter1, _1, iter2, _2, iter3, _3, _4, _5, cont): 
     2173        return For(_0.start, cont.end, [iter1, iter2, iter3], cont) 
     2174 
    21632175 
    21642176class StmtParser(ExprParser): 
  • test/test_ul4.py

    r3527 r3530  
    801801 
    802802 
     803def test_function_zip(): 
     804    checkrunerror("function u?'zip' unknown", "<?print zip()?>") 
     805    checkrunerror("function u?'zip' unknown", "<?print zip(1)?>") 
     806    code = "<?for (ix, iy) in zip(x, y)?><?print ix?>-<?print iy?>;<?end for?>" 
     807    check("", code, x=[], y=[]) 
     808    check("1-3;2-4;", code, x=[1, 2], y=[3, 4]) 
     809    check("1-4;2-5;", code, x=[1, 2, 3], y=[4, 5]) 
     810    code = "<?for (ix, iy, iz) in zip(x, y, z)?><?print ix?>-<?print iy?>+<?print iz?>;<?end for?>" 
     811    check("", code, x=[], y=[], z=[]) 
     812    check("1-3+5;2-4+6;", code, x=[1, 2], y=[3, 4], z=[5, 6]) 
     813    check("1-4+6;", code, x=[1, 2, 3], y=[4, 5], z=[6]) 
     814 
     815 
    803816def test_method_upper(): 
    804817    check("GURK", "<?print 'gurk'.upper()?>")