Permalink
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
Apr 3, 2019
Feb 26, 2018
Apr 3, 2019
Apr 3, 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
Apr 3, 2019
Feb 26, 2018
Apr 3, 2019
Dec 1, 2018
Jun 28, 2018
Mar 7, 2018
Aug 2, 2018
Feb 26, 2018
Newer
100644
889 lines (789 sloc)
25.3 KB
35
}else{
36
for(var k in d.$numeric_dict){
37
items.push([parseFloat(k), d.$numeric_dict[k]])
38
}
42
for(var k in d.$object_dict){
43
d.$object_dict[k].forEach(function(item){
44
items.push(item)
45
})
46
}
49
if(ix !== undefined){
50
return items.map(function(item){return item[ix]})
51
}else{
52
items.__class__ = _b_.tuple
53
return items.map(function(item){
54
item.__class__ = _b_.tuple; return item}
55
)
56
}
59
$B.dict_to_list = to_list // used in py_types.js
60
61
// Special version of __next__ for iterators on dict keys / values / items.
62
// Checks that the dictionary size didn't change during iteration.
63
function dict_iterator_next(self){
64
if(self.len_func() != self.len){
65
throw RuntimeError.$factory("dictionary changed size during iteration")
66
}
67
self.counter++
68
if(self.counter < self.items.length){
69
return self.items[self.counter]
81
si(left, _l[i][0], _l[i][1])
82
if(right.$version != right_version){
83
throw _b_.RuntimeError.$factory("dict mutated during update")
84
}
85
}
88
function rank(self, hash, key){
89
// Search if object key, with hash = hash(key), is in
90
// self.$object_dict
91
var pairs = self.$object_dict[hash]
92
if(pairs !== undefined){
93
for(var i = 0, len = pairs.length; i < len; i++){
94
if($B.rich_comp("__eq__", key, pairs[i][0])){
95
return i
96
}
97
}
98
}
99
return -1
100
}
101
110
var $ = $B.args("__contains__", 2, {self: null, key: null},
111
["self", "key"], arguments, {}, null, null),
121
return self.$numeric_dict[key] !== undefined
122
}
123
124
var hash = _b_.hash(key)
125
if(self.$str_hash[hash] !== undefined &&
126
$B.rich_comp("__eq__", key, self.$str_hash[hash])){return true}
127
if(self.$numeric_dict[hash] !== undefined &&
128
$B.rich_comp("__eq__", key, hash)){return true}
129
return rank(self, hash, key) > -1
134
var $ = $B.args("__eq__", 2, {self: null, arg: null},
135
["self", "arg"], arguments, {}, null, null),
140
if(self.$jsobj[arg] === undefined){throw KeyError.$factory(arg)}
141
delete self.$jsobj[arg]
144
switch(typeof arg){
145
case "string":
146
if(self.$string_dict[arg] === undefined){
147
throw KeyError.$factory(_b_.str.$factory(arg))
148
}
149
delete self.$string_dict[arg]
150
delete self.$str_hash[str_hash(arg)]
153
case "number":
154
if(self.$numeric_dict[arg] === undefined){
155
throw KeyError.$factory(_b_.str.$factory(arg))
166
if((ix = rank(self, hash, arg)) > -1){
167
self.$object_dict[hash].splice(ix, 1)
168
}else{
169
throw KeyError.$factory(_b_.str.$factory(arg))
177
var $ = $B.args("__eq__", 2, {self: null, other: null},
178
["self", "other"], arguments, {}, null, null),
184
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
185
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
195
if(other.$numeric_dict.hasOwnProperty(k)){
196
if(!$B.rich_comp("__eq__", other.$numeric_dict[k],
197
self.$numeric_dict[k])){
198
return false
199
}
200
}else if(other.$object_dict.hasOwnProperty(k)){
201
var pairs = other.$object_dict[k],
202
flag = false
203
for(var i = 0, len = pairs.length; i < len; i++){
204
if($B.rich_comp("__eq__", k, pairs[i][0]) &&
205
$B.rich_comp("__eq__", self.$numeric_dict[k],
206
pairs[i][1])){
207
flag = true
208
break
209
}
217
if(!other.$string_dict.hasOwnProperty(k) ||
218
!$B.rich_comp("__eq__", other.$string_dict[k],
224
var pairs = self.$object_dict[hash]
225
// Get all (key, value) pairs in other that have the same hash
226
var other_pairs = []
227
if(other.$numeric_dict[hash] !== undefined){
228
other_pairs.push([hash, other.$numeric_dict[hash]])
229
}
231
other_pairs = other_pairs.concat(other.$object_dict[hash])
232
}
233
if(other_pairs.length == 0){
234
return false
235
}
236
for(var i = 0, len_i = pairs.length; i < len_i; i++){
237
var flag = false
238
var key = pairs[i][0],
239
value = pairs[i][1]
240
for(var j = 0, len_j = other_pairs.length; j < len_j; j++){
241
if($B.rich_comp("__eq__", key, other_pairs[j][0]) &&
242
$B.rich_comp("__eq__", value, other_pairs[j][1])){
243
flag = true
244
break
256
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
257
["self", "arg"], arguments, {}, null, null),
261
if(!self.$jsobj.hasOwnProperty(arg)){
262
throw _b_.KeyError.$factory(str.$factory(arg))
263
}else if(self.$jsobj[arg] === undefined){
268
269
switch(typeof arg){
270
case "string":
271
if(self.$string_dict[arg] !== undefined){
272
return self.$string_dict[arg]
274
break
275
case "number":
276
if(self.$numeric_dict[arg] !== undefined){
277
return self.$numeric_dict[arg]
279
break
280
}
281
282
// since the key is more complex use 'default' method of getting item
283
292
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
293
return self.$numeric_dict[hash]
294
}
295
if(isinstance(arg, _b_.str)){
296
// string subclass
297
var res = self.$string_dict[arg.valueOf()]
298
if(res !== undefined){return res}
299
}
300
301
var ix = rank(self, hash, arg)
302
if(ix > -1){
303
return self.$object_dict[hash][ix][1]
329
switch(typeof item[0]) {
330
case 'string':
331
self.$string_dict[item[0]] = item[1]
332
self.$str_hash[str_hash(item[0])] = item[0]
333
break
334
case 'number':
335
self.$numeric_dict[item[0]] = item[1]
336
break
337
default:
338
si(self, item[0], item[1])
339
break
340
}
346
if(first === undefined){return $N}
347
if(second === undefined){
348
if(first.__class__ === $B.JSObject){
349
self.$jsobj = first.js
350
return $N
351
}else if(first.$jsobj){
352
self.$jsobj = {}
353
for(var attr in first.$jsobj){
354
self.$jsobj[attr] = first.$jsobj[attr]
364
arguments, {}, "first", "second")
365
var args = $.first
366
if(args.length > 1){
367
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
368
", got 2")
369
}else if(args.length == 1){
370
args = args[0]
371
if(args.__class__ === dict){
372
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
373
forEach(function(d){
374
for(key in args[d]){self[d][key] = args[d][key]}
375
})
379
var keys = $B.$getattr(args, "keys", null)
380
if(keys !== null){
381
var gi = $B.$getattr(args, "__getitem__", null)
382
if(gi !== null){
383
// has keys and __getitem__ : it's a mapping, iterate on
384
// keys and values
385
gi = $B.$call(gi)
386
var kiter = _b_.iter($B.$call(keys)())
387
while(true){
388
try{
389
var key = _b_.next(kiter),
390
value = gi(key)
391
dict.__setitem__(self, key, value)
392
}catch(err){
393
if(err.__class__ === _b_.StopIteration){
394
break
395
}
396
throw err
397
}
398
}
399
return $N
400
}
401
}
402
if(! Array.isArray(args)){
403
args = _b_.list.$factory(args)
404
}
405
// Form "dict([[key1, value1], [key2,value2], ...])"
411
switch(typeof attr){
412
case "string":
413
self.$string_dict[attr] = kw[attr]
414
self.$str_hash[str_hash(attr)] = attr
415
break
416
case "number":
417
self.$numeric_dict[attr] = kw[attr]
418
break
419
default:
420
si(self, attr, kw[attr])
421
break
422
}
439
for(var k in self.$numeric_dict){_count++}
440
for(var k in self.$string_dict){_count++}
441
for(var hash in self.$object_dict){
442
_count += self.$object_dict[hash].length
443
}
462
if(cls !== dict){
463
instance.__dict__ = _b_.dict.$factory()
464
}
465
return instance
474
items.forEach(function(item){
475
if((!self.$jsobj && item[1] === self) ||
476
(self.$jsobj && item[1] === self.$jsobj)){
487
["self", "key", "value"], arguments, {}, null, null)
488
return dict.$setitem($.self, $.key, $.value)
489
}
491
dict.$setitem = function(self, key, value, $hash){
492
// Parameter $hash is only set if this method is called by setdefault.
493
// In this case the hash of key has already been computed and we
494
// know that the key is not present in the dictionary, so it's no
495
// use computing hash(key) again, nor testing equality of keys
497
if(self.$from_js){
498
// dictionary created by method to_dict of JSObject instances
499
value = $B.pyobj2jsobj(value)
500
}
504
// If class attribute __init__ or __new__ are reset,
505
// the factory function has to change
506
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
507
}
508
}else{
531
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
532
self.$numeric_dict[hash] = value
543
// If $setitem is called from setdefault, don't test equality of key
544
// with any object
545
if($hash){
546
if(self.$object_dict[$hash] !== undefined){
547
self.$object_dict[$hash].push([key, value])
548
}else{
549
self.$object_dict[$hash] = [[key, value]]
550
}
551
self.$version++
552
return $N
553
}
554
var ix = rank(self, hash, key)
555
if(ix > -1){
556
// reset value
557
self.$object_dict[hash][ix][1] = value
558
return $N
559
}else if(self.$object_dict.hasOwnProperty(hash)){
560
self.$object_dict[hash].push([key, value])
561
}else{
562
self.$object_dict[hash] = [[key, value]]
607
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
608
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
620
if(klass === dict){dict.$setitem(res, key, value)}
621
else{$B.$getattr(res, "__setitem__")(key, value)}
632
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
633
["self", "key", "_default"], arguments, {_default: $N}, null, null)
636
catch(err){
637
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
638
else{throw err}
639
}
640
}
641
642
var dict_items = $B.make_iterator_class("dict_items")
643
644
dict_items.__eq__ = function(self, other){
645
// compare set of items to other
646
return $B.rich_comp("__eq__", _b_.set.$factory(self),
647
_b_.set.$factory(other))
648
}
649
650
dict_items.__next__ = dict_iterator_next
654
var _len = arguments.length - 1,
655
_msg = "items() takes no arguments (" + _len + " given)"
658
var it = dict_items.$factory(to_list(self))
659
it.len_func = function(){return dict.__len__(self)}
660
return it
663
var dict_keys = $B.make_iterator_class("dict_keys")
664
665
dict_keys.__eq__ = function(self, other){
666
// compare set of items to other
667
return $B.rich_comp("__eq__", _b_.set.$factory(self),
668
_b_.set.$factory(other))
669
}
670
671
dict_keys.__next__ = dict_iterator_next
675
var _len = arguments.length - 1,
676
_msg = "keys() takes no arguments (" + _len + " given)"
679
var it = dict_keys.$factory(to_list(self, 0))
680
it.len_func = function(){return dict.__len__(self)}
681
return it
686
var missing = {},
687
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
688
["self", "key", "_default"], arguments, {_default: missing}, null, null),
720
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
721
["self", "key", "_default"], arguments, {_default: $N}, null, null),
732
var hash = key.$hash
733
key.$hash = undefined
734
dict.$setitem(self, key, _default, hash)
741
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
742
{}, "args", "kw"),
743
self = $.self,
744
args = $.args,
745
kw = $.kw
746
if(args.length > 0){
747
var o = args[0]
748
if(isinstance(o, dict)){
754
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))())
755
for(var i = 0, len = _keys.length; i < len; i++){
757
dict.$setitem(self, _keys[i], _value)
758
}
759
}else{
760
var it = _b_.iter(o),
761
i = 0
762
while(true){
763
try{
764
var item = _b_.next(it)
765
}catch(err){
766
if(err.__class__ === _b_.StopIteration){break}
767
throw err
768
}
769
try{
770
key_value = _b_.list.$factory(item)
771
}catch(err){
772
throw _b_.TypeError.$factory("cannot convert dictionary" +
773
" update sequence element #" + i + " to a sequence")
774
}
775
if(key_value.length !== 2){
776
throw _b_.ValueError.$factory("dictionary update " +
777
"sequence element #" + i + " has length " +
778
key_value.length + "; 2 is required")
779
}
780
dict.$setitem(self, key_value[0], key_value[1])
781
i++
790
var dict_values = $B.make_iterator_class("dict_values")
791
792
dict_values.__next__ = dict_iterator_next
796
var _len = arguments.length - 1,
797
_msg = "values() takes no arguments (" + _len + " given)"
800
var it = dict_values.$factory(to_list(self, 1))
801
it.len_func = function(){return dict.__len__(self)}
802
return it
807
var args = [res]
808
for(var i = 0, len = arguments.length; i < len ; i++){
809
args.push(arguments[i])
810
}
811
dict.__init__.apply(null, args)
819
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
820
// have the attribute $infos
821
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
822
838
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
839
"item assignment")
842
for(var attr in dict){
843
if(mappingproxy[attr] !== undefined ||
844
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
845
"clear", "fromkeys", "pop", "popitem", "setdefault",
846
"update"].indexOf(attr) > -1){
847
continue
848
}
849
if(typeof dict[attr] == "function"){
850
mappingproxy[attr] = (function(key){
851
return function(){
852
return dict[key].apply(null, arguments)
853
}
854
})(attr)
855
}else{
856
mappingproxy[attr] = dict[attr]
857
}
858
}
859
880
if(klass !== undefined && klass.$native){
881
throw _b_.AttributeError.$factory(klass.__name__ +