Permalink
Nov 9, 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
Nov 9, 2015
Nov 9, 2015
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
Jun 10, 2016
Newer
100644
714 lines (601 sloc)
19.8 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) {
102
__repr__:function(){return klass.__name__+'('+ new $item_generator(items).as_list().join(',') + ')'},
115
var $ = $B.args('__contains__', 2, {self:null, item:null},
116
['self', 'item'], arguments, {}, null, null),
117
self=$.self, item=$.item
121
switch(typeof item) {
122
case 'string':
123
return self.$string_dict[item] !==undefined
129
if (self.$str_hash[_key]!==undefined &&
130
_b_.getattr(item,'__eq__')(self.$str_hash[_key])){return true}
131
if (self.$numeric_dict[_key]!==undefined &&
132
_b_.getattr(item,'__eq__')(_key)){return true}
134
// If the key is an object, its hash must be in the dict keys but the
135
// key itself must compare equal to the key associated with the hash
136
// For instance :
137
//
138
// class X:
139
// def __hash__(self): return hash('u')
140
//
141
// a = {'u': 'a', X(): 'b'}
142
// assert set(a.values())=={'a', 'b'}
143
// assert not X() in a
144
151
$DictDict.__delitem__ = function(){
152
153
var $ = $B.args('__eq__', 2, {self:null, arg:null},
154
['self', 'arg'], arguments, {}, null, null),
155
self=$.self, arg=$.arg
156
157
if(self.$jsobj){
158
if(self.$jsobj[arg]===undefined){throw KeyError(arg)}
159
delete self.$jsobj[arg]
162
switch(typeof arg) {
163
case 'string':
164
if (self.$string_dict[arg] === undefined) throw KeyError(_b_.str(arg))
165
delete self.$string_dict[arg]
168
case 'number':
169
if (self.$numeric_dict[arg] === undefined) throw KeyError(_b_.str(arg))
170
delete self.$numeric_dict[arg]
185
$DictDict.__eq__ = function(){
186
var $ = $B.args('__eq__', 2, {self:null, other:null},
187
['self', 'other'], arguments, {}, null, null),
188
self=$.self, other=$.other
189
194
if((self.$numeric_dict.length!=other.$numeric_dict.length) ||
195
(self.$string_dict.length!=other.$string_dict.length) ||
196
(self.$object_dict.length!=other.$object_dict.length)){
197
return false
199
for(var k in self.$numeric_dict){
200
if(!_b_.getattr(other.$numeric_dict[k],'__eq__')(self.$numeric_dict[k])){
201
return false
202
}
203
}
204
for(var k in self.$string_dict){
205
if(!_b_.getattr(other.$string_dict[k],'__eq__')(self.$string_dict[k])){
206
return false
207
}
208
}
209
for(var k in self.$object_dict){
210
if(!_b_.getattr(other.$object_dict[k][1],'__eq__')(self.$object_dict[k][1])){
211
return false
212
}
213
}
214
219
$DictDict.__getitem__ = function(){
220
var $ = $B.args('__getitem__', 2, {self:null, arg:null},
221
['self', 'arg'], arguments, {}, null, null),
222
self=$.self, arg=$.arg
223
224
if(self.$jsobj){
225
if(self.$jsobj[arg]===undefined){return None}
226
return self.$jsobj[arg]
227
}
230
switch(typeof arg) {
231
case 'string':
232
if (self.$string_dict[arg] !== undefined) return self.$string_dict[arg]
233
break
234
case 'number':
235
if (self.$numeric_dict[arg] !== undefined) return self.$numeric_dict[arg]
247
if (self.$numeric_dict[_key]!==undefined && _eq(_key)){
248
return self.$numeric_dict[_key]
250
251
var obj_ref = self.$object_dict[_key]
252
if(obj_ref!==undefined){
253
// An object with the same hash is already stored
254
// Lookup should fail if equality raises an exception
255
_eq(self.$object_dict[_key][0])
258
if(self.__class__!==$DictDict){
259
try{
260
var missing_method = getattr(self.__class__.$factory, '__missing__')
261
return missing_method(self, arg)
262
}catch(err){}
263
}
270
var args = [], pos=0
271
for(var i=1;i<arguments.length;i++){args[pos++]=arguments[i]}
289
if(obj.__class__===$B.JSObject.$dict){
290
// convert a JSObject into a Python dictionary
291
var si = $DictDict.__setitem__
292
for(var attr in obj.js) si(self,attr,obj.js[attr])
293
294
// Attribute $jsobj is used to update the original JS object
295
// when the dictionary is modified
318
}else{
319
var iterable = iter(args[0])
320
while(1){
321
try{
322
var elt = next(iterable)
323
var key = getattr(elt,'__getitem__')(0)
324
var value = getattr(elt,'__getitem__')(1)
325
$DictDict.__setitem__(self, key, value)
326
}catch(err){
350
for (var k in self.$numeric_dict) _count++
351
for (var k in self.$string_dict) _count++
352
for (var k in self.$object_dict) _count+= self.$object_dict[k].length
358
359
$DictDict.__ne__ = function(self,other){return !$DictDict.__eq__(self,other)}
360
361
$DictDict.__next__ = function(self){
369
}
370
}
371
372
$DictDict.__repr__ = function(self){
373
if(self===undefined) return "<class 'dict'>"
374
if(self.$jsobj){ // wrapper around Javascript object
375
var res = []
376
for(var attr in self.$jsobj){
377
if(attr.charAt(0)=='$' || attr=='__class__'){continue}
378
else{
379
try{
380
res.push("'"+attr+"': "+_b_.repr(self.$jsobj[attr]))
381
}catch(err){
382
// FIX ME
383
}
384
}
393
if(itm[1]===self){res[pos++]=repr(itm[0])+': {...}'}
394
else{res[pos++]=repr(itm[0])+': '+repr(itm[1])}
401
var $ = $B.args('__setitem__', 3, {self:null, key:null, value:null},
402
['self', 'key', 'value'], arguments, {}, null, null),
403
self=$.self, key=$.key, value=$.value
404
422
if(self.$numeric_dict[_key]!==undefined && _eq(_key)){
423
self.$numeric_dict[_key] = value
425
}
426
var sk = self.$str_hash[_key]
427
if(sk!==undefined && _eq(sk)){
428
self.$string_dict[sk] = value
432
var obj_ref = self.$object_dict[_key]
433
if(obj_ref!==undefined){
434
// An object with the same hash is already stored
435
// Lookup should fail if equality raises an exception
436
_eq(self.$object_dict[_key][0])
437
}
438
self.$object_dict[_key] = [key, value]
440
}
441
442
$DictDict.__str__ = $DictDict.__repr__
443
444
// add "reflected" methods
445
$B.make_rmethods($DictDict)
446
449
var $ = $B.args('clear',1,{self:null},['self'],arguments,{},null,null),
450
self = $.self
459
}
460
461
$DictDict.copy = function(self){
462
// Return a shallow copy of the dictionary
463
var $ = $B.args('copy',1,{self:null},['self'],arguments,{},null,null),
464
self = $.self,
465
res = _b_.dict()
472
var $ = $B.args('fromkeys', 3, {cls:null, keys:null, value:null},
473
['cls', 'keys', 'value'], arguments, {value:_b_.None}, null, null),
478
var keys_iter = _b_.iter(keys)
479
while(1){
480
try{
481
var key = _b_.next(keys_iter)
482
$DictDict.__setitem__(res,key,value)
483
}catch(err){
484
if($B.is_exc(err,[_b_.StopIteration])){
485
return res
486
}
487
throw err
488
}
489
}
490
}
494
var $ = $B.args('get', 3, {self:null, key:null, _default:null},
495
['self', 'key', '_default'], arguments, {_default:$N}, null, null)
497
try{return $DictDict.__getitem__($.self, $.key)}
498
catch(err){
499
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
500
else{throw err}
501
}
502
}
503
504
var $dict_itemsDict = $B.$iterator_class('dict_items')
505
506
$DictDict.items = function(self){
507
if (arguments.length > 1) {
508
var _len=arguments.length - 1
509
var _msg="items() takes no arguments ("+_len+" given)"
510
throw _b_.TypeError(_msg)
511
}
512
return $iterator_wrapper(new $item_iterator(self), $dict_itemsDict)
513
}
514
515
var $dict_keysDict = $B.$iterator_class('dict_keys')
516
517
$DictDict.keys = function(self){
518
if (arguments.length > 1) {
519
var _len=arguments.length - 1
520
var _msg="keys() takes no arguments ("+_len+" given)"
521
throw _b_.TypeError(_msg)
522
}
523
return $iterator_wrapper(new $key_iterator(self),$dict_keysDict)
524
}
525
526
$DictDict.pop = function(){
527
528
var $ = $B.args('pop', 3, {self:null, key: null, _default:null},
529
['self', 'key', '_default'], arguments, {_default:$N}, null, null),
530
self=$.self, key=$.key, _default=$._default
531
532
try{
533
var res = $DictDict.__getitem__(self,key)
534
$DictDict.__delitem__(self,key)
535
return res
536
}catch(err){
537
if(err.__name__==='KeyError'){
538
if(_default!==undefined) return _default
539
throw err
540
}
541
throw err
542
}
543
}
544
545
$DictDict.popitem = function(self){
546
try{
547
var itm = new $item_iterator(self).next()
548
$DictDict.__delitem__(self,itm[0])
549
return _b_.tuple(itm)
550
}catch(err) {
551
if (err.__name__ == "StopIteration") {
552
throw KeyError("'popitem(): dictionary is empty'")
553
}
554
}
557
$DictDict.setdefault = function(){
558
559
var $ = $B.args('setdefault', 3, {self:null, key: null, _default:null},
560
['self', 'key', '_default'], arguments, {}, null, null),
561
self=$.self, key=$.key, _default=$._default
562
563
try{return $DictDict.__getitem__(self,key)}
564
catch(err){
565
if(_default===undefined) _default=None
566
$DictDict.__setitem__(self,key,_default)
567
return _default
568
}
569
}
570
571
$DictDict.update = function(self){
572
573
var $ = $B.args('update',1,{'self':null},['self'],arguments,{},'args','kw'),
574
self=$.self, args=$.args, kw=$.kw
575
576
if(args.length>0) {
577
var o=args[0]
578
if (isinstance(o,dict)){
579
$copy_dict(self, o)
580
} else if (hasattr(o, '__getitem__') && hasattr(o, 'keys')) {
581
var _keys=_b_.list(getattr(o, 'keys')())
582
var si=$DictDict.__setitem__
583
var i=_keys.length
584
while(i--) {
585
//for (var i=0; i < _keys.length; i++) {
586
var _value = getattr(o, '__getitem__')(_keys[i])
587
si(self, _keys[i], _value)
595
var $dict_valuesDict = $B.$iterator_class('dict_values')
596
597
$DictDict.values = function(self){
598
if (arguments.length > 1) {
599
var _len=arguments.length - 1
600
var _msg="values() takes no arguments ("+_len+" given)"
601
throw _b_.TypeError(_msg)
602
}
603
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
604
}
605
608
var res = {__class__:$DictDict,
609
$numeric_dict : {},
610
$object_dict : {},
611
$string_dict : {},
612
$str_hash: {},
613
length: 0
614
}
615
616
if(args===undefined){return res}
617
618
if(second===undefined){
619
if(Array.isArray(args)){
620
// Form "dict([[key1, value1], [key2,value2], ...])"
621
var i = -1, stop = args.length-1
622
var si = $DictDict.__setitem__
623
while(i++<stop){
624
var item=args[i]
625
switch(typeof item[0]) {
626
case 'string':
627
res.$string_dict[item[0]]=item[1]
628
res.$str_hash[str_hash(item[0])]=item[0]
629
break;
630
case 'number':
631
res.$numeric_dict[item[0]]=item[1]
632
break
633
default:
634
si(res, item[0], item[1])
635
break
636
}
637
}
638
return res
639
}else if(args.$nat=='kw'){
640
// Form dict(k1=v1, k2=v2...)
641
var kw = args['kw']
642
for(var attr in kw){
643
switch(typeof attr) {
644
case 'string':
645
res.$string_dict[attr]=kw[attr]
646
res.$str_hash[str_hash(attr)]=attr
647
break;
648
case 'number':
649
res.$numeric_dict[attr]=kw[attr]
650
break
651
default:
652
si(res, attr, kw[attr])
653
break
654
}
661
var _args = [res], pos=1
662
for(var i=0, _len_i = arguments.length; i < _len_i;i++){_args[pos++]=arguments[i]}
667
dict.__class__ = $B.$factory
668
dict.$dict = $DictDict
669
$DictDict.$factory = dict
670
$DictDict.__new__ = $B.$__new__(dict)
671
672
_b_.dict = dict
674
// following are used for faster access elsewhere
675
$B.$dict_iterator = function(d) { return new $item_generator(d) }
676
$B.$dict_length = $DictDict.__len__
677
$B.$dict_getitem = $DictDict.__getitem__
678
$B.$dict_get = $DictDict.get
679
$B.$dict_set = $DictDict.__setitem__
680
$B.$dict_contains = $DictDict.__contains__
681
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
682
$B.$copy_dict = $copy_dict // copy from right to left
683
$B.$dict_get_copy = $DictDict.copy // return a shallow copy
684
686
// Class for attribute __dict__ of classes
687
var mappingproxyDict = {
688
__class__ : $B.$type,
689
__name__ : "mappingproxy"
690
}
692
693
mappingproxyDict.__setitem__ = function(){
694
throw _b_.TypeError("'mappingproxy' object does not support item assignment")
695
}
696
698
function mappingproxy(obj){
699
var res = obj_dict(obj)
700
res.__class__ = mappingproxyDict
701
return res
702
}
703
mappingproxy.__class__ = $B.$factory
704
mappingproxy.$dict = mappingproxyDict
705
mappingproxyDict.$factory = mappingproxy
706
$B.mappingproxy = mappingproxy
707
708
$B.obj_dict = function(obj){
709
var res = dict()
710
res.$jsobj = obj
711
return res
712
}
713