I spent a lot of time [that I didn't have to spend :-]
changing/fixing/rewriting the convenience methods in the objc module
and the associated tests. Now, most of the various random slicing and
dicing operations can be interchanged between Python and ObjC
objects....
If this doesn't make sense, some examples. NSDictionary can now do
containment and iterations (using generators -- I'm happy that I found
a place to use generators :-):
>>> from Foundation import *
>>> d = NSMutableDictionary.dictionary()
>>> d['a'] = 1
>>> d['b'] = 1
>>> d[2] = 3
>>> d[4] = 5
>>> d
{b = 1; 2 = 3; 4 = 5; a = 1; }
>>> for x in d: print '%s => %s' % (x, d[x])
...
b => 1
2 => 3
4 => 5
a => 1
>>> 'a' in d
1
>>> 'not' in d
0
And arrays -- note that the slicing operators work, including deletions
and assignments of slices.
>>> a = NSMutableArray.arrayWithArray_( range(10,30) )
>>> x
'a'
>>> a
(
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29
)
>>> a[10:5]
[]
>>> a[10:-5]
(20, 21, 22, 23, 24)
>>> b = range(10,30)
>>> b[10:-5]
[20, 21, 22, 23, 24]
>>> a[-5]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "/usr/lib/python2.2/site-packages/objc/_convenience.py", line
119, in <lambda>
CONVENIENCE_METHODS['objectAtIndex:'] = (('__getitem__', lambda
self, index: self.objectAtIndex_( index )),
IndexError: NSRangeException - *** -[NSCFArray objectAtIndex:]: index
(-5) beyond bounds (20)
>>> a[-5:]
(25, 26, 27, 28, 29)
>>> a[1:3]
(11, 12)
>>> a[1:3] = [4,5,6,7,8,9]
>>> a
(
10,
4,
5,
6,
7,
8,
9,
13,
14,
15,
16,
17,
18,
19,
20,
21,
22,
23,
24,
25,
26,
27,
28,
29
)
>>> type(a)
<objective-c class NSCFArray at 0x1036a0>
>>> del a[2:-2]
>>> a
(10, 4, 28, 29)
And, because of the way Ronald implemented the convenience methods in
the first place, NSSet also works:
>>> s = NSMutableSet.setWithArray_(a)
>>> s
<NSCFSet: 0xcb5970> (28, 10, 4, 29)
>>> 10 in s
1
>>> 900 in s
0
And, once I patched things to handle it:
>>> from Foundation import *
>>> x = NSSet.setWithArray_( [1,2,3,4,5] )
>>> for i in x: print i
...
1
2
3
4
5
|