Changeset 5343:8ea8a9784e51 in livinglogic.python.xist

Show
Ignore:
Timestamp:
02/12/13 10:58:47 (7 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Allow multiple arguments in UL4 list methods append and insert. Add UL4 dict method update.

Files:
5 modified

Legend:

Unmodified
Added
Removed
  • MIGRATION.rst

    r5313 r5343  
    55-------------- 
    66 
    7 *   The UL4 tag ``<?code?>`` has been renamed to ``<?exe?>`` and the tag 
    8     ``<?render?>`` have been removed. 
    9  
    10     To update your code replace: 
    11  
    12     *    ``<?code x = 42?>`` with ``<?exe x = 42?>`` and 
    13  
    14     *   ``<?render r.render()?>`` with ``<?exe r.render()?>``. 
     7*   The UL4 tag ``<?render?>`` have been removed. To update your code replace 
     8    ``<?render r.render()?>`` with ``<?exe r.render()?>``. 
    159 
    1610*   The UL4 functions ``vars`` and ``get`` have been removed. 
  • NEWS.rst

    r5331 r5343  
    2020 
    2121*   The tag ``<?render?>`` has been removed. To update your code replace  
    22     ``<?render r.render()?>`` with ``<?exe r.render()?>``. 
     22    ``<?render r.render()?>`` with ``<?code r.render()?>``. 
    2323 
    2424*   The builtin UL4 functions are now real objects that can be passed to 
     
    2929 
    3030*   Lists in UL4 now support the methods ``append``, ``insert`` and ``pop``. 
     31 
     32*   Dictionaries in UL4 now support the method ``update``. 
    3133 
    3234*   The UL4 functions ``vars`` and ``get`` have been removed. 
  • docs/UL4.rst

    r5341 r5343  
    12301230 
    12311231 
     1232``append`` 
     1233"""""""""" 
     1234 
     1235``append`` is a list method. It adds its arguments to the end of the list for 
     1236which it is called:: 
     1237 
     1238    <?code v = [1, 2]?> 
     1239    <?code v.append(3, 4)?> 
     1240    <?print v?> 
     1241 
     1242prints ``[1, 2, 3, 4]``. 
     1243 
     1244 
     1245``insert`` 
     1246"""""""""" 
     1247 
     1248``insert`` is a list method. Its first argument in the insert position, the 
     1249remaining arguments are the intems that will be inserted at that position, so:: 
     1250 
     1251    <?code v = [1, 4]?> 
     1252    <?code v.insert(1, 2, 3)?> 
     1253    <?print v?> 
     1254 
     1255prints ``[1, 2, 3, 4]``. 
     1256 
     1257 
     1258``pop`` 
     1259""""""" 
     1260 
     1261``pop`` is a list method. It removes the last item of the list and returns it. 
     1262If an index is passed the item at that position is removed and returned. 
     1263A negative index is treated as relative to the end of the list. 
     1264 
     1265 
     1266``update`` 
     1267"""""""""" 
     1268 
     1269``update`` is a dictionary method. It supports an arbitrary number of positional 
     1270and keyword arguments. Each positional argument may be a dictionary, all the 
     1271items in the dictionary will be copied to the target dictionary. A positional 
     1272argument may also be an iterable of (key, value) pairs. These will also be copied 
     1273to the target dictionary. After each positional argument is copied over in a last 
     1274step the keyword arguments will be copied to the target dictionary. 
     1275 
     1276 
    12321277Templates as functions 
    12331278====================== 
     
    12521297 
    12531298With this, ``output`` will be the string ``"Javascript"``. 
    1254  
    12551299 
    12561300When no ``<?return?>`` tag is encountered, ``None`` will be returned. 
  • src/ll/ul4c.py

    r5342 r5343  
    33303330 
    33313331@AST.makemethod 
    3332 def _append(obj, item): 
    3333     return obj.append(item) 
    3334  
    3335  
    3336 @AST.makemethod 
    3337 def _insert(obj, pos, item): 
    3338     return obj.insert(pos, item) 
     3332def _append(obj, *items): 
     3333    obj.extend(items) 
     3334 
     3335 
     3336@AST.makemethod 
     3337def _insert(obj, pos, *items): 
     3338    obj[pos:pos] = items 
    33393339 
    33403340 
     
    33423342def _pop(obj, pos=-1): 
    33433343    return obj.pop(pos) 
     3344 
     3345 
     3346@AST.makemethod 
     3347def _update(obj, *others, **kwargs): 
     3348    for other in others: 
     3349        obj.update(other) 
     3350    obj.update(**kwargs) 
    33443351 
    33453352 
  • test/test_ul4.py

    r5331 r5343  
    30243024@pytest.mark.ul4 
    30253025def test_method_append(r): 
    3026     assert '42' == r('<?code l = []?><?code l.append(42)?><?print l[0]?>') 
     3026    assert '[17, 23, 42]' == r('<?code l = [17]?><?code l.append(23, 42)?><?print l?>') 
    30273027 
    30283028 
    30293029@pytest.mark.ul4 
    30303030def test_method_insert(r): 
    3031     assert '17;23;42;' == r('<?code l = []?><?code l.insert(0, 42)?><?code l.insert(0, 17)?><?code l.insert(1, 23)?><?print l[0]?>;<?print l[1]?>;<?print l[2]?>;') 
     3031    assert '[1, 2, 3, 4]' == r('<?code l = [1,4]?><?code l.insert(1, 2, 3)?><?print l?>') 
    30323032 
    30333033 
     
    30353035def test_method_pop(r): 
    30363036    assert '42;17;23;' == r('<?code l = [17, 23, 42]?><?print l.pop()?>;<?print l.pop(-2)?>;<?print l.pop(0)?>;') 
     3037 
     3038 
     3039@pytest.mark.ul4 
     3040def test_method_update(r): 
     3041    assert '0' == r('<?code d = {}?><?code d.update()?><?print len(d)?>') 
     3042    assert '1' == r('<?code d = {}?><?code d.update([["one", 1]])?><?print d.one?>') 
     3043    assert '1' == r('<?code d = {}?><?code d.update({"one": 1})?><?print d.one?>') 
     3044    assert '1' == r('<?code d = {}?><?code d.update(one=1)?><?print d.one?>') 
     3045    assert '1' == r('<?code d = {}?><?code d.update([["one", 0]], {"one": 0}, one=1)?><?print d.one?>') 
    30373046 
    30383047