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
Feb 10, 2018
Feb 10, 2018
Nov 21, 2015
Jan 15, 2018
Nov 21, 2015
Nov 21, 2015
Feb 10, 2018
Jan 15, 2018
May 28, 2016
May 28, 2016
Feb 10, 2018
Feb 9, 2018
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
Nov 21, 2015
Nov 21, 2015
Jun 10, 2016
Jun 10, 2016
Feb 3, 2018
Feb 10, 2018
Jan 15, 2018
Feb 3, 2018
Newer
100644
799 lines (682 sloc)
22.6 KB
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])
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
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
173
$DictDict.__delitem__ = function(){
174
175
var $ = $B.args('__eq__', 2, {self:null, arg:null},
176
['self', 'arg'], arguments, {}, null, null),
177
self=$.self, arg=$.arg
178
186
if (self.$string_dict[arg] === undefined) throw KeyError.$factory(_b_.str.$factory(arg))
191
if (self.$numeric_dict[arg] === undefined) throw KeyError.$factory(_b_.str.$factory(arg))
207
$DictDict.__eq__ = function(){
208
var $ = $B.args('__eq__', 2, {self:null, other:null},
209
['self', 'other'], arguments, {}, null, null),
210
self=$.self, other=$.other
211
214
if(self.$jsobj){self=jsobj2dict(self.$jsobj)}
215
if(other.$jsobj){other=jsobj2dict(other.$jsobj)}
219
if((self.$numeric_dict.length!=other.$numeric_dict.length) ||
220
(self.$string_dict.length!=other.$string_dict.length) ||
221
(self.$object_dict.length!=other.$object_dict.length)){
222
return false
244
$DictDict.__getitem__ = function(){
245
var $ = $B.args('__getitem__', 2, {self:null, arg:null},
246
['self', 'arg'], arguments, {}, null, null),
247
self=$.self, arg=$.arg
250
if(!self.$jsobj.hasOwnProperty(arg)) throw _b_.KeyError.$factory(str.$factory(arg))
251
else if(self.$jsobj[arg]===undefined) return _b_.NotImplemented
252
else if(self.$jsobj[arg]===null){return $N}
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]
275
if (self.$numeric_dict[_key]!==undefined && _eq(_key)){
276
return self.$numeric_dict[_key]
280
var obj_ref = self.$object_dict[_key]
281
if(obj_ref!==undefined){
282
// An object with the same hash is already stored
283
// Lookup should fail if equality raises an exception
284
_eq(self.$object_dict[_key][0])
299
var args = [], pos=0
300
for(var i=1;i<arguments.length;i++){args[pos++]=arguments[i]}
317
if(obj.__class__===$B.JSObject.$dict){
318
// convert a JSObject into a Python dictionary
320
// Attribute $jsobj is used to update the original JS object
321
// when the dictionary is modified
347
while(1){
348
try{
349
var elt = next(iterable)
350
var key = getattr(elt,'__getitem__')(0)
351
var value = getattr(elt,'__getitem__')(1)
352
$DictDict.__setitem__(self, key, value)
353
}catch(err){
380
for (var k in self.$numeric_dict) _count++
381
for (var k in self.$string_dict) _count++
382
for (var k in self.$object_dict) _count+= self.$object_dict[k].length
388
389
$DictDict.__ne__ = function(self,other){return !$DictDict.__eq__(self,other)}
390
392
if(cls===undefined){throw _b_.TypeError.$factory('int.__new__(): not enough arguments')}
393
return {
394
__class__:cls.$factory ? cls : cls.$dict,
395
$numeric_dict : {},
396
$object_dict : {},
397
$string_dict : {},
398
$str_hash: {}
399
}
400
}
401
410
}
411
}
412
413
$DictDict.__repr__ = function(self){
414
if(self===undefined) return "<class 'dict'>"
423
if((!self.$jsobj && itm[1]===self) ||
424
(self.$jsobj && itm[1]===self.$jsobj)){
425
res[pos++]=repr(itm[0])+': {...}'
427
try{
428
res[pos++]=repr(itm[0])+': '+repr(itm[1])
429
}catch(err){
430
res[pos++]=repr(itm[0])+': <unprintable object>'
431
}
432
}
439
var $ = $B.args('__setitem__', 3, {self:null, key:null, value:null},
440
['self', 'key', 'value'], arguments, {}, null, null),
441
self=$.self, key=$.key, value=$.value
442
444
if(self.$jsobj.__class__===$B.$type){
445
self.$jsobj[key] = $B.pyobj2jsobj(value)
446
if(key=="__init__" || key=="__new__"){
447
// If class attribute __init__ or __new__ are reset,
448
// the factory function has to change
449
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
450
}
451
}else{
452
self.$jsobj[key]=$B.pyobj2jsobj(value)
453
}
454
return $N
473
if(self.$numeric_dict[_key]!==undefined && _eq(_key)){
474
self.$numeric_dict[_key] = value
476
}
477
var sk = self.$str_hash[_key]
478
if(sk!==undefined && _eq(sk)){
479
self.$string_dict[sk] = value
483
var obj_ref = self.$object_dict[_key]
484
if(obj_ref!==undefined){
485
// An object with the same hash is already stored
486
// Lookup should fail if equality raises an exception
487
_eq(self.$object_dict[_key][0])
488
}
489
self.$object_dict[_key] = [key, value]
491
}
492
493
$DictDict.__str__ = $DictDict.__repr__
494
495
// add "reflected" methods
496
$B.make_rmethods($DictDict)
497
500
var $ = $B.args('clear',1,{self:null},['self'],arguments,{},null,null),
501
self = $.self
508
if(self.$jsobj){
509
for(var attr in self.$jsobj){
510
if(attr.charAt(0) !== '$' && attr !== "__class__"){
511
delete self.$jsobj[attr]
512
}
513
}
514
}
516
}
517
518
$DictDict.copy = function(self){
519
// Return a shallow copy of the dictionary
520
var $ = $B.args('copy',1,{self:null},['self'],arguments,{},null,null),
521
self = $.self,
522
res = _b_.dict()
529
var $ = $B.args('fromkeys', 3, {cls:null, keys:null, value:null},
530
['cls', 'keys', 'value'], arguments, {value:_b_.None}, null, null),
542
if(klass===dict){$DictDict.__setitem__(res, key, value)}
543
else{_b_.getattr(res, "__setitem__")(key,value)}
556
var $ = $B.args('get', 3, {self:null, key:null, _default:null},
557
['self', 'key', '_default'], arguments, {_default:$N}, null, null)
559
try{return $DictDict.__getitem__($.self, $.key)}
560
catch(err){
561
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
562
else{throw err}
563
}
564
}
565
566
var $dict_itemsDict = $B.$iterator_class('dict_items')
567
568
$DictDict.items = function(self){
569
if (arguments.length > 1) {
570
var _len=arguments.length - 1
571
var _msg="items() takes no arguments ("+_len+" given)"
577
var $dict_keysDict = $B.$iterator_class('dict_keys')
578
579
$DictDict.keys = function(self){
580
if (arguments.length > 1) {
581
var _len=arguments.length - 1
582
var _msg="keys() takes no arguments ("+_len+" given)"
584
}
585
return $iterator_wrapper(new $key_iterator(self),$dict_keysDict)
586
}
587
588
$DictDict.pop = function(){
589
590
var $ = $B.args('pop', 3, {self:null, key: null, _default:null},
591
['self', 'key', '_default'], arguments, {_default:$N}, null, null),
592
self=$.self, key=$.key, _default=$._default
593
594
try{
595
var res = $DictDict.__getitem__(self,key)
596
$DictDict.__delitem__(self,key)
597
return res
598
}catch(err){
599
if(err.__name__==='KeyError'){
600
if(_default!==undefined) return _default
601
throw err
602
}
603
throw err
604
}
605
}
606
607
$DictDict.popitem = function(self){
609
try{
610
var itm = new $item_iterator(self).next()
611
$DictDict.__delitem__(self,itm[0])
612
return _b_.tuple(itm)
613
}catch(err) {
614
if (err.__name__ == "StopIteration") {
621
$DictDict.setdefault = function(){
622
623
var $ = $B.args('setdefault', 3, {self:null, key: null, _default:null},
630
$DictDict.__setitem__(self,key,_default)
631
return _default
632
}
633
}
634
635
$DictDict.update = function(self){
636
637
var $ = $B.args('update',1,{'self':null},['self'],arguments,{},'args','kw'),
638
self=$.self, args=$.args, kw=$.kw
643
$copy_dict(self, o)
644
} else if (hasattr(o, '__getitem__') && hasattr(o, 'keys')) {
645
var _keys=_b_.list(getattr(o, 'keys')())
646
var si=$DictDict.__setitem__
647
var i=_keys.length
648
while(i--) {
649
//for (var i=0; i < _keys.length; i++) {
650
var _value = getattr(o, '__getitem__')(_keys[i])
651
si(self, _keys[i], _value)
659
var $dict_valuesDict = $B.$iterator_class('dict_values')
660
661
$DictDict.values = function(self){
662
if (arguments.length > 1) {
663
var _len=arguments.length - 1
664
var _msg="values() takes no arguments ("+_len+" given)"
666
}
667
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
668
}
669
672
var res = {__class__:$DictDict,
673
$numeric_dict : {},
674
$object_dict : {},
675
$string_dict : {},
681
if(second===undefined){
682
if(Array.isArray(args)){
683
// Form "dict([[key1, value1], [key2,value2], ...])"
684
var i = -1, stop = args.length-1
685
var si = $DictDict.__setitem__
686
while(i++<stop){
687
var item=args[i]
688
switch(typeof item[0]) {
689
case 'string':
690
res.$string_dict[item[0]]=item[1]
691
res.$str_hash[str_hash(item[0])]=item[0]
692
break;
693
case 'number':
694
res.$numeric_dict[item[0]]=item[1]
695
break
696
default:
697
si(res, item[0], item[1])
698
break
699
}
700
}
701
return res
702
}else if(args.$nat=='kw'){
703
// Form dict(k1=v1, k2=v2...)
704
var kw = args['kw']
705
for(var attr in kw){
706
switch(typeof attr) {
707
case 'string':
708
res.$string_dict[attr]=kw[attr]
709
res.$str_hash[str_hash(attr)]=attr
710
break;
711
case 'number':
712
res.$numeric_dict[attr]=kw[attr]
713
break
714
default:
715
si(res, attr, kw[attr])
716
break
717
}
720
}else if(args.$jsobj){
721
res.$jsobj = {}
722
for(var attr in args.$jsobj){res.$jsobj[attr] = args.$jsobj[attr]}
723
return res
728
var _args = [res], pos=1
729
for(var i=0, _len_i = arguments.length; i < _len_i;i++){_args[pos++]=arguments[i]}
734
dict.__class__ = $B.$factory
735
dict.$dict = $DictDict
736
$DictDict.$factory = dict
737
738
_b_.dict = dict
742
// following are used for faster access elsewhere
743
$B.$dict_iterator = function(d) { return new $item_generator(d) }
744
$B.$dict_length = $DictDict.__len__
745
$B.$dict_getitem = $DictDict.__getitem__
746
$B.$dict_get = $DictDict.get
747
$B.$dict_set = $DictDict.__setitem__
748
$B.$dict_contains = $DictDict.__contains__
749
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
750
$B.$copy_dict = $copy_dict // copy from right to left
751
$B.$dict_get_copy = $DictDict.copy // return a shallow copy
752
754
// Class for attribute __dict__ of classes
755
var mappingproxyDict = {
756
__class__ : $B.$type,
757
__name__ : "mappingproxy"
758
}
762
throw _b_.TypeError.$factory("'mappingproxy' object does not support item assignment")
766
function mappingproxy(obj){
767
var res = obj_dict(obj)
768
res.__class__ = mappingproxyDict
769
return res
770
}
771
mappingproxy.__class__ = $B.$factory
772
mappingproxy.$dict = mappingproxyDict
773
mappingproxyDict.$factory = mappingproxy
774
$B.mappingproxy = mappingproxy
775
776
function jsobj2dict(x){
777
var d = dict()
778
for(var attr in x){
779
if(attr.charAt(0)!='$' && attr!=='__class__'){
780
if(x[attr].$jsobj===x){
781
d.$string_dict[attr] = d
782
}else{
783
d.$string_dict[attr] = x[attr]
784
}