1
1
# -*- coding: utf-8 -*-
2
2
3
- cimport cython
4
- from cython cimport Py_ssize_t
3
+ import cython
4
+ from cython import Py_ssize_t
5
5
6
6
from cpython cimport PyObject
7
- from cpython.slice cimport PySlice_Check
8
7
9
8
cdef extern from " Python.h" :
10
9
Py_ssize_t PY_SSIZE_T_MAX
@@ -30,14 +29,15 @@ cdef class BlockPlacement:
30
29
cdef bint _has_slice, _has_array, _is_known_slice_like
31
30
32
31
def __init__ (self , val ):
33
- cdef slice slc
32
+ cdef:
33
+ slice slc
34
34
35
35
self ._as_slice = None
36
36
self ._as_array = None
37
37
self ._has_slice = False
38
38
self ._has_array = False
39
39
40
- if PySlice_Check (val):
40
+ if isinstance (val, slice ):
41
41
slc = slice_canonize(val)
42
42
43
43
if slc.start != slc.stop:
@@ -55,7 +55,8 @@ cdef class BlockPlacement:
55
55
self ._has_array = True
56
56
57
57
def __str__ (self ):
58
- cdef slice s = self ._ensure_has_slice()
58
+ cdef:
59
+ slice s = self ._ensure_has_slice()
59
60
if s is not None :
60
61
v = self ._as_slice
61
62
else :
@@ -66,15 +67,17 @@ cdef class BlockPlacement:
66
67
__repr__ = __str__
67
68
68
69
def __len__ (self ):
69
- cdef slice s = self ._ensure_has_slice()
70
+ cdef:
71
+ slice s = self ._ensure_has_slice()
70
72
if s is not None :
71
73
return slice_len(s)
72
74
else :
73
75
return len (self ._as_array)
74
76
75
77
def __iter__ (self ):
76
- cdef slice s = self ._ensure_has_slice()
77
- cdef Py_ssize_t start, stop, step, _
78
+ cdef:
79
+ slice s = self ._ensure_has_slice()
80
+ Py_ssize_t start, stop, step, _
78
81
if s is not None :
79
82
start, stop, step, _ = slice_get_indices_ex(s)
80
83
return iter (range (start, stop, step))
@@ -83,15 +86,17 @@ cdef class BlockPlacement:
83
86
84
87
@property
85
88
def as_slice (self ):
86
- cdef slice s = self ._ensure_has_slice()
89
+ cdef:
90
+ slice s = self ._ensure_has_slice()
87
91
if s is None :
88
92
raise TypeError (' Not slice-like' )
89
93
else :
90
94
return s
91
95
92
96
@property
93
97
def indexer (self ):
94
- cdef slice s = self ._ensure_has_slice()
98
+ cdef:
99
+ slice s = self ._ensure_has_slice()
95
100
if s is not None :
96
101
return s
97
102
else :
@@ -103,7 +108,8 @@ cdef class BlockPlacement:
103
108
104
109
@property
105
110
def as_array (self ):
106
- cdef Py_ssize_t start, stop, end, _
111
+ cdef:
112
+ Py_ssize_t start, stop, end, _
107
113
if not self ._has_array:
108
114
start, stop, step, _ = slice_get_indices_ex(self ._as_slice)
109
115
self ._as_array = np.arange(start, stop, step,
@@ -113,17 +119,19 @@ cdef class BlockPlacement:
113
119
114
120
@property
115
121
def is_slice_like (self ):
116
- cdef slice s = self ._ensure_has_slice()
122
+ cdef:
123
+ slice s = self ._ensure_has_slice()
117
124
return s is not None
118
125
119
126
def __getitem__ (self , loc ):
120
- cdef slice s = self ._ensure_has_slice()
127
+ cdef:
128
+ slice s = self ._ensure_has_slice()
121
129
if s is not None :
122
130
val = slice_getitem(s, loc)
123
131
else :
124
132
val = self ._as_array[loc]
125
133
126
- if not PySlice_Check (val) and val.ndim == 0 :
134
+ if not isinstance (val, slice ) and val.ndim == 0 :
127
135
return val
128
136
129
137
return BlockPlacement(val)
@@ -139,8 +147,9 @@ cdef class BlockPlacement:
139
147
[o.as_array for o in others]))
140
148
141
149
cdef iadd(self , other):
142
- cdef slice s = self ._ensure_has_slice()
143
- cdef Py_ssize_t other_int, start, stop, step, l
150
+ cdef:
151
+ slice s = self ._ensure_has_slice()
152
+ Py_ssize_t other_int, start, stop, step, l
144
153
145
154
if isinstance (other, int ) and s is not None :
146
155
other_int = < Py_ssize_t> other
@@ -184,7 +193,7 @@ cdef class BlockPlacement:
184
193
return self ._as_slice
185
194
186
195
187
- cdef slice_canonize(slice s):
196
+ cdef slice slice_canonize(slice s):
188
197
"""
189
198
Convert slice to canonical bounded form.
190
199
"""
@@ -282,7 +291,7 @@ def slice_getitem(slice slc not None, ind):
282
291
283
292
s_start, s_stop, s_step, s_len = slice_get_indices_ex(slc)
284
293
285
- if PySlice_Check (ind):
294
+ if isinstance (ind, slice ):
286
295
ind_start, ind_stop, ind_step, ind_len = slice_get_indices_ex(ind,
287
296
s_len)
288
297
0 commit comments