I'm trying to profile my Cython code using the line_profile package (The code runs but I want to know if I can optimize it..) but when I try to profile it I get an AttributeError: 'builtin_function_or_method' object has no attribute '__code__'. (I've tried to get some help from this and this)

This is my cython_agri.pyx file:

# cython: linetrace=True
cimport cython
import numpy as np
from datetime import timedelta, datetime


@cython.binding(True)
cpdef list read_static_binary_data(data_row: list, read_point: int, binary_data: object, tlg_dict: dict, dt: np.dtype,
                                    start_date: datetime):
    """Function to obtain the position and time data """
    cdef int nr_d = 3
    cdef float ten_minus_seven = pow(10, -7)
    ....

This is my setup.py

import setuptools
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Compiler.Options import get_directive_defaults
directive_defaults = get_directive_defaults()
directive_defaults['linetrace'] = True
directive_defaults['binding'] = True

extensions = [
    Extension("pyAgriculture", ["pyAgriculture\cython_agri.pyx"], define_macros=[('CYTHON_TRACE', '1')])
]
setup(ext_modules=cythonize(extensions, language_level="3"))

I compile the code using this command: py pyAgriculture\setup.py build_ext --inplace. This is from my main_python.py file where I run the code:

...
from cython_agri import read_static_binary_data
read_static_binary_data = profile(read_static_binary_data)
data_row, nr_dlvs, nr_static = read_static_binary_data(data_row, read_point, binary_data, tlg_dict, self.dt, self.start_date)

When I try to run the profiler with: kernprof -l agriculture.py I get this error

  File "agriculture.py", line 301, in read_binaryfile
    read_static_binary_data = profile(read_static_binary_data)
  File "C:\dev\program\pyAgriculture11783\venv\lib\site-packages\line_profiler\line_profiler.py", line 53, in __call__
    elif is_generator(func):
  File "C:\dev\program\pyAgriculture11783\venv\lib\site-packages\line_profiler\line_profiler.py", line 38, in is_generator
    isgen = (f.__code__.co_flags & CO_GENERATOR) != 0
AttributeError: 'builtin_function_or_method' object has no attribute '__code__'. Did you mean: '__call__'?

Any ideas what I might have done wrong?

0

There are 0 answers