Skip to content
Permalink
Newer
Older
100644 957 lines (852 sloc) 27.1 KB
Sep 5, 2014
1
;(function($B){
2
3
var bltns = $B.InjectBuiltins()
4
eval(bltns)
str
Feb 10, 2018
7
str_hash = _b_.str.__hash__,
Sep 5, 2014
9
10
// dictionary
11
function $DictClass($keys,$values){
12
this.iter = null
Feb 11, 2018
13
this.__class__ = dict
14
dict.clear(this)
Feb 9, 2015
15
16
var setitem = dict.__setitem__,
17
i = $keys.length
18
while(i--){setitem($keys[i], $values[i])}
Feb 11, 2018
21
var dict = {
Feb 11, 2018
22
__class__: _b_.type,
23
__mro__: [object],
24
$infos: {
25
__module__: "builtins",
26
__name__: "dict"
27
},
Feb 11, 2018
28
$is_class: true,
29
$native: true
Sep 5, 2014
30
}
31
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]}
47
48
var $item_generator = function(d) {
50
this.i = 0
52
if(d.$jsobj){
Mar 7, 2018
55
if(attr.charAt(0) != "$"){
56
var val = d.$jsobj[attr]
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]])
69
70
for(var k in d.$string_dict){items.push([k, d.$string_dict[k]])}
71
72
for(var k in d.$object_dict){
73
d.$object_dict[k].forEach(function(item){
74
items.push(item)
75
})
76
}
77
78
this.items = items
Feb 9, 2015
80
81
$item_generator.prototype.next = function() {
Mar 23, 2018
82
if(this.i < this.items.length){
83
return this.items[this.i++]
85
throw _b_.StopIteration.$factory("StopIteration")
86
}
87
$item_generator.prototype.as_list = function() {
88
return this.items
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(),
104
i = _l.length
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
}
Feb 11, 2018
122
return _b_.set.$factory(res)
123
}
124
125
var $iterator_wrapper = function(items, klass){
127
__class__: klass,
128
__eq__: function(other){
129
// compare set of items to other
130
return $B.rich_comp("__eq__", _b_.set.$factory(res),
131
_b_.set.$factory(other))
133
__iter__: function(){items.iter.i = 0; return res},
134
__len__: function(){return items.length()},
135
__next__: function(){
138
__repr__:function(){
139
var s = []
141
for(var i = 0, len = items.length(); i < len; i++){
142
s.push(_b_.repr(items.next()))
143
}
144
return klass.$infos.__name__ + "(["+ s.join(",") + "])"
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
Feb 11, 2018
169
dict.__bool__ = function () {
Mar 7, 2018
170
var $ = $B.args("__bool__", 1, {self: null}, ["self"],
171
arguments, {}, null, null)
Feb 11, 2018
172
return dict.__len__($.self) > 0
Feb 11, 2018
175
dict.__contains__ = function(){
Nov 21, 2015
176
177
var $ = $B.args("__contains__", 2, {self: null, key: null},
178
["self", "key"], arguments, {}, null, null),
179
self = $.self,
182
if(self.$jsobj){return self.$jsobj[key] !== undefined}
184
switch(typeof key) {
186
return self.$string_dict[key] !== undefined
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
Sep 5, 2014
197
}
198
Feb 11, 2018
199
dict.__delitem__ = function(){
Nov 21, 2015
200
201
var $ = $B.args("__eq__", 2, {self: null, arg: null},
202
["self", "arg"], arguments, {}, null, null),
203
self = $.self,
Nov 21, 2015
205
206
if(self.$jsobj){
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)]
219
return $N
220
case "number":
221
if(self.$numeric_dict[arg] === undefined){
222
throw KeyError.$factory(_b_.str.$factory(arg))
224
delete self.$numeric_dict[arg]
228
// go with defaults
229
230
var hash = _b_.hash(arg),
231
ix
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))
Sep 5, 2014
241
}
242
Feb 11, 2018
243
dict.__eq__ = function(){
Mar 7, 2018
244
var $ = $B.args("__eq__", 2, {self: null, other: null},
245
["self", "other"], arguments, {}, null, null),
246
self = $.self,
247
other = $.other
Mar 7, 2018
249
if(! isinstance(other, dict)){return false}
251
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
252
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
253
if(dict.__len__(self) != dict.__len__(other)){
254
return false
255
}
257
if(self.$string_dict.length != other.$string_dict.length){
261
for(var k in self.$numeric_dict){
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
}
278
if(! flag){return false}
Nov 21, 2015
281
}
282
}
283
for(var k in self.$string_dict){
284
if(!other.$string_dict.hasOwnProperty(k) ||
285
!$B.rich_comp("__eq__", other.$string_dict[k],
286
self.$string_dict[k])){
287
return false
Nov 21, 2015
288
}
289
}
290
for(var hash in self.$object_dict){
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
}
297
if(other.$object_dict[hash] !== undefined){
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
317
}
318
}
319
return true
Sep 5, 2014
320
}
321
Feb 11, 2018
322
dict.__getitem__ = function(){
323
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
324
["self", "arg"], arguments, {}, null, null),
325
self = $.self,
327
if(self.$jsobj){
328
if(!self.$jsobj.hasOwnProperty(arg)){
329
throw _b_.KeyError.$factory(str.$factory(arg))
330
}else if(self.$jsobj[arg] === undefined){
331
return _b_.NotImplemented
332
}else if(self.$jsobj[arg] === null){return $N}
333
return self.$jsobj[arg]
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
351
var hash = _b_.hash(arg),
352
_eq = function(other){return $B.rich_comp("__eq__", arg, other)}
353
354
arg.$hash = hash // cache for setdefault
355
var sk = self.$str_hash[hash]
356
if(sk !== undefined && _eq(sk)){
357
return self.$string_dict[sk]
358
}
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]
373
if(self.__class__ !== dict){
375
var missing_method = getattr(self.__class__, "__missing__",
376
_b_.None)
377
}catch(err){
378
console.log(err)
379
380
}
381
if(missing_method !== _b_.None){
382
return missing_method(self, arg)
Sep 5, 2014
386
}
387
Feb 11, 2018
388
dict.__hash__ = None
Sep 5, 2014
389
390
function init_from_list(self, args){
391
var i = -1,
392
stop = args.length - 1,
393
si = dict.__setitem__
394
while(i++ < stop){
395
var item = args[i]
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
}
408
}
409
}
410
411
dict.__init__ = function(self, first, second){
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]
424
}else if(Array.isArray(first)){
425
init_from_list(self, first)
426
return $N
Sep 5, 2014
427
}
430
$ = $ || $B.args("dict", 1, {self:null}, ["self"],
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
})
443
}else if(isinstance(args, dict)){
444
$copy_dict(self, args)
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], ...])"
473
init_from_list(self, args)
Sep 5, 2014
474
}
476
var kw = $.second.$string_dict
477
for(var attr in kw){
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
}
Sep 5, 2014
492
}
493
Mar 7, 2018
494
var $dict_iterator = $B.$iterator_class("dict iterator")
Feb 11, 2018
495
dict.__iter__ = function(self) {
Sep 5, 2014
497
}
498
Feb 11, 2018
499
dict.__len__ = function(self) {
502
if(self.$jsobj){
Mar 7, 2018
503
for(var attr in self.$jsobj){if(attr.charAt(0) != "$"){_count++}}
504
return _count
505
}
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
}
Sep 5, 2014
515
Mar 7, 2018
516
dict.__ne__ = function(self, other){return ! dict.__eq__(self, other)}
Sep 5, 2014
517
Feb 11, 2018
518
dict.__new__ = function(cls){
519
if(cls === undefined){
Mar 7, 2018
520
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
522
var instance = {
524
$numeric_dict : {},
525
$object_dict : {},
527
$str_hash: {},
528
$version: 0
530
if(cls !== dict){
531
instance.__dict__ = _b_.dict.$factory()
532
}
533
return instance
Feb 11, 2018
536
dict.__next__ = function(self){
537
if(self.$iter == null){
538
self.$iter = new $item_generator(self)
540
try{
542
}catch (err){
543
if(err.__name__ !== "StopIteration"){throw err}
Sep 5, 2014
544
}
545
}
546
Feb 11, 2018
547
dict.__repr__ = function(self){
548
if(self.$jsobj){ // wrapper around Javascript object
Feb 11, 2018
549
return dict.__repr__(jsobj2dict(self.$jsobj))
551
var res = [],
552
items = new $item_generator(self).as_list()
553
items.forEach(function(item){
554
if((!self.$jsobj && item[1] === self) ||
555
(self.$jsobj && item[1] === self.$jsobj)){
Mar 7, 2018
556
res.push(repr(item[0]) + ": {...}")
558
res.push(repr(item[0]) + ": " + repr(item[1]))
Mar 7, 2018
561
return "{" + res.join(", ") + "}"
Sep 5, 2014
562
}
563
564
dict.__setitem__ = function(self, key, value){
Mar 7, 2018
565
var $ = $B.args("__setitem__", 3, {self: null, key: null, value: null},
566
["self", "key", "value"], arguments, {}, null, null)
567
return dict.$setitem($.self, $.key, $.value)
568
}
Nov 21, 2015
569
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
}
580
if(self.$jsobj.__class__ === _b_.type){
581
self.$jsobj[key] = value
582
if(key == "__init__" || key == "__new__"){
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{
588
self.$jsobj[key] = value
593
switch(typeof key){
594
case "string":
595
self.$string_dict[key] = value
596
self.$str_hash[str_hash(key)] = key
598
return $N
599
case "number":
600
self.$numeric_dict[key] = value
602
return $N
605
// if we got here the key is more complex, use default method
606
607
var hash = $hash === undefined ? _b_.hash(key) : $hash,
608
_eq = function(other){return $B.rich_comp("__eq__", key, other)}
609
610
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
611
self.$numeric_dict[hash] = value
615
var sk = self.$str_hash[hash]
616
if(sk !== undefined && _eq(sk)){
617
self.$string_dict[sk] = 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]]
Sep 5, 2014
645
}
646
647
dict.__str__ = function(){return dict.__repr__.apply(null, arguments)}
Sep 5, 2014
648
649
// add "reflected" methods
Feb 11, 2018
650
$B.make_rmethods(dict)
Sep 5, 2014
651
Feb 11, 2018
652
dict.clear = function(){
Sep 5, 2014
653
// Remove all items from the dictionary.
Mar 7, 2018
654
var $ = $B.args("clear", 1, {self: null}, ["self"], arguments, {},
655
null, null),
656
self = $.self
658
self.$numeric_dict = {}
659
self.$string_dict = {}
660
self.$str_hash = {}
661
self.$object_dict = {}
663
if(self.$jsobj){
664
for(var attr in self.$jsobj){
Mar 7, 2018
665
if(attr.charAt(0) !== "$" && attr !== "__class__"){
666
delete self.$jsobj[attr]
667
}
668
}
669
}
Sep 5, 2014
672
}
673
Feb 11, 2018
674
dict.copy = function(self){
Sep 5, 2014
675
// Return a shallow copy of the dictionary
Mar 7, 2018
676
var $ = $B.args("copy", 1, {self: null},["self"], arguments,{},
677
null, null),
678
self = $.self,
Feb 11, 2018
679
res = _b_.dict.$factory()
Sep 5, 2014
681
return res
682
}
683
Feb 11, 2018
684
dict.fromkeys = function(){
Nov 21, 2015
685
Mar 7, 2018
686
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
687
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
688
keys = $.keys,
689
value = $.value
Sep 5, 2014
691
// class method
692
var klass = $.cls,
Sep 5, 2014
696
while(1){
697
try{
698
var key = _b_.next(keys_iter)
699
if(klass === dict){dict.$setitem(res, key, value)}
700
else{$B.$getattr(res, "__setitem__")(key, value)}
Sep 5, 2014
701
}catch(err){
702
if($B.is_exc(err, [_b_.StopIteration])){
Sep 5, 2014
703
return res
704
}
705
throw err
706
}
707
}
708
}
709
Feb 11, 2018
710
dict.get = function(){
Mar 7, 2018
711
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
712
["self", "key", "_default"], arguments, {_default: $N}, null, null)
Feb 11, 2018
714
try{return dict.__getitem__($.self, $.key)}
715
catch(err){
716
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
717
else{throw err}
718
}
719
}
720
Mar 7, 2018
721
var $dict_itemsDict = $B.$iterator_class("dict_items")
722
Feb 11, 2018
723
dict.items = function(self){
Mar 23, 2018
724
if(arguments.length > 1){
725
var _len = arguments.length - 1,
726
_msg = "items() takes no arguments (" + _len + " given)"
727
throw _b_.TypeError.$factory(_msg)
728
}
729
return $iterator_wrapper(new $item_iterator(self), $dict_itemsDict)
730
}
731
Mar 7, 2018
732
var $dict_keysDict = $B.$iterator_class("dict_keys")
Nov 21, 2015
733
Mar 23, 2018
735
if(arguments.length > 1){
736
var _len = arguments.length - 1,
737
_msg = "keys() takes no arguments (" + _len + " given)"
738
throw _b_.TypeError.$factory(_msg)
Nov 21, 2015
739
}
740
return $iterator_wrapper(new $key_iterator(self), $dict_keysDict)
Nov 21, 2015
741
}
742
Feb 11, 2018
743
dict.pop = function(){
Nov 21, 2015
744
745
var missing = {},
746
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
747
["self", "key", "_default"], arguments, {_default: missing}, null, null),
748
self = $.self,
749
key = $.key,
750
_default = $._default
Nov 21, 2015
751
Sep 5, 2014
752
try{
753
var res = dict.__getitem__(self, key)
754
dict.__delitem__(self, key)
Sep 5, 2014
755
return res
756
}catch(err){
757
if(err.__class__ === _b_.KeyError){
758
if(_default !== missing){return _default}
Sep 5, 2014
759
throw err
760
}
761
throw err
762
}
763
}
764
Feb 11, 2018
765
dict.popitem = function(self){
766
try{
767
var itm = new $item_iterator(self).next()
768
dict.__delitem__(self, itm[0])
Feb 11, 2018
769
return _b_.tuple.$factory(itm)
771
if (err.__class__ == _b_.StopIteration) {
772
throw KeyError.$factory("'popitem(): dictionary is empty'")
Sep 5, 2014
775
}
776
Feb 11, 2018
777
dict.setdefault = function(){
Nov 21, 2015
778
Mar 7, 2018
779
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
780
["self", "key", "_default"], arguments, {_default: $N}, null, null),
781
self = $.self,
782
key = $.key,
783
_default = $._default
Nov 21, 2015
784
785
try{return dict.__getitem__(self, key)}
Sep 5, 2014
786
catch(err){
787
if(err.__class__ !== _b_.KeyError){
788
throw err
789
}
790
if(_default === undefined){_default = $N}
791
var hash = key.$hash
792
key.$hash = undefined
793
dict.$setitem(self, key, _default, hash)
Sep 5, 2014
794
return _default
795
}
796
}
797
Feb 11, 2018
798
dict.update = function(self){
Nov 21, 2015
799
Mar 7, 2018
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)){
808
if(o.$jsobj){
809
o = jsobj2dict(o.$jsobj)
810
}
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++
Sep 5, 2014
844
}
Sep 5, 2014
848
}
849
Mar 7, 2018
850
var $dict_valuesDict = $B.$iterator_class("dict_values")
Nov 21, 2015
851
Feb 11, 2018
852
dict.values = function(self){
Mar 23, 2018
853
if(arguments.length > 1){
854
var _len = arguments.length - 1,
855
_msg = "values() takes no arguments (" + _len + " given)"
856
throw _b_.TypeError.$factory(_msg)
Nov 21, 2015
857
}
858
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
859
}
860
861
dict.$factory = function(){
862
var res = dict.__new__(dict)
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)
Sep 5, 2014
868
return res
869
}
Sep 5, 2014
871
_b_.dict = dict
Feb 11, 2018
873
$B.set_func_names(dict, "builtins")
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
879
// following are used for faster access elsewhere
880
$B.$dict_iterator = function(d){return new $item_generator(d)}
Feb 11, 2018
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
Feb 11, 2018
888
$B.$dict_get_copy = dict.copy // return a shallow copy
891
// Class for attribute __dict__ of classes
892
var mappingproxy = $B.mappingproxy = $B.make_class("mappingproxy",
Feb 12, 2018
893
function(obj){
894
if(_b_.isinstance(obj, dict)){
895
// Should be a dictionary
896
var res = $B.obj_dict(obj.$string_dict)
897
}else{
898
var res = $B.obj_dict(obj)
899
}
Feb 12, 2018
900
res.__class__ = mappingproxy
901
return res
902
}
903
)
Feb 12, 2018
905
mappingproxy.__setitem__ = function(){
Mar 7, 2018
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
Feb 12, 2018
928
$B.set_func_names(mappingproxy, "builtins")
Feb 11, 2018
931
var d = dict.$factory()
Mar 7, 2018
933
if(attr.charAt(0) != "$" && attr !== "__class__"){
934
if(x[attr] === undefined){
935
continue
936
}else if(x[attr].$jsobj === x){
937
d.$string_dict[attr] = d
938
}else{
939
d.$string_dict[attr] = x[attr]
940
}
946
$B.obj_dict = function(obj, from_js){
947
var klass = obj.__class__ || $B.get_class(obj)
948
if(klass !== undefined && klass.$native){
949
throw _b_.AttributeError.$factory(klass.__name__ +
950
" has no attribute '__dict__'")}
Feb 11, 2018
951
var res = dict.$factory()
952
res.$jsobj = obj
953
res.$from_js = from_js // set to true if created by JSObject.to_dict()
954
return res
955
}
956
Sep 5, 2014
957
})(__BRYTHON__)