Permalink
Browse files

any type can now be used dictionary keys, including lists and dicts.

  • Loading branch information...
hartsantler committed Oct 10, 2013
1 parent 56f2602 commit f3aa88ffeeb42b6bd8639a222005b45e733160ad
Showing with 101 additions and 5 deletions.
  1. +24 −1 pythonscript.js
  2. +18 −3 runtime/builtins.py
  3. +59 −1 tests/test_dict.html
View
@@ -1,4 +1,4 @@
// PythonScript Runtime - regenerated on: Wed Oct 9 08:04:44 2013
// PythonScript Runtime - regenerated on: Wed Oct 9 19:04:53 2013
var jsrange = function(num) {
"Emulates Python's range function";
var i, r;
@@ -1057,6 +1057,8 @@ list = create_class("list", window["__list_parents"], window["__list_attrs"]);
var dict, __dict_attrs, __dict_parents;
window["__dict_attrs"] = Object();
window["__dict_parents"] = create_array();
__dict_UID = 0;
window["__dict_attrs"]["UID"] = __dict_UID;
var __dict___init__ = function(args, kwargs) {
var signature, arguments;
signature = {"kwargs": {"js_object": undefined}, "args": create_array("self", "js_object")};
@@ -1150,7 +1152,14 @@ arguments = get_arguments(signature, args, kwargs);
var self = arguments['self'];
var key = arguments['key'];
__dict = self["__dict__"]["js_object"];
if(typeof(key) === 'object') {
var uid = key.uid;
return __dict["@"+uid];
}
else {
return __dict[key];
}
}
window["__dict___getitem__"] = __dict___getitem__
@@ -1165,7 +1174,21 @@ var self = arguments['self'];
var key = arguments['key'];
var value = arguments['value'];
__dict = self["__dict__"]["js_object"];
if(typeof(key) === 'object') {
console.log("setitem-object->", key);
if(key.uid === undefined) {
uid = self["__class__"]["__dict__"]["UID"];
key.uid = uid;
self["__class__"]["__dict__"]["UID"] += 1
}
var uid = key.uid;
__dict["@"+uid] = value;
}
else {
__dict[key] = value;
}
}
window["__dict___setitem__"] = __dict___setitem__
View
@@ -170,8 +170,10 @@ def __len__(self):
class dict:
# http://stackoverflow.com/questions/10892322/javascript-hashtable-use-object-key
UID = 0
def __init__(self, js_object=None):
#self.js_object = JS('Object.create(null)')
if js_object:
self.js_object = js_object
else:
@@ -204,11 +206,24 @@ def keys(self):
def __getitem__(self, key):
__dict = self.js_object
return JS('__dict[key]')
if JS("typeof(key) === 'object'"):
JS('var uid = key.uid')
return JS('__dict["@"+uid]') ## "@" is needed so that integers can also be used as keys
else:
return JS('__dict[key]')
def __setitem__(self, key, value):
__dict = self.js_object
JS('__dict[key] = value')
if JS("typeof(key) === 'object'"):
print 'setitem-object->', key
if JS("key.uid === undefined"):
uid = self.UID
JS("key.uid = uid")
self.UID += 1
JS('var uid = key.uid')
JS('__dict["@"+uid] = value')
else:
JS('__dict[key] = value')
class str:
View
@@ -4,8 +4,15 @@
<script type="text/python" closure="false">
class Key1:
def foo(self):
print('FOO')
class Key2:
def bar(self):
print('BAR')
def test():
global d, l
d = dict(a=1, b=2) ## functional style
print( d['a'] )
@@ -34,6 +41,57 @@
e2['B'] = 2
print e1, e2
k1 = Key1()
k2 = Key2()
e1[ k1 ] = 'foo'
e1[ k2 ] = 'bar'
print 'k1', e1[k1]
print 'k2', e1[k2]
e1[ k1 ] = 'F'
e1[ k2 ] = 'B'
print 'k1', e1[k1]
print 'k2', e1[k2]
print( e1 )
e2[ k1 ] = 'key1 in e2'
e2[ k2 ] = 'key2 in e2'
print 'k1', e2[k1]
print 'k2', e2[k2]
e2[ 0 ] = 'key zero'
e2[ 1 ] = 'key one'
e2[ e1 ] = "in python you can't use a dict as a key!" ## in PythonScript we allow this
print 'int-key:0', e2[0]
print 'int-key:1', e2[0]
print 'dict-key:e1', e2[ e1 ]
e2[ 'mykey' ] = 'simple string key'
print 'sting key', e2[ 'mykey' ]
s = 'XXX'
e2[ s ] = 'string var key'
print 'string var key', e2[s]
list1 = [1,2,3]
list2 = [1,2,3]
e1[ list1 ] = 'list1' ## not allowed in python, in PythonScript it is allowed
e1[ list2 ] = 'list2'
print e1[ list1 ]
print e1[ list2 ]
list1.append( 4 )
list2.append( 5 )
print e1[ list1 ]
print e1[ list2 ]
e1[ 1.333 ] = 'myfloat'
print e1[ 1.333 ]
</script>
</head>

0 comments on commit f3aa88f

Please sign in to comment.