Permalink
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
924 lines (824 sloc)
26.5 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
44
return klass
45
}
46
47
// Special version of __next__ for iterators on dict keys / values / items.
48
// Checks that the dictionary size didn't change during iteration.
49
function dict_iterator_next(self){
50
if(self.len_func() != self.len){
51
throw RuntimeError.$factory("dictionary changed size during iteration")
52
}
53
self.counter++
54
if(self.counter < self.items.length){
55
return self.items[self.counter]
56
}
57
throw _b_.StopIteration.$factory("StopIteration")
58
}
59
85
}else{
86
for(var k in d.$numeric_dict){
87
items.push([parseFloat(k), d.$numeric_dict[k]])
88
}
92
for(var k in d.$object_dict){
93
d.$object_dict[k].forEach(function(item){
94
items.push(item)
95
})
96
}
99
if(ix !== undefined){
100
return items.map(function(item){return item[ix]})
101
}else{
102
items.__class__ = _b_.tuple
103
return items.map(function(item){
104
item.__class__ = _b_.tuple; return item}
105
)
106
}
109
$B.dict_to_list = to_list // used in py_types.js
110
111
// Special version of __next__ for iterators on dict keys / values / items.
112
// Checks that the dictionary size didn't change during iteration.
113
function dict_iterator_next(self){
114
if(self.len_func() != self.len){
115
throw RuntimeError.$factory("dictionary changed size during iteration")
116
}
117
self.counter++
118
if(self.counter < self.items.length){
119
return self.items[self.counter]
131
si(left, _l[i][0], _l[i][1])
132
if(right.$version != right_version){
133
throw _b_.RuntimeError.$factory("dict mutated during update")
134
}
135
}
138
function rank(self, hash, key){
139
// Search if object key, with hash = hash(key), is in
140
// self.$object_dict
141
var pairs = self.$object_dict[hash]
142
if(pairs !== undefined){
143
for(var i = 0, len = pairs.length; i < len; i++){
144
if($B.rich_comp("__eq__", key, pairs[i][0])){
145
return i
146
}
147
}
148
}
149
return -1
150
}
151
160
var $ = $B.args("__contains__", 2, {self: null, key: null},
161
["self", "key"], arguments, {}, null, null),
171
return self.$numeric_dict[key] !== undefined
172
}
173
174
var hash = _b_.hash(key)
175
if(self.$str_hash[hash] !== undefined &&
176
$B.rich_comp("__eq__", key, self.$str_hash[hash])){return true}
177
if(self.$numeric_dict[hash] !== undefined &&
178
$B.rich_comp("__eq__", key, hash)){return true}
179
return rank(self, hash, key) > -1
184
var $ = $B.args("__eq__", 2, {self: null, arg: null},
185
["self", "arg"], arguments, {}, null, null),
190
if(self.$jsobj[arg] === undefined){throw KeyError.$factory(arg)}
191
delete self.$jsobj[arg]
194
switch(typeof arg){
195
case "string":
196
if(self.$string_dict[arg] === undefined){
197
throw KeyError.$factory(_b_.str.$factory(arg))
198
}
199
delete self.$string_dict[arg]
200
delete self.$str_hash[str_hash(arg)]
203
case "number":
204
if(self.$numeric_dict[arg] === undefined){
205
throw KeyError.$factory(_b_.str.$factory(arg))
216
if((ix = rank(self, hash, arg)) > -1){
217
self.$object_dict[hash].splice(ix, 1)
218
}else{
219
throw KeyError.$factory(_b_.str.$factory(arg))
227
var $ = $B.args("__eq__", 2, {self: null, other: null},
228
["self", "other"], arguments, {}, null, null),
234
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
235
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
245
if(other.$numeric_dict.hasOwnProperty(k)){
246
if(!$B.rich_comp("__eq__", other.$numeric_dict[k],
247
self.$numeric_dict[k])){
248
return false
249
}
250
}else if(other.$object_dict.hasOwnProperty(k)){
251
var pairs = other.$object_dict[k],
252
flag = false
253
for(var i = 0, len = pairs.length; i < len; i++){
254
if($B.rich_comp("__eq__", k, pairs[i][0]) &&
255
$B.rich_comp("__eq__", self.$numeric_dict[k],
256
pairs[i][1])){
257
flag = true
258
break
259
}
267
if(!other.$string_dict.hasOwnProperty(k) ||
268
!$B.rich_comp("__eq__", other.$string_dict[k],
274
var pairs = self.$object_dict[hash]
275
// Get all (key, value) pairs in other that have the same hash
276
var other_pairs = []
277
if(other.$numeric_dict[hash] !== undefined){
278
other_pairs.push([hash, other.$numeric_dict[hash]])
279
}
281
other_pairs = other_pairs.concat(other.$object_dict[hash])
282
}
283
if(other_pairs.length == 0){
284
return false
285
}
286
for(var i = 0, len_i = pairs.length; i < len_i; i++){
287
var flag = false
288
var key = pairs[i][0],
289
value = pairs[i][1]
290
for(var j = 0, len_j = other_pairs.length; j < len_j; j++){
291
if($B.rich_comp("__eq__", key, other_pairs[j][0]) &&
292
$B.rich_comp("__eq__", value, other_pairs[j][1])){
293
flag = true
294
break
306
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
307
["self", "arg"], arguments, {}, null, null),
311
if(!self.$jsobj.hasOwnProperty(arg)){
312
throw _b_.KeyError.$factory(str.$factory(arg))
313
}else if(self.$jsobj[arg] === undefined){
318
319
switch(typeof arg){
320
case "string":
321
if(self.$string_dict[arg] !== undefined){
322
return self.$string_dict[arg]
324
break
325
case "number":
326
if(self.$numeric_dict[arg] !== undefined){
327
return self.$numeric_dict[arg]
329
break
330
}
331
332
// since the key is more complex use 'default' method of getting item
333
342
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
343
return self.$numeric_dict[hash]
344
}
345
if(isinstance(arg, _b_.str)){
346
// string subclass
347
var res = self.$string_dict[arg.valueOf()]
348
if(res !== undefined){return res}
349
}
350
351
var ix = rank(self, hash, arg)
352
if(ix > -1){
353
return self.$object_dict[hash][ix][1]
379
switch(typeof item[0]) {
380
case 'string':
381
self.$string_dict[item[0]] = item[1]
382
self.$str_hash[str_hash(item[0])] = item[0]
383
break
384
case 'number':
385
self.$numeric_dict[item[0]] = item[1]
386
break
387
default:
388
si(self, item[0], item[1])
389
break
390
}
396
if(first === undefined){return $N}
397
if(second === undefined){
398
if(first.__class__ === $B.JSObject){
399
self.$jsobj = first.js
400
return $N
401
}else if(first.$jsobj){
402
self.$jsobj = {}
403
for(var attr in first.$jsobj){
404
self.$jsobj[attr] = first.$jsobj[attr]
414
arguments, {}, "first", "second")
415
var args = $.first
416
if(args.length > 1){
417
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
418
", got 2")
419
}else if(args.length == 1){
420
args = args[0]
421
if(args.__class__ === dict){
422
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
423
forEach(function(d){
424
for(key in args[d]){self[d][key] = args[d][key]}
425
})
429
var keys = $B.$getattr(args, "keys", null)
430
if(keys !== null){
431
var gi = $B.$getattr(args, "__getitem__", null)
432
if(gi !== null){
433
// has keys and __getitem__ : it's a mapping, iterate on
434
// keys and values
435
gi = $B.$call(gi)
436
var kiter = _b_.iter($B.$call(keys)())
437
while(true){
438
try{
439
var key = _b_.next(kiter),
440
value = gi(key)
441
dict.__setitem__(self, key, value)
442
}catch(err){
443
if(err.__class__ === _b_.StopIteration){
444
break
445
}
446
throw err
447
}
448
}
449
return $N
450
}
451
}
452
if(! Array.isArray(args)){
453
args = _b_.list.$factory(args)
454
}
455
// Form "dict([[key1, value1], [key2,value2], ...])"
461
switch(typeof attr){
462
case "string":
463
self.$string_dict[attr] = kw[attr]
464
self.$str_hash[str_hash(attr)] = attr
465
break
466
case "number":
467
self.$numeric_dict[attr] = kw[attr]
468
break
469
default:
470
si(self, attr, kw[attr])
471
break
472
}
489
for(var k in self.$numeric_dict){_count++}
490
for(var k in self.$string_dict){_count++}
491
for(var hash in self.$object_dict){
492
_count += self.$object_dict[hash].length
493
}
512
if(cls !== dict){
513
instance.__dict__ = _b_.dict.$factory()
514
}
515
return instance
524
items.forEach(function(item){
525
if((!self.$jsobj && item[1] === self) ||
526
(self.$jsobj && item[1] === self.$jsobj)){
537
["self", "key", "value"], arguments, {}, null, null)
538
return dict.$setitem($.self, $.key, $.value)
539
}
541
dict.$setitem = function(self, key, value, $hash){
542
// Parameter $hash is only set if this method is called by setdefault.
543
// In this case the hash of key has already been computed and we
544
// know that the key is not present in the dictionary, so it's no
545
// use computing hash(key) again, nor testing equality of keys
547
if(self.$from_js){
548
// dictionary created by method to_dict of JSObject instances
549
value = $B.pyobj2jsobj(value)
550
}
554
// If class attribute __init__ or __new__ are reset,
555
// the factory function has to change
556
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
557
}
558
}else{
581
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
582
self.$numeric_dict[hash] = value
593
// If $setitem is called from setdefault, don't test equality of key
594
// with any object
595
if($hash){
596
if(self.$object_dict[$hash] !== undefined){
597
self.$object_dict[$hash].push([key, value])
598
}else{
599
self.$object_dict[$hash] = [[key, value]]
600
}
601
self.$version++
602
return $N
603
}
604
var ix = rank(self, hash, key)
605
if(ix > -1){
606
// reset value
607
self.$object_dict[hash][ix][1] = value
608
return $N
609
}else if(self.$object_dict.hasOwnProperty(hash)){
610
self.$object_dict[hash].push([key, value])
611
}else{
612
self.$object_dict[hash] = [[key, value]]
657
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
658
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
670
if(klass === dict){dict.$setitem(res, key, value)}
671
else{$B.$getattr(res, "__setitem__")(key, value)}
682
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
683
["self", "key", "_default"], arguments, {_default: $N}, null, null)
686
catch(err){
687
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
688
else{throw err}
689
}
690
}
691
692
var dict_items = $B.make_view("dict_items", true)
693
dict_items.$iterator = $B.make_iterator_class("dict_itemiterator")
697
var _len = arguments.length - 1,
698
_msg = "items() takes no arguments (" + _len + " given)"
701
var it = dict_items.$factory(to_list(self))
702
it.len_func = function(){return dict.__len__(self)}
703
return it
706
var dict_keys = $B.make_view("dict_keys", true)
707
dict_keys.$iterator = $B.make_iterator_class("dict_keyiterator")
711
var _len = arguments.length - 1,
712
_msg = "keys() takes no arguments (" + _len + " given)"
715
var it = dict_keys.$factory(to_list(self, 0))
716
it.len_func = function(){return dict.__len__(self)}
717
return it
722
var missing = {},
723
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
724
["self", "key", "_default"], arguments, {_default: missing}, null, null),
756
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
757
["self", "key", "_default"], arguments, {_default: $N}, null, null),
768
var hash = key.$hash
769
key.$hash = undefined
770
dict.$setitem(self, key, _default, hash)
777
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
778
{}, "args", "kw"),
779
self = $.self,
780
args = $.args,
781
kw = $.kw
782
if(args.length > 0){
783
var o = args[0]
784
if(isinstance(o, dict)){
790
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))())
791
for(var i = 0, len = _keys.length; i < len; i++){
793
dict.$setitem(self, _keys[i], _value)
794
}
795
}else{
796
var it = _b_.iter(o),
797
i = 0
798
while(true){
799
try{
800
var item = _b_.next(it)
801
}catch(err){
802
if(err.__class__ === _b_.StopIteration){break}
803
throw err
804
}
805
try{
806
key_value = _b_.list.$factory(item)
807
}catch(err){
808
throw _b_.TypeError.$factory("cannot convert dictionary" +
809
" update sequence element #" + i + " to a sequence")
810
}
811
if(key_value.length !== 2){
812
throw _b_.ValueError.$factory("dictionary update " +
813
"sequence element #" + i + " has length " +
814
key_value.length + "; 2 is required")
815
}
816
dict.$setitem(self, key_value[0], key_value[1])
817
i++
826
var dict_values = $B.make_view("dict_values")
827
dict_values.$iterator = $B.make_iterator_class("dict_valueiterator")
831
var _len = arguments.length - 1,
832
_msg = "values() takes no arguments (" + _len + " given)"
835
var it = dict_values.$factory(to_list(self, 1))
836
it.len_func = function(){return dict.__len__(self)}
837
return it
842
var args = [res]
843
for(var i = 0, len = arguments.length; i < len ; i++){
844
args.push(arguments[i])
845
}
846
dict.__init__.apply(null, args)
854
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
855
// have the attribute $infos
856
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
857
873
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
874
"item assignment")
877
for(var attr in dict){
878
if(mappingproxy[attr] !== undefined ||
879
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
880
"clear", "fromkeys", "pop", "popitem", "setdefault",
881
"update"].indexOf(attr) > -1){
882
continue
883
}
884
if(typeof dict[attr] == "function"){
885
mappingproxy[attr] = (function(key){
886
return function(){
887
return dict[key].apply(null, arguments)
888
}
889
})(attr)
890
}else{
891
mappingproxy[attr] = dict[attr]
892
}
893
}
894
915
if(klass !== undefined && klass.$native){
916
throw _b_.AttributeError.$factory(klass.__name__ +