Permalink
May 29, 2017
May 29, 2017
Jan 10, 2017
Nov 9, 2015
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
Sep 24, 2017
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
May 28, 2016
May 28, 2016
Jan 31, 2016
Nov 4, 2015
Dec 31, 2015
Nov 21, 2015
May 28, 2016
Nov 9, 2015
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
Sep 24, 2017
Sep 24, 2017
Newer
100644
766 lines (649 sloc)
21.1 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) {
49
if(attr.charAt(0)!='$'){
50
val = d.$jsobj[attr];
51
if (val === undefined || val === null) this.items.push([attr,$N])
52
else this.items.push([attr,val])
53
}
76
}
77
throw _b_.StopIteration("StopIteration")
78
}
79
$item_generator.prototype.as_list = function() {
81
}
82
83
var $item_iterator = function(d) {
84
this.d = d
85
this.current = 0
86
this.iter = new $item_generator(d)
87
}
89
$item_iterator.prototype.next = function() { return _b_.tuple(this.iter.next()) }
90
91
var $copy_dict = function(left, right) {
98
function toSet(items){
99
// Build a set from the iteration on items
100
var res = []
101
while(true){
102
try{res.push(items.next())}
103
catch(err){break}
104
}
105
return _b_.set(res)
106
}
107
111
__eq__:function(other){
112
// compare set of items to other
113
return getattr(toSet(items), "__eq__")(other)
114
},
120
__repr__:function(){
121
var s = []
122
for(var i=0, len=items.length(); i<len; i++){
123
s.push(_b_.repr(items.next()))
124
}
125
return klass.__name__+'(['+ s.join(',') + '])'
126
},
139
var $ = $B.args('__contains__', 2, {self:null, item:null},
140
['self', 'item'], arguments, {}, null, null),
141
self=$.self, item=$.item
145
switch(typeof item) {
146
case 'string':
147
return self.$string_dict[item] !==undefined
153
if (self.$str_hash[_key]!==undefined &&
154
_b_.getattr(item,'__eq__')(self.$str_hash[_key])){return true}
155
if (self.$numeric_dict[_key]!==undefined &&
156
_b_.getattr(item,'__eq__')(_key)){return true}
158
// If the key is an object, its hash must be in the dict keys but the
159
// key itself must compare equal to the key associated with the hash
160
// For instance :
161
//
162
// class X:
163
// def __hash__(self): return hash('u')
165
// a = {'u': 'a', X(): 'b'}
166
// assert set(a.values())=={'a', 'b'}
167
// assert not X() in a
175
$DictDict.__delitem__ = function(){
176
177
var $ = $B.args('__eq__', 2, {self:null, arg:null},
178
['self', 'arg'], arguments, {}, null, null),
179
self=$.self, arg=$.arg
180
181
if(self.$jsobj){
182
if(self.$jsobj[arg]===undefined){throw KeyError(arg)}
183
delete self.$jsobj[arg]
186
switch(typeof arg) {
187
case 'string':
188
if (self.$string_dict[arg] === undefined) throw KeyError(_b_.str(arg))
189
delete self.$string_dict[arg]
192
case 'number':
193
if (self.$numeric_dict[arg] === undefined) throw KeyError(_b_.str(arg))
194
delete self.$numeric_dict[arg]
209
$DictDict.__eq__ = function(){
210
var $ = $B.args('__eq__', 2, {self:null, other:null},
211
['self', 'other'], arguments, {}, null, null),
212
self=$.self, other=$.other
213
215
216
if(self.$jsobj){self=self.$to_dict()}
217
if(other.$jsobj){other=other.$to_dict()}
218
221
if((self.$numeric_dict.length!=other.$numeric_dict.length) ||
222
(self.$string_dict.length!=other.$string_dict.length) ||
223
(self.$object_dict.length!=other.$object_dict.length)){
224
return false
226
for(var k in self.$numeric_dict){
227
if(!_b_.getattr(other.$numeric_dict[k],'__eq__')(self.$numeric_dict[k])){
228
return false
229
}
230
}
231
for(var k in self.$string_dict){
232
if(!_b_.getattr(other.$string_dict[k],'__eq__')(self.$string_dict[k])){
233
return false
234
}
235
}
236
for(var k in self.$object_dict){
237
if(!_b_.getattr(other.$object_dict[k][1],'__eq__')(self.$object_dict[k][1])){
238
return false
239
}
240
}
246
$DictDict.__getitem__ = function(){
247
var $ = $B.args('__getitem__', 2, {self:null, arg:null},
248
['self', 'arg'], arguments, {}, null, null),
249
self=$.self, arg=$.arg
257
switch(typeof arg) {
258
case 'string':
259
if (self.$string_dict[arg] !== undefined) return self.$string_dict[arg]
260
break
261
case 'number':
262
if (self.$numeric_dict[arg] !== undefined) return self.$numeric_dict[arg]
274
if (self.$numeric_dict[_key]!==undefined && _eq(_key)){
275
return self.$numeric_dict[_key]
277
278
var obj_ref = self.$object_dict[_key]
279
if(obj_ref!==undefined){
280
// An object with the same hash is already stored
281
// Lookup should fail if equality raises an exception
282
_eq(self.$object_dict[_key][0])
285
if(self.__class__!==$DictDict){
286
try{
287
var missing_method = getattr(self.__class__.$factory, '__missing__')
288
return missing_method(self, arg)
289
}catch(err){}
290
}
297
var args = [], pos=0
298
for(var i=1;i<arguments.length;i++){args[pos++]=arguments[i]}
315
if(obj.__class__===$B.JSObject.$dict){
316
// convert a JSObject into a Python dictionary
318
// Attribute $jsobj is used to update the original JS object
319
// when the dictionary is modified
344
while(1){
345
try{
346
var elt = next(iterable)
347
var key = getattr(elt,'__getitem__')(0)
348
var value = getattr(elt,'__getitem__')(1)
349
$DictDict.__setitem__(self, key, value)
350
}catch(err){
374
for (var k in self.$numeric_dict) _count++
375
for (var k in self.$string_dict) _count++
376
for (var k in self.$object_dict) _count+= self.$object_dict[k].length
382
383
$DictDict.__ne__ = function(self,other){return !$DictDict.__eq__(self,other)}
384
385
$DictDict.__next__ = function(self){
393
}
394
}
395
396
$DictDict.__repr__ = function(self){
397
if(self===undefined) return "<class 'dict'>"
401
var res = []
402
for(var attr in self.$jsobj){
403
if(attr.charAt(0)=='$' || attr=='__class__'){continue}
404
else{
405
try{
406
res.push("'"+attr+"': "+_b_.repr(self.$jsobj[attr]))
407
}catch(err){
408
// FIX ME
409
}
410
}
420
if(itm[1]===self){res[pos++]=repr(itm[0])+': {...}'}
421
else{res[pos++]=repr(itm[0])+': '+repr(itm[1])}
428
var $ = $B.args('__setitem__', 3, {self:null, key:null, value:null},
429
['self', 'key', 'value'], arguments, {}, null, null),
430
self=$.self, key=$.key, value=$.value
431
432
if(self.$jsobj){
433
if (value === $N) self.$jsobj[key] = undefined;
434
else self.$jsobj[key]=value;
435
return
436
}
453
if(self.$numeric_dict[_key]!==undefined && _eq(_key)){
454
self.$numeric_dict[_key] = value
456
}
457
var sk = self.$str_hash[_key]
458
if(sk!==undefined && _eq(sk)){
459
self.$string_dict[sk] = value
463
var obj_ref = self.$object_dict[_key]
464
if(obj_ref!==undefined){
465
// An object with the same hash is already stored
466
// Lookup should fail if equality raises an exception
467
_eq(self.$object_dict[_key][0])
468
}
469
self.$object_dict[_key] = [key, value]
471
}
472
473
$DictDict.__str__ = $DictDict.__repr__
474
475
// add "reflected" methods
476
$B.make_rmethods($DictDict)
477
480
var $ = $B.args('clear',1,{self:null},['self'],arguments,{},null,null),
481
self = $.self
490
}
491
492
$DictDict.copy = function(self){
493
// Return a shallow copy of the dictionary
494
var $ = $B.args('copy',1,{self:null},['self'],arguments,{},null,null),
495
self = $.self,
496
res = _b_.dict()
503
var $ = $B.args('fromkeys', 3, {cls:null, keys:null, value:null},
504
['cls', 'keys', 'value'], arguments, {value:_b_.None}, null, null),
515
if(klass===dict){$DictDict.__setitem__(res, key, value)}
516
else{_b_.getattr(res, "__setitem__")(key,value)}
517
}catch(err){
518
if($B.is_exc(err,[_b_.StopIteration])){
519
return res
520
}
521
throw err
522
}
523
}
524
}
528
var $ = $B.args('get', 3, {self:null, key:null, _default:null},
529
['self', 'key', '_default'], arguments, {_default:$N}, null, null)
531
try{return $DictDict.__getitem__($.self, $.key)}
532
catch(err){
533
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
534
else{throw err}
535
}
536
}
537
538
var $dict_itemsDict = $B.$iterator_class('dict_items')
539
540
$DictDict.items = function(self){
541
if (arguments.length > 1) {
542
var _len=arguments.length - 1
543
var _msg="items() takes no arguments ("+_len+" given)"
544
throw _b_.TypeError(_msg)
545
}
546
return $iterator_wrapper(new $item_iterator(self), $dict_itemsDict)
547
}
548
549
var $dict_keysDict = $B.$iterator_class('dict_keys')
550
551
$DictDict.keys = function(self){
552
if (arguments.length > 1) {
553
var _len=arguments.length - 1
554
var _msg="keys() takes no arguments ("+_len+" given)"
555
throw _b_.TypeError(_msg)
556
}
557
return $iterator_wrapper(new $key_iterator(self),$dict_keysDict)
558
}
559
560
$DictDict.pop = function(){
561
562
var $ = $B.args('pop', 3, {self:null, key: null, _default:null},
563
['self', 'key', '_default'], arguments, {_default:$N}, null, null),
564
self=$.self, key=$.key, _default=$._default
565
566
try{
567
var res = $DictDict.__getitem__(self,key)
568
$DictDict.__delitem__(self,key)
569
return res
570
}catch(err){
571
if(err.__name__==='KeyError'){
572
if(_default!==undefined) return _default
573
throw err
574
}
575
throw err
576
}
577
}
578
579
$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") {
586
throw KeyError("'popitem(): dictionary is empty'")
587
}
588
}
591
$DictDict.setdefault = function(){
592
593
var $ = $B.args('setdefault', 3, {self:null, key: null, _default:null},
600
$DictDict.__setitem__(self,key,_default)
601
return _default
602
}
603
}
604
605
$DictDict.update = function(self){
606
607
var $ = $B.args('update',1,{'self':null},['self'],arguments,{},'args','kw'),
608
self=$.self, args=$.args, kw=$.kw
609
610
if(args.length>0) {
611
var o=args[0]
612
if (isinstance(o,dict)){
613
$copy_dict(self, o)
614
} else if (hasattr(o, '__getitem__') && hasattr(o, 'keys')) {
615
var _keys=_b_.list(getattr(o, 'keys')())
616
var si=$DictDict.__setitem__
617
var i=_keys.length
618
while(i--) {
619
//for (var i=0; i < _keys.length; i++) {
620
var _value = getattr(o, '__getitem__')(_keys[i])
621
si(self, _keys[i], _value)
629
var $dict_valuesDict = $B.$iterator_class('dict_values')
630
631
$DictDict.values = function(self){
632
if (arguments.length > 1) {
633
var _len=arguments.length - 1
634
var _msg="values() takes no arguments ("+_len+" given)"
635
throw _b_.TypeError(_msg)
636
}
637
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
638
}
639
642
var res = {__class__:$DictDict,
643
$numeric_dict : {},
644
$object_dict : {},
645
$string_dict : {},
646
$str_hash: {},
647
length: 0
648
}
652
if(second===undefined){
653
if(Array.isArray(args)){
654
// Form "dict([[key1, value1], [key2,value2], ...])"
655
var i = -1, stop = args.length-1
656
var si = $DictDict.__setitem__
657
while(i++<stop){
658
var item=args[i]
659
switch(typeof item[0]) {
660
case 'string':
661
res.$string_dict[item[0]]=item[1]
662
res.$str_hash[str_hash(item[0])]=item[0]
663
break;
664
case 'number':
665
res.$numeric_dict[item[0]]=item[1]
666
break
667
default:
668
si(res, item[0], item[1])
669
break
670
}
671
}
672
return res
673
}else if(args.$nat=='kw'){
674
// Form dict(k1=v1, k2=v2...)
675
var kw = args['kw']
676
for(var attr in kw){
677
switch(typeof attr) {
678
case 'string':
679
res.$string_dict[attr]=kw[attr]
680
res.$str_hash[str_hash(attr)]=attr
681
break;
682
case 'number':
683
res.$numeric_dict[attr]=kw[attr]
684
break
685
default:
686
si(res, attr, kw[attr])
687
break
688
}
695
var _args = [res], pos=1
696
for(var i=0, _len_i = arguments.length; i < _len_i;i++){_args[pos++]=arguments[i]}
701
dict.__class__ = $B.$factory
702
dict.$dict = $DictDict
703
$DictDict.$factory = dict
704
$DictDict.__new__ = $B.$__new__(dict)
705
706
_b_.dict = dict
710
// following are used for faster access elsewhere
711
$B.$dict_iterator = function(d) { return new $item_generator(d) }
712
$B.$dict_length = $DictDict.__len__
713
$B.$dict_getitem = $DictDict.__getitem__
714
$B.$dict_get = $DictDict.get
715
$B.$dict_set = $DictDict.__setitem__
716
$B.$dict_contains = $DictDict.__contains__
717
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
718
$B.$copy_dict = $copy_dict // copy from right to left
719
$B.$dict_get_copy = $DictDict.copy // return a shallow copy
720
722
// Class for attribute __dict__ of classes
723
var mappingproxyDict = {
724
__class__ : $B.$type,
725
__name__ : "mappingproxy"
726
}
728
729
mappingproxyDict.__setitem__ = function(){
730
throw _b_.TypeError("'mappingproxy' object does not support item assignment")
731
}
732
734
function mappingproxy(obj){
735
var res = obj_dict(obj)
736
res.__class__ = mappingproxyDict
737
return res
738
}
739
mappingproxy.__class__ = $B.$factory
740
mappingproxy.$dict = mappingproxyDict
741
mappingproxyDict.$factory = mappingproxy
742
$B.mappingproxy = mappingproxy
743
745
var klass = $B.get_class(obj)
746
if(klass !==undefined && klass.$native){
747
throw _b_.AttributeError(klass.__name__+
748
" has no attribute '__dict__'")}
751
res.$to_dict = (function(x){
752
return function(){
753
var d = dict()
754
for(var attr in x){
755
if(attr.charAt(0)!='$' && attr!=='__class__'){
756
d.$string_dict[attr] = x[attr]
757
d.length++
758
}
759
}
760
return d
761
}
762
})(obj)