# Generates and installs simple Cython modules with specified settings. If you are overwhelmed by the compiler directives and the creation of the setup.py file, this is right for you.
## Tested against Windows 10 / Python 3.11 / Anaconda
### pip install cythonautoinstall
This function takes Cython code along with various setup parameters and generates a
Cython module, compiles it, and installs it as a Python package. It handles the
generation of the Cython code, the creation of a setup script, compilation, and
installation. Made for people who write simple scripts and are overwhelmed
by the compiler directives and the creation of the setup.py file
```python
Parameters:
cython_code (str): The Cython code to be compiled into a module.
foldername (str): The name of the folder where the package will be installed.
setup_name (str): The name of the module to be generated.
setup_sources (tuple or list): A tuple or list of source file paths.
setup_include_dirs (tuple or list): A tuple or list of include directory paths.
setup_py_limited_api (bool, optional): Whether to use Python limited API. Default is False.
setup_define_macros (tuple or list, optional): Define macros for the compilation. Default is ().
setup_undef_macros (tuple or list, optional): Undefine macros for the compilation. Default is ().
setup_library_dirs (tuple or list, optional): Library directories. Default is ().
setup_libraries (tuple or list, optional): Libraries to link against. Default is ().
setup_runtime_library_dirs (tuple or list, optional): Runtime library directories. Default is ().
setup_extra_objects (tuple or list, optional): Extra objects to link with. Default is ().
setup_extra_compile_args (tuple or list, optional): Extra compilation arguments. Default is ().
setup_extra_link_args (tuple or list, optional): Extra link arguments. Default is ().
setup_export_symbols (tuple or list, optional): Exported symbols. Default is ().
setup_swig_opts (tuple or list, optional): SWIG options. Default is ().
setup_depends (tuple or list, optional): Dependency files. Default is ().
setup_language (str, optional): Language for compilation. Default is None.
setup_optional (tuple or list, optional): Optional components. Default is None.
extra_directives (str, optional): Additional Cython compiler directives. Default is "".
distutils_extra_compile_args (str, optional): Additional distutils compilation arguments. Default is "".
distutils_extra_link_args (str, optional): Additional distutils link arguments. Default is "".
distutils_language (str, optional): Language for distutils compilation. Default is "".
distutils_define_macros (str, optional): Define macros for distutils compilation. Default is "NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION".
cython_binding (bool, optional): Whether to generate Cython bindings. Default is True.
cython_boundscheck (bool, optional): Enable bounds checking. Default is True.
cython_wraparound (bool, optional): Enable wraparound checking. Default is True.
cython_initializedcheck (bool, optional): Enable initialized variable checking. Default is False.
cython_nonecheck (bool, optional): Enable None checking. Default is False.
cython_overflowcheck (bool, optional): Enable overflow checking. Default is False.
cython_overflowcheck_fold (bool, optional): Enable folding of overflow checks. Default is True.
cython_embedsignature (bool, optional): Embed function signatures. Default is False.
cython_embedsignature_format (str, optional): Format for embedded signatures. Default is "c".
cython_cdivision (bool, optional): Enable C division. Default is False.
cython_cdivision_warnings (bool, optional): Enable C division warnings. Default is False.
cython_cpow (bool, optional): Enable C pow function. Default is False.
cython_c_api_binop_methods (bool, optional): Enable C API binary operator methods. Default is False.
cython_profile (bool, optional): Enable profiling. Default is False.
cython_linetrace (bool, optional): Enable line tracing. Default is False.
cython_infer_types (bool, optional): Enable type inference. Default is False.
cython_language_level (int, optional): Cython language level. Default is 3.
cython_c_string_type (str, optional): C string type. Default is "bytes".
cython_c_string_encoding (str, optional): C string encoding. Default is "default".
cython_type_version_tag (bool, optional): Enable type version tag. Default is True.
cython_unraisable_tracebacks (bool, optional): Enable unraisable tracebacks. Default is False.
cython_iterable_coroutine (bool, optional): Enable iterable coroutine support. Default is True.
cython_annotation_typing (bool, optional): Enable annotation typing. Default is True.
cython_emit_code_comments (bool, optional): Enable code comments in the generated code. Default is False.
cython_cpp_locals (bool, optional): Enable C++ local variable support. Default is False.
Returns:
str: The path to the generated package's __init__.py file.
Note:
Read https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html#compiler-directives
Example:
import numpy as np
cython_code = '''
from cython.parallel cimport prange
cimport cython
import numpy as np
cimport numpy as np
import cython
from collections import defaultdict
cpdef searchforcolor(unsigned char[:] pic, unsigned char[:] colors, int width, int totallengthpic, int totallengthcolor):
cdef my_dict = defaultdict(list)
cdef int i, j
cdef unsigned char r,g,b
for i in prange(0, totallengthcolor, 3,nogil=True):
r = colors[i]
g = colors[i + 1]
b = colors[i + 2]
for j in range(0, totallengthpic, 3):
if (r == pic[j]) and (g == pic[j+1]) and (b == pic[j+2]):
with gil:
my_dict[(r,g,b)].append(j )
for key in my_dict.keys():
my_dict[key] = np.dstack(np.divmod(np.array(my_dict[key]) // 3, width))[0]
return my_dict
'''
fx = generate_auto_install(
cython_code,
foldername="locatepixelcolcompiledpy",
setup_py_limited_api=False,
setup_name="locatepixelcolorcompiled",
setup_sources=("locatepixelcolorcompiled.pyx",),
setup_include_dirs=(np.get_include(),),
setup_define_macros=(),
setup_undef_macros=(),
setup_library_dirs=(),
setup_libraries=(),
setup_runtime_library_dirs=(),
setup_extra_objects=(),
setup_extra_compile_args=(),
setup_extra_link_args=(),
setup_export_symbols=(),
setup_swig_opts=(),
setup_depends=(),
setup_language=None,
setup_optional=None,
extra_directives="",
distutils_extra_compile_args="/openmp",
distutils_extra_link_args="/openmp",
distutils_language="c",
distutils_define_macros="NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION",
cython_binding=False,
cython_boundscheck=False,
cython_wraparound=False,
cython_initializedcheck=False,
cython_nonecheck=False,
cython_overflowcheck=True,
cython_overflowcheck_fold=False,
cython_embedsignature=False,
cython_embedsignature_format="c",
cython_cdivision=True,
cython_cdivision_warnings=False,
cython_cpow=True,
cython_c_api_binop_methods=True,
cython_profile=False,
cython_linetrace=False,
cython_infer_types=False,
cython_language_level=3,
cython_c_string_type="bytes",
cython_c_string_encoding="default",
cython_type_version_tag=True,
cython_unraisable_tracebacks=False,
cython_iterable_coroutine=True,
cython_annotation_typing=True,
cython_emit_code_comments=False,
cython_cpp_locals=True,
)
print(fx)
# create a Python file
r'''
import numpy as np
import cv2
import locatepixelcolcompiledpy
def search_colors(pic,colors):
if not isinstance(colors, np.ndarray):
colors = np.array(colors, dtype=np.uint8)
pipi = pic.ravel()
cololo = colors.ravel()
totallengthcolor = cololo.shape[0] - 1
totallenghtpic = pipi.shape[0]-1
width = pic.shape[1]
resus0 = locatepixelcolcompiledpy.locatepixelcolorcompiled.searchforcolor(pipi, cololo,width,totallenghtpic,totallengthcolor)
return resus0
# 4525 x 6623 x 3 picture https://www.pexels.com/pt-br/foto/foto-da-raposa-sentada-no-chao-2295744/
picx = r"C:\Users\hansc\Downloads\pexels-alex-andrews-2295744.jpg"
pic = cv2.imread(picx)
colors0 = np.array([[255, 255, 255]],dtype=np.uint8)
resus0 = search_colors(pic=pic, colors=colors0)
colors1=np.array([(66, 71, 69),(62, 67, 65),(144, 155, 153),(52, 57, 55),(127, 138, 136),(53, 58, 56),(51, 56, 54),(32, 27, 18),(24, 17, 8),],dtype=np.uint8)
resus1 = search_colors(pic=pic, colors=colors1)
'''
This is how a generated file looks like (it can be edited after the generation):
import os
import subprocess
import sys
def _dummyimport():
import Cython
try:
from . import locatepixelcolorcompiled
except Exception as e:
cstring = '''# distutils: language=c
# distutils: extra_compile_args=/openmp
# distutils: extra_link_args=/openmp
# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION
# cython: binding=False
# cython: boundscheck=False
# cython: wraparound=False
# cython: initializedcheck=False
# cython: nonecheck=False
# cython: overflowcheck=True
# cython: overflowcheck.fold=False
# cython: embedsignature=False
# cython: embedsignature.format=c
# cython: cdivision=True
# cython: cdivision_warnings=False
# cython: cpow=True
# cython: c_api_binop_methods=True
# cython: profile=False
# cython: linetrace=False
# cython: infer_types=False
# cython: language_level=3
# cython: c_string_type=bytes
# cython: c_string_encoding=default
# cython: type_version_tag=True
# cython: unraisable_tracebacks=False
# cython: iterable_coroutine=True
# cython: annotation_typing=True
# cython: emit_code_comments=False
# cython: cpp_locals=True
from cython.parallel cimport prange
cimport cython
import numpy as np
cimport numpy as np
import cython
from collections import defaultdict
cpdef searchforcolor(unsigned char[:] pic, unsigned char[:] colors, int width, int totallengthpic, int totallengthcolor):
cdef my_dict = defaultdict(list)
cdef int i, j
cdef unsigned char r,g,b
for i in prange(0, totallengthcolor, 3,nogil=True):
r = colors[i]
g = colors[i + 1]
b = colors[i + 2]
for j in range(0, totallengthpic, 3):
if (r == pic[j]) and (g == pic[j+1]) and (b == pic[j+2]):
with gil:
my_dict[(r,g,b)].append(j )
for key in my_dict.keys():
my_dict[key] = np.dstack(np.divmod(np.array(my_dict[key]) // 3, width))[0]
return my_dict'''
pyxfile = f"locatepixelcolorcompiled.pyx"
pyxfilesetup = f"locatepixelcolorcompiled_setup.py"
dirname = os.path.abspath(os.path.dirname(__file__))
pyxfile_complete_path = os.path.join(dirname, pyxfile)
pyxfile_setup_complete_path = os.path.join(dirname, pyxfilesetup)
if os.path.exists(pyxfile_complete_path):
os.remove(pyxfile_complete_path)
if os.path.exists(pyxfile_setup_complete_path):
os.remove(pyxfile_setup_complete_path)
with open(pyxfile_complete_path, mode="w", encoding="utf-8") as f:
f.write(cstring)
compilefile = '''
from setuptools import Extension, setup
from Cython.Build import cythonize
ext_modules = Extension(**{'py_limited_api': False, 'name': 'locatepixelcolorcompiled', 'sources': ['locatepixelcolorcompiled.pyx'], 'include_dirs': ['C:\\Users\\hansc\\.conda\\envs\\dfdir\\Lib\\site-packages\\numpy\\core\\include'], 'define_macros': [], 'undef_macros': [], 'library_dirs': [], 'libraries': [], 'runtime_library_dirs': [], 'extra_objects': [], 'extra_compile_args': [], 'extra_link_args': [], 'export_symbols': [], 'swig_opts': [], 'depends': [], 'language': None, 'optional': None})
setup(
name='locatepixelcolorcompiled',
ext_modules=cythonize(ext_modules),
)
'''
with open(pyxfile_setup_complete_path, mode="w", encoding="utf-8") as f:
f.write('\n'.join([x.lstrip().replace(os.sep, "/") for x in compilefile.splitlines()]))
subprocess.run(
[sys.executable, pyxfile_setup_complete_path, "build_ext", "--inplace"],
cwd=dirname,
shell=True,
env=os.environ.copy(),
)
from . import locatepixelcolorcompiled
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/cythonautoinstall",
"name": "cythonautoinstall",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "cython,compile,automatic",
"author": "Johannes Fischer",
"author_email": "aulasparticularesdealemaosp@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/06/82/b123f204477965a17751b60ad5cf5c54731539bd12553ddc5de215f8fb40/cythonautoinstall-0.10.tar.gz",
"platform": null,
"description": "\r\n# Generates and installs simple Cython modules with specified settings. If you are overwhelmed by the compiler directives and the creation of the setup.py file, this is right for you. \r\n\r\n## Tested against Windows 10 / Python 3.11 / Anaconda\r\n\r\n### pip install cythonautoinstall\r\n\r\n\r\nThis function takes Cython code along with various setup parameters and generates a\r\nCython module, compiles it, and installs it as a Python package. It handles the\r\ngeneration of the Cython code, the creation of a setup script, compilation, and\r\ninstallation. Made for people who write simple scripts and are overwhelmed \r\nby the compiler directives and the creation of the setup.py file\r\n\r\n\r\n```python\r\nParameters:\r\n\tcython_code (str): The Cython code to be compiled into a module.\r\n\tfoldername (str): The name of the folder where the package will be installed.\r\n\tsetup_name (str): The name of the module to be generated.\r\n\tsetup_sources (tuple or list): A tuple or list of source file paths.\r\n\tsetup_include_dirs (tuple or list): A tuple or list of include directory paths.\r\n\tsetup_py_limited_api (bool, optional): Whether to use Python limited API. Default is False.\r\n\tsetup_define_macros (tuple or list, optional): Define macros for the compilation. Default is ().\r\n\tsetup_undef_macros (tuple or list, optional): Undefine macros for the compilation. Default is ().\r\n\tsetup_library_dirs (tuple or list, optional): Library directories. Default is ().\r\n\tsetup_libraries (tuple or list, optional): Libraries to link against. Default is ().\r\n\tsetup_runtime_library_dirs (tuple or list, optional): Runtime library directories. Default is ().\r\n\tsetup_extra_objects (tuple or list, optional): Extra objects to link with. Default is ().\r\n\tsetup_extra_compile_args (tuple or list, optional): Extra compilation arguments. Default is ().\r\n\tsetup_extra_link_args (tuple or list, optional): Extra link arguments. Default is ().\r\n\tsetup_export_symbols (tuple or list, optional): Exported symbols. Default is ().\r\n\tsetup_swig_opts (tuple or list, optional): SWIG options. Default is ().\r\n\tsetup_depends (tuple or list, optional): Dependency files. Default is ().\r\n\tsetup_language (str, optional): Language for compilation. Default is None.\r\n\tsetup_optional (tuple or list, optional): Optional components. Default is None.\r\n\textra_directives (str, optional): Additional Cython compiler directives. Default is \"\".\r\n\tdistutils_extra_compile_args (str, optional): Additional distutils compilation arguments. Default is \"\".\r\n\tdistutils_extra_link_args (str, optional): Additional distutils link arguments. Default is \"\".\r\n\tdistutils_language (str, optional): Language for distutils compilation. Default is \"\".\r\n\tdistutils_define_macros (str, optional): Define macros for distutils compilation. Default is \"NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION\".\r\n\tcython_binding (bool, optional): Whether to generate Cython bindings. Default is True.\r\n\tcython_boundscheck (bool, optional): Enable bounds checking. Default is True.\r\n\tcython_wraparound (bool, optional): Enable wraparound checking. Default is True.\r\n\tcython_initializedcheck (bool, optional): Enable initialized variable checking. Default is False.\r\n\tcython_nonecheck (bool, optional): Enable None checking. Default is False.\r\n\tcython_overflowcheck (bool, optional): Enable overflow checking. Default is False.\r\n\tcython_overflowcheck_fold (bool, optional): Enable folding of overflow checks. Default is True.\r\n\tcython_embedsignature (bool, optional): Embed function signatures. Default is False.\r\n\tcython_embedsignature_format (str, optional): Format for embedded signatures. Default is \"c\".\r\n\tcython_cdivision (bool, optional): Enable C division. Default is False.\r\n\tcython_cdivision_warnings (bool, optional): Enable C division warnings. Default is False.\r\n\tcython_cpow (bool, optional): Enable C pow function. Default is False.\r\n\tcython_c_api_binop_methods (bool, optional): Enable C API binary operator methods. Default is False.\r\n\tcython_profile (bool, optional): Enable profiling. Default is False.\r\n\tcython_linetrace (bool, optional): Enable line tracing. Default is False.\r\n\tcython_infer_types (bool, optional): Enable type inference. Default is False.\r\n\tcython_language_level (int, optional): Cython language level. Default is 3.\r\n\tcython_c_string_type (str, optional): C string type. Default is \"bytes\".\r\n\tcython_c_string_encoding (str, optional): C string encoding. Default is \"default\".\r\n\tcython_type_version_tag (bool, optional): Enable type version tag. Default is True.\r\n\tcython_unraisable_tracebacks (bool, optional): Enable unraisable tracebacks. Default is False.\r\n\tcython_iterable_coroutine (bool, optional): Enable iterable coroutine support. Default is True.\r\n\tcython_annotation_typing (bool, optional): Enable annotation typing. Default is True.\r\n\tcython_emit_code_comments (bool, optional): Enable code comments in the generated code. Default is False.\r\n\tcython_cpp_locals (bool, optional): Enable C++ local variable support. Default is False.\r\n\r\nReturns:\r\n\tstr: The path to the generated package's __init__.py file.\r\n\r\nNote:\r\n\tRead https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html#compiler-directives\r\n\r\nExample:\r\n\r\nimport numpy as np\r\n\r\ncython_code = '''\r\nfrom cython.parallel cimport prange\r\ncimport cython\r\nimport numpy as np\r\ncimport numpy as np\r\nimport cython\r\nfrom collections import defaultdict\r\n\r\ncpdef searchforcolor(unsigned char[:] pic, unsigned char[:] colors, int width, int totallengthpic, int totallengthcolor):\r\n\tcdef my_dict = defaultdict(list)\r\n\tcdef int i, j\r\n\tcdef unsigned char r,g,b\r\n\tfor i in prange(0, totallengthcolor, 3,nogil=True):\r\n\t\tr = colors[i]\r\n\t\tg = colors[i + 1]\r\n\t\tb = colors[i + 2]\r\n\t\tfor j in range(0, totallengthpic, 3):\r\n\t\t\tif (r == pic[j]) and (g == pic[j+1]) and (b == pic[j+2]):\r\n\t\t\t\twith gil:\r\n\t\t\t\t\tmy_dict[(r,g,b)].append(j )\r\n\r\n\tfor key in my_dict.keys():\r\n\t\tmy_dict[key] = np.dstack(np.divmod(np.array(my_dict[key]) // 3, width))[0]\r\n\treturn my_dict\r\n'''\r\n\r\nfx = generate_auto_install(\r\n\tcython_code,\r\n\tfoldername=\"locatepixelcolcompiledpy\",\r\n\tsetup_py_limited_api=False,\r\n\tsetup_name=\"locatepixelcolorcompiled\",\r\n\tsetup_sources=(\"locatepixelcolorcompiled.pyx\",),\r\n\tsetup_include_dirs=(np.get_include(),),\r\n\tsetup_define_macros=(),\r\n\tsetup_undef_macros=(),\r\n\tsetup_library_dirs=(),\r\n\tsetup_libraries=(),\r\n\tsetup_runtime_library_dirs=(),\r\n\tsetup_extra_objects=(),\r\n\tsetup_extra_compile_args=(),\r\n\tsetup_extra_link_args=(),\r\n\tsetup_export_symbols=(),\r\n\tsetup_swig_opts=(),\r\n\tsetup_depends=(),\r\n\tsetup_language=None,\r\n\tsetup_optional=None,\r\n\textra_directives=\"\",\r\n\tdistutils_extra_compile_args=\"/openmp\",\r\n\tdistutils_extra_link_args=\"/openmp\",\r\n\tdistutils_language=\"c\",\r\n\tdistutils_define_macros=\"NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION\",\r\n\tcython_binding=False,\r\n\tcython_boundscheck=False,\r\n\tcython_wraparound=False,\r\n\tcython_initializedcheck=False,\r\n\tcython_nonecheck=False,\r\n\tcython_overflowcheck=True,\r\n\tcython_overflowcheck_fold=False,\r\n\tcython_embedsignature=False,\r\n\tcython_embedsignature_format=\"c\",\r\n\tcython_cdivision=True,\r\n\tcython_cdivision_warnings=False,\r\n\tcython_cpow=True,\r\n\tcython_c_api_binop_methods=True,\r\n\tcython_profile=False,\r\n\tcython_linetrace=False,\r\n\tcython_infer_types=False,\r\n\tcython_language_level=3,\r\n\tcython_c_string_type=\"bytes\",\r\n\tcython_c_string_encoding=\"default\",\r\n\tcython_type_version_tag=True,\r\n\tcython_unraisable_tracebacks=False,\r\n\tcython_iterable_coroutine=True,\r\n\tcython_annotation_typing=True,\r\n\tcython_emit_code_comments=False,\r\n\tcython_cpp_locals=True,\r\n)\r\nprint(fx)\r\n\r\n# create a Python file\r\nr'''\r\nimport numpy as np\r\nimport cv2\r\nimport locatepixelcolcompiledpy\r\n\r\ndef search_colors(pic,colors):\r\n\tif not isinstance(colors, np.ndarray):\r\n\t\tcolors = np.array(colors, dtype=np.uint8)\r\n\tpipi = pic.ravel()\r\n\tcololo = colors.ravel()\r\n\ttotallengthcolor = cololo.shape[0] - 1\r\n\ttotallenghtpic = pipi.shape[0]-1\r\n\twidth = pic.shape[1]\r\n\tresus0 = locatepixelcolcompiledpy.locatepixelcolorcompiled.searchforcolor(pipi, cololo,width,totallenghtpic,totallengthcolor)\r\n\treturn resus0\r\n\r\n\r\n# 4525 x 6623 x 3 picture https://www.pexels.com/pt-br/foto/foto-da-raposa-sentada-no-chao-2295744/\r\npicx = r\"C:\\Users\\hansc\\Downloads\\pexels-alex-andrews-2295744.jpg\"\r\npic = cv2.imread(picx)\r\ncolors0 = np.array([[255, 255, 255]],dtype=np.uint8)\r\nresus0 = search_colors(pic=pic, colors=colors0)\r\ncolors1=np.array([(66, 71, 69),(62, 67, 65),(144, 155, 153),(52, 57, 55),(127, 138, 136),(53, 58, 56),(51, 56, 54),(32, 27, 18),(24, 17, 8),],dtype=np.uint8)\r\nresus1 = search_colors(pic=pic, colors=colors1)\r\n'''\r\nThis is how a generated file looks like (it can be edited after the generation):\r\n\r\n\r\n\timport os\r\n\timport subprocess\r\n\timport sys\r\n\r\n\tdef _dummyimport():\r\n\t\timport Cython\r\n\ttry:\r\n\t\tfrom . import locatepixelcolorcompiled\r\n\texcept Exception as e:\r\n\r\n\t\tcstring = '''# distutils: language=c\r\n\t# distutils: extra_compile_args=/openmp\r\n\t# distutils: extra_link_args=/openmp\r\n\t# distutils: define_macros=NPY_NO_DEPRECATED_API=NPY_1_7_API_VERSION\r\n\t# cython: binding=False\r\n\t# cython: boundscheck=False\r\n\t# cython: wraparound=False\r\n\t# cython: initializedcheck=False\r\n\t# cython: nonecheck=False\r\n\t# cython: overflowcheck=True\r\n\t# cython: overflowcheck.fold=False\r\n\t# cython: embedsignature=False\r\n\t# cython: embedsignature.format=c\r\n\t# cython: cdivision=True\r\n\t# cython: cdivision_warnings=False\r\n\t# cython: cpow=True\r\n\t# cython: c_api_binop_methods=True\r\n\t# cython: profile=False\r\n\t# cython: linetrace=False\r\n\t# cython: infer_types=False\r\n\t# cython: language_level=3\r\n\t# cython: c_string_type=bytes\r\n\t# cython: c_string_encoding=default\r\n\t# cython: type_version_tag=True\r\n\t# cython: unraisable_tracebacks=False\r\n\t# cython: iterable_coroutine=True\r\n\t# cython: annotation_typing=True\r\n\t# cython: emit_code_comments=False\r\n\t# cython: cpp_locals=True\r\n\r\n\r\n\tfrom cython.parallel cimport prange\r\n\tcimport cython\r\n\timport numpy as np\r\n\tcimport numpy as np\r\n\timport cython\r\n\tfrom collections import defaultdict\r\n\r\n\tcpdef searchforcolor(unsigned char[:] pic, unsigned char[:] colors, int width, int totallengthpic, int totallengthcolor):\r\n\t\tcdef my_dict = defaultdict(list)\r\n\t\tcdef int i, j\r\n\t\tcdef unsigned char r,g,b\r\n\t\tfor i in prange(0, totallengthcolor, 3,nogil=True):\r\n\t\t\tr = colors[i]\r\n\t\t\tg = colors[i + 1]\r\n\t\t\tb = colors[i + 2]\r\n\t\t\tfor j in range(0, totallengthpic, 3):\r\n\t\t\t\tif (r == pic[j]) and (g == pic[j+1]) and (b == pic[j+2]):\r\n\t\t\t\t\twith gil:\r\n\t\t\t\t\t\tmy_dict[(r,g,b)].append(j )\r\n\r\n\t\tfor key in my_dict.keys():\r\n\t\t\tmy_dict[key] = np.dstack(np.divmod(np.array(my_dict[key]) // 3, width))[0]\r\n\t\treturn my_dict'''\r\n\t\tpyxfile = f\"locatepixelcolorcompiled.pyx\"\r\n\t\tpyxfilesetup = f\"locatepixelcolorcompiled_setup.py\"\r\n\r\n\t\tdirname = os.path.abspath(os.path.dirname(__file__))\r\n\t\tpyxfile_complete_path = os.path.join(dirname, pyxfile)\r\n\t\tpyxfile_setup_complete_path = os.path.join(dirname, pyxfilesetup)\r\n\r\n\t\tif os.path.exists(pyxfile_complete_path):\r\n\t\t\tos.remove(pyxfile_complete_path)\r\n\t\tif os.path.exists(pyxfile_setup_complete_path):\r\n\t\t\tos.remove(pyxfile_setup_complete_path)\r\n\t\twith open(pyxfile_complete_path, mode=\"w\", encoding=\"utf-8\") as f:\r\n\t\t\tf.write(cstring)\r\n\r\n\t\tcompilefile = '''\r\n\t\tfrom setuptools import Extension, setup\r\n\t\tfrom Cython.Build import cythonize\r\n\t\text_modules = Extension(**{'py_limited_api': False, 'name': 'locatepixelcolorcompiled', 'sources': ['locatepixelcolorcompiled.pyx'], 'include_dirs': ['C:\\\\Users\\\\hansc\\\\.conda\\\\envs\\\\dfdir\\\\Lib\\\\site-packages\\\\numpy\\\\core\\\\include'], 'define_macros': [], 'undef_macros': [], 'library_dirs': [], 'libraries': [], 'runtime_library_dirs': [], 'extra_objects': [], 'extra_compile_args': [], 'extra_link_args': [], 'export_symbols': [], 'swig_opts': [], 'depends': [], 'language': None, 'optional': None})\r\n\r\n\t\tsetup(\r\n\t\t\tname='locatepixelcolorcompiled',\r\n\t\t\text_modules=cythonize(ext_modules),\r\n\t\t)\r\n\t\t\t\t'''\r\n\t\twith open(pyxfile_setup_complete_path, mode=\"w\", encoding=\"utf-8\") as f:\r\n\t\t\tf.write('\\n'.join([x.lstrip().replace(os.sep, \"/\") for x in compilefile.splitlines()]))\r\n\t\tsubprocess.run(\r\n\t\t\t[sys.executable, pyxfile_setup_complete_path, \"build_ext\", \"--inplace\"],\r\n\t\t\tcwd=dirname,\r\n\t\t\tshell=True,\r\n\t\t\tenv=os.environ.copy(),\r\n\t\t)\r\n\t\tfrom . import locatepixelcolorcompiled\r\n\r\n\r\n```\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Generates and installs simple Cython modules with specified settings. If you are overwhelmed by the compiler directives and the creation of the setup.py file, this is right for you.",
"version": "0.10",
"project_urls": {
"Homepage": "https://github.com/hansalemaos/cythonautoinstall"
},
"split_keywords": [
"cython",
"compile",
"automatic"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "c564839fb9607f157dc75286d96a1d94967ae9ea5bb0151fc045a8fa37f121da",
"md5": "b054bea5f731536953fba1fa6f0bcbb9",
"sha256": "09b078f296e63376e26b9d9b225359775862bdf33d52e1ee6408fc06cbcbda0d"
},
"downloads": -1,
"filename": "cythonautoinstall-0.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "b054bea5f731536953fba1fa6f0bcbb9",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 15145,
"upload_time": "2023-09-19T04:05:52",
"upload_time_iso_8601": "2023-09-19T04:05:52.291032Z",
"url": "https://files.pythonhosted.org/packages/c5/64/839fb9607f157dc75286d96a1d94967ae9ea5bb0151fc045a8fa37f121da/cythonautoinstall-0.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "0682b123f204477965a17751b60ad5cf5c54731539bd12553ddc5de215f8fb40",
"md5": "d944cbd860795a4d31188a64c918f63a",
"sha256": "d7691e6c051d649b80f5d797b6e08f3a4c77fc0e013e39212f419dd6649e072c"
},
"downloads": -1,
"filename": "cythonautoinstall-0.10.tar.gz",
"has_sig": false,
"md5_digest": "d944cbd860795a4d31188a64c918f63a",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 10915,
"upload_time": "2023-09-19T04:05:53",
"upload_time_iso_8601": "2023-09-19T04:05:53.665160Z",
"url": "https://files.pythonhosted.org/packages/06/82/b123f204477965a17751b60ad5cf5c54731539bd12553ddc5de215f8fb40/cythonautoinstall-0.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-09-19 04:05:53",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "hansalemaos",
"github_project": "cythonautoinstall",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [],
"lcname": "cythonautoinstall"
}