Permalink
Feb 26, 2018
Feb 3, 2018
Feb 3, 2018
Jan 15, 2018
Dec 10, 2018
Feb 26, 2018
Feb 26, 2018
May 29, 2017
Feb 26, 2018
Dec 10, 2018
Dec 10, 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 10, 2018
Dec 10, 2018
Dec 10, 2018
Dec 10, 2018
Jul 31, 2018
Jul 31, 2018
Dec 10, 2018
Nov 2, 2018
Jul 31, 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
Dec 10, 2018
Mar 7, 2018
Aug 2, 2018
Feb 26, 2018
Newer
100644
876 lines (789 sloc)
25.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
if(d.$str_keys){
64
for(var key in d.$string_dict){
65
this.items.push([key, d.$string_dict[key]])
66
}
67
}else{
68
this.items = d.$items
80
}
81
82
var $item_iterator = function(d) {
83
this.d = d
84
this.current = 0
85
this.iter = new $item_generator(d)
86
}
87
$item_iterator.prototype.length = function(){return this.iter.items.length}
88
$item_iterator.prototype.next = function(){
89
return _b_.tuple.$factory(this.iter.next())
90
}
92
var $copy_dict = function(left, right){
93
var _l = new $item_generator(right).as_list(),
99
function toSet(items){
100
// Build a set from the iteration on items
101
var res = []
102
while(true){
103
try{res.push(items.next())}
104
catch(err){break}
105
}
117
__iter__: function(){items.iter.i = 0; return res},
118
__len__: function(){return items.length()},
119
__next__: function(){
131
klass.__reduce_ex__ = klass.__reduce__ = function(self){
132
return _b_.tuple.$factory([_b_.iter,
133
_b_.tuple.$factory([_b_.list.$factory(self)])])
134
}
138
function object_equals_string(self, hash, key){
139
// Is key hash equal to the hash of one of the string keys ?
140
for(var s in self.$string_dict){
141
if(_b_.hash(s) == hash){
142
// Does the key compare equal to the string ?
143
if($B.rich_comp("__eq__", s, key)){
144
return s
145
}
146
}
147
}
148
return $N
149
}
150
159
var $ = $B.args("__contains__", 2, {self: null, key: null},
160
["self", "key"], arguments, {}, null, null),
162
key = $.key,
163
hash
164
165
if(self.$jsobj){return self.$jsobj[key] !== undefined}
166
167
if(self.$str_keys){
168
if(typeof key == "string"){
169
return self.$string_dict.hasOwnProperty(key)
170
}else{
171
hash = _b_.hash(key)
172
if(object_equals_string(self, hash, key) !== $N){
173
return true
174
}
175
}
176
}
177
var hash = hash === undefined ? _b_.hash(key) : hash,
178
items = self.$hash[hash]
179
if(items !== undefined){
180
for(var i = 0, len = items.length; i < len; i++){
181
var rank = items[i]
182
if(self.$items[rank] === undefined){
183
console.log("self", self, "key", key, "hash", hash,
184
"items", items, "rank", rank)
185
alert("oups")
186
}
187
if($B.rich_comp("__eq__", key, self.$items[rank][0])){
188
return true
189
}
190
}
195
function check_string_keys(self){
196
// If all remaining keys are string, reset $str_keys
197
var all_strings = true
198
for(var k = 0, lenk = self.$items.length; k < lenk; k++){
199
if(! (typeof self.$items[k][0] == "string")){
200
all_strings = false
201
break
202
}else{
203
self.$string_dict[self.$items[k][0]] =
204
self.$items[k][1]
205
}
206
}
207
if(all_strings){
208
self.$hash = {}
209
self.$items = []
210
self.$str_keys = true
211
}else{
212
self.$string_dict = {}
213
}
214
return self
215
}
216
219
var $ = $B.args("__eq__", 2, {self: null, key: null},
220
["self", "key"], arguments, {}, null, null),
225
if(self.$jsobj[key] === undefined){throw KeyError.$factory(key)}
226
delete self.$jsobj[key]
229
230
if(self.$str_keys){
231
if(self.$string_dict.hasOwnProperty(key)){
232
delete self.$string_dict[key]
234
}
235
}else{
236
var hash = _b_.hash(key),
237
items = self.$hash[hash]
238
if(items !== undefined){
239
for(var i = 0, len = items.length; i < len; i++){
240
var rank = items[i]
241
if($B.rich_comp("__eq__", key, self.$items[rank][0])){
242
self.$items.splice(rank, 1)
243
items.splice(i, 1)
244
if(items.length == 0){
245
delete self.$hash[hash]
246
}
247
// If all remaining keys are string, reset $str_keys
248
self = check_string_keys(self)
249
if(!self.$str_keys){
250
self.$string_dict = {}
251
// decrease all indices > rank
252
for(var hash in self.$hash){
253
for(var j = 0, lenj = self.$hash[hash].length;
254
j < lenj; j++){
255
if(self.$hash[hash][j] > rank){
256
self.$hash[hash][j] -= 1
257
}
258
}
259
}
260
}
261
return $N
262
}
270
var $ = $B.args("__eq__", 2, {self: null, other: null},
271
["self", "other"], arguments, {}, null, null),
276
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
277
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
282
if(self.$str_keys){
283
for(var s in self.$string_dict){
284
if(! $B.rich_comp("__eq__", self.$string_dict[s],
285
other.$string_dict[s])){
286
return false
287
}
289
return true
290
}else{
291
for(var hash in self.$hash){
292
var ranks = self.$hash[hash]
293
if(! other.$hash.hasOwnProperty(hash)){
294
return false
295
}else if(other.$hash[hash].length !== ranks.length){
296
return false
297
}else{
298
for(var i = 0, leni = ranks.length; i < leni; i++){
299
var item1 = self.$items[ranks[i]],
300
flag = false
301
for(var j = 0, lenj = other.$hash[hash].length; j < lenj;
302
j++){
303
var item2 = other.$items[other.$hash[hash][j]]
304
if($B.rich_comp("__eq__", item1[0], item2[0]) &&
305
$B.rich_comp("__eq__", item1[1], item2[1])){
306
flag = true
307
break
308
}
309
}
310
if(! flag){return false}
311
}
312
}
319
var $ = $B.args("__getitem__", 2, {self: null, key: null},
320
["self", "key"], arguments, {}, null, null),
325
if(!self.$jsobj.hasOwnProperty(key)){
326
throw _b_.KeyError.$factory(str.$factory(key))
327
}else if(self.$jsobj[key] === undefined){
332
if(self.$str_keys){
333
if(typeof key == "string"){
334
var res = self.$string_dict[key]
335
if(res !== undefined){return res}
336
}else{
337
var hash = _b_.hash(key),
338
s = object_equals_string(self, hash, key)
339
if(s !== $N){
340
return self.$string_dict[s]
342
}
343
}else{
344
var hash = _b_.hash(key),
345
items = self.$hash[hash]
346
if(items !== undefined){
347
for(var i = 0, len = items.length; i < len; i++){
348
var rank = items[i]
349
if($B.rich_comp("__eq__", key, self.$items[rank][0])){
350
return self.$items[rank][1]
351
}
378
if(first === undefined){return $N}
379
if(second === undefined){
380
if(first.__class__ === $B.JSObject){
381
self.$jsobj = first.js
382
return $N
383
}else if(first.$jsobj){
384
self.$jsobj = {}
385
for(var attr in first.$jsobj){
386
self.$jsobj[attr] = first.$jsobj[attr]
396
arguments, {}, "first", "second")
397
var args = $.first
398
if(args.length > 1){
399
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
400
", got 2")
401
}else if(args.length == 1){
402
args = args[0]
403
if(args.__class__ === dict){
404
self.$str_keys = args.$str_keys
405
if(args.$str_keys){
406
for(var key in args.$string_dict){
407
self.$string_dict[key] = args.$string_dict[key]
408
}
409
}else{
410
self.$items = args.$items.slice()
411
for(var hash in args.$hash){
412
self.$hash[hash] = args.$hash[hash]
413
}
414
}
418
var keys = $B.$getattr(args, "keys", null)
419
if(keys !== null){
420
var gi = $B.$getattr(args, "__getitem__", null)
421
if(gi !== null){
422
// has keys and __getitem__ : it's a mapping, iterate on
423
// keys and values
424
gi = $B.$call(gi)
425
var kiter = _b_.iter($B.$call(keys)())
426
while(true){
427
try{
428
var key = _b_.next(kiter),
429
value = gi(key)
430
dict.__setitem__(self, key, value)
431
}catch(err){
432
if(err.__class__ === _b_.StopIteration){
433
break
434
}
435
throw err
436
}
437
}
438
return $N
439
}
440
}
441
if(! Array.isArray(args)){
442
args = _b_.list.$factory(args)
443
}
444
// Form "dict([[key1, value1], [key2,value2], ...])"
468
if(self.$str_keys){
469
return Object.keys(self.$string_dict).length
470
}else{
471
return self.$items.length
472
}
488
if(cls !== dict){
489
instance.__dict__ = _b_.dict.$factory()
490
}
491
return instance
511
items.forEach(function(item){
512
if((!self.$jsobj && item[1] === self) ||
513
(self.$jsobj && item[1] === self.$jsobj)){
528
["self", "key", "value"], arguments, {}, null, null)
529
return dict.$setitem($.self, $.key, $.value)
530
}
535
if(self.$from_js){
536
// dictionary created by method to_dict of JSObject instances
537
value = $B.pyobj2jsobj(value)
538
}
542
// If class attribute __init__ or __new__ are reset,
543
// the factory function has to change
544
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
545
}
546
}else{
556
}else{
557
// Is key hash equal to the hash of one of the string keys ?
558
hash = _b_.hash(key)
559
var s = object_equals_string(self, hash, key)
560
if(s !== $N){
561
// Reset value
562
self.$string_dict[s] = value
563
return $N
564
}
565
self.$str_keys = false
566
for(var s in self.$string_dict){
567
var item = [s, self.$string_dict[s]],
568
hash = _b_.str.__hash__(s)
569
self.$items.push(item)
570
self.$hash[hash] = [self.$items.length - 1]
571
}
572
}
575
var hash = hash === undefined ? _b_.hash(key) : hash,
576
items = self.$hash[hash]
577
if(items !== undefined){
578
for(var i = 0, len = items.length; i < len; i++){
579
if($B.rich_comp("__eq__", key, self.$items[i][0])){
580
self.$items[i][1] = value
581
return $N
582
}
583
}
584
self.$items.push([key, value])
585
items.push(self.$items.length - 1)
586
}else{
587
self.$items.push([key, value])
588
self.$hash[hash] = [self.$items.length - 1]
630
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
631
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
643
if(klass === dict){dict.__setitem__(res, key, value)}
644
else{_b_.getattr(res, "__setitem__")(key, value)}
655
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
656
["self", "key", "_default"], arguments, {_default: $N}, null, null)
659
catch(err){
660
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
661
else{throw err}
662
}
663
}
664
669
var _len = arguments.length - 1,
670
_msg = "items() takes no arguments (" + _len + " given)"
680
var _len = arguments.length - 1,
681
_msg = "keys() takes no arguments (" + _len + " given)"
689
var $ = $B.args("pop", 3, {self: null, key: null, _default: null},
690
["self", "key", "_default"], arguments, {_default: $N}, null, null),
722
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
723
["self", "key", "_default"], arguments, {_default: $N}, null, null),
730
if(_default === undefined){_default = $N}
731
dict.__setitem__(self, key, _default)
738
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
739
{}, "args", "kw"),
740
self = $.self,
741
args = $.args,
742
kw = $.kw
743
if(args.length > 0){
744
var o = args[0]
745
if(isinstance(o, dict)){
749
var _keys = _b_.list.$factory(getattr(o, "keys")()),
750
i = _keys.length
751
while(i--){
752
var _value = getattr(o, "__getitem__")(_keys[i])
765
var _len = arguments.length - 1,
766
_msg = "values() takes no arguments (" + _len + " given)"
768
}
769
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
770
}
771
774
var args = [res]
775
for(var i = 0, len = arguments.length; i < len ; i++){
776
args.push(arguments[i])
777
}
778
dict.__init__.apply(null, args)
786
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
787
// have the attribute $infos
788
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
789
792
$B.$dict_length = dict.__len__
793
$B.$dict_getitem = dict.__getitem__
794
$B.$dict_get = dict.get
795
$B.$dict_set = dict.__setitem__
796
$B.$dict_contains = dict.__contains__
797
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
798
$B.$copy_dict = $copy_dict // copy from right to left
807
var res = {
808
$hash: {},
809
$items: obj.$items.slice(),
810
$str_keys: obj.$str_keys,
811
$string_dict: {}
812
}
813
for(var hash in obj.$hash){res.$hash[hash] = obj.$hash[hash]}
814
for(var key in obj.$string_dict){
815
res.$string_dict[key] = obj.$string_dict[key]
816
}
826
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
827
"item assignment")
830
for(var attr in dict){
831
if(mappingproxy[attr] !== undefined ||
832
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
833
"clear", "fromkeys", "pop", "popitem", "setdefault",
834
"update"].indexOf(attr) > -1){
835
continue
836
}
837
if(typeof dict[attr] == "function"){
838
mappingproxy[attr] = (function(key){
839
return function(){
840
return dict[key].apply(null, arguments)
841
}
842
})(attr)
843
}else{
844
mappingproxy[attr] = dict[attr]
845
}
846
}
847
867
if(klass !== undefined && klass.$native){
868
throw _b_.AttributeError.$factory(klass.__name__ +