Permalink
Jan 15, 2018
May 29, 2017
Jan 10, 2017
Nov 9, 2015
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
Jan 15, 2018
Nov 21, 2015
Nov 21, 2015
Jan 15, 2018
May 28, 2016
May 28, 2016
Jan 31, 2016
Nov 21, 2015
May 28, 2016
Nov 9, 2015
Oct 30, 2017
Nov 9, 2015
Dec 9, 2015
Jan 9, 2017
Nov 9, 2015
Nov 9, 2015
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
Jun 10, 2016
Jun 10, 2016
Jan 15, 2018
Sep 24, 2017
Newer
100644
765 lines (649 sloc)
21.3 KB
5
var $ObjectDict = _b_.object.$dict,
6
str_hash = _b_.str.$dict.__hash__,
7
$N = _b_.None
8
9
// dictionary
10
function $DictClass($keys,$values){
11
this.iter = null
12
this.__class__ = $DictDict
15
var setitem=$DictDict.__setitem__
16
var i=$keys.length
17
while(i--) setitem($keys[i], $values[i])
26
var $key_iterator = function(d) {
27
this.d = d
28
this.current = 0
29
this.iter = new $item_generator(d)
30
}
32
$key_iterator.prototype.next = function() { return this.iter.next()[0] }
33
34
var $value_iterator = function(d) {
35
this.d = d
36
this.current = 0
37
this.iter = new $item_generator(d)
38
}
40
$value_iterator.prototype.next = function() { return this.iter.next()[1] }
41
42
var $item_generator = function(d) {
51
if(val === undefined){val = _b_.NotImplemented}
52
else if(val === null){val = $N}
53
this.items.push([attr, val])
77
}
78
throw _b_.StopIteration("StopIteration")
79
}
80
$item_generator.prototype.as_list = function() {
82
}
83
84
var $item_iterator = function(d) {
85
this.d = d
86
this.current = 0
87
this.iter = new $item_generator(d)
88
}
90
$item_iterator.prototype.next = function() { return _b_.tuple(this.iter.next()) }
91
92
var $copy_dict = function(left, right) {
99
function toSet(items){
100
// Build a set from the iteration on items
101
var res = []
102
while(true){
103
try{res.push(items.next())}
104
catch(err){break}
105
}
106
return _b_.set(res)
107
}
108
121
__repr__:function(){
122
var s = []
123
for(var i=0, len=items.length(); i<len; i++){
124
s.push(_b_.repr(items.next()))
125
}
126
return klass.__name__+'(['+ s.join(',') + '])'
127
},
140
var $ = $B.args('__contains__', 2, {self:null, item:null},
141
['self', 'item'], arguments, {}, null, null),
142
self=$.self, item=$.item
146
switch(typeof item) {
147
case 'string':
148
return self.$string_dict[item] !==undefined
159
// If the key is an object, its hash must be in the dict keys but the
160
// key itself must compare equal to the key associated with the hash
161
// For instance :
162
//
163
// class X:
164
// def __hash__(self): return hash('u')
166
// a = {'u': 'a', X(): 'b'}
167
// assert set(a.values())=={'a', 'b'}
168
// assert not X() in a
174
$DictDict.__delitem__ = function(){
175
176
var $ = $B.args('__eq__', 2, {self:null, arg:null},
177
['self', 'arg'], arguments, {}, null, null),
178
self=$.self, arg=$.arg
179
180
if(self.$jsobj){
181
if(self.$jsobj[arg]===undefined){throw KeyError(arg)}
182
delete self.$jsobj[arg]
185
switch(typeof arg) {
186
case 'string':
187
if (self.$string_dict[arg] === undefined) throw KeyError(_b_.str(arg))
188
delete self.$string_dict[arg]
191
case 'number':
192
if (self.$numeric_dict[arg] === undefined) throw KeyError(_b_.str(arg))
193
delete self.$numeric_dict[arg]
208
$DictDict.__eq__ = function(){
209
var $ = $B.args('__eq__', 2, {self:null, other:null},
210
['self', 'other'], arguments, {}, null, null),
211
self=$.self, other=$.other
212
215
if(self.$jsobj){self=jsobj2dict(self.$jsobj)}
216
if(other.$jsobj){other=jsobj2dict(other.$jsobj)}
220
if((self.$numeric_dict.length!=other.$numeric_dict.length) ||
221
(self.$string_dict.length!=other.$string_dict.length) ||
222
(self.$object_dict.length!=other.$object_dict.length)){
223
return false
245
$DictDict.__getitem__ = function(){
246
var $ = $B.args('__getitem__', 2, {self:null, arg:null},
247
['self', 'arg'], arguments, {}, null, null),
248
self=$.self, arg=$.arg
251
if(!self.$jsobj.hasOwnProperty(arg)) throw _b_.KeyError(str(arg))
252
else if(self.$jsobj[arg]===undefined) return _b_.NotImplemented
253
else if(self.$jsobj[arg]===null){return $N}
258
switch(typeof arg) {
259
case 'string':
260
if (self.$string_dict[arg] !== undefined) return self.$string_dict[arg]
261
break
262
case 'number':
263
if (self.$numeric_dict[arg] !== undefined) return self.$numeric_dict[arg]
275
if (self.$numeric_dict[_key]!==undefined && _eq(_key)){
276
return self.$numeric_dict[_key]
278
279
var obj_ref = self.$object_dict[_key]
280
if(obj_ref!==undefined){
281
// An object with the same hash is already stored
282
// Lookup should fail if equality raises an exception
283
_eq(self.$object_dict[_key][0])
286
if(self.__class__!==$DictDict){
287
try{
288
var missing_method = getattr(self.__class__.$factory, '__missing__')
289
return missing_method(self, arg)
290
}catch(err){}
291
}
298
var args = [], pos=0
299
for(var i=1;i<arguments.length;i++){args[pos++]=arguments[i]}
316
if(obj.__class__===$B.JSObject.$dict){
317
// convert a JSObject into a Python dictionary
319
// Attribute $jsobj is used to update the original JS object
320
// when the dictionary is modified
346
while(1){
347
try{
348
var elt = next(iterable)
349
var key = getattr(elt,'__getitem__')(0)
350
var value = getattr(elt,'__getitem__')(1)
351
$DictDict.__setitem__(self, key, value)
352
}catch(err){
379
for (var k in self.$numeric_dict) _count++
380
for (var k in self.$string_dict) _count++
381
for (var k in self.$object_dict) _count+= self.$object_dict[k].length
387
388
$DictDict.__ne__ = function(self,other){return !$DictDict.__eq__(self,other)}
389
390
$DictDict.__next__ = function(self){
398
}
399
}
400
401
$DictDict.__repr__ = function(self){
402
if(self===undefined) return "<class 'dict'>"
420
var $ = $B.args('__setitem__', 3, {self:null, key:null, value:null},
421
['self', 'key', 'value'], arguments, {}, null, null),
422
self=$.self, key=$.key, value=$.value
423
444
if(self.$numeric_dict[_key]!==undefined && _eq(_key)){
445
self.$numeric_dict[_key] = value
447
}
448
var sk = self.$str_hash[_key]
449
if(sk!==undefined && _eq(sk)){
450
self.$string_dict[sk] = value
454
var obj_ref = self.$object_dict[_key]
455
if(obj_ref!==undefined){
456
// An object with the same hash is already stored
457
// Lookup should fail if equality raises an exception
458
_eq(self.$object_dict[_key][0])
459
}
460
self.$object_dict[_key] = [key, value]
462
}
463
464
$DictDict.__str__ = $DictDict.__repr__
465
466
// add "reflected" methods
467
$B.make_rmethods($DictDict)
468
471
var $ = $B.args('clear',1,{self:null},['self'],arguments,{},null,null),
472
self = $.self
479
if(self.$jsobj){
480
for(var attr in self.$jsobj){
481
if(attr.charAt(0) !== '$' && attr !== "__class__"){
482
delete self.$jsobj[attr]
483
}
484
}
485
}
487
}
488
489
$DictDict.copy = function(self){
490
// Return a shallow copy of the dictionary
491
var $ = $B.args('copy',1,{self:null},['self'],arguments,{},null,null),
492
self = $.self,
493
res = _b_.dict()
500
var $ = $B.args('fromkeys', 3, {cls:null, keys:null, value:null},
501
['cls', 'keys', 'value'], arguments, {value:_b_.None}, null, null),
513
if(klass===dict){$DictDict.__setitem__(res, key, value)}
514
else{_b_.getattr(res, "__setitem__")(key,value)}
527
var $ = $B.args('get', 3, {self:null, key:null, _default:null},
528
['self', 'key', '_default'], arguments, {_default:$N}, null, null)
530
try{return $DictDict.__getitem__($.self, $.key)}
531
catch(err){
532
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
533
else{throw err}
534
}
535
}
536
537
var $dict_itemsDict = $B.$iterator_class('dict_items')
538
539
$DictDict.items = function(self){
540
if (arguments.length > 1) {
541
var _len=arguments.length - 1
542
var _msg="items() takes no arguments ("+_len+" given)"
543
throw _b_.TypeError(_msg)
544
}
545
return $iterator_wrapper(new $item_iterator(self), $dict_itemsDict)
546
}
547
548
var $dict_keysDict = $B.$iterator_class('dict_keys')
549
550
$DictDict.keys = function(self){
551
if (arguments.length > 1) {
552
var _len=arguments.length - 1
553
var _msg="keys() takes no arguments ("+_len+" given)"
554
throw _b_.TypeError(_msg)
555
}
556
return $iterator_wrapper(new $key_iterator(self),$dict_keysDict)
557
}
558
559
$DictDict.pop = function(){
560
561
var $ = $B.args('pop', 3, {self:null, key: null, _default:null},
562
['self', 'key', '_default'], arguments, {_default:$N}, null, null),
563
self=$.self, key=$.key, _default=$._default
564
565
try{
566
var res = $DictDict.__getitem__(self,key)
567
$DictDict.__delitem__(self,key)
568
return res
569
}catch(err){
570
if(err.__name__==='KeyError'){
571
if(_default!==undefined) return _default
572
throw err
573
}
574
throw err
575
}
576
}
577
578
$DictDict.popitem = function(self){
580
try{
581
var itm = new $item_iterator(self).next()
582
$DictDict.__delitem__(self,itm[0])
583
return _b_.tuple(itm)
584
}catch(err) {
585
if (err.__name__ == "StopIteration") {
592
$DictDict.setdefault = function(){
593
594
var $ = $B.args('setdefault', 3, {self:null, key: null, _default:null},
601
$DictDict.__setitem__(self,key,_default)
602
return _default
603
}
604
}
605
606
$DictDict.update = function(self){
607
608
var $ = $B.args('update',1,{'self':null},['self'],arguments,{},'args','kw'),
609
self=$.self, args=$.args, kw=$.kw
610
611
if(args.length>0) {
612
var o=args[0]
613
if (isinstance(o,dict)){
614
$copy_dict(self, o)
615
} else if (hasattr(o, '__getitem__') && hasattr(o, 'keys')) {
616
var _keys=_b_.list(getattr(o, 'keys')())
617
var si=$DictDict.__setitem__
618
var i=_keys.length
619
while(i--) {
620
//for (var i=0; i < _keys.length; i++) {
621
var _value = getattr(o, '__getitem__')(_keys[i])
622
si(self, _keys[i], _value)
630
var $dict_valuesDict = $B.$iterator_class('dict_values')
631
632
$DictDict.values = function(self){
633
if (arguments.length > 1) {
634
var _len=arguments.length - 1
635
var _msg="values() takes no arguments ("+_len+" given)"
636
throw _b_.TypeError(_msg)
637
}
638
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
639
}
640
643
var res = {__class__:$DictDict,
644
$numeric_dict : {},
645
$object_dict : {},
646
$string_dict : {},
647
$str_hash: {},
648
length: 0
649
}
653
if(second===undefined){
654
if(Array.isArray(args)){
655
// Form "dict([[key1, value1], [key2,value2], ...])"
656
var i = -1, stop = args.length-1
657
var si = $DictDict.__setitem__
658
while(i++<stop){
659
var item=args[i]
660
switch(typeof item[0]) {
661
case 'string':
662
res.$string_dict[item[0]]=item[1]
663
res.$str_hash[str_hash(item[0])]=item[0]
664
break;
665
case 'number':
666
res.$numeric_dict[item[0]]=item[1]
667
break
668
default:
669
si(res, item[0], item[1])
670
break
671
}
672
}
673
return res
674
}else if(args.$nat=='kw'){
675
// Form dict(k1=v1, k2=v2...)
676
var kw = args['kw']
677
for(var attr in kw){
678
switch(typeof attr) {
679
case 'string':
680
res.$string_dict[attr]=kw[attr]
681
res.$str_hash[str_hash(attr)]=attr
682
break;
683
case 'number':
684
res.$numeric_dict[attr]=kw[attr]
685
break
686
default:
687
si(res, attr, kw[attr])
688
break
689
}
696
var _args = [res], pos=1
697
for(var i=0, _len_i = arguments.length; i < _len_i;i++){_args[pos++]=arguments[i]}
702
dict.__class__ = $B.$factory
703
dict.$dict = $DictDict
704
$DictDict.$factory = dict
705
$DictDict.__new__ = $B.$__new__(dict)
706
707
_b_.dict = dict
711
// following are used for faster access elsewhere
712
$B.$dict_iterator = function(d) { return new $item_generator(d) }
713
$B.$dict_length = $DictDict.__len__
714
$B.$dict_getitem = $DictDict.__getitem__
715
$B.$dict_get = $DictDict.get
716
$B.$dict_set = $DictDict.__setitem__
717
$B.$dict_contains = $DictDict.__contains__
718
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
719
$B.$copy_dict = $copy_dict // copy from right to left
720
$B.$dict_get_copy = $DictDict.copy // return a shallow copy
721
723
// Class for attribute __dict__ of classes
724
var mappingproxyDict = {
725
__class__ : $B.$type,
726
__name__ : "mappingproxy"
727
}
729
730
mappingproxyDict.__setitem__ = function(){
731
throw _b_.TypeError("'mappingproxy' object does not support item assignment")
732
}
733
735
function mappingproxy(obj){
736
var res = obj_dict(obj)
737
res.__class__ = mappingproxyDict
738
return res
739
}
740
mappingproxy.__class__ = $B.$factory
741
mappingproxy.$dict = mappingproxyDict
742
mappingproxyDict.$factory = mappingproxy
743
$B.mappingproxy = mappingproxy
744
745
function jsobj2dict(x){
746
var d = dict()
747
for(var attr in x){
748
if(attr.charAt(0)!='$' && attr!=='__class__'){
749
d.$string_dict[attr] = x[attr]
750
d.length++
751
}
752
}
753
return d
754
}
756
var klass = $B.get_class(obj)
757
if(klass !==undefined && klass.$native){
758
throw _b_.AttributeError(klass.__name__+
759
" has no attribute '__dict__'")}