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 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
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
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
833 lines (730 sloc)
23.4 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(){
144
var $ = $B.args("__contains__", 2, {self: null, item: null},
145
["self", "item"], arguments, {}, null, null),
147
item = $.item
148
149
if(self.$jsobj){return self.$jsobj[item] !== undefined}
150
151
switch(typeof item) {
152
case "string":
153
return self.$string_dict[item] !== undefined
154
case "number":
155
return self.$numeric_dict[item] !== undefined
156
}
157
158
var _key = hash(item)
159
if(self.$str_hash[_key] !== undefined &&
160
$B.rich_comp("__eq__", item, self.$str_hash[_key])){return true}
161
if(self.$numeric_dict[_key] !== undefined &&
162
$B.rich_comp("__eq__", item, _key)){return true}
163
if(self.$object_dict[_key] !== 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')
170
//
171
// a = {'u': 'a', X(): 'b'}
172
// assert set(a.values()) == {'a', 'b'}
173
// assert not X() in a
174
return $B.rich_comp("__eq__", item, self.$object_dict[_key][0])
181
var $ = $B.args("__eq__", 2, {self: null, arg: null},
182
["self", "arg"], arguments, {}, null, null),
187
if(self.$jsobj[arg] === undefined){throw KeyError.$factory(arg)}
188
delete self.$jsobj[arg]
191
switch(typeof arg){
192
case "string":
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)]
199
case "number":
200
if(self.$numeric_dict[arg] === undefined){
201
throw KeyError.$factory(_b_.str.$factory(arg))
206
// go with defaults
207
208
var _key = hash(arg)
209
210
if(self.$object_dict[_key] !== undefined){
211
delete self.$object_dict[_key]
212
}
213
214
if(self.$jsobj){delete self.$jsobj[arg]}
215
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
235
}
236
for(var k in self.$numeric_dict){
237
if(!$B.rich_comp("__eq__", other.$numeric_dict[k],
238
self.$numeric_dict[k])){
239
return false
241
}
242
for(var k in self.$string_dict){
243
if(!$B.rich_comp("__eq__", other.$string_dict[k],
244
self.$string_dict[k])){
245
return false
248
for(var k in self.$object_dict){
249
if(!$B.rich_comp("__eq__", other.$object_dict[k][1],
250
self.$object_dict[k][1])){
251
return false
252
}
253
}
254
255
return true
256
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){
272
273
switch(typeof arg){
274
case "string":
275
if(self.$string_dict[arg] !== undefined){
276
return self.$string_dict[arg]
278
break
279
case "number":
280
if(self.$numeric_dict[arg] !== undefined){
281
return self.$numeric_dict[arg]
283
break
284
}
285
286
// since the key is more complex use 'default' method of getting item
287
288
var _key = _b_.hash(arg),
289
_eq = function(other){return $B.rich_comp("__eq__", arg, other)}
290
291
var sk = self.$str_hash[_key]
292
if(sk !== undefined && _eq(sk)){
293
return self.$string_dict[sk]
294
}
295
if(self.$numeric_dict[_key] !== undefined && _eq(_key)){
296
return self.$numeric_dict[_key]
297
}
298
if(isinstance(arg, _b_.str)){
299
// string subclass
300
var res = self.$string_dict[arg.valueOf()]
301
if(res !== undefined){return res}
302
}
303
304
var obj_ref = self.$object_dict[_key]
305
if(obj_ref !== undefined){
306
// An object with the same hash is already stored
307
// Lookup should fail if equality raises an exception
308
_eq(self.$object_dict[_key][0])
309
return self.$object_dict[_key][1]
328
switch(typeof item[0]) {
329
case 'string':
330
self.$string_dict[item[0]] = item[1]
331
self.$str_hash[str_hash(item[0])] = item[0]
332
break
333
case 'number':
334
self.$numeric_dict[item[0]] = item[1]
335
break
336
default:
337
si(self, item[0], item[1])
338
break
339
}
345
if(first === undefined){return $N}
346
if(second === undefined){
347
if(first.__class__ === $B.JSObject){
348
self.$jsobj = first.js
349
return $N
350
}else if(first.$jsobj){
351
self.$jsobj = {}
352
for(var attr in first.$jsobj){
353
self.$jsobj[attr] = first.$jsobj[attr]
363
arguments, {}, "first", "second")
364
var args = $.first
365
if(args.length > 1){
366
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
367
", got 2")
368
}else if(args.length == 1){
369
args = args[0]
370
if(args.__class__ === dict){
371
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
372
forEach(function(d){
373
for(key in args[d]){self[d][key] = args[d][key]}
374
})
378
var keys = $B.$getattr(args, "keys", null)
379
if(keys !== null){
380
var gi = $B.$getattr(args, "__getitem__", null)
381
if(gi !== null){
382
// has keys and __getitem__ : it's a mapping, iterate on
383
// keys and values
384
gi = $B.$call(gi)
385
var kiter = _b_.iter($B.$call(keys)())
386
while(true){
387
try{
388
var key = _b_.next(kiter),
389
value = gi(key)
390
dict.__setitem__(self, key, value)
391
}catch(err){
392
if(err.__class__ === _b_.StopIteration){
393
break
394
}
395
throw err
396
}
397
}
398
return $N
399
}
400
}
401
if(! Array.isArray(args)){
402
args = _b_.list.$factory(args)
403
}
404
// Form "dict([[key1, value1], [key2,value2], ...])"
410
switch(typeof attr){
411
case "string":
412
self.$string_dict[attr] = kw[attr]
413
self.$str_hash[str_hash(attr)] = attr
414
break
415
case "number":
416
self.$numeric_dict[attr] = kw[attr]
417
break
418
default:
419
si(self, attr, kw[attr])
420
break
421
}
439
for(var k in self.$numeric_dict){_count++}
440
for(var k in self.$string_dict){_count++}
441
for(var k in self.$object_dict){_count++}
442
443
return _count
459
if(cls !== dict){
460
instance.__dict__ = _b_.dict.$factory()
461
}
462
return instance
482
items.forEach(function(item){
483
if((!self.$jsobj && item[1] === self) ||
484
(self.$jsobj && item[1] === self.$jsobj)){
499
["self", "key", "value"], arguments, {}, null, null)
500
return dict.$setitem($.self, $.key, $.value)
501
}
505
if(self.$from_js){
506
// dictionary created by method to_dict of JSObject instances
507
value = $B.pyobj2jsobj(value)
508
}
512
// If class attribute __init__ or __new__ are reset,
513
// the factory function has to change
514
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
515
}
516
}else{
525
self.$str_hash[str_hash(key)] = key
526
return $N
527
case "number":
528
self.$numeric_dict[key] = value
532
// if we got here the key is more complex, use default method
533
534
var _key = hash(key),
535
_eq = function(other){return $B.rich_comp("__eq__", key, other)}
536
537
if(self.$numeric_dict[_key] !== undefined && _eq(_key)){
538
self.$numeric_dict[_key] = value
539
return $N
540
}
541
var sk = self.$str_hash[_key]
542
if(sk !== undefined && _eq(sk)){
543
self.$string_dict[sk] = value
544
return $N
545
}
546
547
var obj_ref = self.$object_dict[_key]
548
if(obj_ref !== undefined){
549
// An object with the same hash is already stored
550
// Lookup should fail if equality raises an exception
551
_eq(self.$object_dict[_key][0])
595
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
596
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
608
if(klass === dict){dict.__setitem__(res, key, value)}
609
else{_b_.getattr(res, "__setitem__")(key, value)}
620
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
621
["self", "key", "_default"], arguments, {_default: $N}, null, null)
624
catch(err){
625
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
626
else{throw err}
627
}
628
}
629
634
var _len = arguments.length - 1,
635
_msg = "items() takes no arguments (" + _len + " given)"
645
var _len = arguments.length - 1,
646
_msg = "keys() takes no arguments (" + _len + " given)"
654
var $ = $B.args("pop", 3, {self: null, key: null, _default: null},
655
["self", "key", "_default"], arguments, {_default: $N}, null, null),
687
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
688
["self", "key", "_default"], arguments, {_default: $N}, null, null),
695
if(_default === undefined){_default = $N}
696
dict.__setitem__(self, key, _default)
703
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
704
{}, "args", "kw"),
705
self = $.self,
706
args = $.args,
707
kw = $.kw
708
if(args.length > 0){
709
var o = args[0]
710
if(isinstance(o, dict)){
716
i = _keys.length
717
while(i--){
718
var _value = getattr(o, "__getitem__")(_keys[i])
731
var _len = arguments.length - 1,
732
_msg = "values() takes no arguments (" + _len + " given)"
734
}
735
return $iterator_wrapper(new $value_iterator(self), $dict_valuesDict)
736
}
737
740
var args = [res]
741
for(var i = 0, len = arguments.length; i < len ; i++){
742
args.push(arguments[i])
743
}
744
dict.__init__.apply(null, args)
752
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
753
// have the attribute $infos
754
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
755
758
$B.$dict_length = dict.__len__
759
$B.$dict_getitem = dict.__getitem__
760
$B.$dict_get = dict.get
761
$B.$dict_set = dict.__setitem__
762
$B.$dict_contains = dict.__contains__
763
$B.$dict_items = function(d) { return new $item_generator(d).as_list() }
764
$B.$copy_dict = $copy_dict // copy from right to left
783
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
784
"item assignment")
787
for(var attr in dict){
788
if(mappingproxy[attr] !== undefined ||
789
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
790
"clear", "fromkeys", "pop", "popitem", "setdefault",
791
"update"].indexOf(attr) > -1){
792
continue
793
}
794
if(typeof dict[attr] == "function"){
795
mappingproxy[attr] = (function(key){
796
return function(){
797
return dict[key].apply(null, arguments)
798
}
799
})(attr)
800
}else{
801
mappingproxy[attr] = dict[attr]
802
}
803
}
804
824
if(klass !== undefined && klass.$native){
825
throw _b_.AttributeError.$factory(klass.__name__ +