Skip to content

Commit 72623e2

Browse files
author
hartsantler
committed
fixed list comprehensions and set implementation
1 parent 4242758 commit 72623e2

5 files changed

Lines changed: 172 additions & 48 deletions

File tree

pythonjs.js

Lines changed: 106 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// PythonScript Runtime - regenerated on: Tue Nov 26 17:16:46 2013
1+
// PythonScript Runtime - regenerated on: Wed Nov 27 17:19:14 2013
22
__NULL_OBJECT__ = Object.create(null);
33
if (( "window" ) in this && ( "document" ) in this) {
44
__NODEJS__ = false;
@@ -1101,6 +1101,17 @@ _setup_array_prototype = function(args, kwargs) {
11011101
func.kwargs_signature = {};
11021102
func.types_signature = {};
11031103
Array.prototype.append=func;
1104+
var func = function(item) {
1105+
var index;
1106+
index = this.indexOf(item);
1107+
this.splice(index,1);
1108+
}
1109+
1110+
func.NAME = "func";
1111+
func.args_signature = ["item"];
1112+
func.kwargs_signature = {};
1113+
func.types_signature = {};
1114+
Array.prototype.remove=func;
11041115
var func = function(x, low, high) {
11051116
var high, a, low, mid;
11061117
if (( low ) === undefined) {
@@ -1126,6 +1137,43 @@ _setup_array_prototype = function(args, kwargs) {
11261137
func.kwargs_signature = {};
11271138
func.types_signature = {};
11281139
Array.prototype.bisect=func;
1140+
var func = function(other) {
1141+
return this.filter((function (i) {return other.indexOf(i) == -1}));
1142+
}
1143+
1144+
func.NAME = "func";
1145+
func.args_signature = ["other"];
1146+
func.kwargs_signature = {};
1147+
func.types_signature = {};
1148+
Array.prototype.difference=func;
1149+
var func = function(other) {
1150+
return this.filter((function (i) {return other.indexOf(i) != -1}));
1151+
}
1152+
1153+
func.NAME = "func";
1154+
func.args_signature = ["other"];
1155+
func.kwargs_signature = {};
1156+
func.types_signature = {};
1157+
Array.prototype.intersection=func;
1158+
var func = function(other) {
1159+
var iter = this;
1160+
1161+
if (! (iter instanceof Array) ) { iter = __object_keys__(iter) }
1162+
for (var item=0; item < iter.length; item++) {
1163+
var backup = item; item = iter[item];
1164+
if (( other.indexOf(item) ) == -1) {
1165+
return false;
1166+
}
1167+
item = backup;
1168+
}
1169+
return true;
1170+
}
1171+
1172+
func.NAME = "func";
1173+
func.args_signature = ["other"];
1174+
func.kwargs_signature = {};
1175+
func.types_signature = {};
1176+
Array.prototype.issubset=func;
11291177
}
11301178

11311179
_setup_array_prototype.NAME = "_setup_array_prototype";
@@ -1614,6 +1662,25 @@ __tuple___len__.kwargs_signature = { };
16141662
__tuple___len__.types_signature = { };
16151663
__tuple___len__.pythonscript_function = true;
16161664
__tuple_attrs["__len__"] = __tuple___len__;
1665+
__tuple_length__getprop__ = function(args, kwargs) {
1666+
if (args instanceof Array && {}.toString.call(kwargs) === '[object Object]' && ( arguments.length ) == 2) {
1667+
/*pass*/
1668+
} else {
1669+
args = Array.prototype.slice.call(arguments);
1670+
kwargs = Object();
1671+
}
1672+
var signature, arguments;
1673+
signature = {"kwargs": Object(), "args": __create_array__("self")};
1674+
arguments = get_arguments(signature, args, kwargs);
1675+
var self = arguments['self'];
1676+
return self["$wrapped"].length;
1677+
}
1678+
1679+
__tuple_length__getprop__.NAME = "__tuple_length__getprop__";
1680+
__tuple_length__getprop__.args_signature = ["self"];
1681+
__tuple_length__getprop__.kwargs_signature = { };
1682+
__tuple_length__getprop__.types_signature = { };
1683+
__tuple_length__getprop__.pythonscript_function = true;
16171684
__tuple_index = function(args, kwargs) {
16181685
if (args instanceof Array && {}.toString.call(kwargs) === '[object Object]' && ( arguments.length ) == 2) {
16191686
/*pass*/
@@ -1714,6 +1781,8 @@ __tuple___contains__.kwargs_signature = { };
17141781
__tuple___contains__.types_signature = { };
17151782
__tuple___contains__.pythonscript_function = true;
17161783
__tuple_attrs["__contains__"] = __tuple___contains__;
1784+
__tuple_properties["length"] = Object();
1785+
__tuple_properties["length"]["get"] = __tuple_length__getprop__;
17171786
tuple = create_class("tuple", __tuple_parents, __tuple_attrs, __tuple_properties);
17181787
var list, __list_attrs, __list_parents;
17191788
__list_attrs = Object();
@@ -2146,6 +2215,25 @@ __list___len__.kwargs_signature = { };
21462215
__list___len__.types_signature = { };
21472216
__list___len__.pythonscript_function = true;
21482217
__list_attrs["__len__"] = __list___len__;
2218+
__list_length__getprop__ = function(args, kwargs) {
2219+
if (args instanceof Array && {}.toString.call(kwargs) === '[object Object]' && ( arguments.length ) == 2) {
2220+
/*pass*/
2221+
} else {
2222+
args = Array.prototype.slice.call(arguments);
2223+
kwargs = Object();
2224+
}
2225+
var signature, arguments;
2226+
signature = {"kwargs": Object(), "args": __create_array__("self")};
2227+
arguments = get_arguments(signature, args, kwargs);
2228+
var self = arguments['self'];
2229+
return self["$wrapped"].length;
2230+
}
2231+
2232+
__list_length__getprop__.NAME = "__list_length__getprop__";
2233+
__list_length__getprop__.args_signature = ["self"];
2234+
__list_length__getprop__.kwargs_signature = { };
2235+
__list_length__getprop__.types_signature = { };
2236+
__list_length__getprop__.pythonscript_function = true;
21492237
__list___contains__ = function(args, kwargs) {
21502238
if (args instanceof Array && {}.toString.call(kwargs) === '[object Object]' && ( arguments.length ) == 2) {
21512239
/*pass*/
@@ -2171,6 +2259,8 @@ __list___contains__.kwargs_signature = { };
21712259
__list___contains__.types_signature = { };
21722260
__list___contains__.pythonscript_function = true;
21732261
__list_attrs["__contains__"] = __list___contains__;
2262+
__list_properties["length"] = Object();
2263+
__list_properties["length"]["get"] = __list_length__getprop__;
21742264
list = create_class("list", __list_parents, __list_attrs, __list_properties);
21752265
var dict, __dict_attrs, __dict_parents;
21762266
__dict_attrs = Object();
@@ -2550,7 +2640,7 @@ __dict___iter__.pythonscript_function = true;
25502640
__dict_attrs["__iter__"] = __dict___iter__;
25512641
dict = create_class("dict", __dict_parents, __dict_attrs, __dict_properties);
25522642
set = function(args, kwargs) {
2553-
var h, arr;
2643+
var s;
25542644
if (args instanceof Array && {}.toString.call(kwargs) === '[object Object]' && ( arguments.length ) == 2) {
25552645
/*pass*/
25562646
} else {
@@ -2561,38 +2651,25 @@ set = function(args, kwargs) {
25612651
signature = {"kwargs": Object(), "args": __create_array__("a")};
25622652
arguments = get_arguments(signature, args, kwargs);
25632653
var a = arguments['a'];
2564-
arr = [];
2565-
h = Object.create(null);
2654+
"\n Python docs say that set are unordered, yet when created from a list, \n it always moves the last item to the second element.\n ";
2655+
s = [];
25662656
if (isinstance(a, list)) {
2567-
var iter = a["$wrapped"];
2568-
2569-
if (! (iter instanceof Array) ) { iter = __object_keys__(iter) }
2570-
for (var item=0; item < iter.length; item++) {
2571-
var backup = item; item = iter[item];
2572-
if (( item ) in h || Object.hasOwnProperty.call(h, "__contains__") && h["__contains__"](item)) {
2573-
continue;
2574-
} else {
2575-
h[ item ] = true;
2576-
arr.push(item);
2577-
}
2578-
item = backup;
2579-
}
2657+
b = a["$wrapped"].slice();
25802658
} else {
2581-
var iter = a;
2659+
b = a.slice();
2660+
}
2661+
b.splice(1,0,b[ b.length - 1 ]);
2662+
var iter = b;
25822663

2583-
if (! (iter instanceof Array) ) { iter = __object_keys__(iter) }
2584-
for (var item=0; item < iter.length; item++) {
2585-
var backup = item; item = iter[item];
2586-
if (( item ) in h || Object.hasOwnProperty.call(h, "__contains__") && h["__contains__"](item)) {
2587-
continue;
2588-
} else {
2589-
h[ item ] = true;
2590-
arr.push(item);
2591-
}
2592-
item = backup;
2664+
if (! (iter instanceof Array) ) { iter = __object_keys__(iter) }
2665+
for (var item=0; item < iter.length; item++) {
2666+
var backup = item; item = iter[item];
2667+
if (( s.indexOf(item) ) == -1) {
2668+
s.push(item);
25932669
}
2670+
item = backup;
25942671
}
2595-
return arr;
2672+
return s;
25962673
}
25972674

25982675
set.NAME = "set";

pythonjs/python_to_pythonjs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ def visit_ListComp(self, node):
404404
writer.write('var( %s )' %','.join(a) )
405405

406406
generators = list( node.generators )
407+
generators.reverse()
407408
self._gen_comp( generators, node )
408409

409410
self._comprehensions.remove( node )
@@ -1983,7 +1984,6 @@ def visit_ListComp(self, node):
19831984
def collect_comprehensions(node):
19841985
CollectComprehensions._comps_ = comps = []
19851986
CollectComprehensions().visit( node )
1986-
assert comps
19871987
return comps
19881988

19891989

runtime/builtins.py

Lines changed: 45 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,11 @@ def func(start, stop, step):
358358
def func(item):
359359
this.push( item )
360360

361+
@Array.prototype.remove
362+
def func(item):
363+
index = this.indexOf( item )
364+
this.splice(index, 1)
365+
361366
@Array.prototype.bisect
362367
def func(x, low, high):
363368
if low is None: low = 0
@@ -371,6 +376,23 @@ def func(x, low, high):
371376
low = mid + 1
372377
return low
373378

379+
## set-like features ##
380+
## `-` operator
381+
@Array.prototype.difference
382+
def func(other):
383+
return this.filter( lambda i: other.indexOf(i)==-1)
384+
## `&` operator
385+
@Array.prototype.intersection
386+
def func(other):
387+
return this.filter( lambda i: other.indexOf(i)!=-1)
388+
## `<=` operator
389+
@Array.prototype.issubset
390+
def func(other):
391+
for item in this:
392+
if other.indexOf(item) == -1:
393+
return False
394+
return True
395+
374396
_setup_array_prototype()
375397

376398
def bisect(a, x, low=None, high=None):
@@ -499,6 +521,11 @@ def __len__(self):
499521
with javascript:
500522
return self[...].length
501523

524+
@property
525+
def length(self):
526+
with javascript:
527+
return self[...].length
528+
502529
def index(self, obj):
503530
with javascript:
504531
return self[...].indexOf(obj)
@@ -624,6 +651,11 @@ def __len__(self):
624651
with javascript:
625652
return self[...].length
626653

654+
@property
655+
def length(self):
656+
with javascript:
657+
return self[...].length
658+
627659
def __contains__(self, value):
628660
with javascript:
629661
if self[...].indexOf(value) == -1:
@@ -780,24 +812,20 @@ def __iter__(self):
780812

781813

782814
def set(a):
815+
'''
816+
Python docs say that set are unordered, yet when created from a list,
817+
it always moves the last item to the second element.
818+
'''
783819
with javascript:
784-
arr = []
785-
h = Object.create(null)
786-
if isinstance(a, list):
787-
for item in a[...]:
788-
if item in h:
789-
continue
790-
else:
791-
h[item] = True
792-
arr.push( item )
793-
else:
794-
for item in a:
795-
if item in h:
796-
continue
797-
else:
798-
h[item] = True
799-
arr.push( item )
800-
return arr
820+
s = []
821+
if isinstance(a, list): b = a[...].slice()
822+
else: b = a.slice()
823+
b.splice(1, 0, b[b.length-1])
824+
for item in b:
825+
if s.indexOf(item) == -1:
826+
s.push( item )
827+
return s
828+
801829

802830
def frozenset(a):
803831
return set(a)

tests/test_JSArray.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@
3636
print 'checking JSObject'
3737
print jsob
3838

39+
print 'testing difference'
40+
with javascript:
41+
a = [1,2,3,4]
42+
b = [1,2, 55]
43+
c = a.difference( b )
44+
print c
45+
46+
print 'testing intersection'
47+
d = a.intersection( b )
48+
print d
49+
print len(d)
50+
3951
</script>
4052

4153
</head>

tests/test_list_comprehension.html

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
## note: 'string'.split is javascript function, and returns an array not a list!
2323
print 'testing for char in word for word in words...'
2424
words = 'hello world foo bar'.split(' ')
25-
x = [ ord(c) for c in word for word in words ]
25+
x = [ ord(c) for word in words for c in word ]
2626
for v in x:
2727
print chr(v)
2828

@@ -45,6 +45,13 @@
4545
print len(z)
4646
print len(z[0])
4747

48+
print 'testing nested3'
49+
w = 5
50+
h = 10
51+
u = [x+y for y in range(h) for x in range(w)]
52+
print u
53+
if u[5] != 1:
54+
print 'ERROR - u[5] is not 1', u[5]
4855

4956
print 'test complete'
5057

0 commit comments

Comments
 (0)