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
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
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
Dec 1, 2018
Jun 28, 2018
Feb 11, 2018
Mar 7, 2018
Aug 2, 2018
Feb 26, 2018
Newer
100644
857 lines (753 sloc)
24.2 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(),
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(){
133
klass.__reduce_ex__ = klass.__reduce__ = function(self){
134
return _b_.tuple.$factory([_b_.iter,
135
_b_.tuple.$factory([_b_.list.$factory(self)])])
136
}
148
var $ = $B.args("__contains__", 2, {self: null, item: null},
149
["self", "item"], arguments, {}, null, null),
151
item = $.item
152
153
if(self.$jsobj){return self.$jsobj[item] !== undefined}
154
155
switch(typeof item) {
156
case "string":
157
return self.$string_dict[item] !== undefined
158
case "number":
159
return self.$numeric_dict[item] !== undefined
160
}
161
162
var _key = hash(item)
163
if(self.$str_hash[_key] !== undefined &&
164
$B.rich_comp("__eq__", item, self.$str_hash[_key])){return true}
165
if(self.$numeric_dict[_key] !== undefined &&
166
$B.rich_comp("__eq__", item, _key)){return true}
167
if(self.$object_dict[_key] !== undefined){
168
// If the key is an object, its hash must be in the dict keys but the
169
// key itself must compare equal to the key associated with the hash
170
// For instance :
171
//
172
// class X:
173
// def __hash__(self): return hash('u')
174
//
175
// a = {'u': 'a', X(): 'b'}
176
// assert set(a.values()) == {'a', 'b'}
177
// assert not X() in a
178
return $B.rich_comp("__eq__", item, self.$object_dict[_key][0])
185
var $ = $B.args("__eq__", 2, {self: null, arg: null},
186
["self", "arg"], arguments, {}, null, null),
191
if(self.$jsobj[arg] === undefined){throw KeyError.$factory(arg)}
192
delete self.$jsobj[arg]
195
switch(typeof arg){
196
case "string":
197
if(self.$string_dict[arg] === undefined){
198
throw KeyError.$factory(_b_.str.$factory(arg))
199
}
200
delete self.$string_dict[arg]
201
delete self.$str_hash[str_hash(arg)]
203
case "number":
204
if(self.$numeric_dict[arg] === undefined){
205
throw KeyError.$factory(_b_.str.$factory(arg))
210
// go with defaults
211
212
var _key = hash(arg)
213
214
if(self.$object_dict[_key] !== undefined){
215
delete self.$object_dict[_key]
216
}
217
218
if(self.$jsobj){delete self.$jsobj[arg]}
219
return $N
223
var $ = $B.args("__eq__", 2, {self: null, other: null},
224
["self", "other"], arguments, {}, null, null),
230
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
231
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
235
if((self.$numeric_dict.length != other.$numeric_dict.length) ||
236
(self.$string_dict.length != other.$string_dict.length) ||
237
(self.$object_dict.length != other.$object_dict.length)){
238
return false
239
}
240
for(var k in self.$numeric_dict){
241
if(!$B.rich_comp("__eq__", other.$numeric_dict[k],
242
self.$numeric_dict[k])){
243
return false
245
}
246
for(var k in self.$string_dict){
247
if(!$B.rich_comp("__eq__", other.$string_dict[k],
248
self.$string_dict[k])){
249
return false
252
for(var hash in self.$object_dict){
253
self_obj = self.$object_dict[hash][0]
254
self_value = self.$object_dict[hash][1]
255
if(other.$object_dict[hash] !== undefined){
256
if(!$B.rich_comp("__eq__", other.$object_dict[hash][1],
257
self_value)){
258
return false
259
}
260
}else{
261
// Is hash of object a key of numeric dict ?
262
var num_value = other.$numeric_dict[hash]
263
if(num_value !== undefined){
264
if($B.rich_comp("__eq__", self_obj, hash) &&
265
! $B.rich_comp("__eq__", num_value, self_value)){
266
return false
267
}
268
}else{
269
return false
270
}
279
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
280
["self", "arg"], arguments, {}, null, null),
284
if(!self.$jsobj.hasOwnProperty(arg)){
285
throw _b_.KeyError.$factory(str.$factory(arg))
286
}else if(self.$jsobj[arg] === undefined){
291
292
switch(typeof arg){
293
case "string":
294
if(self.$string_dict[arg] !== undefined){
295
return self.$string_dict[arg]
297
break
298
case "number":
299
if(self.$numeric_dict[arg] !== undefined){
300
return self.$numeric_dict[arg]
302
break
303
}
304
305
// since the key is more complex use 'default' method of getting item
306
307
var _key = _b_.hash(arg),
308
_eq = function(other){return $B.rich_comp("__eq__", arg, other)}
309
310
var sk = self.$str_hash[_key]
311
if(sk !== undefined && _eq(sk)){
312
return self.$string_dict[sk]
313
}
314
if(self.$numeric_dict[_key] !== undefined && _eq(_key)){
315
return self.$numeric_dict[_key]
316
}
317
if(isinstance(arg, _b_.str)){
318
// string subclass
319
var res = self.$string_dict[arg.valueOf()]
320
if(res !== undefined){return res}
321
}
322
323
var obj_ref = self.$object_dict[_key]
324
if(obj_ref !== undefined){
325
// An object with the same hash is already stored
326
// Lookup should fail if equality raises an exception
327
_eq(self.$object_dict[_key][0])
328
return self.$object_dict[_key][1]
332
var missing_method = getattr(self.__class__, "__missing__", _b_.None)
333
}catch(err){
334
console.log(err)
335
336
}
337
if(missing_method !== _b_.None){
352
switch(typeof item[0]) {
353
case 'string':
354
self.$string_dict[item[0]] = item[1]
355
self.$str_hash[str_hash(item[0])] = item[0]
356
break
357
case 'number':
358
self.$numeric_dict[item[0]] = item[1]
359
break
360
default:
361
si(self, item[0], item[1])
362
break
363
}
369
if(first === undefined){return $N}
370
if(second === undefined){
371
if(first.__class__ === $B.JSObject){
372
self.$jsobj = first.js
373
return $N
374
}else if(first.$jsobj){
375
self.$jsobj = {}
376
for(var attr in first.$jsobj){
377
self.$jsobj[attr] = first.$jsobj[attr]
387
arguments, {}, "first", "second")
388
var args = $.first
389
if(args.length > 1){
390
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
391
", got 2")
392
}else if(args.length == 1){
393
args = args[0]
394
if(args.__class__ === dict){
395
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
396
forEach(function(d){
397
for(key in args[d]){self[d][key] = args[d][key]}
398
})
402
var keys = $B.$getattr(args, "keys", null)
403
if(keys !== null){
404
var gi = $B.$getattr(args, "__getitem__", null)
405
if(gi !== null){
406
// has keys and __getitem__ : it's a mapping, iterate on
407
// keys and values
408
gi = $B.$call(gi)
409
var kiter = _b_.iter($B.$call(keys)())
410
while(true){
411
try{
412
var key = _b_.next(kiter),
413
value = gi(key)
414
dict.__setitem__(self, key, value)
415
}catch(err){
416
if(err.__class__ === _b_.StopIteration){
417
break
418
}
419
throw err
420
}
421
}
422
return $N
423
}
424
}
425
if(! Array.isArray(args)){
426
args = _b_.list.$factory(args)
427
}
428
// Form "dict([[key1, value1], [key2,value2], ...])"
434
switch(typeof attr){
435
case "string":
436
self.$string_dict[attr] = kw[attr]
437
self.$str_hash[str_hash(attr)] = attr
438
break
439
case "number":
440
self.$numeric_dict[attr] = kw[attr]
441
break
442
default:
443
si(self, attr, kw[attr])
444
break
445
}
463
for(var k in self.$numeric_dict){_count++}
464
for(var k in self.$string_dict){_count++}
465
for(var k in self.$object_dict){_count++}
466
467
return _count
483
if(cls !== dict){
484
instance.__dict__ = _b_.dict.$factory()
485
}
486
return instance
506
items.forEach(function(item){
507
if((!self.$jsobj && item[1] === self) ||
508
(self.$jsobj && item[1] === self.$jsobj)){
523
["self", "key", "value"], arguments, {}, null, null)
524
return dict.$setitem($.self, $.key, $.value)
525
}
529
if(self.$from_js){
530
// dictionary created by method to_dict of JSObject instances
531
value = $B.pyobj2jsobj(value)
532
}
536
// If class attribute __init__ or __new__ are reset,
537
// the factory function has to change
538
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
539
}
540
}else{
549
self.$str_hash[str_hash(key)] = key
550
return $N
551
case "number":
552
self.$numeric_dict[key] = value
556
// if we got here the key is more complex, use default method
557
558
var _key = hash(key),
559
_eq = function(other){return $B.rich_comp("__eq__", key, other)}
560
561
if(self.$numeric_dict[_key] !== undefined && _eq(_key)){
562
self.$numeric_dict[_key] = value
563
return $N
564
}
565
var sk = self.$str_hash[_key]
566
if(sk !== undefined && _eq(sk)){
567
self.$string_dict[sk] = value
568
return $N
569
}
570
571
var obj_ref = self.$object_dict[_key]
572
if(obj_ref !== undefined){
573
// An object with the same hash is already stored
574
// Lookup should fail if equality raises an exception
575
_eq(self.$object_dict[_key][0])
619
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
620
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
632
if(klass === dict){dict.__setitem__(res, key, value)}
633
else{_b_.getattr(res, "__setitem__")(key, value)}
644
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
645
["self", "key", "_default"], arguments, {_default: $N}, null, null)
648
catch(err){
649
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
650
else{throw err}
651
}
652
}
653
658
var _len = arguments.length - 1,
659
_msg = "items() takes no arguments (" + _len + " given)"
669
var _len = arguments.length - 1,
670
_msg = "keys() takes no arguments (" + _len + " given)"
678
var $ = $B.args("pop", 3, {self: null, key: null, _default: null},
679
["self", "key", "_default"], arguments, {_default: $N}, null, null),
711
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
712
["self", "key", "_default"], arguments, {_default: $N}, null, null),
719
if(_default === undefined){_default = $N}
720
dict.__setitem__(self, key, _default)
727
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
728
{}, "args", "kw"),
729
self = $.self,
730
args = $.args,
731
kw = $.kw
732
if(args.length > 0){
733
var o = args[0]
734
if(isinstance(o, dict)){
740
i = _keys.length
741
while(i--){
742
var _value = getattr(o, "__getitem__")(_keys[i])
755
var _len = arguments.length - 1,
756
_msg = "values() takes no arguments (" + _len + " given)"
758
}
759
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
760
}
761
764
var args = [res]
765
for(var i = 0, len = arguments.length; i < len ; i++){
766
args.push(arguments[i])
767
}
768
dict.__init__.apply(null, args)
776
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
777
// have the attribute $infos
778
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
779
782
$B.$dict_length = dict.__len__
783
$B.$dict_getitem = dict.__getitem__
784
$B.$dict_get = dict.get
785
$B.$dict_set = dict.__setitem__
786
$B.$dict_contains = dict.__contains__
787
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
788
$B.$copy_dict = $copy_dict // copy from right to left
807
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
808
"item assignment")
811
for(var attr in dict){
812
if(mappingproxy[attr] !== undefined ||
813
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
814
"clear", "fromkeys", "pop", "popitem", "setdefault",
815
"update"].indexOf(attr) > -1){
816
continue
817
}
818
if(typeof dict[attr] == "function"){
819
mappingproxy[attr] = (function(key){
820
return function(){
821
return dict[key].apply(null, arguments)
822
}
823
})(attr)
824
}else{
825
mappingproxy[attr] = dict[attr]
826
}
827
}
828
848
if(klass !== undefined && klass.$native){
849
throw _b_.AttributeError.$factory(klass.__name__ +