Permalink
May 17, 2019
May 17, 2019
Apr 3, 2019
Apr 3, 2019
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
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
931 lines (830 sloc)
26.7 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),
166
if(self.$is_namespace){key = $B.to_alias(key)} // issue 1244
167
168
if(self.$jsobj){
169
return self.$jsobj[key] !== undefined
170
}
176
return self.$numeric_dict[key] !== undefined
177
}
178
179
var hash = _b_.hash(key)
180
if(self.$str_hash[hash] !== undefined &&
181
$B.rich_comp("__eq__", key, self.$str_hash[hash])){return true}
182
if(self.$numeric_dict[hash] !== undefined &&
183
$B.rich_comp("__eq__", key, hash)){return true}
184
return rank(self, hash, key) > -1
189
var $ = $B.args("__eq__", 2, {self: null, arg: null},
190
["self", "arg"], arguments, {}, null, null),
195
if(self.$jsobj[arg] === undefined){throw KeyError.$factory(arg)}
196
delete self.$jsobj[arg]
199
switch(typeof arg){
200
case "string":
201
if(self.$string_dict[arg] === undefined){
202
throw KeyError.$factory(_b_.str.$factory(arg))
203
}
204
delete self.$string_dict[arg]
205
delete self.$str_hash[str_hash(arg)]
208
case "number":
209
if(self.$numeric_dict[arg] === undefined){
210
throw KeyError.$factory(_b_.str.$factory(arg))
221
if((ix = rank(self, hash, arg)) > -1){
222
self.$object_dict[hash].splice(ix, 1)
223
}else{
224
throw KeyError.$factory(_b_.str.$factory(arg))
232
var $ = $B.args("__eq__", 2, {self: null, other: null},
233
["self", "other"], arguments, {}, null, null),
239
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
240
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
250
if(other.$numeric_dict.hasOwnProperty(k)){
251
if(!$B.rich_comp("__eq__", other.$numeric_dict[k],
252
self.$numeric_dict[k])){
253
return false
254
}
255
}else if(other.$object_dict.hasOwnProperty(k)){
256
var pairs = other.$object_dict[k],
257
flag = false
258
for(var i = 0, len = pairs.length; i < len; i++){
259
if($B.rich_comp("__eq__", k, pairs[i][0]) &&
260
$B.rich_comp("__eq__", self.$numeric_dict[k],
261
pairs[i][1])){
262
flag = true
263
break
264
}
272
if(!other.$string_dict.hasOwnProperty(k) ||
273
!$B.rich_comp("__eq__", other.$string_dict[k],
279
var pairs = self.$object_dict[hash]
280
// Get all (key, value) pairs in other that have the same hash
281
var other_pairs = []
282
if(other.$numeric_dict[hash] !== undefined){
283
other_pairs.push([hash, other.$numeric_dict[hash]])
284
}
286
other_pairs = other_pairs.concat(other.$object_dict[hash])
287
}
288
if(other_pairs.length == 0){
289
return false
290
}
291
for(var i = 0, len_i = pairs.length; i < len_i; i++){
292
var flag = false
293
var key = pairs[i][0],
294
value = pairs[i][1]
295
for(var j = 0, len_j = other_pairs.length; j < len_j; j++){
296
if($B.rich_comp("__eq__", key, other_pairs[j][0]) &&
297
$B.rich_comp("__eq__", value, other_pairs[j][1])){
298
flag = true
299
break
311
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
312
["self", "arg"], arguments, {}, null, null),
316
if(!self.$jsobj.hasOwnProperty(arg)){
317
throw _b_.KeyError.$factory(str.$factory(arg))
318
}else if(self.$jsobj[arg] === undefined){
323
324
switch(typeof arg){
325
case "string":
326
if(self.$string_dict[arg] !== undefined){
327
return self.$string_dict[arg]
329
break
330
case "number":
331
if(self.$numeric_dict[arg] !== undefined){
332
return self.$numeric_dict[arg]
334
break
335
}
336
337
// since the key is more complex use 'default' method of getting item
338
349
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
350
return self.$numeric_dict[hash]
351
}
352
if(isinstance(arg, _b_.str)){
353
// string subclass
354
var res = self.$string_dict[arg.valueOf()]
355
if(res !== undefined){return res}
356
}
357
358
var ix = rank(self, hash, arg)
359
if(ix > -1){
360
return self.$object_dict[hash][ix][1]
386
switch(typeof item[0]) {
387
case 'string':
388
self.$string_dict[item[0]] = item[1]
389
self.$str_hash[str_hash(item[0])] = item[0]
390
break
391
case 'number':
392
self.$numeric_dict[item[0]] = item[1]
393
break
394
default:
395
si(self, item[0], item[1])
396
break
397
}
403
if(first === undefined){return $N}
404
if(second === undefined){
405
if(first.__class__ === $B.JSObject){
406
self.$jsobj = first.js
407
return $N
408
}else if(first.$jsobj){
409
self.$jsobj = {}
410
for(var attr in first.$jsobj){
411
self.$jsobj[attr] = first.$jsobj[attr]
421
arguments, {}, "first", "second")
422
var args = $.first
423
if(args.length > 1){
424
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
425
", got 2")
426
}else if(args.length == 1){
427
args = args[0]
428
if(args.__class__ === dict){
429
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
430
forEach(function(d){
431
for(key in args[d]){self[d][key] = args[d][key]}
432
})
436
var keys = $B.$getattr(args, "keys", null)
437
if(keys !== null){
438
var gi = $B.$getattr(args, "__getitem__", null)
439
if(gi !== null){
440
// has keys and __getitem__ : it's a mapping, iterate on
441
// keys and values
442
gi = $B.$call(gi)
443
var kiter = _b_.iter($B.$call(keys)())
444
while(true){
445
try{
446
var key = _b_.next(kiter),
447
value = gi(key)
448
dict.__setitem__(self, key, value)
449
}catch(err){
450
if(err.__class__ === _b_.StopIteration){
451
break
452
}
453
throw err
454
}
455
}
456
return $N
457
}
458
}
459
if(! Array.isArray(args)){
460
args = _b_.list.$factory(args)
461
}
462
// Form "dict([[key1, value1], [key2,value2], ...])"
468
switch(typeof attr){
469
case "string":
470
self.$string_dict[attr] = kw[attr]
471
self.$str_hash[str_hash(attr)] = attr
472
break
473
case "number":
474
self.$numeric_dict[attr] = kw[attr]
475
break
476
default:
477
si(self, attr, kw[attr])
478
break
479
}
496
for(var k in self.$numeric_dict){_count++}
497
for(var k in self.$string_dict){_count++}
498
for(var hash in self.$object_dict){
499
_count += self.$object_dict[hash].length
500
}
519
if(cls !== dict){
520
instance.__dict__ = _b_.dict.$factory()
521
}
522
return instance
531
items.forEach(function(item){
532
if((!self.$jsobj && item[1] === self) ||
533
(self.$jsobj && item[1] === self.$jsobj)){
544
["self", "key", "value"], arguments, {}, null, null)
545
return dict.$setitem($.self, $.key, $.value)
546
}
548
dict.$setitem = function(self, key, value, $hash){
549
// Parameter $hash is only set if this method is called by setdefault.
550
// In this case the hash of key has already been computed and we
551
// know that the key is not present in the dictionary, so it's no
552
// use computing hash(key) again, nor testing equality of keys
554
if(self.$from_js){
555
// dictionary created by method to_dict of JSObject instances
556
value = $B.pyobj2jsobj(value)
557
}
561
// If class attribute __init__ or __new__ are reset,
562
// the factory function has to change
563
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
564
}
565
}else{
588
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
589
self.$numeric_dict[hash] = value
600
// If $setitem is called from setdefault, don't test equality of key
601
// with any object
602
if($hash){
603
if(self.$object_dict[$hash] !== undefined){
604
self.$object_dict[$hash].push([key, value])
605
}else{
606
self.$object_dict[$hash] = [[key, value]]
607
}
608
self.$version++
609
return $N
610
}
611
var ix = rank(self, hash, key)
612
if(ix > -1){
613
// reset value
614
self.$object_dict[hash][ix][1] = value
615
return $N
616
}else if(self.$object_dict.hasOwnProperty(hash)){
617
self.$object_dict[hash].push([key, value])
618
}else{
619
self.$object_dict[hash] = [[key, value]]
664
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
665
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
677
if(klass === dict){dict.$setitem(res, key, value)}
678
else{$B.$getattr(res, "__setitem__")(key, value)}
689
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
690
["self", "key", "_default"], arguments, {_default: $N}, null, null)
693
catch(err){
694
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
695
else{throw err}
696
}
697
}
698
699
var dict_items = $B.make_view("dict_items", true)
700
dict_items.$iterator = $B.make_iterator_class("dict_itemiterator")
704
var _len = arguments.length - 1,
705
_msg = "items() takes no arguments (" + _len + " given)"
708
var it = dict_items.$factory(to_list(self))
709
it.len_func = function(){return dict.__len__(self)}
710
return it
713
var dict_keys = $B.make_view("dict_keys", true)
714
dict_keys.$iterator = $B.make_iterator_class("dict_keyiterator")
718
var _len = arguments.length - 1,
719
_msg = "keys() takes no arguments (" + _len + " given)"
722
var it = dict_keys.$factory(to_list(self, 0))
723
it.len_func = function(){return dict.__len__(self)}
724
return it
729
var missing = {},
730
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
731
["self", "key", "_default"], arguments, {_default: missing}, null, null),
763
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
764
["self", "key", "_default"], arguments, {_default: $N}, null, null),
775
var hash = key.$hash
776
key.$hash = undefined
777
dict.$setitem(self, key, _default, hash)
784
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
785
{}, "args", "kw"),
786
self = $.self,
787
args = $.args,
788
kw = $.kw
789
if(args.length > 0){
790
var o = args[0]
791
if(isinstance(o, dict)){
797
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))())
798
for(var i = 0, len = _keys.length; i < len; i++){
800
dict.$setitem(self, _keys[i], _value)
801
}
802
}else{
803
var it = _b_.iter(o),
804
i = 0
805
while(true){
806
try{
807
var item = _b_.next(it)
808
}catch(err){
809
if(err.__class__ === _b_.StopIteration){break}
810
throw err
811
}
812
try{
813
key_value = _b_.list.$factory(item)
814
}catch(err){
815
throw _b_.TypeError.$factory("cannot convert dictionary" +
816
" update sequence element #" + i + " to a sequence")
817
}
818
if(key_value.length !== 2){
819
throw _b_.ValueError.$factory("dictionary update " +
820
"sequence element #" + i + " has length " +
821
key_value.length + "; 2 is required")
822
}
823
dict.$setitem(self, key_value[0], key_value[1])
824
i++
833
var dict_values = $B.make_view("dict_values")
834
dict_values.$iterator = $B.make_iterator_class("dict_valueiterator")
838
var _len = arguments.length - 1,
839
_msg = "values() takes no arguments (" + _len + " given)"
842
var it = dict_values.$factory(to_list(self, 1))
843
it.len_func = function(){return dict.__len__(self)}
844
return it
849
var args = [res]
850
for(var i = 0, len = arguments.length; i < len ; i++){
851
args.push(arguments[i])
852
}
853
dict.__init__.apply(null, args)
861
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
862
// have the attribute $infos
863
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
864
880
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
881
"item assignment")
884
for(var attr in dict){
885
if(mappingproxy[attr] !== undefined ||
886
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
887
"clear", "fromkeys", "pop", "popitem", "setdefault",
888
"update"].indexOf(attr) > -1){
889
continue
890
}
891
if(typeof dict[attr] == "function"){
892
mappingproxy[attr] = (function(key){
893
return function(){
894
return dict[key].apply(null, arguments)
895
}
896
})(attr)
897
}else{
898
mappingproxy[attr] = dict[attr]
899
}
900
}
901
922
if(klass !== undefined && klass.$native){
923
throw _b_.AttributeError.$factory(klass.__name__ +