Permalink
Feb 3, 2018
Feb 3, 2018
Jan 15, 2018
Dec 10, 2018
Feb 26, 2018
Feb 26, 2018
Dec 13, 2018
Feb 26, 2018
Dec 11, 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
Feb 26, 2018
Feb 26, 2018
Dec 13, 2018
Mar 7, 2018
Mar 7, 2018
Feb 26, 2018
Dec 13, 2018
Aug 2, 2018
Dec 13, 2018
Feb 26, 2018
Nov 21, 2015
Dec 1, 2018
Jun 28, 2018
Feb 22, 2019
Mar 7, 2018
Aug 2, 2018
Feb 26, 2018
Newer
100644
926 lines (825 sloc)
26.3 KB
21
var $key_iterator = function(d) {
22
this.d = d
23
this.current = 0
24
this.iter = new $item_generator(d)
25
}
26
$key_iterator.prototype.length = function(){return this.iter.items.length}
27
$key_iterator.prototype.next = function(){return this.iter.next()[0]}
28
29
var $value_iterator = function(d) {
30
this.d = d
31
this.current = 0
32
this.iter = new $item_generator(d)
33
}
34
$value_iterator.prototype.length = function(){return this.iter.items.length}
35
$value_iterator.prototype.next = function(){return this.iter.next()[1]}
46
if(val === undefined){val = _b_.NotImplemented}
47
else if(val === null){val = $N}
48
this.items.push([attr, val])
54
var items = []
55
for(var k in d.$numeric_dict){
56
items.push([parseFloat(k), d.$numeric_dict[k]])
61
for(var k in d.$object_dict){
62
d.$object_dict[k].forEach(function(item){
63
items.push(item)
64
})
65
}
78
}
79
80
var $item_iterator = function(d) {
81
this.d = d
82
this.current = 0
83
this.iter = new $item_generator(d)
84
}
85
$item_iterator.prototype.length = function(){return this.iter.items.length}
86
$item_iterator.prototype.next = function(){
87
return _b_.tuple.$factory(this.iter.next())
88
}
90
var $copy_dict = function(left, right){
91
var _l = new $item_generator(right).as_list(),
94
right.$version = right.$version || 0
95
var right_version = right.$version || 0
96
while(i--){
97
si(left, _l[i][0], _l[i][1])
98
if(right.$version != right_version){
99
throw _b_.RuntimeError.$factory("dict mutated during update")
100
}
101
}
112
__iter__: function(){items.iter.i = 0; return res},
113
__len__: function(){return items.length()},
114
__next__: function(){
127
klass.__reduce_ex__ = klass.__reduce__ = function(self){
128
return _b_.tuple.$factory([_b_.iter,
129
_b_.tuple.$factory([_b_.list.$factory(self)])])
130
}
134
function rank(self, hash, key){
135
// Search if object key, with hash = hash(key), is in
136
// self.$object_dict
137
var pairs = self.$object_dict[hash]
138
if(pairs !== undefined){
139
for(var i = 0, len = pairs.length; i < len; i++){
140
if($B.rich_comp("__eq__", key, pairs[i][0])){
141
return i
142
}
143
}
144
}
145
return -1
146
}
147
156
var $ = $B.args("__contains__", 2, {self: null, key: null},
157
["self", "key"], arguments, {}, null, null),
167
return self.$numeric_dict[key] !== undefined
168
}
169
170
var hash = _b_.hash(key)
171
if(self.$str_hash[hash] !== undefined &&
172
$B.rich_comp("__eq__", key, self.$str_hash[hash])){return true}
173
if(self.$numeric_dict[hash] !== undefined &&
174
$B.rich_comp("__eq__", key, hash)){return true}
175
return rank(self, hash, key) > -1
180
var $ = $B.args("__eq__", 2, {self: null, arg: null},
181
["self", "arg"], arguments, {}, null, null),
186
if(self.$jsobj[arg] === undefined){throw KeyError.$factory(arg)}
187
delete self.$jsobj[arg]
190
switch(typeof arg){
191
case "string":
192
if(self.$string_dict[arg] === undefined){
193
throw KeyError.$factory(_b_.str.$factory(arg))
194
}
195
delete self.$string_dict[arg]
196
delete self.$str_hash[str_hash(arg)]
199
case "number":
200
if(self.$numeric_dict[arg] === undefined){
201
throw KeyError.$factory(_b_.str.$factory(arg))
212
if((ix = rank(self, hash, arg)) > -1){
213
self.$object_dict[hash].splice(ix, 1)
214
}else{
215
throw KeyError.$factory(_b_.str.$factory(arg))
223
var $ = $B.args("__eq__", 2, {self: null, other: null},
224
["self", "other"], arguments, {}, null, null),
230
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
231
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
241
if(other.$numeric_dict.hasOwnProperty(k)){
242
if(!$B.rich_comp("__eq__", other.$numeric_dict[k],
243
self.$numeric_dict[k])){
244
return false
245
}
246
}else if(other.$object_dict.hasOwnProperty(k)){
247
var pairs = other.$object_dict[k],
248
flag = false
249
for(var i = 0, len = pairs.length; i < len; i++){
250
if($B.rich_comp("__eq__", k, pairs[i][0]) &&
251
$B.rich_comp("__eq__", self.$numeric_dict[k],
252
pairs[i][1])){
253
flag = true
254
break
255
}
263
if(!other.$string_dict.hasOwnProperty(k) ||
264
!$B.rich_comp("__eq__", other.$string_dict[k],
270
var pairs = self.$object_dict[hash]
271
// Get all (key, value) pairs in other that have the same hash
272
var other_pairs = []
273
if(other.$numeric_dict[hash] !== undefined){
274
other_pairs.push([hash, other.$numeric_dict[hash]])
275
}
277
other_pairs = other_pairs.concat(other.$object_dict[hash])
278
}
279
if(other_pairs.length == 0){
280
return false
281
}
282
for(var i = 0, len_i = pairs.length; i < len_i; i++){
283
var flag = false
284
var key = pairs[i][0],
285
value = pairs[i][1]
286
for(var j = 0, len_j = other_pairs.length; j < len_j; j++){
287
if($B.rich_comp("__eq__", key, other_pairs[j][0]) &&
288
$B.rich_comp("__eq__", value, other_pairs[j][1])){
289
flag = true
290
break
302
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
303
["self", "arg"], arguments, {}, null, null),
307
if(!self.$jsobj.hasOwnProperty(arg)){
308
throw _b_.KeyError.$factory(str.$factory(arg))
309
}else if(self.$jsobj[arg] === undefined){
314
315
switch(typeof arg){
316
case "string":
317
if(self.$string_dict[arg] !== undefined){
318
return self.$string_dict[arg]
320
break
321
case "number":
322
if(self.$numeric_dict[arg] !== undefined){
323
return self.$numeric_dict[arg]
325
break
326
}
327
328
// since the key is more complex use 'default' method of getting item
329
338
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
339
return self.$numeric_dict[hash]
340
}
341
if(isinstance(arg, _b_.str)){
342
// string subclass
343
var res = self.$string_dict[arg.valueOf()]
344
if(res !== undefined){return res}
345
}
346
347
var ix = rank(self, hash, arg)
348
if(ix > -1){
349
return self.$object_dict[hash][ix][1]
375
switch(typeof item[0]) {
376
case 'string':
377
self.$string_dict[item[0]] = item[1]
378
self.$str_hash[str_hash(item[0])] = item[0]
379
break
380
case 'number':
381
self.$numeric_dict[item[0]] = item[1]
382
break
383
default:
384
si(self, item[0], item[1])
385
break
386
}
392
if(first === undefined){return $N}
393
if(second === undefined){
394
if(first.__class__ === $B.JSObject){
395
self.$jsobj = first.js
396
return $N
397
}else if(first.$jsobj){
398
self.$jsobj = {}
399
for(var attr in first.$jsobj){
400
self.$jsobj[attr] = first.$jsobj[attr]
410
arguments, {}, "first", "second")
411
var args = $.first
412
if(args.length > 1){
413
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
414
", got 2")
415
}else if(args.length == 1){
416
args = args[0]
417
if(args.__class__ === dict){
418
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
419
forEach(function(d){
420
for(key in args[d]){self[d][key] = args[d][key]}
421
})
425
var keys = $B.$getattr(args, "keys", null)
426
if(keys !== null){
427
var gi = $B.$getattr(args, "__getitem__", null)
428
if(gi !== null){
429
// has keys and __getitem__ : it's a mapping, iterate on
430
// keys and values
431
gi = $B.$call(gi)
432
var kiter = _b_.iter($B.$call(keys)())
433
while(true){
434
try{
435
var key = _b_.next(kiter),
436
value = gi(key)
437
dict.__setitem__(self, key, value)
438
}catch(err){
439
if(err.__class__ === _b_.StopIteration){
440
break
441
}
442
throw err
443
}
444
}
445
return $N
446
}
447
}
448
if(! Array.isArray(args)){
449
args = _b_.list.$factory(args)
450
}
451
// Form "dict([[key1, value1], [key2,value2], ...])"
457
switch(typeof attr){
458
case "string":
459
self.$string_dict[attr] = kw[attr]
460
self.$str_hash[str_hash(attr)] = attr
461
break
462
case "number":
463
self.$numeric_dict[attr] = kw[attr]
464
break
465
default:
466
si(self, attr, kw[attr])
467
break
468
}
485
for(var k in self.$numeric_dict){_count++}
486
for(var k in self.$string_dict){_count++}
487
for(var hash in self.$object_dict){
488
_count += self.$object_dict[hash].length
489
}
508
if(cls !== dict){
509
instance.__dict__ = _b_.dict.$factory()
510
}
511
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
703
var _len = arguments.length - 1,
704
_msg = "items() takes no arguments (" + _len + " given)"
714
var _len = arguments.length - 1,
715
_msg = "keys() takes no arguments (" + _len + " given)"
723
var missing = {},
724
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
725
["self", "key", "_default"], arguments, {_default: missing}, null, null),
757
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
758
["self", "key", "_default"], arguments, {_default: $N}, null, null),
769
var hash = key.$hash
770
key.$hash = undefined
771
dict.$setitem(self, key, _default, hash)
778
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
779
{}, "args", "kw"),
780
self = $.self,
781
args = $.args,
782
kw = $.kw
783
if(args.length > 0){
784
var o = args[0]
785
if(isinstance(o, dict)){
790
}else if(hasattr(o, "keys")){
791
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))()),
792
i = _keys.length
793
while(i--){
794
var _value = getattr(o, "__getitem__")(_keys[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++
832
var _len = arguments.length - 1,
833
_msg = "values() takes no arguments (" + _len + " given)"
835
}
836
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
837
}
838
841
var args = [res]
842
for(var i = 0, len = arguments.length; i < len ; i++){
843
args.push(arguments[i])
844
}
845
dict.__init__.apply(null, args)
853
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
854
// have the attribute $infos
855
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
856
857
// Use in py_types.js
858
$B.$dict_items = function(d){return new $item_generator(d).as_list()}
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__ +