Skip to content
Permalink
Newer
Older
100644 322 lines (284 sloc) 8.92 KB
Sep 5, 2014
1
;(function($B){
2
3
var _b_ = $B.builtins
4
var $s=[]
5
for(var $b in _b_) $s.push('var ' + $b +'=_b_["'+$b+'"]')
6
eval($s.join(';'))
7
//for(var $py_builtin in _b_){eval("var "+$py_builtin+"=_b_[$py_builtin]")}
8
var $ObjectDict = _b_.object.$dict
9
10
// dictionary
11
function $DictClass($keys,$values){
12
// JS dict objects are indexed by strings, not by arbitrary objects
13
// so we must use 2 arrays, one for keys and one for values
14
this.iter = null
15
this.__class__ = $DictDict
16
this.$keys = $keys // JS Array
17
this.$values = $values // idem
18
}
19
20
var $DictDict = {__class__:$B.$type,
21
__name__ : 'dict',
22
$native:true
23
}
24
25
$DictDict.__bool__ = function (self) {return self.$keys.length>0}
26
27
$DictDict.__contains__ = function(self,item){
28
if(self.$jsobj) return self.$jsobj[item]!==undefined
29
return _b_.list.$dict.__contains__(self.$keys,item)
30
}
31
32
$DictDict.__delitem__ = function(self,arg){
33
// search if arg is in the keys
34
for(var i=0;i<self.$keys.length;i++){
35
if(getattr(arg,'__eq__')(self.$keys[i])){
36
self.$keys.splice(i,1)
37
self.$values.splice(i,1)
38
if(self.$jsobj) delete self.$jsobj[arg]
39
return
40
}
41
}
42
throw KeyError(_b_.str(arg))
43
}
44
45
$DictDict.__eq__ = function(self,other){
46
if(other===undefined){ // compare self to class "dict"
47
return self===dict
48
}
49
if(!isinstance(other,dict)) return False
50
if(other.$keys.length!==self.$keys.length) return False
51
for(var i=0;i<self.$keys.length;i++){
52
var key = self.$keys[i]
53
for(var j=0;j<other.$keys.length;j++){
54
try{
55
if(getattr(other.$keys[j],'__eq__')(key)){
56
if(!getattr(other.$values[j],'__eq__')(self.$values[i])){
57
return False
58
}
59
}
60
}catch(err){$B.$pop_exc()}
61
}
62
}
63
return True
64
}
65
66
$DictDict.__getitem__ = function(self,arg){
67
// search if arg is in the keys
68
for(var i=0;i<self.$keys.length;i++){
69
if(getattr(arg,'__eq__')(self.$keys[i])) return self.$values[i]
70
}
71
throw KeyError(_b_.str(arg))
72
}
73
74
$DictDict.__hash__ = function(self) {throw _b_.TypeError("unhashable type: 'dict'");}
75
76
$DictDict.__init__ = function(self){
77
var args = []
78
for(var i=1;i<arguments.length;i++){args.push(arguments[i])}
79
self.$keys = []
80
self.$values = []
81
if(args.length==0) return
82
83
if(args.length===1){
84
var obj = args[0]
85
if(isinstance(obj,dict)){
86
self.$keys = obj.$keys
87
self.$values = obj.$values
88
return
89
}
90
if(obj.__class__===$B.JSObject.$dict){
91
// convert a JSObject into a Python dictionary
92
var res = new $DictClass([],[])
93
for(var attr in obj.js){
94
$DictDict.__setitem__(res,attr,obj.js[attr])
95
}
96
self.$keys = res.$keys
97
self.$values = res.$values
98
self.$jsobj = obj.js // used to reflect changes in underlying JS object
99
return
100
}
101
}
102
var $ns=$B.$MakeArgs('dict',args,[],[],'args','kw')
103
var args = $ns['args']
104
var kw = $ns['kw']
105
if(args.length>0){
106
if(isinstance(args[0],dict)){
107
self.$keys = args[0].$keys
108
self.$values = args[0].$values
109
return
110
}
111
112
// format dict([(k1,v1),(k2,v2)...])
113
var iterable = iter(args[0])
114
while(1){
115
try{
116
var elt = next(iterable)
117
self.$keys.push(getattr(elt,'__getitem__')(0))
118
self.$values.push(getattr(elt,'__getitem__')(1))
119
}catch(err){
120
if(err.__name__==='StopIteration'){$B.$pop_exc();break}
121
throw err
122
}
123
}
124
return
125
}
126
if(kw.$keys.length>0){ // format dict(k1=v1,k2=v2...)
127
self.$keys = kw.$keys
128
self.$values = kw.$values
129
}
130
}
131
132
var $dict_iterator = $B.$iterator_class('dict iterator')
133
$DictDict.__iter__ = function(self){
134
return $B.$iterator(self.$keys,$dict_iterator)
135
}
136
137
$DictDict.__len__ = function(self) {return self.$keys.length}
138
139
$DictDict.__mro__ = [$DictDict,$ObjectDict]
140
141
$DictDict.__ne__ = function(self,other){return !$DictDict.__eq__(self,other)}
142
143
$DictDict.__next__ = function(self){
144
if(self.iter==null){self.iter==0}
145
if(self.iter<self.$keys.length){
146
self.iter++
147
return self.$keys[self.iter-1]
148
} else {
149
self.iter = null
150
throw _b_.StopIteration()
151
}
152
}
153
154
$DictDict.__repr__ = function(self){
155
if(self===undefined) return "<class 'dict'>"
156
157
var res=[]
158
for(var i=0;i<self.$keys.length;i++){
159
res.push(repr(self.$keys[i])+':'+repr(self.$values[i]))
160
}
161
return '{'+ res.join(',') +'}'
162
}
163
164
$DictDict.__setitem__ = function(self,key,value){
165
for(var i=0;i<self.$keys.length;i++){
166
try{
167
if(getattr(key,'__eq__')(self.$keys[i])){ // reset value
168
self.$values[i]=value
169
return
170
}
171
}catch(err){ // if __eq__ throws an exception
172
$B.$pop_exc()
173
}
174
}
175
// create a new key/value
176
self.$keys.push(key)
177
self.$values.push(value)
178
// if dict wraps a JS object, set its attribute
179
if(self.$jsobj) self.$jsobj[key]=value
180
}
181
182
$DictDict.__str__ = $DictDict.__repr__
183
184
// add "reflected" methods
185
$B.make_rmethods($DictDict)
186
187
$DictDict.clear = function(self){
188
// Remove all items from the dictionary.
189
self.$keys = []
190
self.$values = []
191
if(self.$jsobj) self.$jsobj={}
192
}
193
194
$DictDict.copy = function(self){
195
// Return a shallow copy of the dictionary
196
var res = dict()
197
for(var i=0;i<self.$keys.length;i++){
198
res.$keys.push(self.$keys[i])
199
res.$values.push(self.$values[i])
200
}
201
return res
202
}
203
204
$DictDict.get = function(self,key,_default){
205
try{return $DictDict.__getitem__(self,key)}
206
catch(err){
207
$B.$pop_exc()
208
if(_default!==undefined) return _default
209
return None
210
}
211
}
212
213
var $dict_itemsDict = $B.$iterator_class('dict_itemiterator')
214
215
$DictDict.items = function(self){
216
var items = []
217
for(var i=0;i<self.$keys.length;i++){
218
items.push(_b_.tuple([self.$keys[i],self.$values[i]]))
219
}
220
return $B.$iterator(items,$dict_itemsDict)
221
}
222
223
$DictDict.fromkeys = function(keys,value){
224
// class method
225
if(value===undefined) value=_b_.None
226
var res = dict()
227
var keys_iter = _b_.iter(keys)
228
while(1){
229
try{
230
var key = _b_.next(keys_iter)
231
$DictDict.__setitem__(res,key,value)
232
}catch(err){
233
if($B.is_exc(err,[_b_.StopIteration])){
234
$B.$pop_exc()
235
return res
236
}
237
throw err
238
}
239
}
240
}
241
242
var $dict_keysDict = $B.$iterator_class('dict_keys')
243
244
$DictDict.keys = function(self){
245
return $B.$iterator(self.$keys,$dict_keysDict)
246
}
247
248
$DictDict.pop = function(self,key,_default){
249
try{
250
var res = $DictDict.__getitem__(self,key)
251
$DictDict.__delitem__(self,key)
252
return res
253
}catch(err){
254
$B.$pop_exc()
255
if(err.__name__==='KeyError'){
256
if(_default!==undefined) return _default
257
throw err
258
}
259
throw err
260
}
261
}
262
263
$DictDict.popitem = function(self){
264
if(self.$keys.length===0) throw KeyError("'popitem(): dictionary is empty'")
265
return _b_.tuple([self.$keys.pop(),self.$values.pop()])
266
}
267
268
$DictDict.setdefault = function(self,key,_default){
269
try{return $DictDict.__getitem__(self,key)}
270
catch(err){
271
if(_default===undefined) _default=None
272
$DictDict.__setitem__(self,key,_default)
273
return _default
274
}
275
}
276
277
$DictDict.update = function(self){
278
var params = []
279
for(var i=1;i<arguments.length;i++){params.push(arguments[i])}
280
var $ns=$B.$MakeArgs('$DictDict.update',params,[],[],'args','kw')
281
var args = $ns['args']
282
if(args.length>0 && isinstance(args[0],dict)){
283
var other = args[0]
284
for(var i=0;i<other.$keys.length;i++){
285
$DictDict.__setitem__(self,other.$keys[i],other.$values[i])
286
}
287
}
288
var kw = $ns['kw']
289
var keys = kw.$keys
290
for(var i=0;i<keys.length;i++){
291
$DictDict.__setitem__(self,keys[i],kw.$values(keys[i]))
292
}
293
}
294
295
var $dict_valuesDict = $B.$iterator_class('dict_values')
296
297
$DictDict.values = function(self){
298
return $B.$iterator(self.$values,$dict_valuesDict)
299
}
300
301
function dict(){
302
var res = {__class__:$DictDict}
303
// apply __init__ with arguments of dict()
304
var args = [res]
305
for(var i=0;i<arguments.length;i++){args.push(arguments[i])}
306
$DictDict.__init__.apply(null,args)
307
return res
308
}
309
$B.$dict = dict // used for dict literals : "x={}" is translated to "x=__BRYTHON__.$dict()",
310
// not to "x=dict()"
311
// otherwise this would fail :
312
// def foo(dict=None):
313
// x = {}
314
// because inside the function, 'dict' has beeen set to the
315
// value of argument 'dict'
316
dict.__class__ = $B.$factory
317
dict.$dict = $DictDict
318
$DictDict.$factory = dict
319
$DictDict.__new__ = $B.$__new__(dict)
320
321
_b_.dict = dict
322
})(__BRYTHON__)