Skip to content

Commit 4fc75bd

Browse files
committed
refactor cdef-s in .pyx files
1 parent 013ae3d commit 4fc75bd

14 files changed

+133
-122
lines changed

pandas/_libs/hashtable.pyx

+8-6
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

+4-3
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+
cdef 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

+16-14
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,17 +68,18 @@ 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):
7981
""" return my values or the object if we are say an ndarray """
80-
func: object
82+
cdef object func
8183

8284
func = getattr(obj, 'get_values', None)
8385
if func is not None:
@@ -91,9 +93,9 @@ def values_from_object(obj: object):
9193
def memory_usage_of_objects(arr: object[:]) -> int64_t:
9294
""" return the memory usage of an object array in bytes,
9395
does not include the actual bytes of the pointers """
94-
i: Py_ssize_t
95-
n: Py_ssize_t
96-
size: int64_t
96+
cdef:
97+
Py_ssize_t i, n
98+
int64_t size
9799

98100
size = 0
99101
n = len(arr)

pandas/_libs/missing.pyx

+4-3
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

+13-10
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

+11-16
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@ cdef double Log2(double x):
2121
# TODO: optimize this, make less messy
2222

2323
cdef class Node:
24-
# cdef public:
25-
# double value
26-
# list next
27-
# list width
28-
2924
def __init__(self, double value, list next, list width):
3025
self.value = value
3126
self.next = next
@@ -41,9 +36,6 @@ cdef class IndexableSkiplist:
4136
Sorted collection supporting O(lg n) insertion, removal, and
4237
lookup by rank.
4338
"""
44-
# cdef:
45-
# Py_ssize_t size, maxlevels
46-
# Node head
4739

4840
def __init__(self, expected_size=100):
4941
self.size = 0
@@ -57,8 +49,9 @@ cdef class IndexableSkiplist:
5749
return self.get(i)
5850

5951
cpdef get(self, Py_ssize_t i):
60-
cdef Py_ssize_t level
61-
cdef Node node
52+
cdef:
53+
Py_ssize_t level
54+
Node node
6255

6356
node = self.head
6457
i += 1
@@ -71,9 +64,10 @@ cdef class IndexableSkiplist:
7164
return node.value
7265

7366
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
67+
cdef:
68+
Py_ssize_t level, steps, d
69+
Node node, prevnode, newnode, next_at_level, tmp
70+
list chain, steps_at_level
7771

7872
# find first node on each level where node.next[levels].value > value
7973
chain = [None] * self.maxlevels
@@ -110,9 +104,10 @@ cdef class IndexableSkiplist:
110104
self.size += 1
111105

112106
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
107+
cdef:
108+
Py_ssize_t level, d
109+
Node node, prevnode, tmpnode, next_at_level
110+
list chain
116111

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

pandas/_libs/tslibs/nattype.pyx

-4
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,6 @@ def __nat_unpickle(*args):
9090

9191

9292
cdef class _NaT(datetime):
93-
# cdef readonly:
94-
# int64_t value
95-
# object freq
96-
9793
def __hash__(_NaT self):
9894
# py3k needs this defined here
9995
return hash(self.value)

pandas/_libs/tslibs/parsing.pyx

+3-2
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

+4-3
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

+10-9
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

+8-7
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ 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

37-
cdef inline int int_max(int a, int b): return a if a >= b else b
38-
cdef inline int int_min(int a, int b): return a if a <= b else b
38+
inline int int_max(int a, int b): return a if a >= b else b
39+
inline int int_min(int a, int b): return a if a <= b else b
3940

4041

4142
# Cython implementations of rolling sum, mean, variance, skewness,

pandas/io/msgpack/_packer.pyx

+20-18
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+
cdef bint use_float
85+
cdef 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.")

0 commit comments

Comments
 (0)