Permalink
Browse files

allow any object type to be used to contruct a dict litteral, and oth…

…er improvements to dict class.
  • Loading branch information...
hartsantler committed Oct 10, 2013
1 parent b5fe7f0 commit d59559ede7ba846c31fb294f80234cbd3fd7f25a
Showing with 230 additions and 52 deletions.
  1. +110 −29 pythonscript.js
  2. +11 −4 pythonscript/python_to_pythonjs.py
  3. +57 −18 runtime/builtins.py
  4. +1 −1 tests/test_dict.html
  5. +51 −0 tests/test_dict_advanced.html
View
@@ -1,4 +1,4 @@
// PythonScript Runtime - regenerated on: Wed Oct 9 19:22:57 2013
// PythonScript Runtime - regenerated on: Wed Oct 9 20:55:21 2013
var jsrange = function(num) {
"Emulates Python's range function";
var i, r;
@@ -1066,7 +1066,23 @@ arguments = get_arguments(signature, args, kwargs);
var self = arguments['self'];
var js_object = arguments['js_object'];
if(js_object) {
if(js_object instanceof Array) {
self["__dict__"]["js_object"] = Object.create(null);
i = 0;
while(i < get_attribute(js_object, "length")) {
var key = js_object[i]["key"];
var value = js_object[i]["value"];
var __args_8, __kwargs_8;
__args_8 = create_array(key, value);
__kwargs_8 = Object();
get_attribute(get_attribute(self, "set"), "__call__")(__args_8, __kwargs_8);
i += 1
}
}
else {
self["__dict__"]["js_object"] = js_object;
}
}
else {
self["__dict__"]["js_object"] = Object();
@@ -1080,18 +1096,37 @@ window["__dict_attrs"]["__init__"] = __dict___init__;
var __dict_get = function(args, kwargs) {
var __dict;
var signature, arguments;
signature = {"kwargs": Object(), "args": create_array("self", "key", "d")};
signature = {"kwargs": {"_default": undefined}, "args": create_array("self", "key", "_default")};
arguments = get_arguments(signature, args, kwargs);
var self = arguments['self'];
var key = arguments['key'];
var d = arguments['d'];
var __dict;
var _default = arguments['_default'];
__dict = self["__dict__"]["js_object"];
if(__dict[key]) {
if(typeof(key) === 'object') {
var uid = "@"+key.uid;
if(uid in __dict) {
return __dict[uid];
}
}
else {
if(typeof(key) === 'function') {
var uid = "@"+key.uid;
if(uid in __dict) {
return __dict[uid];
}
}
else {
if(key in __dict) {
return __dict[key];
}
return d;
}
}
return _default;
}
window["__dict_get"] = __dict_get
@@ -1105,9 +1140,34 @@ arguments = get_arguments(signature, args, kwargs);
var self = arguments['self'];
var key = arguments['key'];
var value = arguments['value'];
var __dict;
__dict = self["__dict__"]["js_object"];
if(typeof(key) === 'object') {
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 {
if(typeof(key) === 'function') {
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_set"] = __dict_set
@@ -1119,31 +1179,13 @@ var signature, arguments;
signature = {"kwargs": Object(), "args": create_array("self")};
arguments = get_arguments(signature, args, kwargs);
var self = arguments['self'];
var __dict;
__dict = self["__dict__"]["js_object"];
return Object.keys(__dict).length;
}
window["__dict___len__"] = __dict___len__
__dict___len__.pythonscript_function = true;
window["__dict_attrs"]["__len__"] = __dict___len__;
var __dict_keys = function(args, kwargs) {
var __dict, __keys, out;
var signature, arguments;
signature = {"kwargs": Object(), "args": create_array("self")};
arguments = get_arguments(signature, args, kwargs);
var self = arguments['self'];
var __dict, out;
__dict = self["__dict__"]["js_object"];
__keys = Object.keys(__dict);
out = get_attribute(list, "__call__")(create_array(), Object());
set_attribute(out, "js_object", __keys);
return out;
}
window["__dict_keys"] = __dict_keys
__dict_keys.pythonscript_function = true;
window["__dict_attrs"]["keys"] = __dict_keys;
var __dict___getitem__ = function(args, kwargs) {
var __dict;
var signature, arguments;
@@ -1213,6 +1255,45 @@ window["__dict___setitem__"] = __dict___setitem__
__dict___setitem__.pythonscript_function = true;
window["__dict_attrs"]["__setitem__"] = __dict___setitem__;
var __dict_keys = function(args, kwargs) {
var __dict, __keys, out;
var signature, arguments;
signature = {"kwargs": Object(), "args": create_array("self")};
arguments = get_arguments(signature, args, kwargs);
var self = arguments['self'];
__dict = self["__dict__"]["js_object"];
__keys = Object.keys(__dict);
out = get_attribute(list, "__call__")(create_array(), Object());
set_attribute(out, "js_object", __keys);
return out;
}
window["__dict_keys"] = __dict_keys
__dict_keys.pythonscript_function = true;
window["__dict_attrs"]["keys"] = __dict_keys;
var __dict_values = function(args, kwargs) {
var __dict, __keys, i, out;
var signature, arguments;
signature = {"kwargs": Object(), "args": create_array("self")};
arguments = get_arguments(signature, args, kwargs);
var self = arguments['self'];
__dict = self["__dict__"]["js_object"];
__keys = Object.keys(__dict);
out = get_attribute(list, "__call__")(create_array(), Object());
i = 0;
while(i < get_attribute(__keys, "length")) {
var __args_9, __kwargs_9;
__args_9 = create_array(__dict[ __keys[i] ]);
__kwargs_9 = Object();
get_attribute(get_attribute(out, "append"), "__call__")(__args_9, __kwargs_9);
i += 1
}
return out;
}
window["__dict_values"] = __dict_values
__dict_values.pythonscript_function = true;
window["__dict_attrs"]["values"] = __dict_values;
dict = create_class("dict", window["__dict_parents"], window["__dict_attrs"]);
var str, __str_attrs, __str_parents;
window["__str_attrs"] = Object();
@@ -1234,10 +1315,10 @@ var signature, arguments;
signature = {"kwargs": Object(), "args": create_array("self")};
arguments = get_arguments(signature, args, kwargs);
var self = arguments['self'];
var __args_8, __kwargs_8;
__args_8 = create_array(self["__dict__"]["jsstring"], 0);
__kwargs_8 = Object();
return get_attribute(Iterator, "__call__")(__args_8, __kwargs_8);
var __args_10, __kwargs_10;
__args_10 = create_array(self["__dict__"]["jsstring"], 0);
__kwargs_10 = Object();
return get_attribute(Iterator, "__call__")(__args_10, __kwargs_10);
}
window["__str___iter__"] = __str___iter__
@@ -240,10 +240,17 @@ def visit_Assert(self, node):
def visit_Dict(self, node):
node.returns_type = 'dict'
keys = [x.s for x in node.keys]
values = map(self.visit, node.values)
a = [ '%s=%s'%x for x in zip(keys, values) ]
b = 'JSObject(%s)' %', '.join(a)
#keys = [x.s for x in node.keys]
#values = map(self.visit, node.values)
#a = [ '%s=%s'%x for x in zip(keys, values) ]
#b = 'JSObject(%s)' %', '.join(a)
#return 'get_attribute(dict, "__call__")([], JSObject(js_object=%s))' %b
a = []
for i in range( len(node.keys) ):
k = self.visit( node.keys[ i ] )
v = self.visit( node.values[i] )
a.append( 'JSObject(key=%s, value=%s)'%(k,v) )
b = '[%s]' %', '.join(a)
return 'get_attribute(dict, "__call__")([], JSObject(js_object=%s))' %b
def visit_Tuple(self, node):
View
@@ -175,37 +175,59 @@ class dict:
# http://stackoverflow.com/questions/10858632/are-functions-valid-keys-for-javascript-object-properties
UID = 0
def __init__(self, js_object=None):
#self.js_object = JS('Object.create(null)')
if js_object:
self.js_object = js_object
if JS("js_object instanceof Array"):
self.js_object = JS('Object.create(null)')
i = 0
while i < js_object.length:
JS('var key = js_object[i]["key"]')
JS('var value = js_object[i]["value"]')
self.set(key, value)
i += 1
else:
self.js_object = js_object
else:
self.js_object = JSObject()
def get(self, key, d):
var(__dict)
def get(self, key, _default=None):
__dict = self.js_object
if JS('__dict[key]'):
return JS('__dict[key]')
return d
if JS("typeof(key) === 'object'"):
JS('var uid = "@"+key.uid') ## gotcha - what if "@undefined" was in __dict ?
if JS('uid in __dict'):
return JS('__dict[uid]')
elif JS("typeof(key) === 'function'"):
JS('var uid = "@"+key.uid')
if JS('uid in __dict'):
return JS('__dict[uid]')
else:
if JS('key in __dict'):
return JS('__dict[key]')
return _default
def set(self, key, value):
var(__dict)
__dict = self.js_object
JS('__dict[key] = value')
if JS("typeof(key) === 'object'"):
if JS("key.uid === undefined"):
uid = self.UID
JS("key.uid = uid")
self.UID += 1
JS('var uid = key.uid')
JS('__dict["@"+uid] = value')
elif JS("typeof(key) === 'function'"):
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')
def __len__(self):
var(__dict)
__dict = self.js_object
return JS('Object.keys(__dict).length')
def keys(self):
var(__dict, out)
__dict = self.js_object
__keys = JS('Object.keys(__dict)')
out = list()
out.js_object = __keys
return out
def __getitem__(self, key):
__dict = self.js_object
if JS("typeof(key) === 'object'"):
@@ -236,6 +258,23 @@ def __setitem__(self, key, value):
else:
JS('__dict[key] = value')
def keys(self):
__dict = self.js_object
__keys = JS('Object.keys(__dict)')
out = list()
out.js_object = __keys
return out
def values(self):
__dict = self.js_object
__keys = JS('Object.keys(__dict)')
out = list()
i = 0
while i < __keys.length:
out.append( JS('__dict[ __keys[i] ]') )
i += 1
return out
class str:
View
@@ -71,7 +71,7 @@
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 'int-key:1', e2[1]
print 'dict-key:e1', e2[ e1 ]
e2[ 'mykey' ] = 'simple string key'
@@ -0,0 +1,51 @@
<html>
<head>
<script src="pythonscript.js"></script>
<script type="text/python" closure="false">
class Key1:
def foo(self):
print('FOO')
class Key2:
def bar(self):
print('BAR')
def test():
global d, k1, k2
k1 = Key1()
k2 = Key2()
d = {
k1:'k1 instance',
k2:'k2 instance',
Key1: 'K1 class',
Key2: 'K2 class',
Key1(): 'lost instance!'
}
print d[k1]
print d[k2]
print d[Key1]
print d[Key2]
print d['@4'] ## this is cheating!
print d.get('get default', 'My Default')
print 'this should be undefined:', d.get('get undefined')
d.set(k1, 'changed k1')
print d[k1]
print d.get( k1 )
print d.keys()
print d.values()
</script>
</head>
<body>
<button onclick="test()">click me</button>
</body>
</html>

0 comments on commit d59559e

Please sign in to comment.