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
796 lines (681 sloc)
22.3 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
161
// If the key is an object, its hash must be in the dict keys but the
162
// key itself must compare equal to the key associated with the hash
163
// For instance :
164
//
165
// class X:
166
// def __hash__(self): return hash('u')
168
// a = {'u': 'a', X(): 'b'}
169
// assert set(a.values())=={'a', 'b'}
170
// assert not X() in a
177
178
var $ = $B.args('__eq__', 2, {self:null, arg:null},
179
['self', 'arg'], arguments, {}, null, null),
180
self=$.self, arg=$.arg
181
189
if (self.$string_dict[arg] === undefined) throw KeyError.$factory(_b_.str.$factory(arg))
194
if (self.$numeric_dict[arg] === undefined) throw KeyError.$factory(_b_.str.$factory(arg))
211
var $ = $B.args('__eq__', 2, {self:null, other:null},
212
['self', 'other'], arguments, {}, null, null),
213
self=$.self, other=$.other
214
217
if(self.$jsobj){self=jsobj2dict(self.$jsobj)}
218
if(other.$jsobj){other=jsobj2dict(other.$jsobj)}
222
if((self.$numeric_dict.length!=other.$numeric_dict.length) ||
223
(self.$string_dict.length!=other.$string_dict.length) ||
224
(self.$object_dict.length!=other.$object_dict.length)){
225
return false
248
var $ = $B.args('__getitem__', 2, {self:null, arg:null},
249
['self', 'arg'], arguments, {}, null, null),
250
self=$.self, arg=$.arg
253
if(!self.$jsobj.hasOwnProperty(arg)) throw _b_.KeyError.$factory(str.$factory(arg))
254
else if(self.$jsobj[arg]===undefined) return _b_.NotImplemented
255
else if(self.$jsobj[arg]===null){return $N}
260
switch(typeof arg) {
261
case 'string':
262
if (self.$string_dict[arg] !== undefined) return self.$string_dict[arg]
263
break
264
case 'number':
265
if (self.$numeric_dict[arg] !== undefined) return self.$numeric_dict[arg]
278
if (self.$numeric_dict[_key]!==undefined && _eq(_key)){
279
return self.$numeric_dict[_key]
283
var obj_ref = self.$object_dict[_key]
284
if(obj_ref!==undefined){
285
// An object with the same hash is already stored
286
// Lookup should fail if equality raises an exception
287
_eq(self.$object_dict[_key][0])
302
var args = [], pos=0
303
for(var i=1;i<arguments.length;i++){args[pos++]=arguments[i]}
320
if(obj.__class__===$B.JSObject.$dict){
321
// convert a JSObject into a Python dictionary
323
// Attribute $jsobj is used to update the original JS object
324
// when the dictionary is modified
350
while(1){
351
try{
352
var elt = next(iterable)
353
var key = getattr(elt,'__getitem__')(0)
354
var value = getattr(elt,'__getitem__')(1)
383
for (var k in self.$numeric_dict) _count++
384
for (var k in self.$string_dict) _count++
385
for (var k in self.$object_dict) _count+= self.$object_dict[k].length
393
if(cls===undefined){throw _b_.TypeError.$factory('int.__new__(): not enough arguments')}
394
return {
395
__class__:cls.$factory ? cls : cls.$dict,
396
$numeric_dict : {},
397
$object_dict : {},
398
$string_dict : {},
399
$str_hash: {}
400
}
401
}
402
424
if((!self.$jsobj && itm[1]===self) ||
425
(self.$jsobj && itm[1]===self.$jsobj)){
426
res[pos++]=repr(itm[0])+': {...}'
428
try{
429
res[pos++]=repr(itm[0])+': '+repr(itm[1])
430
}catch(err){
431
res[pos++]=repr(itm[0])+': <unprintable object>'
432
}
433
}
440
var $ = $B.args('__setitem__', 3, {self:null, key:null, value:null},
441
['self', 'key', 'value'], arguments, {}, null, null),
442
self=$.self, key=$.key, value=$.value
443
446
self.$jsobj[key] = $B.pyobj2jsobj(value)
447
if(key=="__init__" || key=="__new__"){
448
// If class attribute __init__ or __new__ are reset,
449
// the factory function has to change
450
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
451
}
452
}else{
453
self.$jsobj[key]=$B.pyobj2jsobj(value)
454
}
455
return $N
474
if(self.$numeric_dict[_key]!==undefined && _eq(_key)){
475
self.$numeric_dict[_key] = value
477
}
478
var sk = self.$str_hash[_key]
479
if(sk!==undefined && _eq(sk)){
480
self.$string_dict[sk] = value
484
var obj_ref = self.$object_dict[_key]
485
if(obj_ref!==undefined){
486
// An object with the same hash is already stored
487
// Lookup should fail if equality raises an exception
488
_eq(self.$object_dict[_key][0])
489
}
490
self.$object_dict[_key] = [key, value]
501
var $ = $B.args('clear',1,{self:null},['self'],arguments,{},null,null),
502
self = $.self
509
if(self.$jsobj){
510
for(var attr in self.$jsobj){
511
if(attr.charAt(0) !== '$' && attr !== "__class__"){
512
delete self.$jsobj[attr]
513
}
514
}
515
}
521
var $ = $B.args('copy',1,{self:null},['self'],arguments,{},null,null),
522
self = $.self,
530
var $ = $B.args('fromkeys', 3, {cls:null, keys:null, value:null},
531
['cls', 'keys', 'value'], arguments, {value:_b_.None}, null, null),
557
var $ = $B.args('get', 3, {self:null, key:null, _default:null},
558
['self', 'key', '_default'], arguments, {_default:$N}, null, null)
561
catch(err){
562
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
563
else{throw err}
564
}
565
}
566
567
var $dict_itemsDict = $B.$iterator_class('dict_items')
568
570
if (arguments.length > 1) {
571
var _len=arguments.length - 1
572
var _msg="items() takes no arguments ("+_len+" given)"
581
if (arguments.length > 1) {
582
var _len=arguments.length - 1
583
var _msg="keys() takes no arguments ("+_len+" given)"
590
591
var $ = $B.args('pop', 3, {self:null, key: null, _default:null},
592
['self', 'key', '_default'], arguments, {_default:$N}, null, null),
593
self=$.self, key=$.key, _default=$._default
594
598
return res
599
}catch(err){
600
if(err.__name__==='KeyError'){
601
if(_default!==undefined) return _default
602
throw err
603
}
604
throw err
605
}
606
}
607
637
638
var $ = $B.args('update',1,{'self':null},['self'],arguments,{},'args','kw'),
639
self=$.self, args=$.args, kw=$.kw
644
$copy_dict(self, o)
645
} else if (hasattr(o, '__getitem__') && hasattr(o, 'keys')) {
648
var i=_keys.length
649
while(i--) {
650
//for (var i=0; i < _keys.length; i++) {
651
var _value = getattr(o, '__getitem__')(_keys[i])
652
si(self, _keys[i], _value)
663
if (arguments.length > 1) {
664
var _len=arguments.length - 1
665
var _msg="values() takes no arguments ("+_len+" given)"
667
}
668
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
669
}
670
682
if(second===undefined){
683
if(Array.isArray(args)){
684
// Form "dict([[key1, value1], [key2,value2], ...])"
685
var i = -1, stop = args.length-1
687
while(i++<stop){
688
var item=args[i]
689
switch(typeof item[0]) {
690
case 'string':
691
res.$string_dict[item[0]]=item[1]
692
res.$str_hash[str_hash(item[0])]=item[0]
693
break;
694
case 'number':
695
res.$numeric_dict[item[0]]=item[1]
696
break
697
default:
698
si(res, item[0], item[1])
699
break
700
}
701
}
702
return res
703
}else if(args.$nat=='kw'){
704
// Form dict(k1=v1, k2=v2...)
705
var kw = args['kw']
706
for(var attr in kw){
707
switch(typeof attr) {
708
case 'string':
709
res.$string_dict[attr]=kw[attr]
710
res.$str_hash[str_hash(attr)]=attr
711
break;
712
case 'number':
713
res.$numeric_dict[attr]=kw[attr]
714
break
715
default:
716
si(res, attr, kw[attr])
717
break
718
}
721
}else if(args.$jsobj){
722
res.$jsobj = {}
723
for(var attr in args.$jsobj){res.$jsobj[attr] = args.$jsobj[attr]}
724
return res
729
var _args = [res], pos=1
730
for(var i=0, _len_i = arguments.length; i < _len_i;i++){_args[pos++]=arguments[i]}
739
// following are used for faster access elsewhere
740
$B.$dict_iterator = function(d) { return new $item_generator(d) }
741
$B.$dict_length = dict.__len__
742
$B.$dict_getitem = dict.__getitem__
743
$B.$dict_get = dict.get
744
$B.$dict_set = dict.__setitem__
745
$B.$dict_contains = dict.__contains__
746
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
747
$B.$copy_dict = $copy_dict // copy from right to left
759
throw _b_.TypeError.$factory("'mappingproxy' object does not support item assignment")
763
function mappingproxy(obj){
764
var res = obj_dict(obj)
765
res.__class__ = mappingproxyDict
766
return res
767
}
768
mappingproxy.__class__ = $B.$factory
769
mappingproxy.$dict = mappingproxyDict
770
mappingproxyDict.$factory = mappingproxy
771
$B.mappingproxy = mappingproxy
772
777
if(x[attr].$jsobj===x){
778
d.$string_dict[attr] = d
779
}else{
780
d.$string_dict[attr] = x[attr]
781
}