Permalink
Feb 26, 2018
Feb 3, 2018
Feb 3, 2018
Jan 15, 2018
Feb 26, 2018
Feb 26, 2018
Feb 26, 2018
May 29, 2017
Feb 26, 2018
Mar 7, 2018
Mar 7, 2018
Nov 21, 2015
Mar 7, 2018
Feb 26, 2018
Feb 26, 2018
Mar 7, 2018
Feb 26, 2018
Feb 26, 2018
Feb 26, 2018
Mar 7, 2018
Feb 26, 2018
Feb 26, 2018
Feb 26, 2018
May 28, 2016
Jul 31, 2018
Jul 31, 2018
Nov 2, 2018
Jul 31, 2018
Jul 31, 2018
Mar 23, 2018
Feb 9, 2018
Feb 26, 2018
May 27, 2018
Oct 24, 2018
Feb 3, 2018
Feb 26, 2018
Feb 26, 2018
May 28, 2016
Feb 26, 2018
Mar 7, 2018
Feb 26, 2018
Mar 7, 2018
Nov 9, 2015
Feb 26, 2018
Feb 26, 2018
Mar 7, 2018
Mar 7, 2018
Feb 26, 2018
Mar 7, 2018
Feb 26, 2018
Aug 2, 2018
Feb 26, 2018
Nov 21, 2015
Jun 28, 2018
Feb 11, 2018
Aug 2, 2018
Mar 7, 2018
Aug 2, 2018
Feb 26, 2018
Newer
100644
813 lines (709 sloc)
23 KB
16
var setitem = dict.__setitem__,
17
i = $keys.length
18
while(i--){setitem($keys[i], $values[i])}
30
var $key_iterator = function(d) {
31
this.d = d
32
this.current = 0
33
this.iter = new $item_generator(d)
34
}
35
$key_iterator.prototype.length = function(){return this.iter.items.length}
36
$key_iterator.prototype.next = function(){return this.iter.next()[0]}
37
38
var $value_iterator = function(d) {
39
this.d = d
40
this.current = 0
41
this.iter = new $item_generator(d)
42
}
43
$value_iterator.prototype.length = function(){return this.iter.items.length}
44
$value_iterator.prototype.next = function(){return this.iter.next()[1]}
55
if(val === undefined){val = _b_.NotImplemented}
56
else if(val === null){val = $N}
57
this.items.push([attr, val])
63
var items = []
64
for(var k in d.$numeric_dict){
65
items.push([parseFloat(k), d.$numeric_dict[k]])
83
}
84
85
var $item_iterator = function(d) {
86
this.d = d
87
this.current = 0
88
this.iter = new $item_generator(d)
89
}
90
$item_iterator.prototype.length = function(){return this.iter.items.length}
91
$item_iterator.prototype.next = function(){
92
return _b_.tuple.$factory(this.iter.next())
93
}
95
var $copy_dict = function(left, right){
96
var _l = new $item_generator(right).as_list(),
97
si = dict.__setitem__,
98
i = _l.length
99
while(i--){si(left, _l[i][0], _l[i][1])}
102
function toSet(items){
103
// Build a set from the iteration on items
104
var res = []
105
while(true){
106
try{res.push(items.next())}
107
catch(err){break}
108
}
119
__iter__: function(){items.iter.i = 0; return res},
120
__len__: function(){return items.length()},
121
__next__: function(){
144
var $ = $B.args("__contains__", 2, {self: null, item: null},
145
["self", "item"], arguments, {}, null, null),
152
case "string":
153
return self.$string_dict[item] !== undefined
154
case "number":
155
return self.$numeric_dict[item] !== undefined
164
// If the key is an object, its hash must be in the dict keys but the
165
// key itself must compare equal to the key associated with the hash
166
// For instance :
167
//
168
// class X:
169
// def __hash__(self): return hash('u')
181
var $ = $B.args("__eq__", 2, {self: null, arg: null},
182
["self", "arg"], arguments, {}, null, null),
193
if(self.$string_dict[arg] === undefined){
194
throw KeyError.$factory(_b_.str.$factory(arg))
195
}
196
delete self.$string_dict[arg]
197
delete self.$str_hash[str_hash(arg)]
198
return $N
200
if(self.$numeric_dict[arg] === undefined){
201
throw KeyError.$factory(_b_.str.$factory(arg))
202
}
203
delete self.$numeric_dict[arg]
204
return $N
219
var $ = $B.args("__eq__", 2, {self: null, other: null},
220
["self", "other"], arguments, {}, null, null),
226
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
227
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
231
if((self.$numeric_dict.length != other.$numeric_dict.length) ||
232
(self.$string_dict.length != other.$string_dict.length) ||
233
(self.$object_dict.length != other.$object_dict.length)){
234
return false
249
if(!$B.rich_comp("__eq__", other.$object_dict[k][1],
250
self.$object_dict[k][1])){
260
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
261
["self", "arg"], arguments, {}, null, null),
265
if(!self.$jsobj.hasOwnProperty(arg)){
266
throw _b_.KeyError.$factory(str.$factory(arg))
267
}else if(self.$jsobj[arg] === undefined){
268
return _b_.NotImplemented
269
}else if(self.$jsobj[arg] === null){return $N}
276
if(self.$string_dict[arg] !== undefined){
277
return self.$string_dict[arg]
278
}
279
break
281
if(self.$numeric_dict[arg] !== undefined){
282
return self.$numeric_dict[arg]
283
}
302
// An object with the same hash is already stored
303
// Lookup should fail if equality raises an exception
304
_eq(self.$object_dict[_key][0])
318
dict.__init__ = function(self, first, second){
319
if(first === undefined){return $N}
320
if(second === undefined){
321
if(first.__class__ === $B.JSObject){
322
self.$jsobj = first.js
323
return $N
324
}else if(first.$jsobj){
325
self.$jsobj = {}
326
for(var attr in first.$jsobj){
327
self.$jsobj[attr] = first.$jsobj[attr]
333
var $ = $B.args("dict", 1, {self:null}, ["self"],
334
arguments, {}, "first", "second")
335
var args = $.first
336
if(args.length > 1){
337
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
338
", got 2")
339
}else if(args.length == 1){
340
args = args[0]
341
if(args.__class__ === dict){
342
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
343
forEach(function(d){
344
for(key in args[d]){self[d][key] = args[d][key]}
345
})
346
}else if(isinstance(args, dict)){
347
$copy_dict(self, args)
349
var keys = $B.$getattr(args, "keys", null)
350
if(keys !== null){
351
var gi = $B.$getattr(args, "__getitem__", null)
352
if(gi !== null){
353
// has keys and __getitem__ : it's a mapping, iterate on
354
// keys and values
355
gi = $B.$call(gi)
356
var kiter = _b_.iter($B.$call(keys)())
357
while(true){
358
try{
359
var key = _b_.next(kiter),
360
value = gi(key)
361
dict.__setitem__(self, key, value)
362
}catch(err){
363
if(err.__class__ === _b_.StopIteration){
364
break
365
}
366
throw err
367
}
368
}
369
return $N
370
}
371
}
372
if(! Array.isArray(args)){
373
args = _b_.list.$factory(args)
374
}
375
// Form "dict([[key1, value1], [key2,value2], ...])"
376
var i = -1,
377
stop = args.length - 1,
378
si = dict.__setitem__
379
while(i++ < stop){
380
var item = args[i]
381
switch(typeof item[0]) {
382
case 'string':
383
self.$string_dict[item[0]] = item[1]
384
self.$str_hash[str_hash(item[0])] = item[0]
385
break
386
case 'number':
387
self.$numeric_dict[item[0]] = item[1]
388
break
389
default:
390
si(self, item[0], item[1])
391
break
396
var kw = $.second.$string_dict
397
for(var attr in kw){
398
switch(typeof attr){
399
case "string":
400
self.$string_dict[attr] = kw[attr]
401
self.$str_hash[str_hash(attr)] = attr
402
break
403
case "number":
404
self.$numeric_dict[attr] = kw[attr]
405
break
406
default:
407
si(self, attr, kw[attr])
408
break
409
}
410
}
427
for(var k in self.$numeric_dict){_count++}
428
for(var k in self.$string_dict){_count++}
442
$numeric_dict : {},
443
$object_dict : {},
444
$string_dict : {},
445
$str_hash: {}
446
}
447
}
448
466
items.forEach(function(item){
467
if((!self.$jsobj && item[1] === self) ||
468
(self.$jsobj && item[1] === self.$jsobj)){
483
["self", "key", "value"], arguments, {}, null, null)
484
return dict.$setitem($.self, $.key, $.value)
485
}
489
if(self.$from_js){
490
// dictionary created by method to_dict of JSObject instances
491
value = $B.pyobj2jsobj(value)
492
}
496
// If class attribute __init__ or __new__ are reset,
497
// the factory function has to change
498
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
499
}
500
}else{
508
self.$string_dict[key] = value
509
self.$str_hash[str_hash(key)] = key
510
return $N
518
var _key = hash(key),
519
_eq = function(other){return $B.rich_comp("__eq__", key, other)}
533
// An object with the same hash is already stored
534
// Lookup should fail if equality raises an exception
535
_eq(self.$object_dict[_key][0])
536
}
537
self.$object_dict[_key] = [key, value]
552
self.$numeric_dict = {}
553
self.$string_dict = {}
554
self.$str_hash = {}
555
self.$object_dict = {}
579
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
580
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
592
if(klass === dict){dict.__setitem__(res, key, value)}
593
else{_b_.getattr(res, "__setitem__")(key, value)}
604
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
605
["self", "key", "_default"], arguments, {_default: $N}, null, null)
608
catch(err){
609
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
610
else{throw err}
611
}
612
}
613
618
var _len = arguments.length - 1,
619
_msg = "items() takes no arguments (" + _len + " given)"
629
var _len = arguments.length - 1,
630
_msg = "keys() takes no arguments (" + _len + " given)"
638
var $ = $B.args("pop", 3, {self: null, key: null, _default: null},
639
["self", "key", "_default"], arguments, {_default: $N}, null, null),
671
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
672
["self", "key", "_default"], arguments, {_default: $N}, null, null),
679
if(_default === undefined){_default = $N}
680
dict.__setitem__(self, key, _default)
687
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
688
{}, "args", "kw"),
689
self = $.self,
690
args = $.args,
691
kw = $.kw
692
if(args.length > 0){
693
var o = args[0]
694
if(isinstance(o, dict)){
698
var _keys = _b_.list.$factory(getattr(o, "keys")()),
699
si = dict.__setitem__,
700
i = _keys.length
701
while(i--){
702
var _value = getattr(o, "__getitem__")(_keys[i])
703
si(self, _keys[i], _value)
704
}
715
var _len = arguments.length - 1,
716
_msg = "values() takes no arguments (" + _len + " given)"
718
}
719
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
720
}
721
732
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
733
// have the attribute $infos
734
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
735
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
751
if(_b_.isinstance(obj, dict)){
752
// Should be a dictionary
753
var res = $B.obj_dict(obj.$string_dict)
754
}else{
755
var res = $B.obj_dict(obj)
756
}
763
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
764
"item assignment")
767
for(var attr in dict){
768
if(mappingproxy[attr] !== undefined ||
769
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
770
"clear", "fromkeys", "pop", "popitem", "setdefault",
771
"update"].indexOf(attr) > -1){
772
continue
773
}
774
if(typeof dict[attr] == "function"){
775
mappingproxy[attr] = (function(key){
776
return function(){
777
return dict[key].apply(null, arguments)
778
}
779
})(attr)
780
}else{
781
mappingproxy[attr] = dict[attr]
782
}
783
}
784
804
if(klass !== undefined && klass.$native){
805
throw _b_.AttributeError.$factory(klass.__name__ +