From dd4d656aaf0497066af774f256372e11cae6f491 Mon Sep 17 00:00:00 2001 From: keesbos Date: Mon, 30 Nov 2009 15:25:08 +0000 Subject: [PATCH] Added enhanced dict initialization: dict(a=1, b=2) git-svn-id: https://pyjamas.svn.sourceforge.net/svnroot/pyjamas/trunk@2329 7a2bd370-bda8-463c-979e-2900ccfb811e --- CHANGELOG | 2 ++ examples/libtest/DictTest.py | 8 +++++++ pyjs/src/pyjs/builtin/pyjslib.py | 38 +++++++++++++++++++++++++++----- pyjs/src/pyjs/translator.py | 2 +- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 545890c7a..ff7552234 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,6 +1,8 @@ Changes made to Pyjamas since 0.6 --------------------------------- + * Enhanced dict initialization: dict(a=1, b=2) + * Added setFocus function to TextBoxBase to override FocusMixin.setFocus because FocusMixin assumes that you called Focus.createFocusable to create the input box. diff --git a/examples/libtest/DictTest.py b/examples/libtest/DictTest.py index 08b9aee2c..9d2bc549a 100644 --- a/examples/libtest/DictTest.py +++ b/examples/libtest/DictTest.py @@ -98,6 +98,14 @@ def testConstructor(self): # XXX: the other constructors handle javascript objets only, # we need the other constructors too, like: # d = dict({1:1, 2:2}) + d = dict(a=1, b=2) + self.assertEqual(d['a'], 1) + self.assertEqual(d['b'], 2) + d = dict([(1, 1), (2,2)], a=1, b=2) + self.assertEqual(d[1], 1) + self.assertEqual(d[2], 2) + self.assertEqual(d['a'], 1) + self.assertEqual(d['b'], 2) def testIter(self): d = {1: [1,2,3], 2: {'a': 1, 'b': 2, 'c': 3}} diff --git a/pyjs/src/pyjs/builtin/pyjslib.py b/pyjs/src/pyjs/builtin/pyjslib.py index 7a58b6a55..93d142a03 100644 --- a/pyjs/src/pyjs/builtin/pyjslib.py +++ b/pyjs/src/pyjs/builtin/pyjslib.py @@ -4052,12 +4052,14 @@ def __rmul__(self, n): tuple = Tuple class Dict: - def __init__(self, data=JS("[]")): + def __init__(self, seq=JS("[]"), **kwargs): + self.__object = JS("{}") # Transform data into an array with [key,value] and add set self.__object # Input data can be Array(key, val), iteratable (key,val) or Object/Function - JS(""" + def init(data): + JS(""" var item, i, n, sKey; - self.__object = {}; + //self.__object = {}; if (data === null) { throw pyjslib['TypeError']("'NoneType' is not iterable"); @@ -4134,6 +4136,9 @@ def __init__(self, data=JS("[]")): } return null; """) + init(seq) + if kwargs: + init(kwargs) def __hash__(self): raise TypeError("dict objects are unhashable") @@ -4376,17 +4381,38 @@ def __repr__(self): s += "}"; return s; """) + + def toString(self): + return self.__repr__() + JS("pyjslib.Dict.has_key = pyjslib.Dict.__contains__;") JS("pyjslib.Dict.iterkeys = pyjslib.Dict.__iter__;") JS("pyjslib.Dict.__str__ = pyjslib.Dict.__repr__;") -JS("pyjslib.Dict.toString = pyjslib.Dict.__str__;") dict = Dict +# __empty_dict is used in kwargs initialization +# There must me a temporary __init__ function used to prevent infinite +# recursion +def __empty_dict(): + JS(""" + var dict__init__ = pyjslib.dict.__init__; + var d; + pyjslib.dict.__init__ = function() { + this.__object = {}; + } + d = pyjslib.dict(); + d.__init__ = pyjslib.dict.__init__ = dict__init__; + return d; +""") + + class set(object): def __init__(self, data=JS("[]")): - # Transform data into an array with [key,value] and add set self.__object - # Input data can be Array(key, val), iteratable (key,val) or Object/Function + # Transform data into an array with [key,value] and add set + # self.__object + # Input data can be Array(key, val), iteratable (key,val) or + # Object/Function if isSet(data): JS(""" self.__object = {}; diff --git a/pyjs/src/pyjs/translator.py b/pyjs/src/pyjs/translator.py index 6bf61cf47..fd88206df 100644 --- a/pyjs/src/pyjs/translator.py +++ b/pyjs/src/pyjs/translator.py @@ -1697,7 +1697,7 @@ def _default_args_handler(self, node, arg_names, current_klass, kwargname, revargs.reverse() print >> output, """\ %(s)sif (typeof %(k)s == 'undefined') { -%(s)s\t%(k)s = pyjslib['Dict']({});\ +%(s)s\t%(k)s = pyjslib['__empty_dict']();\ """ % {'s': self.spacing(), 'k': kwargname} for v in revargs: print >> output, """\