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
926 lines (826 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
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
344
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
345
return self.$numeric_dict[hash]
346
}
347
if(isinstance(arg, _b_.str)){
348
// string subclass
349
var res = self.$string_dict[arg.valueOf()]
350
if(res !== undefined){return res}
351
}
352
353
var ix = rank(self, hash, arg)
354
if(ix > -1){
355
return self.$object_dict[hash][ix][1]
381
switch(typeof item[0]) {
382
case 'string':
383
self.$string_dict[item[0]] = item[1]
384
self.$str_hash[str_hash(item[0])] = item[0]
385
break
386
case 'number':
387
self.$numeric_dict[item[0]] = item[1]
388
break
389
default:
390
si(self, item[0], item[1])
391
break
392
}
398
if(first === undefined){return $N}
399
if(second === undefined){
400
if(first.__class__ === $B.JSObject){
401
self.$jsobj = first.js
402
return $N
403
}else if(first.$jsobj){
404
self.$jsobj = {}
405
for(var attr in first.$jsobj){
406
self.$jsobj[attr] = first.$jsobj[attr]
416
arguments, {}, "first", "second")
417
var args = $.first
418
if(args.length > 1){
419
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
420
", got 2")
421
}else if(args.length == 1){
422
args = args[0]
423
if(args.__class__ === dict){
424
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
425
forEach(function(d){
426
for(key in args[d]){self[d][key] = args[d][key]}
427
})
431
var keys = $B.$getattr(args, "keys", null)
432
if(keys !== null){
433
var gi = $B.$getattr(args, "__getitem__", null)
434
if(gi !== null){
435
// has keys and __getitem__ : it's a mapping, iterate on
436
// keys and values
437
gi = $B.$call(gi)
438
var kiter = _b_.iter($B.$call(keys)())
439
while(true){
440
try{
441
var key = _b_.next(kiter),
442
value = gi(key)
443
dict.__setitem__(self, key, value)
444
}catch(err){
445
if(err.__class__ === _b_.StopIteration){
446
break
447
}
448
throw err
449
}
450
}
451
return $N
452
}
453
}
454
if(! Array.isArray(args)){
455
args = _b_.list.$factory(args)
456
}
457
// Form "dict([[key1, value1], [key2,value2], ...])"
463
switch(typeof attr){
464
case "string":
465
self.$string_dict[attr] = kw[attr]
466
self.$str_hash[str_hash(attr)] = attr
467
break
468
case "number":
469
self.$numeric_dict[attr] = kw[attr]
470
break
471
default:
472
si(self, attr, kw[attr])
473
break
474
}
491
for(var k in self.$numeric_dict){_count++}
492
for(var k in self.$string_dict){_count++}
493
for(var hash in self.$object_dict){
494
_count += self.$object_dict[hash].length
495
}
514
if(cls !== dict){
515
instance.__dict__ = _b_.dict.$factory()
516
}
517
return instance
526
items.forEach(function(item){
527
if((!self.$jsobj && item[1] === self) ||
528
(self.$jsobj && item[1] === self.$jsobj)){
539
["self", "key", "value"], arguments, {}, null, null)
540
return dict.$setitem($.self, $.key, $.value)
541
}
543
dict.$setitem = function(self, key, value, $hash){
544
// Parameter $hash is only set if this method is called by setdefault.
545
// In this case the hash of key has already been computed and we
546
// know that the key is not present in the dictionary, so it's no
547
// use computing hash(key) again, nor testing equality of keys
549
if(self.$from_js){
550
// dictionary created by method to_dict of JSObject instances
551
value = $B.pyobj2jsobj(value)
552
}
556
// If class attribute __init__ or __new__ are reset,
557
// the factory function has to change
558
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
559
}
560
}else{
583
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
584
self.$numeric_dict[hash] = value
595
// If $setitem is called from setdefault, don't test equality of key
596
// with any object
597
if($hash){
598
if(self.$object_dict[$hash] !== undefined){
599
self.$object_dict[$hash].push([key, value])
600
}else{
601
self.$object_dict[$hash] = [[key, value]]
602
}
603
self.$version++
604
return $N
605
}
606
var ix = rank(self, hash, key)
607
if(ix > -1){
608
// reset value
609
self.$object_dict[hash][ix][1] = value
610
return $N
611
}else if(self.$object_dict.hasOwnProperty(hash)){
612
self.$object_dict[hash].push([key, value])
613
}else{
614
self.$object_dict[hash] = [[key, value]]
659
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
660
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
672
if(klass === dict){dict.$setitem(res, key, value)}
673
else{$B.$getattr(res, "__setitem__")(key, value)}
684
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
685
["self", "key", "_default"], arguments, {_default: $N}, null, null)
688
catch(err){
689
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
690
else{throw err}
691
}
692
}
693
694
var dict_items = $B.make_view("dict_items", true)
695
dict_items.$iterator = $B.make_iterator_class("dict_itemiterator")
699
var _len = arguments.length - 1,
700
_msg = "items() takes no arguments (" + _len + " given)"
703
var it = dict_items.$factory(to_list(self))
704
it.len_func = function(){return dict.__len__(self)}
705
return it
708
var dict_keys = $B.make_view("dict_keys", true)
709
dict_keys.$iterator = $B.make_iterator_class("dict_keyiterator")
713
var _len = arguments.length - 1,
714
_msg = "keys() takes no arguments (" + _len + " given)"
717
var it = dict_keys.$factory(to_list(self, 0))
718
it.len_func = function(){return dict.__len__(self)}
719
return it
724
var missing = {},
725
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
726
["self", "key", "_default"], arguments, {_default: missing}, null, null),
758
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
759
["self", "key", "_default"], arguments, {_default: $N}, null, null),
770
var hash = key.$hash
771
key.$hash = undefined
772
dict.$setitem(self, key, _default, hash)
779
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
780
{}, "args", "kw"),
781
self = $.self,
782
args = $.args,
783
kw = $.kw
784
if(args.length > 0){
785
var o = args[0]
786
if(isinstance(o, dict)){
792
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))())
793
for(var i = 0, len = _keys.length; i < len; i++){
795
dict.$setitem(self, _keys[i], _value)
796
}
797
}else{
798
var it = _b_.iter(o),
799
i = 0
800
while(true){
801
try{
802
var item = _b_.next(it)
803
}catch(err){
804
if(err.__class__ === _b_.StopIteration){break}
805
throw err
806
}
807
try{
808
key_value = _b_.list.$factory(item)
809
}catch(err){
810
throw _b_.TypeError.$factory("cannot convert dictionary" +
811
" update sequence element #" + i + " to a sequence")
812
}
813
if(key_value.length !== 2){
814
throw _b_.ValueError.$factory("dictionary update " +
815
"sequence element #" + i + " has length " +
816
key_value.length + "; 2 is required")
817
}
818
dict.$setitem(self, key_value[0], key_value[1])
819
i++
828
var dict_values = $B.make_view("dict_values")
829
dict_values.$iterator = $B.make_iterator_class("dict_valueiterator")
833
var _len = arguments.length - 1,
834
_msg = "values() takes no arguments (" + _len + " given)"
837
var it = dict_values.$factory(to_list(self, 1))
838
it.len_func = function(){return dict.__len__(self)}
839
return it
844
var args = [res]
845
for(var i = 0, len = arguments.length; i < len ; i++){
846
args.push(arguments[i])
847
}
848
dict.__init__.apply(null, args)
856
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
857
// have the attribute $infos
858
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
859
875
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
876
"item assignment")
879
for(var attr in dict){
880
if(mappingproxy[attr] !== undefined ||
881
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
882
"clear", "fromkeys", "pop", "popitem", "setdefault",
883
"update"].indexOf(attr) > -1){
884
continue
885
}
886
if(typeof dict[attr] == "function"){
887
mappingproxy[attr] = (function(key){
888
return function(){
889
return dict[key].apply(null, arguments)
890
}
891
})(attr)
892
}else{
893
mappingproxy[attr] = dict[attr]
894
}
895
}
896
917
if(klass !== undefined && klass.$native){
918
throw _b_.AttributeError.$factory(klass.__name__ +