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
957 lines (852 sloc)
27.1 KB
16
var setitem = dict.__setitem__,
17
i = $keys.length
18
while(i--){setitem($keys[i], $values[i])}
32
var $key_iterator = function(d) {
33
this.d = d
34
this.current = 0
35
this.iter = new $item_generator(d)
36
}
37
$key_iterator.prototype.length = function(){return this.iter.items.length}
38
$key_iterator.prototype.next = function(){return this.iter.next()[0]}
39
40
var $value_iterator = function(d) {
41
this.d = d
42
this.current = 0
43
this.iter = new $item_generator(d)
44
}
45
$value_iterator.prototype.length = function(){return this.iter.items.length}
46
$value_iterator.prototype.next = function(){return this.iter.next()[1]}
57
if(val === undefined){val = _b_.NotImplemented}
58
else if(val === null){val = $N}
59
this.items.push([attr, val])
65
var items = []
66
for(var k in d.$numeric_dict){
67
items.push([parseFloat(k), d.$numeric_dict[k]])
72
for(var k in d.$object_dict){
73
d.$object_dict[k].forEach(function(item){
74
items.push(item)
75
})
76
}
89
}
90
91
var $item_iterator = function(d) {
92
this.d = d
93
this.current = 0
94
this.iter = new $item_generator(d)
95
}
96
$item_iterator.prototype.length = function(){return this.iter.items.length}
97
$item_iterator.prototype.next = function(){
98
return _b_.tuple.$factory(this.iter.next())
99
}
101
var $copy_dict = function(left, right){
102
var _l = new $item_generator(right).as_list(),
105
right.$version = right.$version || 0
106
var right_version = right.$version || 0
107
while(i--){
108
si(left, _l[i][0], _l[i][1])
109
if(right.$version != right_version){
110
throw _b_.RuntimeError.$factory("dict mutated during update")
111
}
112
}
115
function toSet(items){
116
// Build a set from the iteration on items
117
var res = []
118
while(true){
119
try{res.push(items.next())}
120
catch(err){break}
121
}
133
__iter__: function(){items.iter.i = 0; return res},
134
__len__: function(){return items.length()},
135
__next__: function(){
148
klass.__reduce_ex__ = klass.__reduce__ = function(self){
149
return _b_.tuple.$factory([_b_.iter,
150
_b_.tuple.$factory([_b_.list.$factory(self)])])
151
}
155
function rank(self, hash, key){
156
// Search if object key, with hash = hash(key), is in
157
// self.$object_dict
158
var pairs = self.$object_dict[hash]
159
if(pairs !== undefined){
160
for(var i = 0, len = pairs.length; i < len; i++){
161
if($B.rich_comp("__eq__", key, pairs[i][0])){
162
return i
163
}
164
}
165
}
166
return -1
167
}
168
177
var $ = $B.args("__contains__", 2, {self: null, key: null},
178
["self", "key"], arguments, {}, null, null),
188
return self.$numeric_dict[key] !== undefined
189
}
190
191
var hash = _b_.hash(key)
192
if(self.$str_hash[hash] !== undefined &&
193
$B.rich_comp("__eq__", key, self.$str_hash[hash])){return true}
194
if(self.$numeric_dict[hash] !== undefined &&
195
$B.rich_comp("__eq__", key, hash)){return true}
196
return rank(self, hash, key) > -1
201
var $ = $B.args("__eq__", 2, {self: null, arg: null},
202
["self", "arg"], arguments, {}, null, null),
207
if(self.$jsobj[arg] === undefined){throw KeyError.$factory(arg)}
208
delete self.$jsobj[arg]
211
switch(typeof arg){
212
case "string":
213
if(self.$string_dict[arg] === undefined){
214
throw KeyError.$factory(_b_.str.$factory(arg))
215
}
216
delete self.$string_dict[arg]
217
delete self.$str_hash[str_hash(arg)]
220
case "number":
221
if(self.$numeric_dict[arg] === undefined){
222
throw KeyError.$factory(_b_.str.$factory(arg))
233
if((ix = rank(self, hash, arg)) > -1){
234
self.$object_dict[hash].splice(ix, 1)
235
}else{
236
throw KeyError.$factory(_b_.str.$factory(arg))
244
var $ = $B.args("__eq__", 2, {self: null, other: null},
245
["self", "other"], arguments, {}, null, null),
251
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
252
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
262
if(other.$numeric_dict.hasOwnProperty(k)){
263
if(!$B.rich_comp("__eq__", other.$numeric_dict[k],
264
self.$numeric_dict[k])){
265
return false
266
}
267
}else if(other.$object_dict.hasOwnProperty(k)){
268
var pairs = other.$object_dict[k],
269
flag = false
270
for(var i = 0, len = pairs.length; i < len; i++){
271
if($B.rich_comp("__eq__", k, pairs[i][0]) &&
272
$B.rich_comp("__eq__", self.$numeric_dict[k],
273
pairs[i][1])){
274
flag = true
275
break
276
}
284
if(!other.$string_dict.hasOwnProperty(k) ||
285
!$B.rich_comp("__eq__", other.$string_dict[k],
291
var pairs = self.$object_dict[hash]
292
// Get all (key, value) pairs in other that have the same hash
293
var other_pairs = []
294
if(other.$numeric_dict[hash] !== undefined){
295
other_pairs.push([hash, other.$numeric_dict[hash]])
296
}
298
other_pairs = other_pairs.concat(other.$object_dict[hash])
299
}
300
if(other_pairs.length == 0){
301
return false
302
}
303
for(var i = 0, len_i = pairs.length; i < len_i; i++){
304
var flag = false
305
var key = pairs[i][0],
306
value = pairs[i][1]
307
for(var j = 0, len_j = other_pairs.length; j < len_j; j++){
308
if($B.rich_comp("__eq__", key, other_pairs[j][0]) &&
309
$B.rich_comp("__eq__", value, other_pairs[j][1])){
310
flag = true
311
break
323
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
324
["self", "arg"], arguments, {}, null, null),
328
if(!self.$jsobj.hasOwnProperty(arg)){
329
throw _b_.KeyError.$factory(str.$factory(arg))
330
}else if(self.$jsobj[arg] === undefined){
335
336
switch(typeof arg){
337
case "string":
338
if(self.$string_dict[arg] !== undefined){
339
return self.$string_dict[arg]
341
break
342
case "number":
343
if(self.$numeric_dict[arg] !== undefined){
344
return self.$numeric_dict[arg]
346
break
347
}
348
349
// since the key is more complex use 'default' method of getting item
350
359
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
360
return self.$numeric_dict[hash]
361
}
362
if(isinstance(arg, _b_.str)){
363
// string subclass
364
var res = self.$string_dict[arg.valueOf()]
365
if(res !== undefined){return res}
366
}
367
368
var ix = rank(self, hash, arg)
369
if(ix > -1){
370
return self.$object_dict[hash][ix][1]
396
switch(typeof item[0]) {
397
case 'string':
398
self.$string_dict[item[0]] = item[1]
399
self.$str_hash[str_hash(item[0])] = item[0]
400
break
401
case 'number':
402
self.$numeric_dict[item[0]] = item[1]
403
break
404
default:
405
si(self, item[0], item[1])
406
break
407
}
413
if(first === undefined){return $N}
414
if(second === undefined){
415
if(first.__class__ === $B.JSObject){
416
self.$jsobj = first.js
417
return $N
418
}else if(first.$jsobj){
419
self.$jsobj = {}
420
for(var attr in first.$jsobj){
421
self.$jsobj[attr] = first.$jsobj[attr]
431
arguments, {}, "first", "second")
432
var args = $.first
433
if(args.length > 1){
434
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
435
", got 2")
436
}else if(args.length == 1){
437
args = args[0]
438
if(args.__class__ === dict){
439
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
440
forEach(function(d){
441
for(key in args[d]){self[d][key] = args[d][key]}
442
})
446
var keys = $B.$getattr(args, "keys", null)
447
if(keys !== null){
448
var gi = $B.$getattr(args, "__getitem__", null)
449
if(gi !== null){
450
// has keys and __getitem__ : it's a mapping, iterate on
451
// keys and values
452
gi = $B.$call(gi)
453
var kiter = _b_.iter($B.$call(keys)())
454
while(true){
455
try{
456
var key = _b_.next(kiter),
457
value = gi(key)
458
dict.__setitem__(self, key, value)
459
}catch(err){
460
if(err.__class__ === _b_.StopIteration){
461
break
462
}
463
throw err
464
}
465
}
466
return $N
467
}
468
}
469
if(! Array.isArray(args)){
470
args = _b_.list.$factory(args)
471
}
472
// Form "dict([[key1, value1], [key2,value2], ...])"
478
switch(typeof attr){
479
case "string":
480
self.$string_dict[attr] = kw[attr]
481
self.$str_hash[str_hash(attr)] = attr
482
break
483
case "number":
484
self.$numeric_dict[attr] = kw[attr]
485
break
486
default:
487
si(self, attr, kw[attr])
488
break
489
}
507
for(var k in self.$numeric_dict){_count++}
508
for(var k in self.$string_dict){_count++}
509
for(var hash in self.$object_dict){
510
_count += self.$object_dict[hash].length
511
}
530
if(cls !== dict){
531
instance.__dict__ = _b_.dict.$factory()
532
}
533
return instance
553
items.forEach(function(item){
554
if((!self.$jsobj && item[1] === self) ||
555
(self.$jsobj && item[1] === self.$jsobj)){
566
["self", "key", "value"], arguments, {}, null, null)
567
return dict.$setitem($.self, $.key, $.value)
568
}
570
dict.$setitem = function(self, key, value, $hash){
571
// Parameter $hash is only set if this method is called by setdefault.
572
// In this case the hash of key has already been computed and we
573
// know that the key is not present in the dictionary, so it's no
574
// use computing hash(key) again, nor testing equality of keys
576
if(self.$from_js){
577
// dictionary created by method to_dict of JSObject instances
578
value = $B.pyobj2jsobj(value)
579
}
583
// If class attribute __init__ or __new__ are reset,
584
// the factory function has to change
585
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
586
}
587
}else{
610
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
611
self.$numeric_dict[hash] = value
622
// If $setitem is called from setdefault, don't test equality of key
623
// with any object
624
if($hash){
625
if(self.$object_dict[$hash] !== undefined){
626
self.$object_dict[$hash].push([key, value])
627
}else{
628
self.$object_dict[$hash] = [[key, value]]
629
}
630
self.$version++
631
return $N
632
}
633
var ix = rank(self, hash, key)
634
if(ix > -1){
635
// reset value
636
self.$object_dict[hash][ix][1] = value
637
return $N
638
}else if(self.$object_dict.hasOwnProperty(hash)){
639
self.$object_dict[hash].push([key, value])
640
}else{
641
self.$object_dict[hash] = [[key, value]]
686
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
687
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
699
if(klass === dict){dict.$setitem(res, key, value)}
700
else{$B.$getattr(res, "__setitem__")(key, value)}
711
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
712
["self", "key", "_default"], arguments, {_default: $N}, null, null)
715
catch(err){
716
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
717
else{throw err}
718
}
719
}
720
725
var _len = arguments.length - 1,
726
_msg = "items() takes no arguments (" + _len + " given)"
736
var _len = arguments.length - 1,
737
_msg = "keys() takes no arguments (" + _len + " given)"
745
var missing = {},
746
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
747
["self", "key", "_default"], arguments, {_default: missing}, null, null),
779
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
780
["self", "key", "_default"], arguments, {_default: $N}, null, null),
791
var hash = key.$hash
792
key.$hash = undefined
793
dict.$setitem(self, key, _default, hash)
800
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
801
{}, "args", "kw"),
802
self = $.self,
803
args = $.args,
804
kw = $.kw
805
if(args.length > 0){
806
var o = args[0]
807
if(isinstance(o, dict)){
812
}else if(hasattr(o, "keys")){
813
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))()),
814
i = _keys.length
815
while(i--){
816
var _value = getattr(o, "__getitem__")(_keys[i])
817
dict.$setitem(self, _keys[i], _value)
818
}
819
}else{
820
var it = _b_.iter(o),
821
i = 0
822
while(true){
823
try{
824
var item = _b_.next(it)
825
}catch(err){
826
if(err.__class__ === _b_.StopIteration){break}
827
throw err
828
}
829
try{
830
key_value = _b_.list.$factory(item)
831
}catch(err){
832
throw _b_.TypeError.$factory("cannot convert dictionary" +
833
" update sequence element #" + i + " to a sequence")
834
}
835
if(key_value.length !== 2){
836
throw _b_.ValueError.$factory("dictionary update " +
837
"sequence element #" + i + " has length " +
838
key_value.length + "; 2 is required")
839
}
840
dict.$setitem(self, key_value[0], key_value[1])
841
i++
854
var _len = arguments.length - 1,
855
_msg = "values() takes no arguments (" + _len + " given)"
857
}
858
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
859
}
860
863
var args = [res]
864
for(var i = 0, len = arguments.length; i < len ; i++){
865
args.push(arguments[i])
866
}
867
dict.__init__.apply(null, args)
875
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
876
// have the attribute $infos
877
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
878
881
$B.$dict_length = dict.__len__
882
$B.$dict_getitem = dict.__getitem__
883
$B.$dict_get = dict.get
884
$B.$dict_set = dict.__setitem__
885
$B.$dict_contains = dict.__contains__
886
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
887
$B.$copy_dict = $copy_dict // copy from right to left
906
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
907
"item assignment")
910
for(var attr in dict){
911
if(mappingproxy[attr] !== undefined ||
912
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
913
"clear", "fromkeys", "pop", "popitem", "setdefault",
914
"update"].indexOf(attr) > -1){
915
continue
916
}
917
if(typeof dict[attr] == "function"){
918
mappingproxy[attr] = (function(key){
919
return function(){
920
return dict[key].apply(null, arguments)
921
}
922
})(attr)
923
}else{
924
mappingproxy[attr] = dict[attr]
925
}
926
}
927
948
if(klass !== undefined && klass.$native){
949
throw _b_.AttributeError.$factory(klass.__name__ +