Permalink
Feb 26, 2018
Feb 3, 2018
Feb 3, 2018
Jan 15, 2018
Dec 10, 2018
Feb 26, 2018
Feb 26, 2018
Dec 13, 2018
May 29, 2017
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 11, 2018
Mar 7, 2018
Aug 2, 2018
Feb 26, 2018
Newer
100644
955 lines (850 sloc)
27.1 KB
16
var setitem = dict.__setitem__,
17
i = $keys.length
18
while(i--){setitem($keys[i], $values[i])}
30
var $key_iterator = function(d) {
31
this.d = d
32
this.current = 0
33
this.iter = new $item_generator(d)
34
}
35
$key_iterator.prototype.length = function(){return this.iter.items.length}
36
$key_iterator.prototype.next = function(){return this.iter.next()[0]}
37
38
var $value_iterator = function(d) {
39
this.d = d
40
this.current = 0
41
this.iter = new $item_generator(d)
42
}
43
$value_iterator.prototype.length = function(){return this.iter.items.length}
44
$value_iterator.prototype.next = function(){return this.iter.next()[1]}
55
if(val === undefined){val = _b_.NotImplemented}
56
else if(val === null){val = $N}
57
this.items.push([attr, val])
63
var items = []
64
for(var k in d.$numeric_dict){
65
items.push([parseFloat(k), d.$numeric_dict[k]])
70
for(var k in d.$object_dict){
71
d.$object_dict[k].forEach(function(item){
72
items.push(item)
73
})
74
}
87
}
88
89
var $item_iterator = function(d) {
90
this.d = d
91
this.current = 0
92
this.iter = new $item_generator(d)
93
}
94
$item_iterator.prototype.length = function(){return this.iter.items.length}
95
$item_iterator.prototype.next = function(){
96
return _b_.tuple.$factory(this.iter.next())
97
}
99
var $copy_dict = function(left, right){
100
var _l = new $item_generator(right).as_list(),
103
right.$version = right.$version || 0
104
var right_version = right.$version || 0
105
while(i--){
106
si(left, _l[i][0], _l[i][1])
107
if(right.$version != right_version){
108
throw _b_.RuntimeError.$factory("dict mutated during update")
109
}
110
}
113
function toSet(items){
114
// Build a set from the iteration on items
115
var res = []
116
while(true){
117
try{res.push(items.next())}
118
catch(err){break}
119
}
131
__iter__: function(){items.iter.i = 0; return res},
132
__len__: function(){return items.length()},
133
__next__: function(){
146
klass.__reduce_ex__ = klass.__reduce__ = function(self){
147
return _b_.tuple.$factory([_b_.iter,
148
_b_.tuple.$factory([_b_.list.$factory(self)])])
149
}
153
function rank(self, hash, key){
154
// Search if object key, with hash = hash(key), is in
155
// self.$object_dict
156
var pairs = self.$object_dict[hash]
157
if(pairs !== undefined){
158
for(var i = 0, len = pairs.length; i < len; i++){
159
if($B.rich_comp("__eq__", key, pairs[i][0])){
160
return i
161
}
162
}
163
}
164
return -1
165
}
166
175
var $ = $B.args("__contains__", 2, {self: null, key: null},
176
["self", "key"], arguments, {}, null, null),
186
return self.$numeric_dict[key] !== undefined
187
}
188
189
var hash = _b_.hash(key)
190
if(self.$str_hash[hash] !== undefined &&
191
$B.rich_comp("__eq__", key, self.$str_hash[hash])){return true}
192
if(self.$numeric_dict[hash] !== undefined &&
193
$B.rich_comp("__eq__", key, hash)){return true}
194
return rank(self, hash, key) > -1
199
var $ = $B.args("__eq__", 2, {self: null, arg: null},
200
["self", "arg"], arguments, {}, null, null),
205
if(self.$jsobj[arg] === undefined){throw KeyError.$factory(arg)}
206
delete self.$jsobj[arg]
209
switch(typeof arg){
210
case "string":
211
if(self.$string_dict[arg] === undefined){
212
throw KeyError.$factory(_b_.str.$factory(arg))
213
}
214
delete self.$string_dict[arg]
215
delete self.$str_hash[str_hash(arg)]
218
case "number":
219
if(self.$numeric_dict[arg] === undefined){
220
throw KeyError.$factory(_b_.str.$factory(arg))
231
if((ix = rank(self, hash, arg)) > -1){
232
self.$object_dict[hash].splice(ix, 1)
233
}else{
234
throw KeyError.$factory(_b_.str.$factory(arg))
242
var $ = $B.args("__eq__", 2, {self: null, other: null},
243
["self", "other"], arguments, {}, null, null),
249
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
250
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
260
if(other.$numeric_dict.hasOwnProperty(k)){
261
if(!$B.rich_comp("__eq__", other.$numeric_dict[k],
262
self.$numeric_dict[k])){
263
return false
264
}
265
}else if(other.$object_dict.hasOwnProperty(k)){
266
var pairs = other.$object_dict[k],
267
flag = false
268
for(var i = 0, len = pairs.length; i < len; i++){
269
if($B.rich_comp("__eq__", k, pairs[i][0]) &&
270
$B.rich_comp("__eq__", self.$numeric_dict[k],
271
pairs[i][1])){
272
flag = true
273
break
274
}
282
if(!other.$string_dict.hasOwnProperty(k) ||
283
!$B.rich_comp("__eq__", other.$string_dict[k],
289
var pairs = self.$object_dict[hash]
290
// Get all (key, value) pairs in other that have the same hash
291
var other_pairs = []
292
if(other.$numeric_dict[hash] !== undefined){
293
other_pairs.push([hash, other.$numeric_dict[hash]])
294
}
296
other_pairs = other_pairs.concat(other.$object_dict[hash])
297
}
298
if(other_pairs.length == 0){
299
return false
300
}
301
for(var i = 0, len_i = pairs.length; i < len_i; i++){
302
var flag = false
303
var key = pairs[i][0],
304
value = pairs[i][1]
305
for(var j = 0, len_j = other_pairs.length; j < len_j; j++){
306
if($B.rich_comp("__eq__", key, other_pairs[j][0]) &&
307
$B.rich_comp("__eq__", value, other_pairs[j][1])){
308
flag = true
309
break
321
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
322
["self", "arg"], arguments, {}, null, null),
326
if(!self.$jsobj.hasOwnProperty(arg)){
327
throw _b_.KeyError.$factory(str.$factory(arg))
328
}else if(self.$jsobj[arg] === undefined){
333
334
switch(typeof arg){
335
case "string":
336
if(self.$string_dict[arg] !== undefined){
337
return self.$string_dict[arg]
339
break
340
case "number":
341
if(self.$numeric_dict[arg] !== undefined){
342
return self.$numeric_dict[arg]
344
break
345
}
346
347
// since the key is more complex use 'default' method of getting item
348
357
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
358
return self.$numeric_dict[hash]
359
}
360
if(isinstance(arg, _b_.str)){
361
// string subclass
362
var res = self.$string_dict[arg.valueOf()]
363
if(res !== undefined){return res}
364
}
365
366
var ix = rank(self, hash, arg)
367
if(ix > -1){
368
return self.$object_dict[hash][ix][1]
394
switch(typeof item[0]) {
395
case 'string':
396
self.$string_dict[item[0]] = item[1]
397
self.$str_hash[str_hash(item[0])] = item[0]
398
break
399
case 'number':
400
self.$numeric_dict[item[0]] = item[1]
401
break
402
default:
403
si(self, item[0], item[1])
404
break
405
}
411
if(first === undefined){return $N}
412
if(second === undefined){
413
if(first.__class__ === $B.JSObject){
414
self.$jsobj = first.js
415
return $N
416
}else if(first.$jsobj){
417
self.$jsobj = {}
418
for(var attr in first.$jsobj){
419
self.$jsobj[attr] = first.$jsobj[attr]
429
arguments, {}, "first", "second")
430
var args = $.first
431
if(args.length > 1){
432
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
433
", got 2")
434
}else if(args.length == 1){
435
args = args[0]
436
if(args.__class__ === dict){
437
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
438
forEach(function(d){
439
for(key in args[d]){self[d][key] = args[d][key]}
440
})
444
var keys = $B.$getattr(args, "keys", null)
445
if(keys !== null){
446
var gi = $B.$getattr(args, "__getitem__", null)
447
if(gi !== null){
448
// has keys and __getitem__ : it's a mapping, iterate on
449
// keys and values
450
gi = $B.$call(gi)
451
var kiter = _b_.iter($B.$call(keys)())
452
while(true){
453
try{
454
var key = _b_.next(kiter),
455
value = gi(key)
456
dict.__setitem__(self, key, value)
457
}catch(err){
458
if(err.__class__ === _b_.StopIteration){
459
break
460
}
461
throw err
462
}
463
}
464
return $N
465
}
466
}
467
if(! Array.isArray(args)){
468
args = _b_.list.$factory(args)
469
}
470
// Form "dict([[key1, value1], [key2,value2], ...])"
476
switch(typeof attr){
477
case "string":
478
self.$string_dict[attr] = kw[attr]
479
self.$str_hash[str_hash(attr)] = attr
480
break
481
case "number":
482
self.$numeric_dict[attr] = kw[attr]
483
break
484
default:
485
si(self, attr, kw[attr])
486
break
487
}
505
for(var k in self.$numeric_dict){_count++}
506
for(var k in self.$string_dict){_count++}
507
for(var hash in self.$object_dict){
508
_count += self.$object_dict[hash].length
509
}
528
if(cls !== dict){
529
instance.__dict__ = _b_.dict.$factory()
530
}
531
return instance
551
items.forEach(function(item){
552
if((!self.$jsobj && item[1] === self) ||
553
(self.$jsobj && item[1] === self.$jsobj)){
564
["self", "key", "value"], arguments, {}, null, null)
565
return dict.$setitem($.self, $.key, $.value)
566
}
568
dict.$setitem = function(self, key, value, $hash){
569
// Parameter $hash is only set if this method is called by setdefault.
570
// In this case the hash of key has already been computed and we
571
// know that the key is not present in the dictionary, so it's no
572
// use computing hash(key) again, nor testing equality of keys
574
if(self.$from_js){
575
// dictionary created by method to_dict of JSObject instances
576
value = $B.pyobj2jsobj(value)
577
}
581
// If class attribute __init__ or __new__ are reset,
582
// the factory function has to change
583
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
584
}
585
}else{
608
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
609
self.$numeric_dict[hash] = value
620
// If $setitem is called from setdefault, don't test equality of key
621
// with any object
622
if($hash){
623
if(self.$object_dict[$hash] !== undefined){
624
self.$object_dict[$hash].push([key, value])
625
}else{
626
self.$object_dict[$hash] = [[key, value]]
627
}
628
self.$version++
629
return $N
630
}
631
var ix = rank(self, hash, key)
632
if(ix > -1){
633
// reset value
634
self.$object_dict[hash][ix][1] = value
635
return $N
636
}else if(self.$object_dict.hasOwnProperty(hash)){
637
self.$object_dict[hash].push([key, value])
638
}else{
639
self.$object_dict[hash] = [[key, value]]
684
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
685
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
697
if(klass === dict){dict.$setitem(res, key, value)}
698
else{$B.$getattr(res, "__setitem__")(key, value)}
709
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
710
["self", "key", "_default"], arguments, {_default: $N}, null, null)
713
catch(err){
714
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
715
else{throw err}
716
}
717
}
718
723
var _len = arguments.length - 1,
724
_msg = "items() takes no arguments (" + _len + " given)"
734
var _len = arguments.length - 1,
735
_msg = "keys() takes no arguments (" + _len + " given)"
743
var missing = {},
744
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
745
["self", "key", "_default"], arguments, {_default: missing}, null, null),
777
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
778
["self", "key", "_default"], arguments, {_default: $N}, null, null),
789
var hash = key.$hash
790
key.$hash = undefined
791
dict.$setitem(self, key, _default, hash)
798
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
799
{}, "args", "kw"),
800
self = $.self,
801
args = $.args,
802
kw = $.kw
803
if(args.length > 0){
804
var o = args[0]
805
if(isinstance(o, dict)){
810
}else if(hasattr(o, "keys")){
811
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))()),
812
i = _keys.length
813
while(i--){
814
var _value = getattr(o, "__getitem__")(_keys[i])
815
dict.$setitem(self, _keys[i], _value)
816
}
817
}else{
818
var it = _b_.iter(o),
819
i = 0
820
while(true){
821
try{
822
var item = _b_.next(it)
823
}catch(err){
824
if(err.__class__ === _b_.StopIteration){break}
825
throw err
826
}
827
try{
828
key_value = _b_.list.$factory(item)
829
}catch(err){
830
throw _b_.TypeError.$factory("cannot convert dictionary" +
831
" update sequence element #" + i + " to a sequence")
832
}
833
if(key_value.length !== 2){
834
throw _b_.ValueError.$factory("dictionary update " +
835
"sequence element #" + i + " has length " +
836
key_value.length + "; 2 is required")
837
}
838
dict.$setitem(self, key_value[0], key_value[1])
839
i++
852
var _len = arguments.length - 1,
853
_msg = "values() takes no arguments (" + _len + " given)"
855
}
856
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
857
}
858
861
var args = [res]
862
for(var i = 0, len = arguments.length; i < len ; i++){
863
args.push(arguments[i])
864
}
865
dict.__init__.apply(null, args)
873
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
874
// have the attribute $infos
875
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
876
879
$B.$dict_length = dict.__len__
880
$B.$dict_getitem = dict.__getitem__
881
$B.$dict_get = dict.get
882
$B.$dict_set = dict.__setitem__
883
$B.$dict_contains = dict.__contains__
884
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
885
$B.$copy_dict = $copy_dict // copy from right to left
904
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
905
"item assignment")
908
for(var attr in dict){
909
if(mappingproxy[attr] !== undefined ||
910
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
911
"clear", "fromkeys", "pop", "popitem", "setdefault",
912
"update"].indexOf(attr) > -1){
913
continue
914
}
915
if(typeof dict[attr] == "function"){
916
mappingproxy[attr] = (function(key){
917
return function(){
918
return dict[key].apply(null, arguments)
919
}
920
})(attr)
921
}else{
922
mappingproxy[attr] = dict[attr]
923
}
924
}
925
946
if(klass !== undefined && klass.$native){
947
throw _b_.AttributeError.$factory(klass.__name__ +