Skip to content
Permalink
Newer
Older
100644 955 lines (850 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,
Feb 11, 2018
23
__module__: "builtins",
Feb 11, 2018
24
__mro__: [object],
Mar 7, 2018
25
__name__ : "dict",
Feb 11, 2018
26
$is_class: true,
27
$native: true
Sep 5, 2014
28
}
29
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]}
45
46
var $item_generator = function(d) {
48
this.i = 0
50
if(d.$jsobj){
Mar 7, 2018
53
if(attr.charAt(0) != "$"){
54
var val = d.$jsobj[attr]
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]])
67
68
for(var k in d.$string_dict){items.push([k, d.$string_dict[k]])}
69
70
for(var k in d.$object_dict){
71
d.$object_dict[k].forEach(function(item){
72
items.push(item)
73
})
74
}
75
76
this.items = items
Feb 9, 2015
78
79
$item_generator.prototype.next = function() {
Mar 23, 2018
80
if(this.i < this.items.length){
81
return this.items[this.i++]
83
throw _b_.StopIteration.$factory("StopIteration")
84
}
85
$item_generator.prototype.as_list = function() {
86
return this.items
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(),
102
i = _l.length
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
}
Feb 11, 2018
120
return _b_.set.$factory(res)
121
}
122
123
var $iterator_wrapper = function(items, klass){
125
__class__: klass,
126
__eq__: function(other){
127
// compare set of items to other
128
return $B.rich_comp("__eq__", _b_.set.$factory(res),
129
_b_.set.$factory(other))
131
__iter__: function(){items.iter.i = 0; return res},
132
__len__: function(){return items.length()},
133
__next__: function(){
136
__repr__:function(){
137
var s = []
139
for(var i = 0, len = items.length(); i < len; i++){
140
s.push(_b_.repr(items.next()))
141
}
Mar 7, 2018
142
return klass.__name__ + "(["+ s.join(",") + "])"
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
Feb 11, 2018
167
dict.__bool__ = function () {
Mar 7, 2018
168
var $ = $B.args("__bool__", 1, {self: null}, ["self"],
169
arguments, {}, null, null)
Feb 11, 2018
170
return dict.__len__($.self) > 0
Feb 11, 2018
173
dict.__contains__ = function(){
Nov 21, 2015
174
175
var $ = $B.args("__contains__", 2, {self: null, key: null},
176
["self", "key"], arguments, {}, null, null),
177
self = $.self,
180
if(self.$jsobj){return self.$jsobj[key] !== undefined}
182
switch(typeof key) {
184
return self.$string_dict[key] !== undefined
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
Sep 5, 2014
195
}
196
Feb 11, 2018
197
dict.__delitem__ = function(){
Nov 21, 2015
198
199
var $ = $B.args("__eq__", 2, {self: null, arg: null},
200
["self", "arg"], arguments, {}, null, null),
201
self = $.self,
Nov 21, 2015
203
204
if(self.$jsobj){
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)]
217
return $N
218
case "number":
219
if(self.$numeric_dict[arg] === undefined){
220
throw KeyError.$factory(_b_.str.$factory(arg))
222
delete self.$numeric_dict[arg]
226
// go with defaults
227
228
var hash = _b_.hash(arg),
229
ix
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))
Sep 5, 2014
239
}
240
Feb 11, 2018
241
dict.__eq__ = function(){
Mar 7, 2018
242
var $ = $B.args("__eq__", 2, {self: null, other: null},
243
["self", "other"], arguments, {}, null, null),
244
self = $.self,
245
other = $.other
Mar 7, 2018
247
if(! isinstance(other, dict)){return false}
249
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
250
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
251
if(dict.__len__(self) != dict.__len__(other)){
252
return false
253
}
255
if(self.$string_dict.length != other.$string_dict.length){
259
for(var k in self.$numeric_dict){
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
}
276
if(! flag){return false}
Nov 21, 2015
279
}
280
}
281
for(var k in self.$string_dict){
282
if(!other.$string_dict.hasOwnProperty(k) ||
283
!$B.rich_comp("__eq__", other.$string_dict[k],
284
self.$string_dict[k])){
285
return false
Nov 21, 2015
286
}
287
}
288
for(var hash in self.$object_dict){
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
}
295
if(other.$object_dict[hash] !== undefined){
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
315
}
316
}
317
return true
Sep 5, 2014
318
}
319
Feb 11, 2018
320
dict.__getitem__ = function(){
321
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
322
["self", "arg"], arguments, {}, null, null),
323
self = $.self,
325
if(self.$jsobj){
326
if(!self.$jsobj.hasOwnProperty(arg)){
327
throw _b_.KeyError.$factory(str.$factory(arg))
328
}else if(self.$jsobj[arg] === undefined){
329
return _b_.NotImplemented
330
}else if(self.$jsobj[arg] === null){return $N}
331
return self.$jsobj[arg]
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
349
var hash = _b_.hash(arg),
350
_eq = function(other){return $B.rich_comp("__eq__", arg, other)}
351
352
arg.$hash = hash // cache for setdefault
353
var sk = self.$str_hash[hash]
354
if(sk !== undefined && _eq(sk)){
355
return self.$string_dict[sk]
356
}
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]
371
if(self.__class__ !== dict){
373
var missing_method = getattr(self.__class__, "__missing__",
374
_b_.None)
375
}catch(err){
376
console.log(err)
377
378
}
379
if(missing_method !== _b_.None){
380
return missing_method(self, arg)
Sep 5, 2014
384
}
385
Feb 11, 2018
386
dict.__hash__ = None
Sep 5, 2014
387
388
function init_from_list(self, args){
389
var i = -1,
390
stop = args.length - 1,
391
si = dict.__setitem__
392
while(i++ < stop){
393
var item = args[i]
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
}
406
}
407
}
408
409
dict.__init__ = function(self, first, second){
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]
422
}else if(Array.isArray(first)){
423
init_from_list(self, first)
424
return $N
Sep 5, 2014
425
}
428
$ = $ || $B.args("dict", 1, {self:null}, ["self"],
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
})
441
}else if(isinstance(args, dict)){
442
$copy_dict(self, args)
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], ...])"
471
init_from_list(self, args)
Sep 5, 2014
472
}
474
var kw = $.second.$string_dict
475
for(var attr in kw){
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
}
Sep 5, 2014
490
}
491
Mar 7, 2018
492
var $dict_iterator = $B.$iterator_class("dict iterator")
Feb 11, 2018
493
dict.__iter__ = function(self) {
Sep 5, 2014
495
}
496
Feb 11, 2018
497
dict.__len__ = function(self) {
500
if(self.$jsobj){
Mar 7, 2018
501
for(var attr in self.$jsobj){if(attr.charAt(0) != "$"){_count++}}
502
return _count
503
}
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
}
Sep 5, 2014
513
Mar 7, 2018
514
dict.__ne__ = function(self, other){return ! dict.__eq__(self, other)}
Sep 5, 2014
515
Feb 11, 2018
516
dict.__new__ = function(cls){
517
if(cls === undefined){
Mar 7, 2018
518
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
520
var instance = {
522
$numeric_dict : {},
523
$object_dict : {},
525
$str_hash: {},
526
$version: 0
528
if(cls !== dict){
529
instance.__dict__ = _b_.dict.$factory()
530
}
531
return instance
Feb 11, 2018
534
dict.__next__ = function(self){
535
if(self.$iter == null){
536
self.$iter = new $item_generator(self)
538
try{
540
}catch (err){
541
if(err.__name__ !== "StopIteration"){throw err}
Sep 5, 2014
542
}
543
}
544
Feb 11, 2018
545
dict.__repr__ = function(self){
546
if(self.$jsobj){ // wrapper around Javascript object
Feb 11, 2018
547
return dict.__repr__(jsobj2dict(self.$jsobj))
549
var res = [],
550
items = new $item_generator(self).as_list()
551
items.forEach(function(item){
552
if((!self.$jsobj && item[1] === self) ||
553
(self.$jsobj && item[1] === self.$jsobj)){
Mar 7, 2018
554
res.push(repr(item[0]) + ": {...}")
556
res.push(repr(item[0]) + ": " + repr(item[1]))
Mar 7, 2018
559
return "{" + res.join(", ") + "}"
Sep 5, 2014
560
}
561
562
dict.__setitem__ = function(self, key, value){
Mar 7, 2018
563
var $ = $B.args("__setitem__", 3, {self: null, key: null, value: null},
564
["self", "key", "value"], arguments, {}, null, null)
565
return dict.$setitem($.self, $.key, $.value)
566
}
Nov 21, 2015
567
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
}
578
if(self.$jsobj.__class__ === _b_.type){
579
self.$jsobj[key] = value
580
if(key == "__init__" || key == "__new__"){
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{
586
self.$jsobj[key] = value
591
switch(typeof key){
592
case "string":
593
self.$string_dict[key] = value
594
self.$str_hash[str_hash(key)] = key
596
return $N
597
case "number":
598
self.$numeric_dict[key] = value
600
return $N
603
// if we got here the key is more complex, use default method
604
605
var hash = $hash === undefined ? _b_.hash(key) : $hash,
606
_eq = function(other){return $B.rich_comp("__eq__", key, other)}
607
608
if(self.$numeric_dict[hash] !== undefined && _eq(hash)){
609
self.$numeric_dict[hash] = value
613
var sk = self.$str_hash[hash]
614
if(sk !== undefined && _eq(sk)){
615
self.$string_dict[sk] = 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]]
Sep 5, 2014
643
}
644
645
dict.__str__ = function(){return dict.__repr__.apply(null, arguments)}
Sep 5, 2014
646
647
// add "reflected" methods
Feb 11, 2018
648
$B.make_rmethods(dict)
Sep 5, 2014
649
Feb 11, 2018
650
dict.clear = function(){
Sep 5, 2014
651
// Remove all items from the dictionary.
Mar 7, 2018
652
var $ = $B.args("clear", 1, {self: null}, ["self"], arguments, {},
653
null, null),
654
self = $.self
656
self.$numeric_dict = {}
657
self.$string_dict = {}
658
self.$str_hash = {}
659
self.$object_dict = {}
661
if(self.$jsobj){
662
for(var attr in self.$jsobj){
Mar 7, 2018
663
if(attr.charAt(0) !== "$" && attr !== "__class__"){
664
delete self.$jsobj[attr]
665
}
666
}
667
}
Sep 5, 2014
670
}
671
Feb 11, 2018
672
dict.copy = function(self){
Sep 5, 2014
673
// Return a shallow copy of the dictionary
Mar 7, 2018
674
var $ = $B.args("copy", 1, {self: null},["self"], arguments,{},
675
null, null),
676
self = $.self,
Feb 11, 2018
677
res = _b_.dict.$factory()
Sep 5, 2014
679
return res
680
}
681
Feb 11, 2018
682
dict.fromkeys = function(){
Nov 21, 2015
683
Mar 7, 2018
684
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
685
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
686
keys = $.keys,
687
value = $.value
Sep 5, 2014
689
// class method
690
var klass = $.cls,
Sep 5, 2014
694
while(1){
695
try{
696
var key = _b_.next(keys_iter)
697
if(klass === dict){dict.$setitem(res, key, value)}
698
else{$B.$getattr(res, "__setitem__")(key, value)}
Sep 5, 2014
699
}catch(err){
700
if($B.is_exc(err, [_b_.StopIteration])){
Sep 5, 2014
701
return res
702
}
703
throw err
704
}
705
}
706
}
707
Feb 11, 2018
708
dict.get = function(){
Mar 7, 2018
709
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
710
["self", "key", "_default"], arguments, {_default: $N}, null, null)
Feb 11, 2018
712
try{return dict.__getitem__($.self, $.key)}
713
catch(err){
714
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
715
else{throw err}
716
}
717
}
718
Mar 7, 2018
719
var $dict_itemsDict = $B.$iterator_class("dict_items")
720
Feb 11, 2018
721
dict.items = function(self){
Mar 23, 2018
722
if(arguments.length > 1){
723
var _len = arguments.length - 1,
724
_msg = "items() takes no arguments (" + _len + " given)"
725
throw _b_.TypeError.$factory(_msg)
726
}
727
return $iterator_wrapper(new $item_iterator(self), $dict_itemsDict)
728
}
729
Mar 7, 2018
730
var $dict_keysDict = $B.$iterator_class("dict_keys")
Nov 21, 2015
731
Mar 23, 2018
733
if(arguments.length > 1){
734
var _len = arguments.length - 1,
735
_msg = "keys() takes no arguments (" + _len + " given)"
736
throw _b_.TypeError.$factory(_msg)
Nov 21, 2015
737
}
738
return $iterator_wrapper(new $key_iterator(self), $dict_keysDict)
Nov 21, 2015
739
}
740
Feb 11, 2018
741
dict.pop = function(){
Nov 21, 2015
742
743
var missing = {},
744
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
745
["self", "key", "_default"], arguments, {_default: missing}, null, null),
746
self = $.self,
747
key = $.key,
748
_default = $._default
Nov 21, 2015
749
Sep 5, 2014
750
try{
751
var res = dict.__getitem__(self, key)
752
dict.__delitem__(self, key)
Sep 5, 2014
753
return res
754
}catch(err){
755
if(err.__class__ === _b_.KeyError){
756
if(_default !== missing){return _default}
Sep 5, 2014
757
throw err
758
}
759
throw err
760
}
761
}
762
Feb 11, 2018
763
dict.popitem = function(self){
764
try{
765
var itm = new $item_iterator(self).next()
766
dict.__delitem__(self, itm[0])
Feb 11, 2018
767
return _b_.tuple.$factory(itm)
769
if (err.__class__ == _b_.StopIteration) {
770
throw KeyError.$factory("'popitem(): dictionary is empty'")
Sep 5, 2014
773
}
774
Feb 11, 2018
775
dict.setdefault = function(){
Nov 21, 2015
776
Mar 7, 2018
777
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
778
["self", "key", "_default"], arguments, {_default: $N}, null, null),
779
self = $.self,
780
key = $.key,
781
_default = $._default
Nov 21, 2015
782
783
try{return dict.__getitem__(self, key)}
Sep 5, 2014
784
catch(err){
785
if(err.__class__ !== _b_.KeyError){
786
throw err
787
}
788
if(_default === undefined){_default = $N}
789
var hash = key.$hash
790
key.$hash = undefined
791
dict.$setitem(self, key, _default, hash)
Sep 5, 2014
792
return _default
793
}
794
}
795
Feb 11, 2018
796
dict.update = function(self){
Nov 21, 2015
797
Mar 7, 2018
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)){
806
if(o.$jsobj){
807
o = jsobj2dict(o.$jsobj)
808
}
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++
Sep 5, 2014
842
}
Sep 5, 2014
846
}
847
Mar 7, 2018
848
var $dict_valuesDict = $B.$iterator_class("dict_values")
Nov 21, 2015
849
Feb 11, 2018
850
dict.values = function(self){
Mar 23, 2018
851
if(arguments.length > 1){
852
var _len = arguments.length - 1,
853
_msg = "values() takes no arguments (" + _len + " given)"
854
throw _b_.TypeError.$factory(_msg)
Nov 21, 2015
855
}
856
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
857
}
858
859
dict.$factory = function(){
860
var res = dict.__new__(dict)
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)
Sep 5, 2014
866
return res
867
}
Sep 5, 2014
869
_b_.dict = dict
Feb 11, 2018
871
$B.set_func_names(dict, "builtins")
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
877
// following are used for faster access elsewhere
878
$B.$dict_iterator = function(d){return new $item_generator(d)}
Feb 11, 2018
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
Feb 11, 2018
886
$B.$dict_get_copy = dict.copy // return a shallow copy
889
// Class for attribute __dict__ of classes
890
var mappingproxy = $B.mappingproxy = $B.make_class("mappingproxy",
Feb 12, 2018
891
function(obj){
892
if(_b_.isinstance(obj, dict)){
893
// Should be a dictionary
894
var res = $B.obj_dict(obj.$string_dict)
895
}else{
896
var res = $B.obj_dict(obj)
897
}
Feb 12, 2018
898
res.__class__ = mappingproxy
899
return res
900
}
901
)
Feb 12, 2018
903
mappingproxy.__setitem__ = function(){
Mar 7, 2018
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
Feb 12, 2018
926
$B.set_func_names(mappingproxy, "builtins")
Feb 11, 2018
929
var d = dict.$factory()
Mar 7, 2018
931
if(attr.charAt(0) != "$" && attr !== "__class__"){
932
if(x[attr] === undefined){
933
continue
934
}else if(x[attr].$jsobj === x){
935
d.$string_dict[attr] = d
936
}else{
937
d.$string_dict[attr] = x[attr]
938
}
944
$B.obj_dict = function(obj, from_js){
945
var klass = obj.__class__ || $B.get_class(obj)
946
if(klass !== undefined && klass.$native){
947
throw _b_.AttributeError.$factory(klass.__name__ +
948
" has no attribute '__dict__'")}
Feb 11, 2018
949
var res = dict.$factory()
950
res.$jsobj = obj
951
res.$from_js = from_js // set to true if created by JSObject.to_dict()
952
return res
953
}
954
Sep 5, 2014
955
})(__BRYTHON__)