summaryrefslogtreecommitdiff
path: root/scipy/base/chararray.py
blob: d81c2fffdf5e42216583d6faf6029a05501fc503 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from numerictypes import character, string, unicode_, obj2dtype, integer
from numeric import ndarray, multiter, empty

# special sub-class for character arrays (string and unicode_)
# This adds equality testing and methods of str and unicode types
#  which operate on an element-by-element basis

class ndchararray(ndarray):
    def __new__(subtype, shape, itemlen=1, unicode=False, buffer=None,
                offset=0, strides=None, swap=0, fortran=0):

        if unicode:
            dtype = 'U%d' % itemlen
        else:
            dtype = 'U%d' % itemlen
            swap = 0


        if buffer is None:
            self = ndarray.__new__(subtype, shape, dtype, fortran=fortran)
        else:
            self = ndarray.__new__(subtype, shape, dtype, buffer=buffer,
                                   offset=offset, strides=strides,
                                   swap=swap, fortran=fortran)
        return self


    def __reduce__(self):
        pass

    # these should be moved to C
    def __eq__(self, other):
        b = multiter(self, other)
        result = empty(b.shape, dtype=bool)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = (val[0] == val[1])
        return result

    def __ne__(self, other):
        b = multiter(self, other)
        result = empty(b.shape, dtype=bool)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = (val[0] != val[1])
        return result

    def __ge__(self, other):
        b = multiter(self, other)
        result = empty(b.shape, dtype=bool)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = (val[0] >= val[1])
        return result
        
    def __le__(self, other):
        b = multiter(self, other)
        result = empty(b.shape, dtype=bool)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = (val[0] <= val[1])
        return result        

    def __gt__(self, other):
        b = multiter(self, other)
        result = empty(b.shape, dtype=bool)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = (val[0] > val[1])
        return result
    
    def __lt__(self, other):
        b = multiter(self, other)
        result = empty(b.shape, dtype=bool)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = (val[0] < val[1])
        return result        
        
    def __add__(self, other):
        b = multiter(self, other)
        arr = b.iters[1].base
        outitem = self.itemsize + arr.itemsize
        dtype = self.dtypestr[1:2] + str(outitem)
        result = empty(b.shape, dtype=dtype)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = (val[0] + val[1])
        return result 

    def __radd__(self, other):
        b = multiter(other, self)
        outitem = b.iters[0].base.itemsize + \
                  b.iters[1].base.itemsize
        dtype = self.dtypestr[1:2] + str(outitem)
        result = empty(b.shape, dtype=dtype)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = (val[0] + val[1])
        return result 

    def __mul__(self, other):
        b = multiter(self, other)
        arr = b.iters[1].base
        if not issubclass(arr.dtype, integer):
            raise ValueError, "Can only multiply by integers"
        outitem = b.iters[0].base.itemsize * arr.max()
        dtype = self.dtypestr[1:2] + str(outitem)
        result = empty(b.shape, dtype=dtype)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = val[0]*val[1]
        return result

    def __rmul__(self, other):
        b = multiter(self, other)
        arr = b.iters[1].base
        if not issubclass(arr.dtype, integer):
            raise ValueError, "Can only multiply by integers"
        outitem = b.iters[0].base.itemsize * arr.max()
        dtype = self.dtypestr[1:2] + str(outitem)
        result = empty(b.shape, dtype=dtype)
        res = result.flat
        for k, val in enumerate(b):
            res[k] = val[0]*val[1]
        return result

    def __mod__(self, other):
        return NotImplemented

    def __rmod__(self, other):
        return NotImplemented

    def capitalize(self):
        pass

    def center(self):
        pass

    def count(self):
        pass

    def decode(self):
        pass

    def encode(self):
        pass

    def endswith(self):
        pass

    def expandtabs(self):
        pass

    def find(self):
        pass

    def index(self):
        pass

    def isalnum(self):
        pass

    def isalpha(self):
        pass

    def isdigit(self):
        pass

    def islower(self):
        pass

    def isspace(self):
        pass

    def istitle(self):
        pass

    def isupper(self):
        pass

    def join(self):
        pass

    def ljust(self):
        pass

    def lower(self):
        pass

    def lstrip(self):
        pass

    def replace(self):
        pass

    def rfind(self):
        pass

    def rindex(self):
        pass

    def rjust(self):
        pass

    def rsplit(self):
        pass

    def rstrip(self):
        pass

    def split(self):
        pass

    def splitlines(self):
        pass

    def startswith(self):
        pass

    def strip(self):
        pass

    def swapcase(self):
        pass

    def title(self):
        pass

    def translate(self):
        pass

    def upper(self):
        pass

    def zfill(self):
        pass

                
def chararray(obj, itemlen=7, copy=True, unicode=False, fortran=False):

    if isinstance(obj, charndarray):
        if copy or (itemlen != obj.itemlen) \
           or (not unicode and obj.dtype == unicode_) \
           or (unicode and obj.dtype == string):
            return obj.astype(obj.dtypestr[1:])
        else:
            return obj

        
    if isinstance(obj, ndarray) and (obj.dtype in [unicode_, string]):
        copied = 0
        
        if unicode:
            dtype = 'U%d' % obj.itemlen
            if obj.dtype == string:
                obj = obj.astype(dtype)
                copied = 1
        else:
            dtype = 'S%d' % obj.itemlen
            if obj.dtype == unicode_:
                obj = obj.astype(dtype)
                copied = 1

        if copy and not copied:
            obj = obj.copy()

        return ndarray.__new__(chararray, obj.shape)

    if unicode:
        dtype = "U%d" % itemlen
    else:
        dtype = "S%d" % itemlen

    val = asarray(obj).astype(dtype)
    
    return ndchararray(val.shape, itemlen, unicode, buffer=val,
                       strides=val.strides, fortran=fortran)

    
def aschararray(obj):
    return chararray(obj, copy=False)