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
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
Newer
100644
739 lines (623 sloc)
20.4 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) {
48
for(var attr in d.$jsobj){
49
if(attr.charAt(0)!='$'){this.items.push([attr,d.$jsobj[attr]])}
50
}
72
}
73
throw _b_.StopIteration("StopIteration")
74
}
75
$item_generator.prototype.as_list = function() {
77
}
78
79
var $item_iterator = function(d) {
80
this.d = d
81
this.current = 0
82
this.iter = new $item_generator(d)
83
}
85
$item_iterator.prototype.next = function() { return _b_.tuple(this.iter.next()) }
86
87
var $copy_dict = function(left, right) {
94
function toSet(items){
95
// Build a set from the iteration on items
96
var res = []
97
while(true){
98
try{res.push(items.next())}
99
catch(err){break}
100
}
101
return _b_.set(res)
102
}
103
107
__eq__:function(other){
108
// compare set of items to other
109
return getattr(toSet(items), "__eq__")(other)
110
},
116
__repr__:function(){
117
var s = []
118
for(var i=0, len=items.length(); i<len; i++){
119
s.push(_b_.repr(items.next()))
120
}
121
return klass.__name__+'(['+ s.join(',') + '])'
122
},
135
var $ = $B.args('__contains__', 2, {self:null, item:null},
136
['self', 'item'], arguments, {}, null, null),
137
self=$.self, item=$.item
141
switch(typeof item) {
142
case 'string':
143
return self.$string_dict[item] !==undefined
149
if (self.$str_hash[_key]!==undefined &&
150
_b_.getattr(item,'__eq__')(self.$str_hash[_key])){return true}
151
if (self.$numeric_dict[_key]!==undefined &&
152
_b_.getattr(item,'__eq__')(_key)){return true}
154
// If the key is an object, its hash must be in the dict keys but the
155
// key itself must compare equal to the key associated with the hash
156
// For instance :
157
//
158
// class X:
159
// def __hash__(self): return hash('u')
160
//
161
// a = {'u': 'a', X(): 'b'}
162
// assert set(a.values())=={'a', 'b'}
163
// assert not X() in a
164
171
$DictDict.__delitem__ = function(){
172
173
var $ = $B.args('__eq__', 2, {self:null, arg:null},
174
['self', 'arg'], arguments, {}, null, null),
175
self=$.self, arg=$.arg
176
177
if(self.$jsobj){
178
if(self.$jsobj[arg]===undefined){throw KeyError(arg)}
179
delete self.$jsobj[arg]
182
switch(typeof arg) {
183
case 'string':
184
if (self.$string_dict[arg] === undefined) throw KeyError(_b_.str(arg))
185
delete self.$string_dict[arg]
188
case 'number':
189
if (self.$numeric_dict[arg] === undefined) throw KeyError(_b_.str(arg))
190
delete self.$numeric_dict[arg]
205
$DictDict.__eq__ = function(){
206
var $ = $B.args('__eq__', 2, {self:null, other:null},
207
['self', 'other'], arguments, {}, null, null),
208
self=$.self, other=$.other
209
214
if((self.$numeric_dict.length!=other.$numeric_dict.length) ||
215
(self.$string_dict.length!=other.$string_dict.length) ||
216
(self.$object_dict.length!=other.$object_dict.length)){
217
return false
219
for(var k in self.$numeric_dict){
220
if(!_b_.getattr(other.$numeric_dict[k],'__eq__')(self.$numeric_dict[k])){
221
return false
222
}
223
}
224
for(var k in self.$string_dict){
225
if(!_b_.getattr(other.$string_dict[k],'__eq__')(self.$string_dict[k])){
226
return false
227
}
228
}
229
for(var k in self.$object_dict){
231
if(!_b_.getattr(other.$object_dict[k][1],'__eq__')(self.$object_dict[k][1])){
232
return false
233
}
234
}
235
240
$DictDict.__getitem__ = function(){
241
var $ = $B.args('__getitem__', 2, {self:null, arg:null},
242
['self', 'arg'], arguments, {}, null, null),
243
self=$.self, arg=$.arg
244
245
if(self.$jsobj){
246
if(self.$jsobj[arg]===undefined){return None}
247
return self.$jsobj[arg]
248
}
251
switch(typeof arg) {
252
case 'string':
253
if (self.$string_dict[arg] !== undefined) return self.$string_dict[arg]
254
break
255
case 'number':
256
if (self.$numeric_dict[arg] !== undefined) return self.$numeric_dict[arg]
268
if (self.$numeric_dict[_key]!==undefined && _eq(_key)){
269
return self.$numeric_dict[_key]
271
272
var obj_ref = self.$object_dict[_key]
273
if(obj_ref!==undefined){
274
// An object with the same hash is already stored
275
// Lookup should fail if equality raises an exception
276
_eq(self.$object_dict[_key][0])
279
if(self.__class__!==$DictDict){
280
try{
281
var missing_method = getattr(self.__class__.$factory, '__missing__')
282
return missing_method(self, arg)
283
}catch(err){}
284
}
291
var args = [], pos=0
292
for(var i=1;i<arguments.length;i++){args[pos++]=arguments[i]}
309
if(obj.__class__===$B.JSObject.$dict){
310
// convert a JSObject into a Python dictionary
311
var si = $DictDict.__setitem__
312
for(var attr in obj.js) si(self,attr,obj.js[attr])
313
314
// Attribute $jsobj is used to update the original JS object
315
// when the dictionary is modified
340
while(1){
341
try{
342
var elt = next(iterable)
343
var key = getattr(elt,'__getitem__')(0)
344
var value = getattr(elt,'__getitem__')(1)
345
$DictDict.__setitem__(self, key, value)
346
}catch(err){
370
for (var k in self.$numeric_dict) _count++
371
for (var k in self.$string_dict) _count++
372
for (var k in self.$object_dict) _count+= self.$object_dict[k].length
378
379
$DictDict.__ne__ = function(self,other){return !$DictDict.__eq__(self,other)}
380
381
$DictDict.__next__ = function(self){
389
}
390
}
391
392
$DictDict.__repr__ = function(self){
393
if(self===undefined) return "<class 'dict'>"
394
if(self.$jsobj){ // wrapper around Javascript object
395
var res = []
396
for(var attr in self.$jsobj){
397
if(attr.charAt(0)=='$' || attr=='__class__'){continue}
398
else{
399
try{
400
res.push("'"+attr+"': "+_b_.repr(self.$jsobj[attr]))
401
}catch(err){
402
// FIX ME
403
}
404
}
413
if(itm[1]===self){res[pos++]=repr(itm[0])+': {...}'}
414
else{res[pos++]=repr(itm[0])+': '+repr(itm[1])}
421
var $ = $B.args('__setitem__', 3, {self:null, key:null, value:null},
422
['self', 'key', 'value'], arguments, {}, null, null),
423
self=$.self, key=$.key, value=$.value
424
442
if(self.$numeric_dict[_key]!==undefined && _eq(_key)){
443
self.$numeric_dict[_key] = value
445
}
446
var sk = self.$str_hash[_key]
447
if(sk!==undefined && _eq(sk)){
448
self.$string_dict[sk] = value
452
var obj_ref = self.$object_dict[_key]
453
if(obj_ref!==undefined){
454
// An object with the same hash is already stored
455
// Lookup should fail if equality raises an exception
456
_eq(self.$object_dict[_key][0])
457
}
458
self.$object_dict[_key] = [key, value]
460
}
461
462
$DictDict.__str__ = $DictDict.__repr__
463
464
// add "reflected" methods
465
$B.make_rmethods($DictDict)
466
469
var $ = $B.args('clear',1,{self:null},['self'],arguments,{},null,null),
470
self = $.self
479
}
480
481
$DictDict.copy = function(self){
482
// Return a shallow copy of the dictionary
483
var $ = $B.args('copy',1,{self:null},['self'],arguments,{},null,null),
484
self = $.self,
485
res = _b_.dict()
492
var $ = $B.args('fromkeys', 3, {cls:null, keys:null, value:null},
493
['cls', 'keys', 'value'], arguments, {value:_b_.None}, null, null),
504
if(klass===dict){$DictDict.__setitem__(res, key, value)}
505
else{_b_.getattr(res, "__setitem__")(key,value)}
506
}catch(err){
507
if($B.is_exc(err,[_b_.StopIteration])){
508
return res
509
}
510
throw err
511
}
512
}
513
}
517
var $ = $B.args('get', 3, {self:null, key:null, _default:null},
518
['self', 'key', '_default'], arguments, {_default:$N}, null, null)
520
try{return $DictDict.__getitem__($.self, $.key)}
521
catch(err){
522
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
523
else{throw err}
524
}
525
}
526
527
var $dict_itemsDict = $B.$iterator_class('dict_items')
528
529
$DictDict.items = function(self){
530
if (arguments.length > 1) {
531
var _len=arguments.length - 1
532
var _msg="items() takes no arguments ("+_len+" given)"
533
throw _b_.TypeError(_msg)
534
}
535
return $iterator_wrapper(new $item_iterator(self), $dict_itemsDict)
536
}
537
538
var $dict_keysDict = $B.$iterator_class('dict_keys')
539
540
$DictDict.keys = function(self){
541
if (arguments.length > 1) {
542
var _len=arguments.length - 1
543
var _msg="keys() takes no arguments ("+_len+" given)"
544
throw _b_.TypeError(_msg)
545
}
546
return $iterator_wrapper(new $key_iterator(self),$dict_keysDict)
547
}
548
549
$DictDict.pop = function(){
550
551
var $ = $B.args('pop', 3, {self:null, key: null, _default:null},
552
['self', 'key', '_default'], arguments, {_default:$N}, null, null),
553
self=$.self, key=$.key, _default=$._default
554
555
try{
556
var res = $DictDict.__getitem__(self,key)
557
$DictDict.__delitem__(self,key)
558
return res
559
}catch(err){
560
if(err.__name__==='KeyError'){
561
if(_default!==undefined) return _default
562
throw err
563
}
564
throw err
565
}
566
}
567
568
$DictDict.popitem = function(self){
569
try{
570
var itm = new $item_iterator(self).next()
571
$DictDict.__delitem__(self,itm[0])
572
return _b_.tuple(itm)
573
}catch(err) {
574
if (err.__name__ == "StopIteration") {
575
throw KeyError("'popitem(): dictionary is empty'")
576
}
577
}
580
$DictDict.setdefault = function(){
581
582
var $ = $B.args('setdefault', 3, {self:null, key: null, _default:null},
586
try{return $DictDict.__getitem__(self,key)}
587
catch(err){
588
if(_default===undefined) _default=None
589
$DictDict.__setitem__(self,key,_default)
590
return _default
591
}
592
}
593
594
$DictDict.update = function(self){
595
596
var $ = $B.args('update',1,{'self':null},['self'],arguments,{},'args','kw'),
597
self=$.self, args=$.args, kw=$.kw
598
599
if(args.length>0) {
600
var o=args[0]
601
if (isinstance(o,dict)){
602
$copy_dict(self, o)
603
} else if (hasattr(o, '__getitem__') && hasattr(o, 'keys')) {
604
var _keys=_b_.list(getattr(o, 'keys')())
605
var si=$DictDict.__setitem__
606
var i=_keys.length
607
while(i--) {
608
//for (var i=0; i < _keys.length; i++) {
609
var _value = getattr(o, '__getitem__')(_keys[i])
610
si(self, _keys[i], _value)
618
var $dict_valuesDict = $B.$iterator_class('dict_values')
619
620
$DictDict.values = function(self){
621
if (arguments.length > 1) {
622
var _len=arguments.length - 1
623
var _msg="values() takes no arguments ("+_len+" given)"
624
throw _b_.TypeError(_msg)
625
}
626
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
627
}
628
631
var res = {__class__:$DictDict,
632
$numeric_dict : {},
633
$object_dict : {},
634
$string_dict : {},
635
$str_hash: {},
636
length: 0
637
}
638
639
if(args===undefined){return res}
640
641
if(second===undefined){
642
if(Array.isArray(args)){
643
// Form "dict([[key1, value1], [key2,value2], ...])"
644
var i = -1, stop = args.length-1
645
var si = $DictDict.__setitem__
646
while(i++<stop){
647
var item=args[i]
648
switch(typeof item[0]) {
649
case 'string':
650
res.$string_dict[item[0]]=item[1]
651
res.$str_hash[str_hash(item[0])]=item[0]
652
break;
653
case 'number':
654
res.$numeric_dict[item[0]]=item[1]
655
break
656
default:
657
si(res, item[0], item[1])
658
break
659
}
660
}
661
return res
662
}else if(args.$nat=='kw'){
663
// Form dict(k1=v1, k2=v2...)
664
var kw = args['kw']
665
for(var attr in kw){
666
switch(typeof attr) {
667
case 'string':
668
res.$string_dict[attr]=kw[attr]
669
res.$str_hash[str_hash(attr)]=attr
670
break;
671
case 'number':
672
res.$numeric_dict[attr]=kw[attr]
673
break
674
default:
675
si(res, attr, kw[attr])
676
break
677
}
684
var _args = [res], pos=1
685
for(var i=0, _len_i = arguments.length; i < _len_i;i++){_args[pos++]=arguments[i]}
690
dict.__class__ = $B.$factory
691
dict.$dict = $DictDict
692
$DictDict.$factory = dict
693
$DictDict.__new__ = $B.$__new__(dict)
694
695
_b_.dict = dict
699
// following are used for faster access elsewhere
700
$B.$dict_iterator = function(d) { return new $item_generator(d) }
701
$B.$dict_length = $DictDict.__len__
702
$B.$dict_getitem = $DictDict.__getitem__
703
$B.$dict_get = $DictDict.get
704
$B.$dict_set = $DictDict.__setitem__
705
$B.$dict_contains = $DictDict.__contains__
706
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
707
$B.$copy_dict = $copy_dict // copy from right to left
708
$B.$dict_get_copy = $DictDict.copy // return a shallow copy
709
711
// Class for attribute __dict__ of classes
712
var mappingproxyDict = {
713
__class__ : $B.$type,
714
__name__ : "mappingproxy"
715
}
717
718
mappingproxyDict.__setitem__ = function(){
719
throw _b_.TypeError("'mappingproxy' object does not support item assignment")
720
}
721
723
function mappingproxy(obj){
724
var res = obj_dict(obj)
725
res.__class__ = mappingproxyDict
726
return res
727
}
728
mappingproxy.__class__ = $B.$factory
729
mappingproxy.$dict = mappingproxyDict
730
mappingproxyDict.$factory = mappingproxy
731
$B.mappingproxy = mappingproxy
732
733
$B.obj_dict = function(obj){
734
var res = dict()
735
res.$jsobj = obj
736
return res
737
}
738