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
Dec 13, 2018
Aug 2, 2018
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
891 lines (791 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]
79
right.$version = right.$version || 0
80
var right_version = right.$version || 0
81
while(i--){
82
si(left, _l[i][0], _l[i][1])
83
if(right.$version != right_version){
84
throw _b_.RuntimeError.$factory("dict mutated during update")
85
}
86
}
89
function rank(self, hash, key){
90
// Search if object key, with hash = hash(key), is in
91
// self.$object_dict
92
var pairs = self.$object_dict[hash]
93
if(pairs !== undefined){
94
for(var i = 0, len = pairs.length; i < len; i++){
95
if($B.rich_comp("__eq__", key, pairs[i][0])){
96
return i
97
}
98
}
99
}
100
return -1
101
}
102
111
var $ = $B.args("__contains__", 2, {self: null, key: null},
112
["self", "key"], arguments, {}, null, null),
122
return self.$numeric_dict[key] !== undefined
123
}
124
125
var hash = _b_.hash(key)
126
if(self.$str_hash[hash] !== undefined &&
127
$B.rich_comp("__eq__", key, self.$str_hash[hash])){return true}
128
if(self.$numeric_dict[hash] !== undefined &&
129
$B.rich_comp("__eq__", key, hash)){return true}
130
return rank(self, hash, key) > -1
135
var $ = $B.args("__eq__", 2, {self: null, arg: null},
136
["self", "arg"], arguments, {}, null, null),
141
if(self.$jsobj[arg] === undefined){throw KeyError.$factory(arg)}
142
delete self.$jsobj[arg]
145
switch(typeof arg){
146
case "string":
147
if(self.$string_dict[arg] === undefined){
148
throw KeyError.$factory(_b_.str.$factory(arg))
149
}
150
delete self.$string_dict[arg]
151
delete self.$str_hash[str_hash(arg)]
154
case "number":
155
if(self.$numeric_dict[arg] === undefined){
156
throw KeyError.$factory(_b_.str.$factory(arg))
167
if((ix = rank(self, hash, arg)) > -1){
168
self.$object_dict[hash].splice(ix, 1)
169
}else{
170
throw KeyError.$factory(_b_.str.$factory(arg))
178
var $ = $B.args("__eq__", 2, {self: null, other: null},
179
["self", "other"], arguments, {}, null, null),
185
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
186
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
196
if(other.$numeric_dict.hasOwnProperty(k)){
197
if(!$B.rich_comp("__eq__", other.$numeric_dict[k],
198
self.$numeric_dict[k])){
199
return false
200
}
201
}else if(other.$object_dict.hasOwnProperty(k)){
202
var pairs = other.$object_dict[k],
203
flag = false
204
for(var i = 0, len = pairs.length; i < len; i++){
205
if($B.rich_comp("__eq__", k, pairs[i][0]) &&
206
$B.rich_comp("__eq__", self.$numeric_dict[k],
207
pairs[i][1])){
208
flag = true
209
break
210
}
218
if(!other.$string_dict.hasOwnProperty(k) ||
219
!$B.rich_comp("__eq__", other.$string_dict[k],
225
var pairs = self.$object_dict[hash]
226
// Get all (key, value) pairs in other that have the same hash
227
var other_pairs = []
228
if(other.$numeric_dict[hash] !== undefined){
229
other_pairs.push([hash, other.$numeric_dict[hash]])
230
}
232
other_pairs = other_pairs.concat(other.$object_dict[hash])
233
}
234
if(other_pairs.length == 0){
235
return false
236
}
237
for(var i = 0, len_i = pairs.length; i < len_i; i++){
238
var flag = false
239
var key = pairs[i][0],
240
value = pairs[i][1]
241
for(var j = 0, len_j = other_pairs.length; j < len_j; j++){
242
if($B.rich_comp("__eq__", key, other_pairs[j][0]) &&
243
$B.rich_comp("__eq__", value, other_pairs[j][1])){
244
flag = true
245
break
257
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
258
["self", "arg"], arguments, {}, null, null),
262
if(!self.$jsobj.hasOwnProperty(arg)){
263
throw _b_.KeyError.$factory(str.$factory(arg))
264
}else if(self.$jsobj[arg] === undefined){
269
270
switch(typeof arg){
271
case "string":
272
if(self.$string_dict[arg] !== undefined){
273
return self.$string_dict[arg]
275
break
276
case "number":
277
if(self.$numeric_dict[arg] !== undefined){
278
return self.$numeric_dict[arg]
280
break
281
}
282
283
// since the key is more complex use 'default' method of getting item
284
293
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
294
return self.$numeric_dict[hash]
295
}
296
if(isinstance(arg, _b_.str)){
297
// string subclass
298
var res = self.$string_dict[arg.valueOf()]
299
if(res !== undefined){return res}
300
}
301
302
var ix = rank(self, hash, arg)
303
if(ix > -1){
304
return self.$object_dict[hash][ix][1]
330
switch(typeof item[0]) {
331
case 'string':
332
self.$string_dict[item[0]] = item[1]
333
self.$str_hash[str_hash(item[0])] = item[0]
334
break
335
case 'number':
336
self.$numeric_dict[item[0]] = item[1]
337
break
338
default:
339
si(self, item[0], item[1])
340
break
341
}
347
if(first === undefined){return $N}
348
if(second === undefined){
349
if(first.__class__ === $B.JSObject){
350
self.$jsobj = first.js
351
return $N
352
}else if(first.$jsobj){
353
self.$jsobj = {}
354
for(var attr in first.$jsobj){
355
self.$jsobj[attr] = first.$jsobj[attr]
365
arguments, {}, "first", "second")
366
var args = $.first
367
if(args.length > 1){
368
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
369
", got 2")
370
}else if(args.length == 1){
371
args = args[0]
372
if(args.__class__ === dict){
373
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
374
forEach(function(d){
375
for(key in args[d]){self[d][key] = args[d][key]}
376
})
380
var keys = $B.$getattr(args, "keys", null)
381
if(keys !== null){
382
var gi = $B.$getattr(args, "__getitem__", null)
383
if(gi !== null){
384
// has keys and __getitem__ : it's a mapping, iterate on
385
// keys and values
386
gi = $B.$call(gi)
387
var kiter = _b_.iter($B.$call(keys)())
388
while(true){
389
try{
390
var key = _b_.next(kiter),
391
value = gi(key)
392
dict.__setitem__(self, key, value)
393
}catch(err){
394
if(err.__class__ === _b_.StopIteration){
395
break
396
}
397
throw err
398
}
399
}
400
return $N
401
}
402
}
403
if(! Array.isArray(args)){
404
args = _b_.list.$factory(args)
405
}
406
// Form "dict([[key1, value1], [key2,value2], ...])"
412
switch(typeof attr){
413
case "string":
414
self.$string_dict[attr] = kw[attr]
415
self.$str_hash[str_hash(attr)] = attr
416
break
417
case "number":
418
self.$numeric_dict[attr] = kw[attr]
419
break
420
default:
421
si(self, attr, kw[attr])
422
break
423
}
440
for(var k in self.$numeric_dict){_count++}
441
for(var k in self.$string_dict){_count++}
442
for(var hash in self.$object_dict){
443
_count += self.$object_dict[hash].length
444
}
463
if(cls !== dict){
464
instance.__dict__ = _b_.dict.$factory()
465
}
466
return instance
475
items.forEach(function(item){
476
if((!self.$jsobj && item[1] === self) ||
477
(self.$jsobj && item[1] === self.$jsobj)){
488
["self", "key", "value"], arguments, {}, null, null)
489
return dict.$setitem($.self, $.key, $.value)
490
}
492
dict.$setitem = function(self, key, value, $hash){
493
// Parameter $hash is only set if this method is called by setdefault.
494
// In this case the hash of key has already been computed and we
495
// know that the key is not present in the dictionary, so it's no
496
// use computing hash(key) again, nor testing equality of keys
498
if(self.$from_js){
499
// dictionary created by method to_dict of JSObject instances
500
value = $B.pyobj2jsobj(value)
501
}
505
// If class attribute __init__ or __new__ are reset,
506
// the factory function has to change
507
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
508
}
509
}else{
532
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
533
self.$numeric_dict[hash] = value
544
// If $setitem is called from setdefault, don't test equality of key
545
// with any object
546
if($hash){
547
if(self.$object_dict[$hash] !== undefined){
548
self.$object_dict[$hash].push([key, value])
549
}else{
550
self.$object_dict[$hash] = [[key, value]]
551
}
552
self.$version++
553
return $N
554
}
555
var ix = rank(self, hash, key)
556
if(ix > -1){
557
// reset value
558
self.$object_dict[hash][ix][1] = value
559
return $N
560
}else if(self.$object_dict.hasOwnProperty(hash)){
561
self.$object_dict[hash].push([key, value])
562
}else{
563
self.$object_dict[hash] = [[key, value]]
608
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
609
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
621
if(klass === dict){dict.$setitem(res, key, value)}
622
else{$B.$getattr(res, "__setitem__")(key, value)}
633
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
634
["self", "key", "_default"], arguments, {_default: $N}, null, null)
637
catch(err){
638
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
639
else{throw err}
640
}
641
}
642
643
var dict_items = $B.make_iterator_class("dict_items")
644
645
dict_items.__eq__ = function(self, other){
646
// compare set of items to other
647
return $B.rich_comp("__eq__", _b_.set.$factory(self),
648
_b_.set.$factory(other))
649
}
650
651
dict_items.__next__ = dict_iterator_next
655
var _len = arguments.length - 1,
656
_msg = "items() takes no arguments (" + _len + " given)"
659
var it = dict_items.$factory(to_list(self))
660
it.len_func = function(){return dict.__len__(self)}
661
return it
664
var dict_keys = $B.make_iterator_class("dict_keys")
665
666
dict_keys.__eq__ = function(self, other){
667
// compare set of items to other
668
return $B.rich_comp("__eq__", _b_.set.$factory(self),
669
_b_.set.$factory(other))
670
}
671
672
dict_keys.__next__ = dict_iterator_next
676
var _len = arguments.length - 1,
677
_msg = "keys() takes no arguments (" + _len + " given)"
680
var it = dict_keys.$factory(to_list(self, 0))
681
it.len_func = function(){return dict.__len__(self)}
682
return it
687
var missing = {},
688
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
689
["self", "key", "_default"], arguments, {_default: missing}, null, null),
721
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
722
["self", "key", "_default"], arguments, {_default: $N}, null, null),
733
var hash = key.$hash
734
key.$hash = undefined
735
dict.$setitem(self, key, _default, hash)
742
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
743
{}, "args", "kw"),
744
self = $.self,
745
args = $.args,
746
kw = $.kw
747
if(args.length > 0){
748
var o = args[0]
749
if(isinstance(o, dict)){
754
}else if(hasattr(o, "keys")){
755
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))()),
756
i = _keys.length
757
while(i--){
758
var _value = getattr(o, "__getitem__")(_keys[i])
759
dict.$setitem(self, _keys[i], _value)
760
}
761
}else{
762
var it = _b_.iter(o),
763
i = 0
764
while(true){
765
try{
766
var item = _b_.next(it)
767
}catch(err){
768
if(err.__class__ === _b_.StopIteration){break}
769
throw err
770
}
771
try{
772
key_value = _b_.list.$factory(item)
773
}catch(err){
774
throw _b_.TypeError.$factory("cannot convert dictionary" +
775
" update sequence element #" + i + " to a sequence")
776
}
777
if(key_value.length !== 2){
778
throw _b_.ValueError.$factory("dictionary update " +
779
"sequence element #" + i + " has length " +
780
key_value.length + "; 2 is required")
781
}
782
dict.$setitem(self, key_value[0], key_value[1])
783
i++
792
var dict_values = $B.make_iterator_class("dict_values")
793
794
dict_values.__next__ = dict_iterator_next
798
var _len = arguments.length - 1,
799
_msg = "values() takes no arguments (" + _len + " given)"
802
var it = dict_values.$factory(to_list(self, 1))
803
it.len_func = function(){return dict.__len__(self)}
804
return it
809
var args = [res]
810
for(var i = 0, len = arguments.length; i < len ; i++){
811
args.push(arguments[i])
812
}
813
dict.__init__.apply(null, args)
821
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
822
// have the attribute $infos
823
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
824
840
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
841
"item assignment")
844
for(var attr in dict){
845
if(mappingproxy[attr] !== undefined ||
846
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
847
"clear", "fromkeys", "pop", "popitem", "setdefault",
848
"update"].indexOf(attr) > -1){
849
continue
850
}
851
if(typeof dict[attr] == "function"){
852
mappingproxy[attr] = (function(key){
853
return function(){
854
return dict[key].apply(null, arguments)
855
}
856
})(attr)
857
}else{
858
mappingproxy[attr] = dict[attr]
859
}
860
}
861
882
if(klass !== undefined && klass.$native){
883
throw _b_.AttributeError.$factory(klass.__name__ +