3
3
cimport cython
4
4
from cython cimport Py_ssize_t
5
5
6
- from cpython cimport PyObject, PyInt_Check
7
- from cpython.slice cimport PySlice_Check
6
+ cdef extern from " Python.h " :
7
+ Py_ssize_t PY_SSIZE_T_MAX
8
8
9
9
import numpy as np
10
10
cimport numpy as np
11
11
from numpy cimport int64_t
12
12
13
- cdef extern from " Python.h" :
14
- Py_ssize_t PY_SSIZE_T_MAX
15
-
16
13
cdef extern from " compat_helper.h" :
17
14
cdef int slice_get_indices(PyObject* s, Py_ssize_t length,
18
15
Py_ssize_t * start, Py_ssize_t * stop,
@@ -22,19 +19,18 @@ cdef extern from "compat_helper.h":
22
19
23
20
cdef class BlockPlacement:
24
21
# __slots__ = '_as_slice', '_as_array', '_len'
25
- cdef:
26
- slice _as_slice
27
- object _as_array
28
- bint _has_slice, _has_array, _is_known_slice_like
22
+ cdef slice _as_slice
23
+ cdef object _as_array
24
+
25
+ cdef bint _has_slice, _has_array, _is_known_slice_like
29
26
30
27
def __init__ (self , val ):
31
- cdef:
32
- slice slc
28
+ cdef slice slc
33
29
34
30
self ._has_slice = False
35
31
self ._has_array = False
36
32
37
- if PySlice_Check (val):
33
+ if isinstance (val, slice ):
38
34
slc = slice_canonize(val)
39
35
40
36
if slc.start != slc.stop:
@@ -52,8 +48,7 @@ cdef class BlockPlacement:
52
48
self ._has_array = True
53
49
54
50
def __str__ (self ):
55
- cdef:
56
- slice s = self ._ensure_has_slice()
51
+ cdef slice s = self ._ensure_has_slice()
57
52
if s is not None :
58
53
v = self ._as_slice
59
54
else :
@@ -64,17 +59,15 @@ cdef class BlockPlacement:
64
59
__repr__ = __str__
65
60
66
61
def __len__ (self ):
67
- cdef:
68
- slice s = self ._ensure_has_slice()
62
+ cdef slice s = self ._ensure_has_slice()
69
63
if s is not None :
70
64
return slice_len(s)
71
65
else :
72
66
return len (self ._as_array)
73
67
74
68
def __iter__ (self ):
75
- cdef:
76
- slice s = self ._ensure_has_slice()
77
- Py_ssize_t start, stop, step, _
69
+ cdef slice s = self ._ensure_has_slice()
70
+ cdef Py_ssize_t start, stop, step, _
78
71
if s is not None :
79
72
start, stop, step, _ = slice_get_indices_ex(s)
80
73
return iter (range (start, stop, step))
@@ -83,17 +76,15 @@ cdef class BlockPlacement:
83
76
84
77
@property
85
78
def as_slice (self ):
86
- cdef:
87
- slice s = self ._ensure_has_slice()
79
+ cdef slice s = self ._ensure_has_slice()
88
80
if s is None :
89
81
raise TypeError (' Not slice-like' )
90
82
else :
91
83
return s
92
84
93
85
@property
94
86
def indexer (self ):
95
- cdef:
96
- slice s = self ._ensure_has_slice()
87
+ cdef slice s = self ._ensure_has_slice()
97
88
if s is not None :
98
89
return s
99
90
else :
@@ -105,8 +96,7 @@ cdef class BlockPlacement:
105
96
106
97
@property
107
98
def as_array (self ):
108
- cdef:
109
- Py_ssize_t start, stop, end, _
99
+ cdef Py_ssize_t start, stop, end, _
110
100
if not self ._has_array:
111
101
start, stop, step, _ = slice_get_indices_ex(self ._as_slice)
112
102
self ._as_array = np.arange(start, stop, step,
@@ -116,19 +106,17 @@ cdef class BlockPlacement:
116
106
117
107
@property
118
108
def is_slice_like (self ):
119
- cdef:
120
- slice s = self ._ensure_has_slice()
109
+ cdef slice s = self ._ensure_has_slice()
121
110
return s is not None
122
111
123
112
def __getitem__ (self , loc ):
124
- cdef:
125
- slice s = self ._ensure_has_slice()
113
+ cdef slice s = self ._ensure_has_slice()
126
114
if s is not None :
127
115
val = slice_getitem(s, loc)
128
116
else :
129
117
val = self ._as_array[loc]
130
118
131
- if not PySlice_Check (val) and val.ndim == 0 :
119
+ if not isinstance (val, slice ) and val.ndim == 0 :
132
120
return val
133
121
134
122
return BlockPlacement(val)
@@ -144,11 +132,10 @@ cdef class BlockPlacement:
144
132
[o.as_array for o in others]))
145
133
146
134
cdef iadd(self , other):
147
- cdef:
148
- slice s = self ._ensure_has_slice()
149
- Py_ssize_t other_int, start, stop, step, l
135
+ cdef slice s = self ._ensure_has_slice()
136
+ cdef Py_ssize_t other_int, start, stop, step, l
150
137
151
- if PyInt_Check (other) and s is not None :
138
+ if isinstance (other, int ) and s is not None :
152
139
other_int = < Py_ssize_t> other
153
140
154
141
if other_int == 0 :
@@ -182,8 +169,7 @@ cdef class BlockPlacement:
182
169
return self
183
170
184
171
cdef BlockPlacement copy(self ):
185
- cdef:
186
- slice s = self ._ensure_has_slice()
172
+ cdef slice s = self ._ensure_has_slice()
187
173
if s is not None :
188
174
return BlockPlacement(s)
189
175
else :
@@ -202,7 +188,7 @@ cdef class BlockPlacement:
202
188
return self ._as_slice
203
189
204
190
205
- cdef slice_canonize(slice s):
191
+ cpdef slice_canonize(slice s):
206
192
"""
207
193
Convert slice to canonical bounded form.
208
194
"""
@@ -248,8 +234,8 @@ cdef slice_canonize(slice s):
248
234
return slice (start, stop, step)
249
235
250
236
251
- cpdef Py_ssize_t slice_len(slice slc,
252
- Py_ssize_t objlen = PY_SSIZE_T_MAX) except - 1 :
237
+ cpdef Py_ssize_t slice_len(
238
+ slice slc, Py_ssize_t objlen = PY_SSIZE_T_MAX) except - 1 :
253
239
"""
254
240
Get length of a bounded slice.
255
241
@@ -293,14 +279,14 @@ cpdef slice_get_indices_ex(slice slc, Py_ssize_t objlen=PY_SSIZE_T_MAX):
293
279
return start, stop, step, length
294
280
295
281
296
- cdef slice_getitem(slice slc, ind):
282
+ def slice_getitem (slice slc not None , ind ):
297
283
cdef:
298
284
Py_ssize_t s_start, s_stop, s_step, s_len
299
285
Py_ssize_t ind_start, ind_stop, ind_step, ind_len
300
286
301
287
s_start, s_stop, s_step, s_len = slice_get_indices_ex(slc)
302
288
303
- if PySlice_Check (ind):
289
+ if isinstance (ind, slice ):
304
290
ind_start, ind_stop, ind_step, ind_len = slice_get_indices_ex(ind,
305
291
s_len)
306
292
@@ -402,7 +388,7 @@ def get_blkno_indexers(int64_t[:] blknos, bint group=True):
402
388
start = 0
403
389
cur_blkno = blknos[start]
404
390
405
- if group is False :
391
+ if group == False :
406
392
for i in range (1 , n):
407
393
if blknos[i] != cur_blkno:
408
394
yield cur_blkno, slice (start, i)
@@ -447,4 +433,4 @@ def get_blkno_indexers(int64_t[:] blknos, bint group=True):
447
433
res_view[i] = diff
448
434
i += 1
449
435
450
- yield blkno, result
436
+ yield blkno, result
0 commit comments