Skip to content

Commit

Permalink
Added enhanced dict initialization: dict(a=1, b=2)
Browse files Browse the repository at this point in the history
git-svn-id: https://pyjamas.svn.sourceforge.net/svnroot/pyjamas/trunk@2329 7a2bd370-bda8-463c-979e-2900ccfb811e
  • Loading branch information
keesbos committed Nov 30, 2009
1 parent 5a58288 commit dd4d656
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
2 changes: 2 additions & 0 deletions 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.
Expand Down
8 changes: 8 additions & 0 deletions examples/libtest/DictTest.py
Expand Up @@ -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}}
Expand Down
38 changes: 32 additions & 6 deletions pyjs/src/pyjs/builtin/pyjslib.py
Expand Up @@ -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");
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 = {};
Expand Down
2 changes: 1 addition & 1 deletion pyjs/src/pyjs/translator.py
Expand Up @@ -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, """\
Expand Down

0 comments on commit dd4d656

Please sign in to comment.