From f0cefa72325cabcd2bff1952816a84a380dc6e2f Mon Sep 17 00:00:00 2001 From: Arthur Maniet Date: Wed, 4 Feb 2015 13:45:19 +0100 Subject: [ADD] py.js: len() and truthiness on lists, tuples and dicts * use new support to colorize reconciled bank statement lines * fixes tokenization of ``is not`` to not blow up on ``not foo`` * improves calling code to get truthiness of evaluated expression instead of truthiness of javascript-equivalent objects: an empty list is falsy in Python but truthy in javascript, since the evaluated expression is in Python one would expect Python truthiness rules without having to manually convert to boolean closes #5070 --- addons/web/static/lib/py.js/README.rst | 2 +- addons/web/static/lib/py.js/TODO.rst | 1 - addons/web/static/lib/py.js/lib/py.js | 14 +++++++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) (limited to 'addons/web/static/lib/py.js') diff --git a/addons/web/static/lib/py.js/README.rst b/addons/web/static/lib/py.js/README.rst index 7601318df6d..5380f3e6c8f 100644 --- a/addons/web/static/lib/py.js/README.rst +++ b/addons/web/static/lib/py.js/README.rst @@ -53,7 +53,7 @@ Builtins Same as tuple (``list`` is currently an alias for ``tuple``) ``dict`` - Implements trivial getting and setting, nothing beyond that. + Implements trivial getting, setting and len, nothing beyond that. Note that most methods are probably missing from all of these. diff --git a/addons/web/static/lib/py.js/TODO.rst b/addons/web/static/lib/py.js/TODO.rst index a1309bd3fc5..dd3237e3572 100644 --- a/addons/web/static/lib/py.js/TODO.rst +++ b/addons/web/static/lib/py.js/TODO.rst @@ -35,7 +35,6 @@ Base methods requirement * ``__getattr__`` * ``dict.get`` -* ``__len__`` In datamodel, not implemented in any type, untested *************************************************** diff --git a/addons/web/static/lib/py.js/lib/py.js b/addons/web/static/lib/py.js/lib/py.js index 2c738dc95d1..2c6c832f9fe 100644 --- a/addons/web/static/lib/py.js/lib/py.js +++ b/addons/web/static/lib/py.js/lib/py.js @@ -320,7 +320,7 @@ var py = {}; if (token === 'in' && tokens[tokens.length-1].id === 'not') { symbol = symbols['not in']; tokens.pop(); - } else if (token === 'not' && tokens[tokens.length-1].id === 'is') { + } else if (token === 'not' && tokens.length > 0 && tokens[tokens.length-1].id === 'is') { symbol = symbols['is not']; tokens.pop(); } else { @@ -969,6 +969,12 @@ var py = {}; __init__: function () { this._values = []; }, + __len__: function () { + return this._values.length; + }, + __nonzero__: function () { + return py.PY_size(this) > 0 ? py.True : py.False; + }, __contains__: function (value) { for(var i=0, len=this._values.length; i 0 ? py.True : py.False; + }, get: function () { var args = py.PY_parseArgs(arguments, ['k', ['d', py.None]]); var h = args.k.__hash__(); -- cgit v1.2.3