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
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
791 lines (686 sloc)
22 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++}
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)){
461
["self", "key", "value"], arguments, {}, null, null)
462
return dict.$setitem($.self, $.key, $.value)
463
}
467
if(self.$from_js){
468
// dictionary created by method to_dict of JSObject instances
469
value = $B.pyobj2jsobj(value)
470
}
474
// If class attribute __init__ or __new__ are reset,
475
// the factory function has to change
476
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
477
}
478
}else{
486
self.$string_dict[key] = value
487
self.$str_hash[str_hash(key)] = key
488
return $N
496
var _key = hash(key),
497
_eq = function(other){return $B.rich_comp("__eq__", key, other)}
511
// An object with the same hash is already stored
512
// Lookup should fail if equality raises an exception
513
_eq(self.$object_dict[_key][0])
514
}
515
self.$object_dict[_key] = [key, value]
530
self.$numeric_dict = {}
531
self.$string_dict = {}
532
self.$str_hash = {}
533
self.$object_dict = {}
557
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
558
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
570
if(klass === dict){dict.__setitem__(res, key, value)}
571
else{_b_.getattr(res, "__setitem__")(key, value)}
582
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
583
["self", "key", "_default"], arguments, {_default: $N}, null, null)
586
catch(err){
587
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
588
else{throw err}
589
}
590
}
591
596
var _len = arguments.length - 1,
597
_msg = "items() takes no arguments (" + _len + " given)"
607
var _len = arguments.length - 1,
608
_msg = "keys() takes no arguments (" + _len + " given)"
616
var $ = $B.args("pop", 3, {self: null, key: null, _default: null},
617
["self", "key", "_default"], arguments, {_default: $N}, null, null),
649
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
650
["self", "key", "_default"], arguments, {_default: $N}, null, null),
657
if(_default === undefined){_default = $N}
658
dict.__setitem__(self, key, _default)
665
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
666
{}, "args", "kw"),
667
self = $.self,
668
args = $.args,
669
kw = $.kw
670
if(args.length > 0){
671
var o = args[0]
672
if(isinstance(o, dict)){
676
var _keys = _b_.list.$factory(getattr(o, "keys")()),
677
si = dict.__setitem__,
678
i = _keys.length
679
while(i--){
680
var _value = getattr(o, "__getitem__")(_keys[i])
681
si(self, _keys[i], _value)
682
}
693
var _len = arguments.length - 1,
694
_msg = "values() takes no arguments (" + _len + " given)"
696
}
697
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
698
}
699
710
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
711
// have the attribute $infos
712
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
713
716
$B.$dict_length = dict.__len__
717
$B.$dict_getitem = dict.__getitem__
718
$B.$dict_get = dict.get
719
$B.$dict_set = dict.__setitem__
720
$B.$dict_contains = dict.__contains__
721
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
722
$B.$copy_dict = $copy_dict // copy from right to left
729
if(_b_.isinstance(obj, dict)){
730
// Should be a dictionary
731
var res = $B.obj_dict(obj.$string_dict)
732
}else{
733
var res = $B.obj_dict(obj)
734
}
741
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
742
"item assignment")
745
for(var attr in dict){
746
if(mappingproxy[attr] !== undefined ||
747
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
748
"clear", "fromkeys", "pop", "popitem", "setdefault",
749
"update"].indexOf(attr) > -1){
750
continue
751
}
752
if(typeof dict[attr] == "function"){
753
mappingproxy[attr] = (function(key){
754
return function(){
755
return dict[key].apply(null, arguments)
756
}
757
})(attr)
758
}else{
759
mappingproxy[attr] = dict[attr]
760
}
761
}
762
782
if(klass !== undefined && klass.$native){
783
throw _b_.AttributeError.$factory(klass.__name__ +