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
Jul 31, 2018
Jul 31, 2018
Mar 23, 2018
Feb 9, 2018
Feb 26, 2018
May 27, 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
Mar 7, 2018
Feb 26, 2018
Nov 21, 2015
Jun 28, 2018
Feb 11, 2018
Feb 12, 2018
Mar 7, 2018
Feb 26, 2018
Newer
100644
765 lines (659 sloc)
21.1 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])
320
if(first === undefined){return $N}
321
if(second === undefined){
322
if(first.__class__ === $B.JSObject){
323
self.$jsobj = first.js
324
return $N
325
}else if(first.$jsobj){
326
self.$jsobj = {}
327
for(var attr in first.$jsobj){
328
self.$jsobj[attr] = first.$jsobj[attr]
334
var $ = $B.args("dict", 1, {self:null}, ["self"],
335
arguments, {}, "first", "second")
336
var args = $.first
337
if(args.length > 1){
338
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
339
", got 2")
340
}else if(args.length == 1){
341
args = args[0]
342
if(args.__class__ === dict){
343
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
344
forEach(function(d){
345
for(key in args[d]){self[d][key] = args[d][key]}
346
})
347
}else if(isinstance(args, dict)){
348
$copy_dict(self, args)
350
if(! Array.isArray(args)){
351
args = _b_.list.$factory(args)
352
}
353
// Form "dict([[key1, value1], [key2,value2], ...])"
354
var i = -1,
355
stop = args.length - 1,
356
si = dict.__setitem__
357
while(i++ < stop){
358
var item = args[i]
359
switch(typeof item[0]) {
360
case 'string':
361
self.$string_dict[item[0]] = item[1]
362
self.$str_hash[str_hash(item[0])] = item[0]
363
break
364
case 'number':
365
self.$numeric_dict[item[0]] = item[1]
366
break
367
default:
368
si(self, item[0], item[1])
369
break
374
var kw = $.second.$string_dict
375
for(var attr in kw){
376
switch(typeof attr){
377
case "string":
378
self.$string_dict[attr] = kw[attr]
379
self.$str_hash[str_hash(attr)] = attr
380
break
381
case "number":
382
self.$numeric_dict[attr] = kw[attr]
383
break
384
default:
385
si(self, attr, kw[attr])
386
break
387
}
388
}
405
for(var k in self.$numeric_dict){_count++}
406
for(var k in self.$string_dict){_count++}
407
for(var k in self.$object_dict){_count += self.$object_dict[k].length}
420
$numeric_dict : {},
421
$object_dict : {},
422
$string_dict : {},
423
$str_hash: {}
424
}
425
}
426
444
items.forEach(function(item){
445
if((!self.$jsobj && item[1] === self) ||
446
(self.$jsobj && item[1] === self.$jsobj)){
462
["self", "key", "value"], arguments, {}, null, null)
463
return dict.$setitem($.self, $.key, $.value)
464
}
471
// If class attribute __init__ or __new__ are reset,
472
// the factory function has to change
473
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
474
}
475
}else{
483
self.$string_dict[key] = value
484
self.$str_hash[str_hash(key)] = key
485
return $N
493
var _key = hash(key),
494
_eq = function(other){return $B.rich_comp("__eq__", key, other)}
508
// An object with the same hash is already stored
509
// Lookup should fail if equality raises an exception
510
_eq(self.$object_dict[_key][0])
511
}
512
self.$object_dict[_key] = [key, value]
527
self.$numeric_dict = {}
528
self.$string_dict = {}
529
self.$str_hash = {}
530
self.$object_dict = {}
554
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
555
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
567
if(klass === dict){dict.__setitem__(res, key, value)}
568
else{_b_.getattr(res, "__setitem__")(key, value)}
579
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
580
["self", "key", "_default"], arguments, {_default: $N}, null, null)
583
catch(err){
584
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
585
else{throw err}
586
}
587
}
588
593
var _len = arguments.length - 1,
594
_msg = "items() takes no arguments (" + _len + " given)"
604
var _len = arguments.length - 1,
605
_msg = "keys() takes no arguments (" + _len + " given)"
613
var $ = $B.args("pop", 3, {self: null, key: null, _default: null},
614
["self", "key", "_default"], arguments, {_default: $N}, null, null),
646
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
647
["self", "key", "_default"], arguments, {_default: $N}, null, null),
654
if(_default === undefined){_default = $N}
655
dict.__setitem__(self, key, _default)
662
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
663
{}, "args", "kw"),
664
self = $.self,
665
args = $.args,
666
kw = $.kw
667
668
if(args.length > 0){
669
var o = args[0]
670
if(isinstance(o, dict)){
671
if(o.$jsobj){o = jsobj2dict(o)}
672
$copy_dict(self, o)
673
}else if(hasattr(o, "__getitem__") && hasattr(o, "keys")){
674
var _keys = _b_.list.$factory(getattr(o, "keys")()),
691
var _len = arguments.length - 1,
692
_msg = "values() takes no arguments (" + _len + " given)"
694
}
695
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
696
}
697
708
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
709
// have the attribute $infos
710
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
711
714
$B.$dict_length = dict.__len__
715
$B.$dict_getitem = dict.__getitem__
716
$B.$dict_get = dict.get
717
$B.$dict_set = dict.__setitem__
718
$B.$dict_contains = dict.__contains__
719
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
720
$B.$copy_dict = $copy_dict // copy from right to left
725
var mappingproxy = $B.make_class("mappingproxy",
726
function(obj){
727
var res = obj_dict(obj)
728
res.__class__ = mappingproxy
729
return res
730
}
731
)
734
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
735
"item assignment")
757
if(klass !== undefined && klass.$native){
758
throw _b_.AttributeError.$factory(klass.__name__ +