Permalink
Feb 3, 2018
Feb 3, 2018
Jan 15, 2018
Feb 11, 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
Nov 9, 2015
Nov 9, 2015
Nov 9, 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 11, 2018
Feb 12, 2018
Feb 10, 2018
Feb 3, 2018
Newer
100644
786 lines (672 sloc)
21.9 KB
29
var $key_iterator = function(d) {
30
this.d = d
31
this.current = 0
32
this.iter = new $item_generator(d)
33
}
34
$key_iterator.prototype.length = function(){return this.iter.items.length}
35
$key_iterator.prototype.next = function(){return this.iter.next()[0]}
36
37
var $value_iterator = function(d) {
38
this.d = d
39
this.current = 0
40
this.iter = new $item_generator(d)
41
}
42
$value_iterator.prototype.length = function(){return this.iter.items.length}
43
$value_iterator.prototype.next = function(){return this.iter.next()[1]}
54
if(val === undefined){val = _b_.NotImplemented}
55
else if(val === null){val = $N}
56
this.items.push([attr, val])
83
}
84
85
var $item_iterator = function(d) {
86
this.d = d
87
this.current = 0
88
this.iter = new $item_generator(d)
89
}
91
$item_iterator.prototype.next = function() { return _b_.tuple.$factory(this.iter.next()) }
100
function toSet(items){
101
// Build a set from the iteration on items
102
var res = []
103
while(true){
104
try{res.push(items.next())}
105
catch(err){break}
106
}
122
__repr__:function(){
123
var s = []
124
for(var i=0, len=items.length(); i<len; i++){
125
s.push(_b_.repr(items.next()))
126
}
127
return klass.__name__+'(['+ s.join(',') + '])'
128
},
141
var $ = $B.args('__contains__', 2, {self:null, item:null},
142
['self', 'item'], arguments, {}, null, null),
143
self=$.self, item=$.item
147
switch(typeof item) {
148
case 'string':
149
return self.$string_dict[item] !==undefined
160
// If the key is an object, its hash must be in the dict keys but the
161
// key itself must compare equal to the key associated with the hash
162
// For instance :
163
//
164
// class X:
165
// def __hash__(self): return hash('u')
167
// a = {'u': 'a', X(): 'b'}
168
// assert set(a.values())=={'a', 'b'}
169
// assert not X() in a
176
177
var $ = $B.args('__eq__', 2, {self:null, arg:null},
178
['self', 'arg'], arguments, {}, null, null),
179
self=$.self, arg=$.arg
180
188
if (self.$string_dict[arg] === undefined) throw KeyError.$factory(_b_.str.$factory(arg))
193
if (self.$numeric_dict[arg] === undefined) throw KeyError.$factory(_b_.str.$factory(arg))
210
var $ = $B.args('__eq__', 2, {self:null, other:null},
211
['self', 'other'], arguments, {}, null, null),
212
self=$.self, other=$.other
213
216
if(self.$jsobj){self=jsobj2dict(self.$jsobj)}
217
if(other.$jsobj){other=jsobj2dict(other.$jsobj)}
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
247
var $ = $B.args('__getitem__', 2, {self:null, arg:null},
248
['self', 'arg'], arguments, {}, null, null),
249
self=$.self, arg=$.arg
252
if(!self.$jsobj.hasOwnProperty(arg)) throw _b_.KeyError.$factory(str.$factory(arg))
253
else if(self.$jsobj[arg]===undefined) return _b_.NotImplemented
254
else if(self.$jsobj[arg]===null){return $N}
259
switch(typeof arg) {
260
case 'string':
261
if (self.$string_dict[arg] !== undefined) return self.$string_dict[arg]
262
break
263
case 'number':
264
if (self.$numeric_dict[arg] !== undefined) return self.$numeric_dict[arg]
276
if (self.$numeric_dict[_key]!==undefined && _eq(_key)){
277
return self.$numeric_dict[_key]
281
var obj_ref = self.$object_dict[_key]
282
if(obj_ref!==undefined){
283
// An object with the same hash is already stored
284
// Lookup should fail if equality raises an exception
285
_eq(self.$object_dict[_key][0])
300
var args = [], pos=0
301
for(var i=1;i<arguments.length;i++){args[pos++]=arguments[i]}
321
// Attribute $jsobj is used to update the original JS object
322
// when the dictionary is modified
348
while(1){
349
try{
350
var elt = next(iterable)
351
var key = getattr(elt,'__getitem__')(0)
352
var value = getattr(elt,'__getitem__')(1)
381
for (var k in self.$numeric_dict) _count++
382
for (var k in self.$string_dict) _count++
383
for (var k in self.$object_dict) _count+= self.$object_dict[k].length
391
if(cls===undefined){throw _b_.TypeError.$factory('int.__new__(): not enough arguments')}
394
$numeric_dict : {},
395
$object_dict : {},
396
$string_dict : {},
397
$str_hash: {}
398
}
399
}
400
422
if((!self.$jsobj && itm[1]===self) ||
423
(self.$jsobj && itm[1]===self.$jsobj)){
424
res[pos++]=repr(itm[0])+': {...}'
426
try{
427
res[pos++]=repr(itm[0])+': '+repr(itm[1])
428
}catch(err){
429
res[pos++]=repr(itm[0])+': <unprintable object>'
430
}
431
}
438
var $ = $B.args('__setitem__', 3, {self:null, key:null, value:null},
439
['self', 'key', 'value'], arguments, {}, null, null),
440
self=$.self, key=$.key, value=$.value
441
444
self.$jsobj[key] = $B.pyobj2jsobj(value)
445
if(key=="__init__" || key=="__new__"){
446
// If class attribute __init__ or __new__ are reset,
447
// the factory function has to change
448
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
449
}
450
}else{
451
self.$jsobj[key]=$B.pyobj2jsobj(value)
452
}
453
return $N
471
if(self.$numeric_dict[_key]!==undefined && _eq(_key)){
472
self.$numeric_dict[_key] = value
474
}
475
var sk = self.$str_hash[_key]
476
if(sk!==undefined && _eq(sk)){
477
self.$string_dict[sk] = value
481
var obj_ref = self.$object_dict[_key]
482
if(obj_ref!==undefined){
483
// An object with the same hash is already stored
484
// Lookup should fail if equality raises an exception
485
_eq(self.$object_dict[_key][0])
486
}
487
self.$object_dict[_key] = [key, value]
498
var $ = $B.args('clear',1,{self:null},['self'],arguments,{},null,null),
499
self = $.self
506
if(self.$jsobj){
507
for(var attr in self.$jsobj){
508
if(attr.charAt(0) !== '$' && attr !== "__class__"){
509
delete self.$jsobj[attr]
510
}
511
}
512
}
518
var $ = $B.args('copy',1,{self:null},['self'],arguments,{},null,null),
519
self = $.self,
527
var $ = $B.args('fromkeys', 3, {cls:null, keys:null, value:null},
528
['cls', 'keys', 'value'], arguments, {value:_b_.None}, null, null),
554
var $ = $B.args('get', 3, {self:null, key:null, _default:null},
555
['self', 'key', '_default'], arguments, {_default:$N}, null, null)
558
catch(err){
559
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
560
else{throw err}
561
}
562
}
563
564
var $dict_itemsDict = $B.$iterator_class('dict_items')
565
567
if (arguments.length > 1) {
568
var _len=arguments.length - 1
569
var _msg="items() takes no arguments ("+_len+" given)"
578
if (arguments.length > 1) {
579
var _len=arguments.length - 1
580
var _msg="keys() takes no arguments ("+_len+" given)"
587
588
var $ = $B.args('pop', 3, {self:null, key: null, _default:null},
589
['self', 'key', '_default'], arguments, {_default:$N}, null, null),
590
self=$.self, key=$.key, _default=$._default
591
595
return res
596
}catch(err){
597
if(err.__name__==='KeyError'){
598
if(_default!==undefined) return _default
599
throw err
600
}
601
throw err
602
}
603
}
604
634
635
var $ = $B.args('update',1,{'self':null},['self'],arguments,{},'args','kw'),
636
self=$.self, args=$.args, kw=$.kw
641
$copy_dict(self, o)
642
} else if (hasattr(o, '__getitem__') && hasattr(o, 'keys')) {
645
var i=_keys.length
646
while(i--) {
647
//for (var i=0; i < _keys.length; i++) {
648
var _value = getattr(o, '__getitem__')(_keys[i])
649
si(self, _keys[i], _value)
660
if (arguments.length > 1) {
661
var _len=arguments.length - 1
662
var _msg="values() takes no arguments ("+_len+" given)"
664
}
665
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
666
}
667
679
if(second===undefined){
680
if(Array.isArray(args)){
681
// Form "dict([[key1, value1], [key2,value2], ...])"
682
var i = -1, stop = args.length-1
684
while(i++<stop){
685
var item=args[i]
686
switch(typeof item[0]) {
687
case 'string':
688
res.$string_dict[item[0]]=item[1]
689
res.$str_hash[str_hash(item[0])]=item[0]
690
break;
691
case 'number':
692
res.$numeric_dict[item[0]]=item[1]
693
break
694
default:
695
si(res, item[0], item[1])
696
break
697
}
698
}
699
return res
700
}else if(args.$nat=='kw'){
701
// Form dict(k1=v1, k2=v2...)
702
var kw = args['kw']
703
for(var attr in kw){
704
switch(typeof attr) {
705
case 'string':
706
res.$string_dict[attr]=kw[attr]
707
res.$str_hash[str_hash(attr)]=attr
708
break;
709
case 'number':
710
res.$numeric_dict[attr]=kw[attr]
711
break
712
default:
713
si(res, attr, kw[attr])
714
break
715
}
718
}else if(args.$jsobj){
719
res.$jsobj = {}
720
for(var attr in args.$jsobj){res.$jsobj[attr] = args.$jsobj[attr]}
721
return res
726
var _args = [res], pos=1
727
for(var i=0, _len_i = arguments.length; i < _len_i;i++){_args[pos++]=arguments[i]}
736
// following are used for faster access elsewhere
737
$B.$dict_iterator = function(d) { return new $item_generator(d) }
738
$B.$dict_length = dict.__len__
739
$B.$dict_getitem = dict.__getitem__
740
$B.$dict_get = dict.get
741
$B.$dict_set = dict.__setitem__
742
$B.$dict_contains = dict.__contains__
743
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
744
$B.$copy_dict = $copy_dict // copy from right to left
749
var mappingproxy = $B.make_class("mappingproxy",
750
function(obj){
751
var res = obj_dict(obj)
752
res.__class__ = mappingproxy
753
return res
754
}
755
)
758
throw _b_.TypeError.$factory("'mappingproxy' object does not support item assignment")
767
if(x[attr].$jsobj===x){
768
d.$string_dict[attr] = d
769
}else{
770
d.$string_dict[attr] = x[attr]
771
}