Skip to content

Commit 7c6187e

Browse files
committed
refactor cdef-s in .pyx files
1 parent 013ae3d commit 7c6187e

13 files changed

+127
-104
lines changed

pandas/_libs/hashtable.pyx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ include "hashtable_class_helper.pxi"
5252
include "hashtable_func_helper.pxi"
5353

5454
cdef class Factorizer:
55-
cdef public PyObjectHashTable table
56-
cdef public ObjectVector uniques
57-
cdef public Py_ssize_t count
55+
cdef public:
56+
PyObjectHashTable table
57+
ObjectVector uniques
58+
Py_ssize_t count
5859

5960
def __init__(self, size_hint):
6061
self.table = PyObjectHashTable(size_hint)
@@ -96,9 +97,10 @@ cdef class Factorizer:
9697

9798

9899
cdef class Int64Factorizer:
99-
cdef public Int64HashTable table
100-
cdef public Int64Vector uniques
101-
cdef public Py_ssize_t count
100+
cdef public:
101+
Int64HashTable table
102+
Int64Vector uniques
103+
Py_ssize_t count
102104

103105
def __init__(self, size_hint):
104106
self.table = Int64HashTable(size_hint)

pandas/_libs/internals.pyx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ from pandas._libs.algos import ensure_int64
2323

2424
cdef class BlockPlacement:
2525
# __slots__ = '_as_slice', '_as_array', '_len'
26-
cdef slice _as_slice
27-
cdef object _as_array
26+
cdef:
27+
slice _as_slice
28+
object _as_array
2829

29-
cdef bint _has_slice, _has_array, _is_known_slice_like
30+
bint _has_slice, _has_array, _is_known_slice_like
3031

3132
def __init__(self, val):
3233
cdef:

pandas/_libs/lib.pyx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ cdef extern from "numpy/arrayobject.h":
4040
# Use PyDataType_* macros when possible, however there are no macros
4141
# for accessing some of the fields, so some are defined. Please
4242
# ask on cython-dev if you need more.
43-
cdef int type_num
44-
cdef int itemsize "elsize"
45-
cdef char byteorder
46-
cdef object fields
47-
cdef tuple names
43+
cdef:
44+
int type_num
45+
int itemsize "elsize"
46+
char byteorder
47+
object fields
48+
tuple names
4849

4950

5051
cdef extern from "src/parse_helper.h":
@@ -67,12 +68,13 @@ from pandas._libs.missing cimport (
6768

6869
# constants that will be compared to potentially arbitrarily large
6970
# python int
70-
cdef object oINT64_MAX = <int64_t>INT64_MAX
71-
cdef object oINT64_MIN = <int64_t>INT64_MIN
72-
cdef object oUINT64_MAX = <uint64_t>UINT64_MAX
71+
cdef:
72+
object oINT64_MAX = <int64_t>INT64_MAX
73+
object oINT64_MIN = <int64_t>INT64_MIN
74+
object oUINT64_MAX = <uint64_t>UINT64_MAX
7375

74-
cdef bint PY2 = sys.version_info[0] == 2
75-
cdef float64_t NaN = <float64_t>np.NaN
76+
bint PY2 = sys.version_info[0] == 2
77+
float64_t NaN = <float64_t>np.NaN
7678

7779

7880
def values_from_object(obj: object):

pandas/_libs/missing.pyx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ from pandas._libs.tslibs.nattype cimport (
1616
checknull_with_nat, c_NaT as NaT, is_null_datetimelike)
1717

1818

19-
cdef float64_t INF = <float64_t>np.inf
20-
cdef float64_t NEGINF = -INF
19+
cdef:
20+
float64_t INF = <float64_t>np.inf
21+
float64_t NEGINF = -INF
2122

22-
cdef int64_t NPY_NAT = util.get_nat()
23+
int64_t NPY_NAT = util.get_nat()
2324

2425

2526
cpdef bint checknull(object val):

pandas/_libs/parsers.pyx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,11 @@ from pandas.errors import (ParserError, DtypeWarning,
6464
CParserError = ParserError
6565

6666

67-
cdef bint PY3 = (sys.version_info[0] >= 3)
67+
cdef:
68+
bint PY3 = (sys.version_info[0] >= 3)
6869

69-
cdef float64_t INF = <float64_t>np.inf
70-
cdef float64_t NEGINF = -INF
70+
float64_t INF = <float64_t>np.inf
71+
float64_t NEGINF = -INF
7172

7273

7374
cdef extern from "errno.h":
@@ -735,7 +736,7 @@ cdef class TextReader:
735736
int status
736737
int64_t hr, data_line
737738
char *errors = "strict"
738-
cdef StringPath path = _string_path(self.c_encoding)
739+
StringPath path = _string_path(self.c_encoding)
739740

740741
header = []
741742
unnamed_cols = set()
@@ -1389,8 +1390,9 @@ cdef class TextReader:
13891390
return None
13901391

13911392

1392-
cdef object _true_values = [b'True', b'TRUE', b'true']
1393-
cdef object _false_values = [b'False', b'FALSE', b'false']
1393+
cdef:
1394+
object _true_values = [b'True', b'TRUE', b'true']
1395+
object _false_values = [b'False', b'FALSE', b'false']
13941396

13951397

13961398
def _ensure_encoded(list lst):
@@ -1637,7 +1639,7 @@ cdef _categorical_convert(parser_t *parser, int64_t col,
16371639
int64_t current_category = 0
16381640

16391641
char *errors = "strict"
1640-
cdef StringPath path = _string_path(encoding)
1642+
StringPath path = _string_path(encoding)
16411643

16421644
int ret = 0
16431645
kh_str_t *table
@@ -1727,9 +1729,10 @@ cdef inline void _to_fw_string_nogil(parser_t *parser, int64_t col,
17271729
data += width
17281730

17291731

1730-
cdef char* cinf = b'inf'
1731-
cdef char* cposinf = b'+inf'
1732-
cdef char* cneginf = b'-inf'
1732+
cdef:
1733+
char* cinf = b'inf'
1734+
char* cposinf = b'+inf'
1735+
char* cneginf = b'-inf'
17331736

17341737

17351738
cdef _try_double(parser_t *parser, int64_t col,

pandas/_libs/skiplist.pyx

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ cdef class IndexableSkiplist:
5757
return self.get(i)
5858

5959
cpdef get(self, Py_ssize_t i):
60-
cdef Py_ssize_t level
61-
cdef Node node
60+
cdef:
61+
Py_ssize_t level
62+
Node node
6263

6364
node = self.head
6465
i += 1
@@ -71,9 +72,10 @@ cdef class IndexableSkiplist:
7172
return node.value
7273

7374
cpdef insert(self, double value):
74-
cdef Py_ssize_t level, steps, d
75-
cdef Node node, prevnode, newnode, next_at_level, tmp
76-
cdef list chain, steps_at_level
75+
cdef:
76+
Py_ssize_t level, steps, d
77+
Node node, prevnode, newnode, next_at_level, tmp
78+
list chain, steps_at_level
7779

7880
# find first node on each level where node.next[levels].value > value
7981
chain = [None] * self.maxlevels
@@ -110,9 +112,10 @@ cdef class IndexableSkiplist:
110112
self.size += 1
111113

112114
cpdef remove(self, double value):
113-
cdef Py_ssize_t level, d
114-
cdef Node node, prevnode, tmpnode, next_at_level
115-
cdef list chain
115+
cdef:
116+
Py_ssize_t level, d
117+
Node node, prevnode, tmpnode, next_at_level
118+
list chain
116119

117120
# find first node on each level where node.next[levels].value >= value
118121
chain = [None] * self.maxlevels

pandas/_libs/tslibs/parsing.pyx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ class DateParseError(ValueError):
4444
_DEFAULT_DATETIME = datetime(1, 1, 1).replace(hour=0, minute=0,
4545
second=0, microsecond=0)
4646

47-
cdef object _TIMEPAT = re.compile(r'^([01]?[0-9]|2[0-3]):([0-5][0-9])')
47+
cdef:
48+
object _TIMEPAT = re.compile(r'^([01]?[0-9]|2[0-3]):([0-5][0-9])')
4849

49-
cdef set _not_datelike_strings = {'a', 'A', 'm', 'M', 'p', 'P', 't', 'T'}
50+
set _not_datelike_strings = {'a', 'A', 'm', 'M', 'p', 'P', 't', 'T'}
5051

5152
# ----------------------------------------------------------------------
5253

pandas/_libs/tslibs/period.pyx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,10 @@ from pandas._libs.tslibs.nattype cimport (
5252
from pandas._libs.tslibs.offsets cimport to_offset
5353
from pandas._libs.tslibs.offsets import _Tick
5454

55-
cdef bint PY2 = str == bytes
56-
cdef enum:
57-
INT32_MIN = -2147483648
55+
cdef:
56+
bint PY2 = str == bytes
57+
enum:
58+
INT32_MIN = -2147483648
5859

5960

6061
ctypedef struct asfreq_info:

pandas/_libs/tslibs/resolution.pyx

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,16 @@ from pandas._libs.tslibs.ccalendar cimport get_days_in_month
1616
# ----------------------------------------------------------------------
1717
# Constants
1818

19-
cdef int64_t NPY_NAT = get_nat()
20-
21-
cdef int RESO_NS = 0
22-
cdef int RESO_US = 1
23-
cdef int RESO_MS = 2
24-
cdef int RESO_SEC = 3
25-
cdef int RESO_MIN = 4
26-
cdef int RESO_HR = 5
27-
cdef int RESO_DAY = 6
19+
cdef:
20+
int64_t NPY_NAT = get_nat()
21+
22+
int RESO_NS = 0
23+
int RESO_US = 1
24+
int RESO_MS = 2
25+
int RESO_SEC = 3
26+
int RESO_MIN = 4
27+
int RESO_HR = 5
28+
int RESO_DAY = 6
2829

2930
# ----------------------------------------------------------------------
3031

pandas/_libs/window.pyx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,14 @@ from pandas._libs.skiplist cimport (
2626
skiplist_t, skiplist_init, skiplist_destroy, skiplist_get, skiplist_insert,
2727
skiplist_remove)
2828

29-
cdef float32_t MINfloat32 = np.NINF
30-
cdef float64_t MINfloat64 = np.NINF
29+
cdef:
30+
float32_t MINfloat32 = np.NINF
31+
float64_t MINfloat64 = np.NINF
3132

32-
cdef float32_t MAXfloat32 = np.inf
33-
cdef float64_t MAXfloat64 = np.inf
33+
float32_t MAXfloat32 = np.inf
34+
float64_t MAXfloat64 = np.inf
3435

35-
cdef float64_t NaN = <float64_t>np.NaN
36+
float64_t NaN = <float64_t>np.NaN
3637

3738
cdef inline int int_max(int a, int b): return a if a >= b else b
3839
cdef inline int int_min(int a, int b): return a if a <= b else b

pandas/io/msgpack/_packer.pyx

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ cdef class Packer(object):
7474
Use bin type introduced in msgpack spec 2.0 for bytes.
7575
It also enable str8 type for unicode.
7676
"""
77-
cdef msgpack_packer pk
78-
cdef object _default
79-
cdef object _bencoding
80-
cdef object _berrors
81-
cdef char *encoding
82-
cdef char *unicode_errors
83-
cdef bint use_float
84-
cdef bint autoreset
77+
cdef:
78+
msgpack_packer pk
79+
object _default
80+
object _bencoding
81+
object _berrors
82+
char *encoding
83+
char *unicode_errors
84+
bint use_float
85+
bint autoreset
8586

8687
def __cinit__(self):
8788
cdef int buf_size = 1024 * 1024
@@ -123,16 +124,17 @@ cdef class Packer(object):
123124

124125
cdef int _pack(self, object o,
125126
int nest_limit=DEFAULT_RECURSE_LIMIT) except -1:
126-
cdef long long llval
127-
cdef unsigned long long ullval
128-
cdef long longval
129-
cdef float fval
130-
cdef double dval
131-
cdef char* rawval
132-
cdef int ret
133-
cdef dict d
134-
cdef size_t L
135-
cdef int default_used = 0
127+
cdef:
128+
long long llval
129+
unsigned long long ullval
130+
long longval
131+
float fval
132+
double dval
133+
char* rawval
134+
int ret
135+
dict d
136+
size_t L
137+
int default_used = 0
136138

137139
if nest_limit < 0:
138140
raise PackValueError("recursion limit exceeded.")

pandas/io/msgpack/_unpacker.pyx

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,15 @@ def unpackb(object packed, object object_hook=None, object list_hook=None,
120120
121121
See :class:`Unpacker` for options.
122122
"""
123-
cdef unpack_context ctx
124-
cdef size_t off = 0
125-
cdef int ret
123+
cdef:
124+
unpack_context ctx
125+
size_t off = 0
126+
int ret
126127

127-
cdef char* buf
128-
cdef Py_ssize_t buf_len
129-
cdef char* cenc = NULL
130-
cdef char* cerr = NULL
128+
char* buf
129+
Py_ssize_t buf_len
130+
char* cenc = NULL
131+
char* cerr = NULL
131132

132133
PyObject_AsReadBuffer(packed, <const void**>&buf, &buf_len)
133134

@@ -243,16 +244,17 @@ cdef class Unpacker(object):
243244
for o in unpacker:
244245
process(o)
245246
"""
246-
cdef unpack_context ctx
247-
cdef char* buf
248-
cdef size_t buf_size, buf_head, buf_tail
249-
cdef object file_like
250-
cdef object file_like_read
251-
cdef Py_ssize_t read_size
252-
# To maintain refcnt.
253-
cdef object object_hook, object_pairs_hook, list_hook, ext_hook
254-
cdef object encoding, unicode_errors
255-
cdef size_t max_buffer_size
247+
cdef:
248+
unpack_context ctx
249+
char* buf
250+
size_t buf_size, buf_head, buf_tail
251+
object file_like
252+
object file_like_read
253+
Py_ssize_t read_size
254+
# To maintain refcnt.
255+
object object_hook, object_pairs_hook, list_hook, ext_hook
256+
object encoding, unicode_errors
257+
size_t max_buffer_size
256258

257259
def __cinit__(self):
258260
self.buf = NULL
@@ -270,8 +272,9 @@ cdef class Unpacker(object):
270272
Py_ssize_t max_array_len=2147483647,
271273
Py_ssize_t max_map_len=2147483647,
272274
Py_ssize_t max_ext_len=2147483647):
273-
cdef char *cenc=NULL,
274-
cdef char *cerr=NULL
275+
cdef:
276+
char *cenc=NULL,
277+
char *cerr=NULL
275278

276279
self.object_hook = object_hook
277280
self.object_pairs_hook = object_pairs_hook
@@ -388,9 +391,10 @@ cdef class Unpacker(object):
388391

389392
cdef object _unpack(self, execute_fn execute,
390393
object write_bytes, bint iter=0):
391-
cdef int ret
392-
cdef object obj
393-
cdef size_t prev_head
394+
cdef:
395+
int ret
396+
object obj
397+
size_t prev_head
394398

395399
if self.buf_head >= self.buf_tail and self.file_like is not None:
396400
self.read_from_file()

0 commit comments

Comments
 (0)