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
Nov 28, 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
817 lines (713 sloc)
23.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])
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
if(cls !== dict){
448
instance.__dict__ = _b_.dict.$factory()
449
}
450
return instance
470
items.forEach(function(item){
471
if((!self.$jsobj && item[1] === self) ||
472
(self.$jsobj && item[1] === self.$jsobj)){
487
["self", "key", "value"], arguments, {}, null, null)
488
return dict.$setitem($.self, $.key, $.value)
489
}
493
if(self.$from_js){
494
// dictionary created by method to_dict of JSObject instances
495
value = $B.pyobj2jsobj(value)
496
}
500
// If class attribute __init__ or __new__ are reset,
501
// the factory function has to change
502
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
503
}
504
}else{
512
self.$string_dict[key] = value
513
self.$str_hash[str_hash(key)] = key
514
return $N
522
var _key = hash(key),
523
_eq = function(other){return $B.rich_comp("__eq__", key, other)}
537
// An object with the same hash is already stored
538
// Lookup should fail if equality raises an exception
539
_eq(self.$object_dict[_key][0])
540
}
541
self.$object_dict[_key] = [key, value]
556
self.$numeric_dict = {}
557
self.$string_dict = {}
558
self.$str_hash = {}
559
self.$object_dict = {}
583
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
584
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
596
if(klass === dict){dict.__setitem__(res, key, value)}
597
else{_b_.getattr(res, "__setitem__")(key, value)}
608
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
609
["self", "key", "_default"], arguments, {_default: $N}, null, null)
612
catch(err){
613
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
614
else{throw err}
615
}
616
}
617
622
var _len = arguments.length - 1,
623
_msg = "items() takes no arguments (" + _len + " given)"
633
var _len = arguments.length - 1,
634
_msg = "keys() takes no arguments (" + _len + " given)"
642
var $ = $B.args("pop", 3, {self: null, key: null, _default: null},
643
["self", "key", "_default"], arguments, {_default: $N}, null, null),
675
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
676
["self", "key", "_default"], arguments, {_default: $N}, null, null),
683
if(_default === undefined){_default = $N}
684
dict.__setitem__(self, key, _default)
691
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
692
{}, "args", "kw"),
693
self = $.self,
694
args = $.args,
695
kw = $.kw
696
if(args.length > 0){
697
var o = args[0]
698
if(isinstance(o, dict)){
702
var _keys = _b_.list.$factory(getattr(o, "keys")()),
703
si = dict.__setitem__,
704
i = _keys.length
705
while(i--){
706
var _value = getattr(o, "__getitem__")(_keys[i])
707
si(self, _keys[i], _value)
708
}
719
var _len = arguments.length - 1,
720
_msg = "values() takes no arguments (" + _len + " given)"
722
}
723
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
724
}
725
736
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
737
// have the attribute $infos
738
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
739
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
755
if(_b_.isinstance(obj, dict)){
756
// Should be a dictionary
757
var res = $B.obj_dict(obj.$string_dict)
758
}else{
759
var res = $B.obj_dict(obj)
760
}
767
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
768
"item assignment")
771
for(var attr in dict){
772
if(mappingproxy[attr] !== undefined ||
773
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
774
"clear", "fromkeys", "pop", "popitem", "setdefault",
775
"update"].indexOf(attr) > -1){
776
continue
777
}
778
if(typeof dict[attr] == "function"){
779
mappingproxy[attr] = (function(key){
780
return function(){
781
return dict[key].apply(null, arguments)
782
}
783
})(attr)
784
}else{
785
mappingproxy[attr] = dict[attr]
786
}
787
}
788
808
if(klass !== undefined && klass.$native){
809
throw _b_.AttributeError.$factory(klass.__name__ +