Permalink
Nov 21, 2015
Nov 9, 2015
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
Nov 21, 2015
Jan 31, 2016
Nov 4, 2015
Dec 31, 2015
Nov 21, 2015
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
Newer
100644
685 lines (577 sloc)
19 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) {
92
}
93
94
$iterator_wrapper = function(items,klass){
95
var res = {
96
__class__:klass,
102
__repr__:function(){return klass.__name__+'('+ new $item_generator(items).as_list().join(',') + ')'},
108
$DictDict.__bool__ = function (self) {
109
var $=$B.args('__bool__',1,{self:null},['self'],arguments,{},null,null)
110
return $DictDict.__len__(self) > 0
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
}
229
switch(typeof arg) {
230
case 'string':
231
if (self.$string_dict[arg] !== undefined) return self.$string_dict[arg]
232
break
233
case 'number':
234
if (self.$numeric_dict[arg] !== undefined) return self.$numeric_dict[arg]
240
var sk = self.$str_hash[_key]
241
if (sk!==undefined && _b_.getattr(arg,'__eq__')(sk)){
242
return self.$string_dict[sk]
243
}
244
if (self.$numeric_dict[_key]!==undefined &&
245
_b_.getattr(arg,'__eq__')(_key)){
246
return self.$numeric_dict[_key]
247
}
251
if(self.__class__!==$DictDict){
252
try{
253
var missing_method = getattr(self.__class__.$factory, '__missing__')
254
return missing_method(self, arg)
255
}catch(err){}
256
}
260
$DictDict.__hash__ = function(self) {
261
if (self === undefined) {
262
return $DictDict.__hashvalue__ || $B.$py_next_hash-- // for hash of dict type (not instance of dict)
263
}
264
throw _b_.TypeError("unhashable type: 'dict'");
265
}
268
var args = [], pos=0
269
for(var i=1;i<arguments.length;i++){args[pos++]=arguments[i]}
287
if(obj.__class__===$B.JSObject.$dict){
288
// convert a JSObject into a Python dictionary
289
var si = $DictDict.__setitem__
290
for(var attr in obj.js) si(self,attr,obj.js[attr])
291
292
// Attribute $jsobj is used to update the original JS object
293
// when the dictionary is modified
316
}else{
317
var iterable = iter(args[0])
318
while(1){
319
try{
320
var elt = next(iterable)
321
var key = getattr(elt,'__getitem__')(0)
322
var value = getattr(elt,'__getitem__')(1)
323
$DictDict.__setitem__(self, key, value)
324
}catch(err){
348
for (var k in self.$numeric_dict) _count++
349
for (var k in self.$string_dict) _count++
350
for (var k in self.$object_dict) _count+= self.$object_dict[k].length
354
355
$DictDict.__mro__ = [$DictDict,$ObjectDict]
356
357
$DictDict.__ne__ = function(self,other){return !$DictDict.__eq__(self,other)}
358
359
$DictDict.__next__ = function(self){
367
}
368
}
369
370
$DictDict.__repr__ = function(self){
371
if(self===undefined) return "<class 'dict'>"
372
if(self.$jsobj){ // wrapper around Javascript object
373
var res = []
374
for(var attr in self.$jsobj){
375
if(attr.charAt(0)=='$' || attr=='__class__'){continue}
376
else{
377
try{
378
res.push("'"+attr+"': "+_b_.repr(self.$jsobj[attr]))
379
}catch(err){
380
// FIX ME
381
}
382
}
391
if(itm[1]===self){res[pos++]=repr(itm[0])+': {...}'}
392
else{res[pos++]=repr(itm[0])+': '+repr(itm[1])}
399
var $ = $B.args('__setitem__', 3, {self:null, key:null, value:null},
400
['self', 'key', 'value'], arguments, {}, null, null),
401
self=$.self, key=$.key, value=$.value
402
418
var _eq=getattr(key, '__eq__')
419
420
if(self.$numeric_dict[_key]!==undefined && _eq(_key)){
421
self.$numeric_dict[_key] = value
423
}
424
var sk = self.$str_hash[_key]
425
if(sk!==undefined && _eq(sk)){
426
self.$string_dict[sk] = value
432
}
433
434
$DictDict.__str__ = $DictDict.__repr__
435
436
// add "reflected" methods
437
$B.make_rmethods($DictDict)
438
441
var $ = $B.args('clear',1,{self:null},['self'],arguments,{},null,null),
442
self = $.self
451
}
452
453
$DictDict.copy = function(self){
454
// Return a shallow copy of the dictionary
455
var $ = $B.args('copy',1,{self:null},['self'],arguments,{},null,null),
456
self = $.self,
457
res = _b_.dict()
464
var $ = $B.args('fromkeys', 3, {cls:null, keys:null, value:null},
465
['cls', 'keys', 'value'], arguments, {value:_b_.None}, null, null),
468
// class method
469
var res = dict()
470
var keys_iter = _b_.iter(keys)
471
while(1){
472
try{
473
var key = _b_.next(keys_iter)
474
$DictDict.__setitem__(res,key,value)
475
}catch(err){
476
if($B.is_exc(err,[_b_.StopIteration])){
477
return res
478
}
479
throw err
480
}
481
}
482
}
486
var $ = $B.args('get', 3, {self:null, key:null, _default:null},
487
['self', 'key', '_default'], arguments, {_default:$N}, null, null)
489
try{return $DictDict.__getitem__($.self, $.key)}
490
catch(err){
491
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
492
else{throw err}
493
}
494
}
495
496
var $dict_itemsDict = $B.$iterator_class('dict_items')
497
498
$DictDict.items = function(self){
499
if (arguments.length > 1) {
500
var _len=arguments.length - 1
501
var _msg="items() takes no arguments ("+_len+" given)"
502
throw _b_.TypeError(_msg)
503
}
504
return $iterator_wrapper(new $item_iterator(self), $dict_itemsDict)
505
}
506
507
var $dict_keysDict = $B.$iterator_class('dict_keys')
508
509
$DictDict.keys = function(self){
510
if (arguments.length > 1) {
511
var _len=arguments.length - 1
512
var _msg="keys() takes no arguments ("+_len+" given)"
513
throw _b_.TypeError(_msg)
514
}
515
return $iterator_wrapper(new $key_iterator(self),$dict_keysDict)
516
}
517
518
$DictDict.pop = function(){
519
520
var $ = $B.args('pop', 3, {self:null, key: null, _default:null},
521
['self', 'key', '_default'], arguments, {_default:$N}, null, null),
522
self=$.self, key=$.key, _default=$._default
523
524
try{
525
var res = $DictDict.__getitem__(self,key)
526
$DictDict.__delitem__(self,key)
527
return res
528
}catch(err){
529
if(err.__name__==='KeyError'){
530
if(_default!==undefined) return _default
531
throw err
532
}
533
throw err
534
}
535
}
536
537
$DictDict.popitem = function(self){
538
try{
539
var itm = new $item_iterator(self).next()
540
$DictDict.__delitem__(self,itm[0])
541
return _b_.tuple(itm)
542
}catch(err) {
543
if (err.__name__ == "StopIteration") {
544
throw KeyError("'popitem(): dictionary is empty'")
545
}
546
}
549
$DictDict.setdefault = function(){
550
551
var $ = $B.args('setdefault', 3, {self:null, key: null, _default:null},
552
['self', 'key', '_default'], arguments, {}, null, null),
553
self=$.self, key=$.key, _default=$._default
554
555
try{return $DictDict.__getitem__(self,key)}
556
catch(err){
557
if(_default===undefined) _default=None
558
$DictDict.__setitem__(self,key,_default)
559
return _default
560
}
561
}
562
563
$DictDict.update = function(self){
564
565
var $ = $B.args('update',1,{'self':null},['self'],arguments,{},'args','kw'),
566
self=$.self, args=$.args, kw=$.kw
567
568
if(args.length>0) {
569
var o=args[0]
570
if (isinstance(o,dict)){
571
$copy_dict(self, o)
572
} else if (hasattr(o, '__getitem__') && hasattr(o, 'keys')) {
573
var _keys=_b_.list(getattr(o, 'keys')())
574
var si=$DictDict.__setitem__
575
var i=_keys.length
576
while(i--) {
577
//for (var i=0; i < _keys.length; i++) {
578
var _value = getattr(o, '__getitem__')(_keys[i])
579
si(self, _keys[i], _value)
587
var $dict_valuesDict = $B.$iterator_class('dict_values')
588
589
$DictDict.values = function(self){
590
if (arguments.length > 1) {
591
var _len=arguments.length - 1
592
var _msg="values() takes no arguments ("+_len+" given)"
593
throw _b_.TypeError(_msg)
594
}
595
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
596
}
597
612
var item=args[i]
613
switch(typeof item[0]) {
614
case 'string':
615
res.$string_dict[item[0]]=item[1]
617
break;
618
case 'number':
619
res.$numeric_dict[item[0]]=item[1]
620
break
621
default:
622
si(res, item[0], item[1])
632
var _args = [res], pos=1
633
for(var i=0, _len_i = arguments.length; i < _len_i;i++){_args[pos++]=arguments[i]}
638
dict.__class__ = $B.$factory
639
dict.$dict = $DictDict
640
$DictDict.$factory = dict
641
$DictDict.__new__ = $B.$__new__(dict)
642
643
_b_.dict = dict
645
// following are used for faster access elsewhere
646
$B.$dict_iterator = function(d) { return new $item_generator(d) }
647
$B.$dict_length = $DictDict.__len__
648
$B.$dict_getitem = $DictDict.__getitem__
649
$B.$dict_get = $DictDict.get
650
$B.$dict_set = $DictDict.__setitem__
651
$B.$dict_contains = $DictDict.__contains__
652
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
653
$B.$copy_dict = $copy_dict // copy from right to left
654
$B.$dict_get_copy = $DictDict.copy // return a shallow copy
655
657
// Class for attribute __dict__ of classes
658
var mappingproxyDict = {
659
__class__ : $B.$type,
660
__name__ : "mappingproxy"
661
}
662
mappingproxyDict.__mro__ = [mappingproxyDict, _b_.object.$dict]
663
664
mappingproxyDict.__setitem__ = function(){
665
throw _b_.TypeError("'mappingproxy' object does not support item assignment")
666
}
667
669
function mappingproxy(obj){
670
var res = obj_dict(obj)
671
res.__class__ = mappingproxyDict
672
return res
673
}
674
mappingproxy.__class__ = $B.$factory
675
mappingproxy.$dict = mappingproxyDict
676
mappingproxyDict.$factory = mappingproxy
677
$B.mappingproxy = mappingproxy
678
679
$B.obj_dict = function(obj){
680
var res = dict()
681
res.$jsobj = obj
682
return res
683
}
684