Skip to content

Commit 70a2d41

Browse files
committed
Fixed key-less sort so that numbers automatically sort properly
Created copy module and implemented copy and deepcopy Removed broken copy and deepcopy functions from builtins Moved dict related autotest from div_issues to dictionaries Added additional autotests for sort and sorted Added module_copy autotest module Created autotests for copy and deepcopy
1 parent 80f80cf commit 70a2d41

File tree

9 files changed

+154
-60
lines changed

9 files changed

+154
-60
lines changed

transcrypt/development/automated_tests/transcrypt/autotest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
if __pragma__ ('defined', 'undefined'):
3939
import module_collections
4040

41+
import module_copy
4142
import module_datetime
4243
import module_itertools
4344
import module_math
@@ -96,6 +97,7 @@
9697
if __pragma__ ('defined', 'undefined'):
9798
autoTester.run (module_collections, 'module_collections')
9899

100+
autoTester.run (module_copy, 'module_copy')
99101
autoTester.run (module_datetime, 'module_datetime')
100102
autoTester.run (module_itertools, 'module_itertools')
101103
autoTester.run (module_math, 'module_math')

transcrypt/development/automated_tests/transcrypt/dictionaries/__init__.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ def run (autoTester):
8989
autoTester.check (tel.pop ('foo', None))
9090

9191
# Check compound keys (issue 281)
92-
9392
d = {}
9493
d ['a'] = 3777
9594
d [(1, 2)] = 4777
@@ -101,7 +100,16 @@ def run (autoTester):
101100
d [(1, 2)] = 4777
102101
autoTester.check (d ['a'], d [(1, 2)])
103102
__pragma__ ('noopov')
104-
103+
104+
# Check dict.popitem (issue 306)
105+
dict_306 = {'Abraham': 'Lincoln', 'Barack': 'O\'Bama', 'Thomas': 'Jefferson'}
106+
results = []
107+
try:
108+
while True:
109+
results.append (list(dict_306.popitem ()))
110+
except Exception as exception:
111+
autoTester.check (sorted (results))
112+
105113
# Check exceptions
106114
knights = {'robin': 'the brave', 'gallahad': 'the pure'}
107115
autoTester.check (

transcrypt/development/automated_tests/transcrypt/div_issues/__init__.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -264,16 +264,6 @@ def filter_word (word0, word1):
264264
autoTester.check (filter_word ('_in_', 'kind'))
265265
autoTester.check (filter_word ('min_', 'kind'))
266266

267-
autoTester.check ('Issue 306')
268-
dict_306 = {'Abraham': 'Lincoln', 'Barack': 'O\'Bama', 'Thomas': 'Jefferson'}
269-
results = []
270-
try:
271-
while True:
272-
results.append (dict_306.popitem ())
273-
except Exception as exception:
274-
autoTester.check (sorted (results))
275-
autoTester.check ('That\'s it')
276-
277267
autoTester.check ('Issue 314')
278268
try:
279269
autoTester.check (int (float (123)))

transcrypt/development/automated_tests/transcrypt/general_functions/__init__.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,36 @@ def run (autoTester):
3535
autoTester.check (len (collection))
3636

3737
autoTester.check ('sort and sorted<br>')
38-
a = [1, 5, 3, 2, -1]
38+
a = [11, 7, 23, 4, -1]
3939
b = ['sun', 'earth', 'moon']
40-
40+
c = [{'name': 'Defg', 'qty': 99}, {'name': 'Abcd', 'qty': 42}, {'name': 'Hijk', 'qty': 11}]
41+
d = [[21, 19, 54], [22, 7, 12, 37]]
42+
43+
autoTester.check (a)
4144
autoTester.check (sorted (a))
45+
autoTester.check (a)
46+
autoTester.check (sorted (a, key=str))
47+
autoTester.check (sorted (a, key=int))
4248
autoTester.check (sorted (b))
43-
49+
50+
autoTester.check (sorted(c, key=lambda k: k['qty']))
51+
autoTester.check (sorted(c, key=lambda k: k['name']))
52+
autoTester.check (sorted(c, key=lambda k: k['name'], reverse=True))
53+
54+
autoTester.check (sorted(d))
55+
autoTester.check (sorted(d, key=sum))
56+
57+
# Make sure sorted is doing a proper shallow copy (issue 866)
58+
e = sorted(d)
59+
autoTester.check(e)
60+
d[1][1] = 9
61+
autoTester.check(d)
62+
autoTester.check(e)
63+
d[1] = [22,14,36]
64+
autoTester.check(d)
65+
autoTester.check(e)
66+
67+
4468
a.sort ()
4569
autoTester.check (a)
4670

@@ -53,6 +77,9 @@ def run (autoTester):
5377
a.sort (reverse = True)
5478
autoTester.check (a)
5579

80+
a.sort (key=int)
81+
autoTester.check (a)
82+
5683
b.sort (reverse = True)
5784
autoTester.check (b)
5885

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import copy
2+
3+
def run (autoTester):
4+
a = {'a': 1, 'b': 2, 'c': 3}
5+
b = copy.copy(a)
6+
c = [[1,2,3],[4,5,6]]
7+
d = copy.copy(c)
8+
e = copy.deepcopy(c)
9+
10+
autoTester.check ('copy')
11+
autoTester.check (b)
12+
autoTester.check (a)
13+
a['b'] = 9
14+
autoTester.check (a)
15+
autoTester.check (b)
16+
17+
autoTester.check ('deepcopy')
18+
autoTester.check (d)
19+
autoTester.check (e)
20+
c[1][1] = 9
21+
autoTester.check (c)
22+
autoTester.check (d)
23+
autoTester.check (e)
Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11

22
def run (autoTester):
3-
autoTester.check (list (filter (lambda x: x % 2 == 0, range (10))))
3+
autoTester.check ('filter',
4+
list (filter (lambda x: x % 2 == 0, range (10))),
5+
)
46

5-
autoTester.check (list (map (lambda x: x* x, range(0, 31, 3))))
6-
autoTester.check (list (map (lambda x, y: x * y, range(0, 31, 3), [1, 2, 3])))
7-
autoTester.check (list (map (lambda x, y, z: x * y + z, range(0, 31, 3), [1, 2, 3, 4], (2, 4, 6))))
7+
autoTester.check ('map',
8+
list (map (lambda x: x* x, range(0, 31, 3))),
9+
list (map (lambda x, y: x * y, range(0, 31, 3), [1, 2, 3])),
10+
list (map (lambda x, y, z: x * y + z, range(0, 31, 3), [1, 2, 3, 4], (2, 4, 6))),
11+
)

transcrypt/modules/copy/__init__.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export function __copy(isDeep, obj) {
2+
var result;
3+
4+
// Handle primitives
5+
if (obj === null || typeof obj !== "object") return obj;
6+
7+
// Handle Date
8+
if (obj instanceof Date) {
9+
result = new Date();
10+
result.setTime(obj.getTime());
11+
return result;
12+
}
13+
14+
// Handle Array
15+
if (obj instanceof Array) {
16+
result = [];
17+
for (var i = 0, len = obj.length; i < len; i++) {
18+
result[i] = isDeep ? __copy(isDeep, obj[i]) : obj[i];
19+
}
20+
return result;
21+
}
22+
23+
// Handle Object
24+
if (obj instanceof Object) {
25+
result = {};
26+
for (var attr in obj) {
27+
if (obj.hasOwnProperty(attr)) result[attr] = isDeep ? __copy(isDeep, obj[attr]) : obj[attr];
28+
}
29+
return result;
30+
}
31+
32+
throw new Error("Copy not implemented for type ".concat(Object.prototype.toString.call(obj)));
33+
}
34+
35+
export function copy(obj) {
36+
return __copy(false, obj);
37+
}
38+
39+
export function deepcopy(obj) {
40+
return __copy(true, obj);
41+
}

transcrypt/modules/org/transcrypt/__builtin__.js

Lines changed: 31 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -686,9 +686,9 @@ function min_max (f_compare, ...args) {
686686
if (args.length > 1 && dflt !== undefined) throw TypeError("Cannot specify a default with multiple positional arguments", new Error ());
687687
if (args.length === 1){
688688
if (Object.prototype.toString.call(args[0]) !== '[object Array]') throw TypeError("object is not iterable", new Error());
689-
args = args[0];
689+
args = args[0]; // Passed in arg is itself an iterable
690690
}
691-
if (args === null || args.length === 0){
691+
if (args.length === 0){
692692
if (dflt === undefined) throw ValueError ("arg is an empty sequence", new Error ());
693693
return dflt
694694
}
@@ -699,13 +699,11 @@ function min_max (f_compare, ...args) {
699699
// Maximum of n values
700700
export function max (...args) {
701701
return min_max(function (a, b){return a > b}, ...args)
702-
// return arguments.length == 1 ? Math.max (...nrOrSeq) : Math.max (...arguments);
703702
}
704703

705704
// Minimum of n numbers
706705
export function min (...args) {
707706
return min_max(function (a, b){return a < b}, ...args)
708-
// return arguments.length == 1 ? Math.min (...nrOrSeq) : Math.min (...arguments);
709707
}
710708

711709
// Integer to binary
@@ -978,35 +976,35 @@ export function enumerate(iterable, start = 0) {
978976

979977
// Shallow and deepcopy
980978

981-
export function copy (anObject) {
982-
if (anObject == null || typeof anObject == "object") {
983-
return anObject;
984-
}
985-
else {
986-
var result = {};
987-
for (var attrib in obj) {
988-
if (anObject.hasOwnProperty (attrib)) {
989-
result [attrib] = anObject [attrib];
990-
}
991-
}
992-
return result;
993-
}
994-
}
995-
996-
export function deepcopy (anObject) {
997-
if (anObject == null || typeof anObject == "object") {
998-
return anObject;
999-
}
1000-
else {
1001-
var result = {};
1002-
for (var attrib in obj) {
1003-
if (anObject.hasOwnProperty (attrib)) {
1004-
result [attrib] = deepcopy (anObject [attrib]);
1005-
}
1006-
}
1007-
return result;
1008-
}
1009-
}
979+
// export function copy (anObject) {
980+
// if (anObject == null || typeof anObject == "object") {
981+
// return anObject;
982+
// }
983+
// else {
984+
// var result = {};
985+
// for (var attrib in obj) {
986+
// if (anObject.hasOwnProperty (attrib)) {
987+
// result [attrib] = anObject [attrib];
988+
// }
989+
// }
990+
// return result;
991+
// }
992+
// }
993+
//
994+
// export function deepcopy (anObject) {
995+
// if (anObject == null || typeof anObject == "object") {
996+
// return anObject;
997+
// }
998+
// else {
999+
// var result = {};
1000+
// for (var attrib in obj) {
1001+
// if (anObject.hasOwnProperty (attrib)) {
1002+
// result [attrib] = deepcopy (anObject [attrib]);
1003+
// }
1004+
// }
1005+
// return result;
1006+
// }
1007+
// }
10101008

10111009
// List extensions to Array
10121010

transcrypt/modules/org/transcrypt/__runtime__.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
#__pragma__ ('js', '{}', __include__ ('org/transcrypt/__builtin__.js'))
66

77
#__pragma__ ('skip')
8-
copy = Math = __typeof__ = __repr__ = document = console = window = 0
8+
Math = __typeof__ = __repr__ = document = console = window = 0
99
#__pragma__ ('noskip')
10+
from copy import copy as _copy
1011

1112
#__pragma__ ('notconv') # !!! tconv gives a problem with __terminal__, needs investigation
1213
#__pragma__ ('nokwargs')
@@ -102,30 +103,30 @@ class RuntimeWarning (Warning):
102103

103104
#__pragma__ ('kwargs')
104105

105-
def __sort__ (iterable, key = None, reverse = False): # Used by py_sort, can deal with kwargs
106+
def __sort__(iterable, key = None, reverse = False): # Used by py_sort, can deal with kwargs
106107
if key:
107108
iterable.sort (lambda a, b: 1 if key (a) > key (b) else -1) # JavaScript sort, case '==' is irrelevant for sorting
108109
else:
109-
iterable.sort () # JavaScript sort
110+
iterable.sort (lambda a, b: 1 if a > b else -1) # JavaScript sort
110111

111112
if reverse:
112113
iterable.reverse ()
113114

114-
def sorted (iterable, key = None, reverse = False):
115+
def sorted(iterable, key = None, reverse = False):
115116
if type (iterable) == dict:
116-
result = copy (iterable.keys ())
117+
result = _copy (iterable.keys ())
117118
else:
118-
result = copy (iterable)
119+
result = _copy (iterable)
119120

120121
__sort__ (result, key, reverse)
121122
return result
122123

123124
#__pragma__ ('nokwargs')
124125

125-
def map (func, *iterables):
126+
def map(func, *iterables):
126127
return [func(*items) for items in zip(*iterables)]
127128

128-
def filter (func, iterable):
129+
def filter(func, iterable):
129130
if func == None:
130131
func = bool
131132
return [item for item in iterable if func (item)]

0 commit comments

Comments
 (0)