Permalink
Feb 3, 2018
Feb 3, 2018
Jan 15, 2018
Feb 3, 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
Feb 3, 2018
Feb 3, 2018
Nov 21, 2015
Feb 3, 2018
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
Feb 3, 2018
Jan 15, 2018
Feb 3, 2018
Sep 24, 2017
Newer
100644
792 lines (675 sloc)
22.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
}
31
$key_iterator.prototype.length = function(){return this.iter.items.length}
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
}
39
$value_iterator.prototype.length = function(){return this.iter.items.length}
40
$value_iterator.prototype.next = function(){return this.iter.next()[1]}
51
if(val === undefined){val = _b_.NotImplemented}
52
else if(val === null){val = $N}
53
this.items.push([attr, val])
75
}
76
throw _b_.StopIteration("StopIteration")
77
}
78
$item_generator.prototype.as_list = function() {
80
}
81
82
var $item_iterator = function(d) {
83
this.d = d
84
this.current = 0
85
this.iter = new $item_generator(d)
86
}
88
$item_iterator.prototype.next = function() { return _b_.tuple(this.iter.next()) }
89
90
var $copy_dict = function(left, right) {
91
var _l=new $item_generator(right).as_list(),
92
si=$DictDict.__setitem__,
93
i=_l.length
97
function toSet(items){
98
// Build a set from the iteration on items
99
var res = []
100
while(true){
101
try{res.push(items.next())}
102
catch(err){break}
103
}
104
return _b_.set(res)
105
}
106
119
__repr__:function(){
120
var s = []
121
for(var i=0, len=items.length(); i<len; i++){
122
s.push(_b_.repr(items.next()))
123
}
124
return klass.__name__+'(['+ s.join(',') + '])'
125
},
138
var $ = $B.args('__contains__', 2, {self:null, item:null},
139
['self', 'item'], arguments, {}, null, null),
140
self=$.self, item=$.item
144
switch(typeof item) {
145
case 'string':
146
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]
276
if (self.$numeric_dict[_key]!==undefined && _eq(_key)){
277
return self.$numeric_dict[_key]
281
var obj_ref = self.$object_dict[_key]
282
if(obj_ref!==undefined){
283
// An object with the same hash is already stored
284
// Lookup should fail if equality raises an exception
285
_eq(self.$object_dict[_key][0])
288
if(self.__class__!==$DictDict){
289
try{
290
var missing_method = getattr(self.__class__.$factory, '__missing__')
291
return missing_method(self, arg)
292
}catch(err){}
293
}
300
var args = [], pos=0
301
for(var i=1;i<arguments.length;i++){args[pos++]=arguments[i]}
318
if(obj.__class__===$B.JSObject.$dict){
319
// convert a JSObject into a Python dictionary
321
// Attribute $jsobj is used to update the original JS object
322
// when the dictionary is modified
348
while(1){
349
try{
350
var elt = next(iterable)
351
var key = getattr(elt,'__getitem__')(0)
352
var value = getattr(elt,'__getitem__')(1)
353
$DictDict.__setitem__(self, key, value)
354
}catch(err){
381
for (var k in self.$numeric_dict) _count++
382
for (var k in self.$string_dict) _count++
383
for (var k in self.$object_dict) _count+= self.$object_dict[k].length
389
390
$DictDict.__ne__ = function(self,other){return !$DictDict.__eq__(self,other)}
391
392
$DictDict.__next__ = function(self){
400
}
401
}
402
403
$DictDict.__repr__ = function(self){
404
if(self===undefined) return "<class 'dict'>"
413
if((!self.$jsobj && itm[1]===self) ||
414
(self.$jsobj && itm[1]===self.$jsobj)){
415
res[pos++]=repr(itm[0])+': {...}'
416
}
418
else{
419
try{
420
res[pos++]=repr(itm[0])+': '+repr(itm[1])
421
}catch(err){
422
res[pos++]=repr(itm[0])+': <unprintable object>'
423
}
424
}
431
var $ = $B.args('__setitem__', 3, {self:null, key:null, value:null},
432
['self', 'key', 'value'], arguments, {}, null, null),
433
self=$.self, key=$.key, value=$.value
434
436
if(self.$jsobj.__class__===$B.$type){
437
self.$jsobj[key] = $B.pyobj2jsobj(value)
438
if(key=="__init__" || key=="__new__"){
439
// If class attribute __init__ or __new__ are reset,
440
// the factory function has to change
441
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
442
}
443
}else{
444
self.$jsobj[key]=$B.pyobj2jsobj(value)
445
}
446
return $N
465
if(self.$numeric_dict[_key]!==undefined && _eq(_key)){
466
self.$numeric_dict[_key] = value
468
}
469
var sk = self.$str_hash[_key]
470
if(sk!==undefined && _eq(sk)){
471
self.$string_dict[sk] = value
475
var obj_ref = self.$object_dict[_key]
476
if(obj_ref!==undefined){
477
// An object with the same hash is already stored
478
// Lookup should fail if equality raises an exception
479
_eq(self.$object_dict[_key][0])
480
}
481
self.$object_dict[_key] = [key, value]
483
}
484
485
$DictDict.__str__ = $DictDict.__repr__
486
487
// add "reflected" methods
488
$B.make_rmethods($DictDict)
489
492
var $ = $B.args('clear',1,{self:null},['self'],arguments,{},null,null),
493
self = $.self
500
if(self.$jsobj){
501
for(var attr in self.$jsobj){
502
if(attr.charAt(0) !== '$' && attr !== "__class__"){
503
delete self.$jsobj[attr]
504
}
505
}
506
}
508
}
509
510
$DictDict.copy = function(self){
511
// Return a shallow copy of the dictionary
512
var $ = $B.args('copy',1,{self:null},['self'],arguments,{},null,null),
513
self = $.self,
514
res = _b_.dict()
521
var $ = $B.args('fromkeys', 3, {cls:null, keys:null, value:null},
522
['cls', 'keys', 'value'], arguments, {value:_b_.None}, null, null),
534
if(klass===dict){$DictDict.__setitem__(res, key, value)}
535
else{_b_.getattr(res, "__setitem__")(key,value)}
548
var $ = $B.args('get', 3, {self:null, key:null, _default:null},
549
['self', 'key', '_default'], arguments, {_default:$N}, null, null)
551
try{return $DictDict.__getitem__($.self, $.key)}
552
catch(err){
553
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
554
else{throw err}
555
}
556
}
557
558
var $dict_itemsDict = $B.$iterator_class('dict_items')
559
560
$DictDict.items = function(self){
561
if (arguments.length > 1) {
562
var _len=arguments.length - 1
563
var _msg="items() takes no arguments ("+_len+" given)"
564
throw _b_.TypeError(_msg)
565
}
566
return $iterator_wrapper(new $item_iterator(self), $dict_itemsDict)
567
}
568
569
var $dict_keysDict = $B.$iterator_class('dict_keys')
570
571
$DictDict.keys = function(self){
572
if (arguments.length > 1) {
573
var _len=arguments.length - 1
574
var _msg="keys() takes no arguments ("+_len+" given)"
575
throw _b_.TypeError(_msg)
576
}
577
return $iterator_wrapper(new $key_iterator(self),$dict_keysDict)
578
}
579
580
$DictDict.pop = function(){
581
582
var $ = $B.args('pop', 3, {self:null, key: null, _default:null},
583
['self', 'key', '_default'], arguments, {_default:$N}, null, null),
584
self=$.self, key=$.key, _default=$._default
585
586
try{
587
var res = $DictDict.__getitem__(self,key)
588
$DictDict.__delitem__(self,key)
589
return res
590
}catch(err){
591
if(err.__name__==='KeyError'){
592
if(_default!==undefined) return _default
593
throw err
594
}
595
throw err
596
}
597
}
598
599
$DictDict.popitem = function(self){
601
try{
602
var itm = new $item_iterator(self).next()
603
$DictDict.__delitem__(self,itm[0])
604
return _b_.tuple(itm)
605
}catch(err) {
606
if (err.__name__ == "StopIteration") {
613
$DictDict.setdefault = function(){
614
615
var $ = $B.args('setdefault', 3, {self:null, key: null, _default:null},
622
$DictDict.__setitem__(self,key,_default)
623
return _default
624
}
625
}
626
627
$DictDict.update = function(self){
628
629
var $ = $B.args('update',1,{'self':null},['self'],arguments,{},'args','kw'),
630
self=$.self, args=$.args, kw=$.kw
635
$copy_dict(self, o)
636
} else if (hasattr(o, '__getitem__') && hasattr(o, 'keys')) {
637
var _keys=_b_.list(getattr(o, 'keys')())
638
var si=$DictDict.__setitem__
639
var i=_keys.length
640
while(i--) {
641
//for (var i=0; i < _keys.length; i++) {
642
var _value = getattr(o, '__getitem__')(_keys[i])
643
si(self, _keys[i], _value)
651
var $dict_valuesDict = $B.$iterator_class('dict_values')
652
653
$DictDict.values = function(self){
654
if (arguments.length > 1) {
655
var _len=arguments.length - 1
656
var _msg="values() takes no arguments ("+_len+" given)"
657
throw _b_.TypeError(_msg)
658
}
659
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
660
}
661
664
var res = {__class__:$DictDict,
665
$numeric_dict : {},
666
$object_dict : {},
667
$string_dict : {},
673
if(second===undefined){
674
if(Array.isArray(args)){
675
// Form "dict([[key1, value1], [key2,value2], ...])"
676
var i = -1, stop = args.length-1
677
var si = $DictDict.__setitem__
678
while(i++<stop){
679
var item=args[i]
680
switch(typeof item[0]) {
681
case 'string':
682
res.$string_dict[item[0]]=item[1]
683
res.$str_hash[str_hash(item[0])]=item[0]
684
break;
685
case 'number':
686
res.$numeric_dict[item[0]]=item[1]
687
break
688
default:
689
si(res, item[0], item[1])
690
break
691
}
692
}
693
return res
694
}else if(args.$nat=='kw'){
695
// Form dict(k1=v1, k2=v2...)
696
var kw = args['kw']
697
for(var attr in kw){
698
switch(typeof attr) {
699
case 'string':
700
res.$string_dict[attr]=kw[attr]
701
res.$str_hash[str_hash(attr)]=attr
702
break;
703
case 'number':
704
res.$numeric_dict[attr]=kw[attr]
705
break
706
default:
707
si(res, attr, kw[attr])
708
break
709
}
712
}else if(args.$jsobj){
713
res.$jsobj = {}
714
for(var attr in args.$jsobj){res.$jsobj[attr] = args.$jsobj[attr]}
715
return res
720
var _args = [res], pos=1
721
for(var i=0, _len_i = arguments.length; i < _len_i;i++){_args[pos++]=arguments[i]}
726
dict.__class__ = $B.$factory
727
dict.$dict = $DictDict
728
$DictDict.$factory = dict
729
$DictDict.__new__ = $B.$__new__(dict)
730
731
_b_.dict = dict
735
// following are used for faster access elsewhere
736
$B.$dict_iterator = function(d) { return new $item_generator(d) }
737
$B.$dict_length = $DictDict.__len__
738
$B.$dict_getitem = $DictDict.__getitem__
739
$B.$dict_get = $DictDict.get
740
$B.$dict_set = $DictDict.__setitem__
741
$B.$dict_contains = $DictDict.__contains__
742
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
743
$B.$copy_dict = $copy_dict // copy from right to left
744
$B.$dict_get_copy = $DictDict.copy // return a shallow copy
745
747
// Class for attribute __dict__ of classes
748
var mappingproxyDict = {
749
__class__ : $B.$type,
750
__name__ : "mappingproxy"
751
}
753
754
mappingproxyDict.__setitem__ = function(){
755
throw _b_.TypeError("'mappingproxy' object does not support item assignment")
756
}
757
759
function mappingproxy(obj){
760
var res = obj_dict(obj)
761
res.__class__ = mappingproxyDict
762
return res
763
}
764
mappingproxy.__class__ = $B.$factory
765
mappingproxy.$dict = mappingproxyDict
766
mappingproxyDict.$factory = mappingproxy
767
$B.mappingproxy = mappingproxy
768
769
function jsobj2dict(x){
770
var d = dict()
771
for(var attr in x){
772
if(attr.charAt(0)!='$' && attr!=='__class__'){
773
if(x[attr].$jsobj===x){
774
d.$string_dict[attr] = d
775
}else{
776
d.$string_dict[attr] = x[attr]
777
}
783
var klass = $B.get_class(obj)
784
if(klass !==undefined && klass.$native){
785
throw _b_.AttributeError(klass.__name__+
786
" has no attribute '__dict__'")}