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 10, 2018
Feb 3, 2018
Newer
100644
797 lines (681 sloc)
22.3 KB
28
var $key_iterator = function(d) {
29
this.d = d
30
this.current = 0
31
this.iter = new $item_generator(d)
32
}
33
$key_iterator.prototype.length = function(){return this.iter.items.length}
34
$key_iterator.prototype.next = function(){return this.iter.next()[0]}
35
36
var $value_iterator = function(d) {
37
this.d = d
38
this.current = 0
39
this.iter = new $item_generator(d)
40
}
41
$value_iterator.prototype.length = function(){return this.iter.items.length}
42
$value_iterator.prototype.next = function(){return this.iter.next()[1]}
53
if(val === undefined){val = _b_.NotImplemented}
54
else if(val === null){val = $N}
55
this.items.push([attr, val])
82
}
83
84
var $item_iterator = function(d) {
85
this.d = d
86
this.current = 0
87
this.iter = new $item_generator(d)
88
}
90
$item_iterator.prototype.next = function() { return _b_.tuple.$factory(this.iter.next()) }
99
function toSet(items){
100
// Build a set from the iteration on items
101
var res = []
102
while(true){
103
try{res.push(items.next())}
104
catch(err){break}
105
}
121
__repr__:function(){
122
var s = []
123
for(var i=0, len=items.length(); i<len; i++){
124
s.push(_b_.repr(items.next()))
125
}
126
return klass.__name__+'(['+ s.join(',') + '])'
127
},
140
var $ = $B.args('__contains__', 2, {self:null, item:null},
141
['self', 'item'], arguments, {}, null, null),
142
self=$.self, item=$.item
146
switch(typeof item) {
147
case 'string':
148
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]
277
if (self.$numeric_dict[_key]!==undefined && _eq(_key)){
278
return self.$numeric_dict[_key]
282
var obj_ref = self.$object_dict[_key]
283
if(obj_ref!==undefined){
284
// An object with the same hash is already stored
285
// Lookup should fail if equality raises an exception
286
_eq(self.$object_dict[_key][0])
301
var args = [], pos=0
302
for(var i=1;i<arguments.length;i++){args[pos++]=arguments[i]}
319
if(obj.__class__===$B.JSObject.$dict){
320
// convert a JSObject into a Python dictionary
322
// Attribute $jsobj is used to update the original JS object
323
// when the dictionary is modified
349
while(1){
350
try{
351
var elt = next(iterable)
352
var key = getattr(elt,'__getitem__')(0)
353
var value = getattr(elt,'__getitem__')(1)
382
for (var k in self.$numeric_dict) _count++
383
for (var k in self.$string_dict) _count++
384
for (var k in self.$object_dict) _count+= self.$object_dict[k].length
394
if(cls===undefined){throw _b_.TypeError.$factory('int.__new__(): not enough arguments')}
395
return {
396
__class__:cls.$factory ? cls : cls.$dict,
397
$numeric_dict : {},
398
$object_dict : {},
399
$string_dict : {},
400
$str_hash: {}
401
}
402
}
403
425
if((!self.$jsobj && itm[1]===self) ||
426
(self.$jsobj && itm[1]===self.$jsobj)){
427
res[pos++]=repr(itm[0])+': {...}'
429
try{
430
res[pos++]=repr(itm[0])+': '+repr(itm[1])
431
}catch(err){
432
res[pos++]=repr(itm[0])+': <unprintable object>'
433
}
434
}
441
var $ = $B.args('__setitem__', 3, {self:null, key:null, value:null},
442
['self', 'key', 'value'], arguments, {}, null, null),
443
self=$.self, key=$.key, value=$.value
444
446
if(self.$jsobj.__class__===$B.$type){
447
self.$jsobj[key] = $B.pyobj2jsobj(value)
448
if(key=="__init__" || key=="__new__"){
449
// If class attribute __init__ or __new__ are reset,
450
// the factory function has to change
451
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
452
}
453
}else{
454
self.$jsobj[key]=$B.pyobj2jsobj(value)
455
}
456
return $N
475
if(self.$numeric_dict[_key]!==undefined && _eq(_key)){
476
self.$numeric_dict[_key] = value
478
}
479
var sk = self.$str_hash[_key]
480
if(sk!==undefined && _eq(sk)){
481
self.$string_dict[sk] = value
485
var obj_ref = self.$object_dict[_key]
486
if(obj_ref!==undefined){
487
// An object with the same hash is already stored
488
// Lookup should fail if equality raises an exception
489
_eq(self.$object_dict[_key][0])
490
}
491
self.$object_dict[_key] = [key, value]
502
var $ = $B.args('clear',1,{self:null},['self'],arguments,{},null,null),
503
self = $.self
510
if(self.$jsobj){
511
for(var attr in self.$jsobj){
512
if(attr.charAt(0) !== '$' && attr !== "__class__"){
513
delete self.$jsobj[attr]
514
}
515
}
516
}
522
var $ = $B.args('copy',1,{self:null},['self'],arguments,{},null,null),
523
self = $.self,
531
var $ = $B.args('fromkeys', 3, {cls:null, keys:null, value:null},
532
['cls', 'keys', 'value'], arguments, {value:_b_.None}, null, null),
558
var $ = $B.args('get', 3, {self:null, key:null, _default:null},
559
['self', 'key', '_default'], arguments, {_default:$N}, null, null)
562
catch(err){
563
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
564
else{throw err}
565
}
566
}
567
568
var $dict_itemsDict = $B.$iterator_class('dict_items')
569
571
if (arguments.length > 1) {
572
var _len=arguments.length - 1
573
var _msg="items() takes no arguments ("+_len+" given)"
582
if (arguments.length > 1) {
583
var _len=arguments.length - 1
584
var _msg="keys() takes no arguments ("+_len+" given)"
591
592
var $ = $B.args('pop', 3, {self:null, key: null, _default:null},
593
['self', 'key', '_default'], arguments, {_default:$N}, null, null),
594
self=$.self, key=$.key, _default=$._default
595
599
return res
600
}catch(err){
601
if(err.__name__==='KeyError'){
602
if(_default!==undefined) return _default
603
throw err
604
}
605
throw err
606
}
607
}
608
638
639
var $ = $B.args('update',1,{'self':null},['self'],arguments,{},'args','kw'),
640
self=$.self, args=$.args, kw=$.kw
645
$copy_dict(self, o)
646
} else if (hasattr(o, '__getitem__') && hasattr(o, 'keys')) {
649
var i=_keys.length
650
while(i--) {
651
//for (var i=0; i < _keys.length; i++) {
652
var _value = getattr(o, '__getitem__')(_keys[i])
653
si(self, _keys[i], _value)
664
if (arguments.length > 1) {
665
var _len=arguments.length - 1
666
var _msg="values() takes no arguments ("+_len+" given)"
668
}
669
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
670
}
671
683
if(second===undefined){
684
if(Array.isArray(args)){
685
// Form "dict([[key1, value1], [key2,value2], ...])"
686
var i = -1, stop = args.length-1
688
while(i++<stop){
689
var item=args[i]
690
switch(typeof item[0]) {
691
case 'string':
692
res.$string_dict[item[0]]=item[1]
693
res.$str_hash[str_hash(item[0])]=item[0]
694
break;
695
case 'number':
696
res.$numeric_dict[item[0]]=item[1]
697
break
698
default:
699
si(res, item[0], item[1])
700
break
701
}
702
}
703
return res
704
}else if(args.$nat=='kw'){
705
// Form dict(k1=v1, k2=v2...)
706
var kw = args['kw']
707
for(var attr in kw){
708
switch(typeof attr) {
709
case 'string':
710
res.$string_dict[attr]=kw[attr]
711
res.$str_hash[str_hash(attr)]=attr
712
break;
713
case 'number':
714
res.$numeric_dict[attr]=kw[attr]
715
break
716
default:
717
si(res, attr, kw[attr])
718
break
719
}
722
}else if(args.$jsobj){
723
res.$jsobj = {}
724
for(var attr in args.$jsobj){res.$jsobj[attr] = args.$jsobj[attr]}
725
return res
730
var _args = [res], pos=1
731
for(var i=0, _len_i = arguments.length; i < _len_i;i++){_args[pos++]=arguments[i]}
740
// following are used for faster access elsewhere
741
$B.$dict_iterator = function(d) { return new $item_generator(d) }
742
$B.$dict_length = dict.__len__
743
$B.$dict_getitem = dict.__getitem__
744
$B.$dict_get = dict.get
745
$B.$dict_set = dict.__setitem__
746
$B.$dict_contains = dict.__contains__
747
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
748
$B.$copy_dict = $copy_dict // copy from right to left
752
// Class for attribute __dict__ of classes
753
var mappingproxyDict = {
754
__class__ : $B.$type,
755
__name__ : "mappingproxy"
756
}
760
throw _b_.TypeError.$factory("'mappingproxy' object does not support item assignment")
764
function mappingproxy(obj){
765
var res = obj_dict(obj)
766
res.__class__ = mappingproxyDict
767
return res
768
}
769
mappingproxy.__class__ = $B.$factory
770
mappingproxy.$dict = mappingproxyDict
771
mappingproxyDict.$factory = mappingproxy
772
$B.mappingproxy = mappingproxy
773
778
if(x[attr].$jsobj===x){
779
d.$string_dict[attr] = d
780
}else{
781
d.$string_dict[attr] = x[attr]
782
}