Permalink
Feb 26, 2018
Feb 3, 2018
Feb 3, 2018
Jan 15, 2018
Dec 10, 2018
Dec 10, 2018
Feb 26, 2018
Feb 26, 2018
Dec 13, 2018
May 29, 2017
Feb 26, 2018
Dec 11, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Mar 7, 2018
Feb 26, 2018
Dec 10, 2018
Dec 10, 2018
Dec 11, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Dec 11, 2018
Dec 10, 2018
Jul 31, 2018
Jul 31, 2018
Dec 10, 2018
Nov 2, 2018
Jul 31, 2018
Dec 10, 2018
Dec 10, 2018
Nov 28, 2018
Feb 26, 2018
May 27, 2018
Oct 24, 2018
Feb 3, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Mar 7, 2018
Feb 26, 2018
Mar 7, 2018
Nov 9, 2015
Feb 26, 2018
Feb 26, 2018
Dec 13, 2018
Mar 7, 2018
Mar 7, 2018
Feb 26, 2018
Dec 13, 2018
Aug 2, 2018
Dec 13, 2018
Feb 26, 2018
Nov 21, 2015
Dec 1, 2018
Jun 28, 2018
Feb 11, 2018
Mar 7, 2018
Aug 2, 2018
Feb 26, 2018
Newer
100644
897 lines (793 sloc)
25.5 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]])
67
68
for(var k in d.$string_dict){items.push([k, d.$string_dict[k]])}
69
70
for(var k in d.$object_dict){items.push(d.$object_dict[k])}
71
72
this.items = items
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(),
99
right.$version = right.$version || 0
100
var right_version = right.$version || 0
101
while(i--){
102
si(left, _l[i][0], _l[i][1])
103
if(right.$version != right_version){
104
throw _b_.RuntimeError.$factory("dict mutated during update")
105
}
106
}
109
function toSet(items){
110
// Build a set from the iteration on items
111
var res = []
112
while(true){
113
try{res.push(items.next())}
114
catch(err){break}
115
}
127
__iter__: function(){items.iter.i = 0; return res},
128
__len__: function(){return items.length()},
129
__next__: function(){
142
klass.__reduce_ex__ = klass.__reduce__ = function(self){
143
return _b_.tuple.$factory([_b_.iter,
144
_b_.tuple.$factory([_b_.list.$factory(self)])])
145
}
157
var $ = $B.args("__contains__", 2, {self: null, item: null},
158
["self", "item"], arguments, {}, null, null),
160
item = $.item
161
162
if(self.$jsobj){return self.$jsobj[item] !== undefined}
163
164
switch(typeof item) {
165
case "string":
166
return self.$string_dict[item] !== undefined
167
case "number":
168
return self.$numeric_dict[item] !== undefined
169
}
170
171
var _key = hash(item)
172
if(self.$str_hash[_key] !== undefined &&
173
$B.rich_comp("__eq__", item, self.$str_hash[_key])){return true}
174
if(self.$numeric_dict[_key] !== undefined &&
175
$B.rich_comp("__eq__", item, _key)){return true}
176
if(self.$object_dict[_key] !== undefined){
177
// If the key is an object, its hash must be in the dict keys but the
178
// key itself must compare equal to the key associated with the hash
179
// For instance :
180
//
181
// class X:
182
// def __hash__(self): return hash('u')
183
//
184
// a = {'u': 'a', X(): 'b'}
185
// assert set(a.values()) == {'a', 'b'}
186
// assert not X() in a
187
return $B.rich_comp("__eq__", item, self.$object_dict[_key][0])
194
var $ = $B.args("__eq__", 2, {self: null, arg: null},
195
["self", "arg"], arguments, {}, null, null),
200
if(self.$jsobj[arg] === undefined){throw KeyError.$factory(arg)}
201
delete self.$jsobj[arg]
204
switch(typeof arg){
205
case "string":
206
if(self.$string_dict[arg] === undefined){
207
throw KeyError.$factory(_b_.str.$factory(arg))
208
}
209
delete self.$string_dict[arg]
210
delete self.$str_hash[str_hash(arg)]
213
case "number":
214
if(self.$numeric_dict[arg] === undefined){
215
throw KeyError.$factory(_b_.str.$factory(arg))
221
// go with defaults
222
223
var _key = hash(arg)
224
225
if(self.$object_dict[_key] !== undefined){
226
delete self.$object_dict[_key]
227
}
228
229
if(self.$jsobj){delete self.$jsobj[arg]}
235
var $ = $B.args("__eq__", 2, {self: null, other: null},
236
["self", "other"], arguments, {}, null, null),
242
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
243
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
247
if((self.$numeric_dict.length != other.$numeric_dict.length) ||
248
(self.$string_dict.length != other.$string_dict.length) ||
249
(self.$object_dict.length != other.$object_dict.length)){
250
return false
251
}
252
for(var k in self.$numeric_dict){
253
if(!$B.rich_comp("__eq__", other.$numeric_dict[k],
254
self.$numeric_dict[k])){
255
return false
257
}
258
for(var k in self.$string_dict){
259
if(!$B.rich_comp("__eq__", other.$string_dict[k],
260
self.$string_dict[k])){
261
return false
264
for(var hash in self.$object_dict){
265
self_obj = self.$object_dict[hash][0]
266
self_value = self.$object_dict[hash][1]
267
if(other.$object_dict[hash] !== undefined){
268
if(!$B.rich_comp("__eq__", other.$object_dict[hash][1],
269
self_value)){
270
return false
271
}
272
}else{
273
// Is hash of object a key of numeric dict ?
274
var num_value = other.$numeric_dict[hash]
275
if(num_value !== undefined){
276
if($B.rich_comp("__eq__", self_obj, hash) &&
277
! $B.rich_comp("__eq__", num_value, self_value)){
278
return false
279
}
280
}else{
281
return false
282
}
291
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
292
["self", "arg"], arguments, {}, null, null),
296
if(!self.$jsobj.hasOwnProperty(arg)){
297
throw _b_.KeyError.$factory(str.$factory(arg))
298
}else if(self.$jsobj[arg] === undefined){
303
304
switch(typeof arg){
305
case "string":
306
if(self.$string_dict[arg] !== undefined){
307
return self.$string_dict[arg]
309
break
310
case "number":
311
if(self.$numeric_dict[arg] !== undefined){
312
return self.$numeric_dict[arg]
314
break
315
}
316
317
// since the key is more complex use 'default' method of getting item
318
319
var _key = _b_.hash(arg),
320
_eq = function(other){return $B.rich_comp("__eq__", arg, other)}
321
322
var sk = self.$str_hash[_key]
323
if(sk !== undefined && _eq(sk)){
324
return self.$string_dict[sk]
325
}
326
if(self.$numeric_dict[_key] !== undefined && _eq(_key)){
327
return self.$numeric_dict[_key]
328
}
329
if(isinstance(arg, _b_.str)){
330
// string subclass
331
var res = self.$string_dict[arg.valueOf()]
332
if(res !== undefined){return res}
333
}
334
335
var obj_ref = self.$object_dict[_key]
336
if(obj_ref !== undefined){
337
// An object with the same hash is already stored
338
// Lookup should fail if equality raises an exception
339
_eq(self.$object_dict[_key][0])
340
return self.$object_dict[_key][1]
344
var missing_method = getattr(self.__class__, "__missing__", _b_.None)
345
}catch(err){
346
console.log(err)
347
348
}
349
if(missing_method !== _b_.None){
364
switch(typeof item[0]) {
365
case 'string':
366
self.$string_dict[item[0]] = item[1]
367
self.$str_hash[str_hash(item[0])] = item[0]
368
break
369
case 'number':
370
self.$numeric_dict[item[0]] = item[1]
371
break
372
default:
373
si(self, item[0], item[1])
374
break
375
}
381
if(first === undefined){return $N}
382
if(second === undefined){
383
if(first.__class__ === $B.JSObject){
384
self.$jsobj = first.js
385
return $N
386
}else if(first.$jsobj){
387
self.$jsobj = {}
388
for(var attr in first.$jsobj){
389
self.$jsobj[attr] = first.$jsobj[attr]
399
arguments, {}, "first", "second")
400
var args = $.first
401
if(args.length > 1){
402
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
403
", got 2")
404
}else if(args.length == 1){
405
args = args[0]
406
if(args.__class__ === dict){
407
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
408
forEach(function(d){
409
for(key in args[d]){self[d][key] = args[d][key]}
410
})
414
var keys = $B.$getattr(args, "keys", null)
415
if(keys !== null){
416
var gi = $B.$getattr(args, "__getitem__", null)
417
if(gi !== null){
418
// has keys and __getitem__ : it's a mapping, iterate on
419
// keys and values
420
gi = $B.$call(gi)
421
var kiter = _b_.iter($B.$call(keys)())
422
while(true){
423
try{
424
var key = _b_.next(kiter),
425
value = gi(key)
426
dict.__setitem__(self, key, value)
427
}catch(err){
428
if(err.__class__ === _b_.StopIteration){
429
break
430
}
431
throw err
432
}
433
}
434
return $N
435
}
436
}
437
if(! Array.isArray(args)){
438
args = _b_.list.$factory(args)
439
}
440
// Form "dict([[key1, value1], [key2,value2], ...])"
446
switch(typeof attr){
447
case "string":
448
self.$string_dict[attr] = kw[attr]
449
self.$str_hash[str_hash(attr)] = attr
450
break
451
case "number":
452
self.$numeric_dict[attr] = kw[attr]
453
break
454
default:
455
si(self, attr, kw[attr])
456
break
457
}
475
for(var k in self.$numeric_dict){_count++}
476
for(var k in self.$string_dict){_count++}
477
for(var k in self.$object_dict){_count++}
478
479
return _count
496
if(cls !== dict){
497
instance.__dict__ = _b_.dict.$factory()
498
}
499
return instance
519
items.forEach(function(item){
520
if((!self.$jsobj && item[1] === self) ||
521
(self.$jsobj && item[1] === self.$jsobj)){
532
["self", "key", "value"], arguments, {}, null, null)
533
return dict.$setitem($.self, $.key, $.value)
534
}
538
if(self.$from_js){
539
// dictionary created by method to_dict of JSObject instances
540
value = $B.pyobj2jsobj(value)
541
}
545
// If class attribute __init__ or __new__ are reset,
546
// the factory function has to change
547
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
548
}
549
}else{
567
// if we got here the key is more complex, use default method
568
569
var _key = hash(key),
570
_eq = function(other){return $B.rich_comp("__eq__", key, other)}
571
572
if(self.$numeric_dict[_key] !== undefined && _eq(_key)){
573
self.$numeric_dict[_key] = value
575
return $N
576
}
577
var sk = self.$str_hash[_key]
578
if(sk !== undefined && _eq(sk)){
579
self.$string_dict[sk] = value
581
return $N
582
}
583
584
var obj_ref = self.$object_dict[_key]
585
if(obj_ref !== undefined){
586
// An object with the same hash is already stored
587
// Lookup should fail if equality raises an exception
588
_eq(self.$object_dict[_key][0])
634
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
635
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
647
if(klass === dict){dict.__setitem__(res, key, value)}
648
else{_b_.getattr(res, "__setitem__")(key, value)}
659
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
660
["self", "key", "_default"], arguments, {_default: $N}, null, null)
663
catch(err){
664
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
665
else{throw err}
666
}
667
}
668
673
var _len = arguments.length - 1,
674
_msg = "items() takes no arguments (" + _len + " given)"
684
var _len = arguments.length - 1,
685
_msg = "keys() takes no arguments (" + _len + " given)"
693
var missing = {},
694
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
695
["self", "key", "_default"], arguments, {_default: missing}, null, null),
727
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
728
["self", "key", "_default"], arguments, {_default: $N}, null, null),
743
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
744
{}, "args", "kw"),
745
self = $.self,
746
args = $.args,
747
kw = $.kw
748
if(args.length > 0){
749
var o = args[0]
750
if(isinstance(o, dict)){
753
}else if(hasattr(o, "keys")){
754
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))()),
755
i = _keys.length
756
while(i--){
757
var _value = getattr(o, "__getitem__")(_keys[i])
758
dict.$setitem(self, _keys[i], _value)
759
}
760
}else{
761
var it = _b_.iter(o),
762
i = 0
763
while(true){
764
try{
765
var item = _b_.next(it)
766
}catch(err){
767
if(err.__class__ === _b_.StopIteration){break}
768
throw err
769
}
770
try{
771
key_value = _b_.list.$factory(item)
772
}catch(err){
773
throw _b_.TypeError.$factory("cannot convert dictionary" +
774
" update sequence element #" + i + " to a sequence")
775
}
776
if(key_value.length !== 2){
777
throw _b_.ValueError.$factory("dictionary update " +
778
"sequence element #" + i + " has length " +
779
key_value.length + "; 2 is required")
780
}
781
dict.$setitem(self, key_value[0], key_value[1])
782
i++
795
var _len = arguments.length - 1,
796
_msg = "values() takes no arguments (" + _len + " given)"
798
}
799
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
800
}
801
804
var args = [res]
805
for(var i = 0, len = arguments.length; i < len ; i++){
806
args.push(arguments[i])
807
}
808
dict.__init__.apply(null, args)
816
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
817
// have the attribute $infos
818
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
819
822
$B.$dict_length = dict.__len__
823
$B.$dict_getitem = dict.__getitem__
824
$B.$dict_get = dict.get
825
$B.$dict_set = dict.__setitem__
826
$B.$dict_contains = dict.__contains__
827
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
828
$B.$copy_dict = $copy_dict // copy from right to left
847
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
848
"item assignment")
851
for(var attr in dict){
852
if(mappingproxy[attr] !== undefined ||
853
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
854
"clear", "fromkeys", "pop", "popitem", "setdefault",
855
"update"].indexOf(attr) > -1){
856
continue
857
}
858
if(typeof dict[attr] == "function"){
859
mappingproxy[attr] = (function(key){
860
return function(){
861
return dict[key].apply(null, arguments)
862
}
863
})(attr)
864
}else{
865
mappingproxy[attr] = dict[attr]
866
}
867
}
868
888
if(klass !== undefined && klass.$native){
889
throw _b_.AttributeError.$factory(klass.__name__ +