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 17, 2018
Dec 17, 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
906 lines (802 sloc)
25.8 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)}
244
if(dict.__len__(self) != dict.__len__(other)){return false}
251
if(other.$numeric_dict.hasOwnProperty(k)){
252
if(!$B.rich_comp("__eq__", other.$numeric_dict[k],
253
self.$numeric_dict[k])){
254
return false
255
}
256
}else if(other.$object_dict.hasOwnProperty(k)){
257
if(!$B.rich_comp("__eq__", k, other.$object_dict[k][0]) ||
258
!$B.rich_comp("__eq__", self.$numeric_dict[k],
259
other.$object_dict[k][1])){
260
return false
261
}
262
}else{
267
if(!other.$string_dict.hasOwnProperty(k) ||
268
!$B.rich_comp("__eq__", other.$string_dict[k],
273
for(var hash in self.$object_dict){
274
self_obj = self.$object_dict[hash][0]
275
self_value = self.$object_dict[hash][1]
276
if(other.$object_dict[hash] !== undefined){
277
if(!$B.rich_comp("__eq__", other.$object_dict[hash][1],
278
self_value)){
279
return false
280
}
281
}else{
282
// Is hash of object a key of numeric dict ?
283
var num_value = other.$numeric_dict[hash]
284
if(num_value !== undefined){
285
if($B.rich_comp("__eq__", self_obj, hash) &&
286
! $B.rich_comp("__eq__", num_value, self_value)){
287
return false
288
}
289
}else{
290
return false
291
}
300
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
301
["self", "arg"], arguments, {}, null, null),
305
if(!self.$jsobj.hasOwnProperty(arg)){
306
throw _b_.KeyError.$factory(str.$factory(arg))
307
}else if(self.$jsobj[arg] === undefined){
312
313
switch(typeof arg){
314
case "string":
315
if(self.$string_dict[arg] !== undefined){
316
return self.$string_dict[arg]
318
break
319
case "number":
320
if(self.$numeric_dict[arg] !== undefined){
321
return self.$numeric_dict[arg]
323
break
324
}
325
326
// since the key is more complex use 'default' method of getting item
327
328
var _key = _b_.hash(arg),
329
_eq = function(other){return $B.rich_comp("__eq__", arg, other)}
330
331
var sk = self.$str_hash[_key]
332
if(sk !== undefined && _eq(sk)){
333
return self.$string_dict[sk]
334
}
335
if(self.$numeric_dict[_key] !== undefined && _eq(_key)){
336
return self.$numeric_dict[_key]
337
}
338
if(isinstance(arg, _b_.str)){
339
// string subclass
340
var res = self.$string_dict[arg.valueOf()]
341
if(res !== undefined){return res}
342
}
343
344
var obj_ref = self.$object_dict[_key]
345
if(obj_ref !== undefined){
346
// An object with the same hash is already stored
347
// Lookup should fail if equality raises an exception
348
_eq(self.$object_dict[_key][0])
349
return self.$object_dict[_key][1]
353
var missing_method = getattr(self.__class__, "__missing__", _b_.None)
354
}catch(err){
355
console.log(err)
356
357
}
358
if(missing_method !== _b_.None){
373
switch(typeof item[0]) {
374
case 'string':
375
self.$string_dict[item[0]] = item[1]
376
self.$str_hash[str_hash(item[0])] = item[0]
377
break
378
case 'number':
379
self.$numeric_dict[item[0]] = item[1]
380
break
381
default:
382
si(self, item[0], item[1])
383
break
384
}
390
if(first === undefined){return $N}
391
if(second === undefined){
392
if(first.__class__ === $B.JSObject){
393
self.$jsobj = first.js
394
return $N
395
}else if(first.$jsobj){
396
self.$jsobj = {}
397
for(var attr in first.$jsobj){
398
self.$jsobj[attr] = first.$jsobj[attr]
408
arguments, {}, "first", "second")
409
var args = $.first
410
if(args.length > 1){
411
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
412
", got 2")
413
}else if(args.length == 1){
414
args = args[0]
415
if(args.__class__ === dict){
416
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
417
forEach(function(d){
418
for(key in args[d]){self[d][key] = args[d][key]}
419
})
423
var keys = $B.$getattr(args, "keys", null)
424
if(keys !== null){
425
var gi = $B.$getattr(args, "__getitem__", null)
426
if(gi !== null){
427
// has keys and __getitem__ : it's a mapping, iterate on
428
// keys and values
429
gi = $B.$call(gi)
430
var kiter = _b_.iter($B.$call(keys)())
431
while(true){
432
try{
433
var key = _b_.next(kiter),
434
value = gi(key)
435
dict.__setitem__(self, key, value)
436
}catch(err){
437
if(err.__class__ === _b_.StopIteration){
438
break
439
}
440
throw err
441
}
442
}
443
return $N
444
}
445
}
446
if(! Array.isArray(args)){
447
args = _b_.list.$factory(args)
448
}
449
// Form "dict([[key1, value1], [key2,value2], ...])"
455
switch(typeof attr){
456
case "string":
457
self.$string_dict[attr] = kw[attr]
458
self.$str_hash[str_hash(attr)] = attr
459
break
460
case "number":
461
self.$numeric_dict[attr] = kw[attr]
462
break
463
default:
464
si(self, attr, kw[attr])
465
break
466
}
484
for(var k in self.$numeric_dict){_count++}
485
for(var k in self.$string_dict){_count++}
486
for(var k in self.$object_dict){_count++}
487
488
return _count
505
if(cls !== dict){
506
instance.__dict__ = _b_.dict.$factory()
507
}
508
return instance
528
items.forEach(function(item){
529
if((!self.$jsobj && item[1] === self) ||
530
(self.$jsobj && item[1] === self.$jsobj)){
541
["self", "key", "value"], arguments, {}, null, null)
542
return dict.$setitem($.self, $.key, $.value)
543
}
547
if(self.$from_js){
548
// dictionary created by method to_dict of JSObject instances
549
value = $B.pyobj2jsobj(value)
550
}
554
// If class attribute __init__ or __new__ are reset,
555
// the factory function has to change
556
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
557
}
558
}else{
576
// if we got here the key is more complex, use default method
577
578
var _key = hash(key),
579
_eq = function(other){return $B.rich_comp("__eq__", key, other)}
580
581
if(self.$numeric_dict[_key] !== undefined && _eq(_key)){
582
self.$numeric_dict[_key] = value
584
return $N
585
}
586
var sk = self.$str_hash[_key]
587
if(sk !== undefined && _eq(sk)){
588
self.$string_dict[sk] = value
590
return $N
591
}
592
593
var obj_ref = self.$object_dict[_key]
594
if(obj_ref !== undefined){
595
// An object with the same hash is already stored
596
// Lookup should fail if equality raises an exception
597
_eq(self.$object_dict[_key][0])
643
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
644
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
656
if(klass === dict){dict.__setitem__(res, key, value)}
657
else{_b_.getattr(res, "__setitem__")(key, value)}
668
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
669
["self", "key", "_default"], arguments, {_default: $N}, null, null)
672
catch(err){
673
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
674
else{throw err}
675
}
676
}
677
682
var _len = arguments.length - 1,
683
_msg = "items() takes no arguments (" + _len + " given)"
693
var _len = arguments.length - 1,
694
_msg = "keys() takes no arguments (" + _len + " given)"
702
var missing = {},
703
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
704
["self", "key", "_default"], arguments, {_default: missing}, null, null),
736
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
737
["self", "key", "_default"], arguments, {_default: $N}, null, null),
752
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
753
{}, "args", "kw"),
754
self = $.self,
755
args = $.args,
756
kw = $.kw
757
if(args.length > 0){
758
var o = args[0]
759
if(isinstance(o, dict)){
762
}else if(hasattr(o, "keys")){
763
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))()),
764
i = _keys.length
765
while(i--){
766
var _value = getattr(o, "__getitem__")(_keys[i])
767
dict.$setitem(self, _keys[i], _value)
768
}
769
}else{
770
var it = _b_.iter(o),
771
i = 0
772
while(true){
773
try{
774
var item = _b_.next(it)
775
}catch(err){
776
if(err.__class__ === _b_.StopIteration){break}
777
throw err
778
}
779
try{
780
key_value = _b_.list.$factory(item)
781
}catch(err){
782
throw _b_.TypeError.$factory("cannot convert dictionary" +
783
" update sequence element #" + i + " to a sequence")
784
}
785
if(key_value.length !== 2){
786
throw _b_.ValueError.$factory("dictionary update " +
787
"sequence element #" + i + " has length " +
788
key_value.length + "; 2 is required")
789
}
790
dict.$setitem(self, key_value[0], key_value[1])
791
i++
804
var _len = arguments.length - 1,
805
_msg = "values() takes no arguments (" + _len + " given)"
807
}
808
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
809
}
810
813
var args = [res]
814
for(var i = 0, len = arguments.length; i < len ; i++){
815
args.push(arguments[i])
816
}
817
dict.__init__.apply(null, args)
825
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
826
// have the attribute $infos
827
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
828
831
$B.$dict_length = dict.__len__
832
$B.$dict_getitem = dict.__getitem__
833
$B.$dict_get = dict.get
834
$B.$dict_set = dict.__setitem__
835
$B.$dict_contains = dict.__contains__
836
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
837
$B.$copy_dict = $copy_dict // copy from right to left
856
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
857
"item assignment")
860
for(var attr in dict){
861
if(mappingproxy[attr] !== undefined ||
862
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
863
"clear", "fromkeys", "pop", "popitem", "setdefault",
864
"update"].indexOf(attr) > -1){
865
continue
866
}
867
if(typeof dict[attr] == "function"){
868
mappingproxy[attr] = (function(key){
869
return function(){
870
return dict[key].apply(null, arguments)
871
}
872
})(attr)
873
}else{
874
mappingproxy[attr] = dict[attr]
875
}
876
}
877
897
if(klass !== undefined && klass.$native){
898
throw _b_.AttributeError.$factory(klass.__name__ +