Permalink
May 17, 2019
May 17, 2019
Apr 3, 2019
Apr 3, 2019
Apr 3, 2019
Apr 3, 2019
Dec 13, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Mar 7, 2018
Feb 26, 2018
Dec 17, 2018
Dec 17, 2018
Dec 10, 2018
Dec 10, 2018
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
Oct 24, 2018
Feb 3, 2018
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
Mar 7, 2018
Aug 2, 2018
Feb 26, 2018
Newer
100644
927 lines (827 sloc)
26.6 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
86
}else{
87
for(var k in d.$numeric_dict){
88
items.push([parseFloat(k), d.$numeric_dict[k]])
89
}
93
for(var k in d.$object_dict){
94
d.$object_dict[k].forEach(function(item){
95
items.push(item)
96
})
97
}
100
if(ix !== undefined){
101
return items.map(function(item){return item[ix]})
102
}else{
103
items.__class__ = _b_.tuple
104
return items.map(function(item){
105
item.__class__ = _b_.tuple; return item}
106
)
107
}
110
$B.dict_to_list = to_list // used in py_types.js
111
112
// Special version of __next__ for iterators on dict keys / values / items.
113
// Checks that the dictionary size didn't change during iteration.
114
function dict_iterator_next(self){
115
if(self.len_func() != self.len){
116
throw RuntimeError.$factory("dictionary changed size during iteration")
117
}
118
self.counter++
119
if(self.counter < self.items.length){
120
return self.items[self.counter]
132
si(left, _l[i][0], _l[i][1])
133
if(right.$version != right_version){
134
throw _b_.RuntimeError.$factory("dict mutated during update")
135
}
136
}
139
function rank(self, hash, key){
140
// Search if object key, with hash = hash(key), is in
141
// self.$object_dict
142
var pairs = self.$object_dict[hash]
143
if(pairs !== undefined){
144
for(var i = 0, len = pairs.length; i < len; i++){
145
if($B.rich_comp("__eq__", key, pairs[i][0])){
146
return i
147
}
148
}
149
}
150
return -1
151
}
152
161
var $ = $B.args("__contains__", 2, {self: null, key: null},
162
["self", "key"], arguments, {}, null, null),
172
return self.$numeric_dict[key] !== undefined
173
}
174
175
var hash = _b_.hash(key)
176
if(self.$str_hash[hash] !== undefined &&
177
$B.rich_comp("__eq__", key, self.$str_hash[hash])){return true}
178
if(self.$numeric_dict[hash] !== undefined &&
179
$B.rich_comp("__eq__", key, hash)){return true}
180
return rank(self, hash, key) > -1
185
var $ = $B.args("__eq__", 2, {self: null, arg: null},
186
["self", "arg"], arguments, {}, null, null),
191
if(self.$jsobj[arg] === undefined){throw KeyError.$factory(arg)}
192
delete self.$jsobj[arg]
195
switch(typeof arg){
196
case "string":
197
if(self.$string_dict[arg] === undefined){
198
throw KeyError.$factory(_b_.str.$factory(arg))
199
}
200
delete self.$string_dict[arg]
201
delete self.$str_hash[str_hash(arg)]
204
case "number":
205
if(self.$numeric_dict[arg] === undefined){
206
throw KeyError.$factory(_b_.str.$factory(arg))
217
if((ix = rank(self, hash, arg)) > -1){
218
self.$object_dict[hash].splice(ix, 1)
219
}else{
220
throw KeyError.$factory(_b_.str.$factory(arg))
228
var $ = $B.args("__eq__", 2, {self: null, other: null},
229
["self", "other"], arguments, {}, null, null),
235
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
236
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
246
if(other.$numeric_dict.hasOwnProperty(k)){
247
if(!$B.rich_comp("__eq__", other.$numeric_dict[k],
248
self.$numeric_dict[k])){
249
return false
250
}
251
}else if(other.$object_dict.hasOwnProperty(k)){
252
var pairs = other.$object_dict[k],
253
flag = false
254
for(var i = 0, len = pairs.length; i < len; i++){
255
if($B.rich_comp("__eq__", k, pairs[i][0]) &&
256
$B.rich_comp("__eq__", self.$numeric_dict[k],
257
pairs[i][1])){
258
flag = true
259
break
260
}
268
if(!other.$string_dict.hasOwnProperty(k) ||
269
!$B.rich_comp("__eq__", other.$string_dict[k],
275
var pairs = self.$object_dict[hash]
276
// Get all (key, value) pairs in other that have the same hash
277
var other_pairs = []
278
if(other.$numeric_dict[hash] !== undefined){
279
other_pairs.push([hash, other.$numeric_dict[hash]])
280
}
282
other_pairs = other_pairs.concat(other.$object_dict[hash])
283
}
284
if(other_pairs.length == 0){
285
return false
286
}
287
for(var i = 0, len_i = pairs.length; i < len_i; i++){
288
var flag = false
289
var key = pairs[i][0],
290
value = pairs[i][1]
291
for(var j = 0, len_j = other_pairs.length; j < len_j; j++){
292
if($B.rich_comp("__eq__", key, other_pairs[j][0]) &&
293
$B.rich_comp("__eq__", value, other_pairs[j][1])){
294
flag = true
295
break
307
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
308
["self", "arg"], arguments, {}, null, null),
312
if(!self.$jsobj.hasOwnProperty(arg)){
313
throw _b_.KeyError.$factory(str.$factory(arg))
314
}else if(self.$jsobj[arg] === undefined){
319
320
switch(typeof arg){
321
case "string":
322
if(self.$string_dict[arg] !== undefined){
323
return self.$string_dict[arg]
325
break
326
case "number":
327
if(self.$numeric_dict[arg] !== undefined){
328
return self.$numeric_dict[arg]
330
break
331
}
332
333
// since the key is more complex use 'default' method of getting item
334
345
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
346
return self.$numeric_dict[hash]
347
}
348
if(isinstance(arg, _b_.str)){
349
// string subclass
350
var res = self.$string_dict[arg.valueOf()]
351
if(res !== undefined){return res}
352
}
353
354
var ix = rank(self, hash, arg)
355
if(ix > -1){
356
return self.$object_dict[hash][ix][1]
382
switch(typeof item[0]) {
383
case 'string':
384
self.$string_dict[item[0]] = item[1]
385
self.$str_hash[str_hash(item[0])] = item[0]
386
break
387
case 'number':
388
self.$numeric_dict[item[0]] = item[1]
389
break
390
default:
391
si(self, item[0], item[1])
392
break
393
}
399
if(first === undefined){return $N}
400
if(second === undefined){
401
if(first.__class__ === $B.JSObject){
402
self.$jsobj = first.js
403
return $N
404
}else if(first.$jsobj){
405
self.$jsobj = {}
406
for(var attr in first.$jsobj){
407
self.$jsobj[attr] = first.$jsobj[attr]
417
arguments, {}, "first", "second")
418
var args = $.first
419
if(args.length > 1){
420
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
421
", got 2")
422
}else if(args.length == 1){
423
args = args[0]
424
if(args.__class__ === dict){
425
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
426
forEach(function(d){
427
for(key in args[d]){self[d][key] = args[d][key]}
428
})
432
var keys = $B.$getattr(args, "keys", null)
433
if(keys !== null){
434
var gi = $B.$getattr(args, "__getitem__", null)
435
if(gi !== null){
436
// has keys and __getitem__ : it's a mapping, iterate on
437
// keys and values
438
gi = $B.$call(gi)
439
var kiter = _b_.iter($B.$call(keys)())
440
while(true){
441
try{
442
var key = _b_.next(kiter),
443
value = gi(key)
444
dict.__setitem__(self, key, value)
445
}catch(err){
446
if(err.__class__ === _b_.StopIteration){
447
break
448
}
449
throw err
450
}
451
}
452
return $N
453
}
454
}
455
if(! Array.isArray(args)){
456
args = _b_.list.$factory(args)
457
}
458
// Form "dict([[key1, value1], [key2,value2], ...])"
464
switch(typeof attr){
465
case "string":
466
self.$string_dict[attr] = kw[attr]
467
self.$str_hash[str_hash(attr)] = attr
468
break
469
case "number":
470
self.$numeric_dict[attr] = kw[attr]
471
break
472
default:
473
si(self, attr, kw[attr])
474
break
475
}
492
for(var k in self.$numeric_dict){_count++}
493
for(var k in self.$string_dict){_count++}
494
for(var hash in self.$object_dict){
495
_count += self.$object_dict[hash].length
496
}
515
if(cls !== dict){
516
instance.__dict__ = _b_.dict.$factory()
517
}
518
return instance
527
items.forEach(function(item){
528
if((!self.$jsobj && item[1] === self) ||
529
(self.$jsobj && item[1] === self.$jsobj)){
540
["self", "key", "value"], arguments, {}, null, null)
541
return dict.$setitem($.self, $.key, $.value)
542
}
544
dict.$setitem = function(self, key, value, $hash){
545
// Parameter $hash is only set if this method is called by setdefault.
546
// In this case the hash of key has already been computed and we
547
// know that the key is not present in the dictionary, so it's no
548
// use computing hash(key) again, nor testing equality of keys
550
if(self.$from_js){
551
// dictionary created by method to_dict of JSObject instances
552
value = $B.pyobj2jsobj(value)
553
}
557
// If class attribute __init__ or __new__ are reset,
558
// the factory function has to change
559
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
560
}
561
}else{
584
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
585
self.$numeric_dict[hash] = value
596
// If $setitem is called from setdefault, don't test equality of key
597
// with any object
598
if($hash){
599
if(self.$object_dict[$hash] !== undefined){
600
self.$object_dict[$hash].push([key, value])
601
}else{
602
self.$object_dict[$hash] = [[key, value]]
603
}
604
self.$version++
605
return $N
606
}
607
var ix = rank(self, hash, key)
608
if(ix > -1){
609
// reset value
610
self.$object_dict[hash][ix][1] = value
611
return $N
612
}else if(self.$object_dict.hasOwnProperty(hash)){
613
self.$object_dict[hash].push([key, value])
614
}else{
615
self.$object_dict[hash] = [[key, value]]
660
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
661
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
673
if(klass === dict){dict.$setitem(res, key, value)}
674
else{$B.$getattr(res, "__setitem__")(key, value)}
685
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
686
["self", "key", "_default"], arguments, {_default: $N}, null, null)
689
catch(err){
690
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
691
else{throw err}
692
}
693
}
694
695
var dict_items = $B.make_view("dict_items", true)
696
dict_items.$iterator = $B.make_iterator_class("dict_itemiterator")
700
var _len = arguments.length - 1,
701
_msg = "items() takes no arguments (" + _len + " given)"
704
var it = dict_items.$factory(to_list(self))
705
it.len_func = function(){return dict.__len__(self)}
706
return it
709
var dict_keys = $B.make_view("dict_keys", true)
710
dict_keys.$iterator = $B.make_iterator_class("dict_keyiterator")
714
var _len = arguments.length - 1,
715
_msg = "keys() takes no arguments (" + _len + " given)"
718
var it = dict_keys.$factory(to_list(self, 0))
719
it.len_func = function(){return dict.__len__(self)}
720
return it
725
var missing = {},
726
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
727
["self", "key", "_default"], arguments, {_default: missing}, null, null),
759
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
760
["self", "key", "_default"], arguments, {_default: $N}, null, null),
771
var hash = key.$hash
772
key.$hash = undefined
773
dict.$setitem(self, key, _default, hash)
780
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
781
{}, "args", "kw"),
782
self = $.self,
783
args = $.args,
784
kw = $.kw
785
if(args.length > 0){
786
var o = args[0]
787
if(isinstance(o, dict)){
793
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))())
794
for(var i = 0, len = _keys.length; i < len; i++){
796
dict.$setitem(self, _keys[i], _value)
797
}
798
}else{
799
var it = _b_.iter(o),
800
i = 0
801
while(true){
802
try{
803
var item = _b_.next(it)
804
}catch(err){
805
if(err.__class__ === _b_.StopIteration){break}
806
throw err
807
}
808
try{
809
key_value = _b_.list.$factory(item)
810
}catch(err){
811
throw _b_.TypeError.$factory("cannot convert dictionary" +
812
" update sequence element #" + i + " to a sequence")
813
}
814
if(key_value.length !== 2){
815
throw _b_.ValueError.$factory("dictionary update " +
816
"sequence element #" + i + " has length " +
817
key_value.length + "; 2 is required")
818
}
819
dict.$setitem(self, key_value[0], key_value[1])
820
i++
829
var dict_values = $B.make_view("dict_values")
830
dict_values.$iterator = $B.make_iterator_class("dict_valueiterator")
834
var _len = arguments.length - 1,
835
_msg = "values() takes no arguments (" + _len + " given)"
838
var it = dict_values.$factory(to_list(self, 1))
839
it.len_func = function(){return dict.__len__(self)}
840
return it
845
var args = [res]
846
for(var i = 0, len = arguments.length; i < len ; i++){
847
args.push(arguments[i])
848
}
849
dict.__init__.apply(null, args)
857
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
858
// have the attribute $infos
859
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
860
876
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
877
"item assignment")
880
for(var attr in dict){
881
if(mappingproxy[attr] !== undefined ||
882
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
883
"clear", "fromkeys", "pop", "popitem", "setdefault",
884
"update"].indexOf(attr) > -1){
885
continue
886
}
887
if(typeof dict[attr] == "function"){
888
mappingproxy[attr] = (function(key){
889
return function(){
890
return dict[key].apply(null, arguments)
891
}
892
})(attr)
893
}else{
894
mappingproxy[attr] = dict[attr]
895
}
896
}
897
918
if(klass !== undefined && klass.$native){
919
throw _b_.AttributeError.$factory(klass.__name__ +