diff options
| author | Arthur Maniet <arthurmaniet@me.com> | 2015-02-04 13:45:19 +0100 |
|---|---|---|
| committer | Xavier Morel <xmo@odoo.com> | 2015-02-05 13:08:15 +0100 |
| commit | f0cefa72325cabcd2bff1952816a84a380dc6e2f (patch) | |
| tree | 0d11938b1c0376d6085e234df522d67879dc43db /addons/web/static/lib/py.js | |
| parent | 691254cbfe0d4c64762d2d1c2c5e3b0ad2c5423b (diff) | |
[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
Diffstat (limited to 'addons/web/static/lib/py.js')
| -rw-r--r-- | addons/web/static/lib/py.js/README.rst | 2 | ||||
| -rw-r--r-- | addons/web/static/lib/py.js/TODO.rst | 1 | ||||
| -rw-r--r-- | addons/web/static/lib/py.js/lib/py.js | 14 |
3 files changed, 14 insertions, 3 deletions
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<len; ++i) { if (py.PY_isTrue(this._values[i].__eq__(value))) { @@ -1013,6 +1019,12 @@ var py = {}; __setitem__: function (key, value) { this._store[key.__hash__()] = [key, value]; }, + __len__: function () { + return Object.keys(this._store).length + }, + __nonzero__: function () { + return py.PY_size(this) > 0 ? py.True : py.False; + }, get: function () { var args = py.PY_parseArgs(arguments, ['k', ['d', py.None]]); var h = args.k.__hash__(); |
