Permalink
May 17, 2019
May 17, 2019
Feb 1, 2020
Apr 3, 2019
Apr 3, 2019
Feb 1, 2020
Apr 3, 2019
Apr 3, 2019
Dec 13, 2018
Nov 6, 2019
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Mar 7, 2018
Feb 26, 2018
Feb 1, 2020
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Jul 31, 2018
Jul 31, 2018
Dec 10, 2018
Nov 2, 2018
Jul 31, 2018
Dec 10, 2018
Dec 10, 2018
Nov 28, 2018
Feb 26, 2018
May 27, 2018
Feb 1, 2020
Oct 24, 2018
Feb 3, 2018
Feb 1, 2020
Mar 7, 2018
Mar 7, 2018
Nov 9, 2015
May 17, 2019
Feb 26, 2018
Apr 3, 2019
May 17, 2019
Feb 26, 2018
Apr 3, 2019
Dec 13, 2018
Mar 7, 2018
Mar 7, 2018
Feb 26, 2018
Apr 14, 2019
Dec 13, 2018
May 17, 2019
Feb 26, 2018
Apr 3, 2019
Dec 1, 2018
Jun 28, 2018
Feb 1, 2020
Mar 7, 2018
Aug 2, 2018
Feb 26, 2018
Newer
100644
982 lines (880 sloc)
28.8 KB
10
var set_ops = ["eq", "add", "sub", "and", "or", "xor", "le", "lt", "ge", "gt"]
11
12
$B.make_view = function(name, set_like){
13
var klass = $B.make_class(name, function(items){
14
return {
15
__class__: klass,
16
__dict__: _b_.dict.$factory(),
17
counter: -1,
18
items: items,
19
len: items.length
20
}
21
})
22
23
if(set_like){
24
for(var i = 0, len = set_ops.length; i < len; i++){
25
var op = "__" + set_ops[i] + "__"
26
klass[op] = (function(op){
27
return function(self, other){
28
// compare set of items to other
29
return _b_.set[op](_b_.set.$factory(self),
30
_b_.set.$factory(other))
31
}
32
})(op)
33
}
34
}
35
klass.__iter__ = function(self){
36
var it = klass.$iterator.$factory(self.items)
37
it.len_func = self.len_func
38
return it
39
}
40
klass.__repr__ = function(self){
41
return klass.$infos.__name__ + '(' + _b_.repr(self.items) + ')'
42
}
43
45
return klass
46
}
47
48
// Special version of __next__ for iterators on dict keys / values / items.
49
// Checks that the dictionary size didn't change during iteration.
50
function dict_iterator_next(self){
51
if(self.len_func() != self.len){
52
throw RuntimeError.$factory("dictionary changed size during iteration")
53
}
54
self.counter++
55
if(self.counter < self.items.length){
56
return self.items[self.counter]
57
}
58
throw _b_.StopIteration.$factory("StopIteration")
59
}
60
72
dict.$to_obj = function(d){
73
// Function applied to dictionary that only have string keys,
74
// return a Javascript objects with the kays mapped to the value,
75
// excluding the insertion rank
76
var res = {}
77
for(var key in d.$string_dict){
78
res[key] = d.$string_dict[key][0]
79
}
80
return res
81
}
82
97
}else{
98
for(var k in d.$numeric_dict){
99
items.push([parseFloat(k), d.$numeric_dict[k]])
100
}
104
for(var k in d.$object_dict){
105
d.$object_dict[k].forEach(function(item){
106
items.push(item)
107
})
108
}
109
// sort by insertion order
110
items.sort(function(a, b){
111
return a[1][1] - b[1][1]
112
})
113
items = items.map(function(item){return [item[0], item[1][0]]})
116
if(ix !== undefined){
117
return items.map(function(item){return item[ix]})
118
}else{
119
items.__class__ = _b_.tuple
120
return items.map(function(item){
121
item.__class__ = _b_.tuple; return item}
122
)
123
}
126
$B.dict_to_list = to_list // used in py_types.js
127
128
// Special version of __next__ for iterators on dict keys / values / items.
129
// Checks that the dictionary size didn't change during iteration.
130
function dict_iterator_next(self){
131
if(self.len_func() != self.len){
132
throw RuntimeError.$factory("dictionary changed size during iteration")
133
}
134
self.counter++
135
if(self.counter < self.items.length){
136
return self.items[self.counter]
148
si(left, _l[i][0], _l[i][1])
149
if(right.$version != right_version){
150
throw _b_.RuntimeError.$factory("dict mutated during update")
151
}
152
}
155
function rank(self, hash, key){
156
// Search if object key, with hash = hash(key), is in
157
// self.$object_dict
158
var pairs = self.$object_dict[hash]
159
if(pairs !== undefined){
160
for(var i = 0, len = pairs.length; i < len; i++){
161
if($B.rich_comp("__eq__", key, pairs[i][0])){
162
return i
163
}
164
}
165
}
166
return -1
167
}
168
177
var $ = $B.args("__contains__", 2, {self: null, key: null},
178
["self", "key"], arguments, {}, null, null),
181
if(self.$is_namespace){key = $B.to_alias(key)} // issue 1244
182
183
if(self.$jsobj){
184
return self.$jsobj[key] !== undefined
185
}
191
return self.$numeric_dict[key] !== undefined
192
}
193
194
var hash = _b_.hash(key)
195
if(self.$str_hash[hash] !== undefined &&
196
$B.rich_comp("__eq__", key, self.$str_hash[hash])){return true}
197
if(self.$numeric_dict[hash] !== undefined &&
198
$B.rich_comp("__eq__", key, hash)){return true}
199
return rank(self, hash, key) > -1
204
var $ = $B.args("__eq__", 2, {self: null, arg: null},
205
["self", "arg"], arguments, {}, null, null),
210
if(self.$jsobj[arg] === undefined){throw KeyError.$factory(arg)}
211
delete self.$jsobj[arg]
214
switch(typeof arg){
215
case "string":
216
if(self.$string_dict[arg] === undefined){
217
throw KeyError.$factory(_b_.str.$factory(arg))
218
}
219
delete self.$string_dict[arg]
220
delete self.$str_hash[str_hash(arg)]
223
case "number":
224
if(self.$numeric_dict[arg] === undefined){
225
throw KeyError.$factory(_b_.str.$factory(arg))
236
if((ix = rank(self, hash, arg)) > -1){
237
self.$object_dict[hash].splice(ix, 1)
238
}else{
239
throw KeyError.$factory(_b_.str.$factory(arg))
247
var $ = $B.args("__eq__", 2, {self: null, other: null},
248
["self", "other"], arguments, {}, null, null),
254
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
255
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
266
if(!$B.rich_comp("__eq__", other.$numeric_dict[k][0],
267
self.$numeric_dict[k][0])){
271
var pairs = other.$object_dict[k],
272
flag = false
273
for(var i = 0, len = pairs.length; i < len; i++){
274
if($B.rich_comp("__eq__", k, pairs[i][0]) &&
275
$B.rich_comp("__eq__", self.$numeric_dict[k],
276
pairs[i][1])){
277
flag = true
278
break
279
}
294
var pairs = self.$object_dict[hash]
295
// Get all (key, value) pairs in other that have the same hash
296
var other_pairs = []
297
if(other.$numeric_dict[hash] !== undefined){
298
other_pairs.push([hash, other.$numeric_dict[hash]])
299
}
301
other_pairs = other_pairs.concat(other.$object_dict[hash])
302
}
303
if(other_pairs.length == 0){
304
return false
305
}
306
for(var i = 0, len_i = pairs.length; i < len_i; i++){
307
var flag = false
308
var key = pairs[i][0],
310
for(var j = 0, len_j = other_pairs.length; j < len_j; j++){
311
if($B.rich_comp("__eq__", key, other_pairs[j][0]) &&
326
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
327
["self", "arg"], arguments, {}, null, null),
335
if(!self.$jsobj.hasOwnProperty(arg)){
336
throw _b_.KeyError.$factory(str.$factory(arg))
337
}else if(self.$jsobj[arg] === undefined){
342
343
switch(typeof arg){
344
case "string":
345
if(self.$string_dict[arg] !== undefined){
353
break
354
}
355
356
// since the key is more complex use 'default' method of getting item
357
370
}
371
if(isinstance(arg, _b_.str)){
372
// string subclass
373
var res = self.$string_dict[arg.valueOf()]
424
if(first === undefined){return $N}
425
if(second === undefined){
426
if(first.__class__ === $B.JSObject){
427
self.$jsobj = first.js
428
return $N
429
}else if(first.$jsobj){
430
self.$jsobj = {}
431
for(var attr in first.$jsobj){
432
self.$jsobj[attr] = first.$jsobj[attr]
442
arguments, {}, "first", "second")
443
var args = $.first
444
if(args.length > 1){
445
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
446
", got 2")
447
}else if(args.length == 1){
448
args = args[0]
449
if(args.__class__ === dict){
450
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
451
forEach(function(d){
452
for(key in args[d]){self[d][key] = args[d][key]}
453
})
457
var keys = $B.$getattr(args, "keys", null)
458
if(keys !== null){
459
var gi = $B.$getattr(args, "__getitem__", null)
460
if(gi !== null){
461
// has keys and __getitem__ : it's a mapping, iterate on
462
// keys and values
463
gi = $B.$call(gi)
464
var kiter = _b_.iter($B.$call(keys)())
465
while(true){
466
try{
467
var key = _b_.next(kiter),
468
value = gi(key)
469
dict.__setitem__(self, key, value)
470
}catch(err){
471
if(err.__class__ === _b_.StopIteration){
472
break
473
}
474
throw err
475
}
476
}
477
return $N
478
}
479
}
480
if(! Array.isArray(args)){
481
args = _b_.list.$factory(args)
482
}
483
// Form "dict([[key1, value1], [key2,value2], ...])"
489
switch(typeof attr){
490
case "string":
491
self.$string_dict[attr] = kw[attr]
492
self.$str_hash[str_hash(attr)] = attr
493
break
494
case "number":
495
self.$numeric_dict[attr] = kw[attr]
496
break
497
default:
498
si(self, attr, kw[attr])
499
break
500
}
517
for(var k in self.$numeric_dict){_count++}
518
for(var k in self.$string_dict){_count++}
519
for(var hash in self.$object_dict){
520
_count += self.$object_dict[hash].length
521
}
540
if(cls !== dict){
541
instance.__dict__ = _b_.dict.$factory()
542
}
543
return instance
552
items.forEach(function(item){
553
if((!self.$jsobj && item[1] === self) ||
554
(self.$jsobj && item[1] === self.$jsobj)){
565
["self", "key", "value"], arguments, {}, null, null)
566
return dict.$setitem($.self, $.key, $.value)
567
}
570
// Set a dictionary item mapping key and value.
571
//
572
// If key is a string, set $string_dict[key] = value and
573
// $str_hash[hash(key)] to key
574
//
575
// If key is a number, set $numeric_dict[key] = value
576
//
577
// If key is another object, compute its hash value:
578
// - if the hash is a key of $str_hash, and key == $str_hash[hash],
579
// replace $string_dict[$str_hash[hash]] by value
580
// - if the hash is a key of $numeric_dict, and hash == key, replace
581
// $numeric_dict[hash] by value
582
// - if the hash is a key of $object_dict: $object_dict[hash] is a list
583
// of [k, v] pairs. If key is equal to one of the "k", replace the
584
// matching v by value. Otherwise, add [key, value] to the list
585
// - else set $object_dict[hash] = [[key, value]]
586
//
587
// In all cases, increment attribute $version, used to detect dictionary
588
// cahnges during an iteration.
589
//
590
// Parameter $hash is only set if this method is called by setdefault.
591
// In this case the hash of key has already been computed and we
592
// know that the key is not present in the dictionary, so it's no
593
// use computing hash(key) again, nor testing equality of keys
595
if(self.$from_js){
596
// dictionary created by method to_dict of JSObject instances
597
value = $B.pyobj2jsobj(value)
598
}
602
// If class attribute __init__ or __new__ are reset,
603
// the factory function has to change
604
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
605
}
606
}else{
614
if(self.$string_dict === undefined){
615
console.log("pas de string dict", self, key, value)
616
}
617
self.$string_dict[key] = [value, self.$version]
644
// If $setitem is called from setdefault, don't test equality of key
645
// with any object
646
if($hash){
647
if(self.$object_dict[$hash] !== undefined){
651
}
652
self.$version++
653
return $N
654
}
655
var ix = rank(self, hash, key)
656
if(ix > -1){
657
// reset value
711
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
712
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
724
if(klass === dict){dict.$setitem(res, key, value)}
725
else{$B.$getattr(res, "__setitem__")(key, value)}
736
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
737
["self", "key", "_default"], arguments, {_default: $N}, null, null)
740
catch(err){
741
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
742
else{throw err}
743
}
744
}
745
746
var dict_items = $B.make_view("dict_items", true)
747
dict_items.$iterator = $B.make_iterator_class("dict_itemiterator")
751
var _len = arguments.length - 1,
752
_msg = "items() takes no arguments (" + _len + " given)"
755
var it = dict_items.$factory(to_list(self))
756
it.len_func = function(){return dict.__len__(self)}
757
return it
760
var dict_keys = $B.make_view("dict_keys", true)
761
dict_keys.$iterator = $B.make_iterator_class("dict_keyiterator")
765
var _len = arguments.length - 1,
766
_msg = "keys() takes no arguments (" + _len + " given)"
769
var it = dict_keys.$factory(to_list(self, 0))
770
it.len_func = function(){return dict.__len__(self)}
771
return it
776
var missing = {},
777
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
778
["self", "key", "_default"], arguments, {_default: missing}, null, null),
810
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
811
["self", "key", "_default"], arguments, {_default: $N}, null, null),
822
var hash = key.$hash
823
key.$hash = undefined
824
dict.$setitem(self, key, _default, hash)
831
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
832
{}, "args", "kw"),
833
self = $.self,
834
args = $.args,
835
kw = $.kw
836
if(args.length > 0){
837
var o = args[0]
838
if(isinstance(o, dict)){
844
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))())
845
for(var i = 0, len = _keys.length; i < len; i++){
847
dict.$setitem(self, _keys[i], _value)
848
}
849
}else{
850
var it = _b_.iter(o),
851
i = 0
852
while(true){
853
try{
854
var item = _b_.next(it)
855
}catch(err){
856
if(err.__class__ === _b_.StopIteration){break}
857
throw err
858
}
859
try{
860
key_value = _b_.list.$factory(item)
861
}catch(err){
862
throw _b_.TypeError.$factory("cannot convert dictionary" +
863
" update sequence element #" + i + " to a sequence")
864
}
865
if(key_value.length !== 2){
866
throw _b_.ValueError.$factory("dictionary update " +
867
"sequence element #" + i + " has length " +
868
key_value.length + "; 2 is required")
869
}
870
dict.$setitem(self, key_value[0], key_value[1])
871
i++
880
var dict_values = $B.make_view("dict_values")
881
dict_values.$iterator = $B.make_iterator_class("dict_valueiterator")
885
var _len = arguments.length - 1,
886
_msg = "values() takes no arguments (" + _len + " given)"
889
var it = dict_values.$factory(to_list(self, 1))
890
it.len_func = function(){return dict.__len__(self)}
891
return it
896
var args = [res]
897
for(var i = 0, len = arguments.length; i < len ; i++){
898
args.push(arguments[i])
899
}
900
dict.__init__.apply(null, args)
908
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
909
// have the attribute $infos
910
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
911
916
// obj is a dictionary, with $string_dict table such that
917
// obj.$string_dict[key] = [value, rank]
918
// Transform it into an object with attribute $jsobj such that
919
// res.$jsobj[key] = value
920
var res = $B.obj_dict(dict.$to_obj(obj))
930
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
931
"item assignment")
934
for(var attr in dict){
935
if(mappingproxy[attr] !== undefined ||
936
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
937
"clear", "fromkeys", "pop", "popitem", "setdefault",
938
"update"].indexOf(attr) > -1){
939
continue
940
}
941
if(typeof dict[attr] == "function"){
942
mappingproxy[attr] = (function(key){
943
return function(){
944
return dict[key].apply(null, arguments)
945
}
946
})(attr)
947
}else{
948
mappingproxy[attr] = dict[attr]
949
}
950
}
951
973
if(klass !== undefined && klass.$native){
974
throw _b_.AttributeError.$factory(klass.__name__ +