Skip to main content

Cython Compilation Made Simple

Project description

Cython Compilation Made Simple

cycompi is a lightweight Python module designed to simplify the compilation of Cython code by providing a convenient interface. With Cycompi, you can effortlessly compile your Cython modules with minimal setup and enjoy the advantages of improved performance and enhanced functionality.

Tested against Windows / Python 3.11 / Anaconda

pip install cycompi

    Compile Cython code using the provided configuration, options, and command line arguments.
    :param name: name of the module 
    :param configdict: dictionary containing configuration settings (passed to setuptools.Extension)
    :param optionsdict: dictionary containing options settings (passed to Cython.Compiler.Options)
    :param cmd_line_args: command line arguments (passed to setup as compiler_directives)
    :param **kwargs: additional keyword arguments to be passed to subprocess.run

    :return: None

    Example usage:
        from cycompi import compile_cython_code
        import numpy as np
        import os

        numpyincludefolder = np.get_include()
		
		# Explanation: https://cython.readthedocs.io/en/latest/src/userguide/source_files_and_compilation.html
		# Most of the time, I use this configuration: 
        optionsdict = {
            "Options.docstrings": False,
            "Options.embed_pos_in_docstring": False,
            "Options.generate_cleanup_code": False,
            "Options.clear_to_none": True,
            "Options.annotate": True,
            "Options.fast_fail": False,
            "Options.warning_errors": False,
            "Options.error_on_unknown_names": True,
            "Options.error_on_uninitialized": True,
            "Options.convert_range": True,
            "Options.cache_builtins": True,
            "Options.gcc_branch_hints": True,
            "Options.lookup_module_cpdef": False,
            "Options.embed": False,
            "Options.cimport_from_pyx": False,
            "Options.buffer_max_dims": 8,
            "Options.closure_freelist_size": 8,
        }
        configdict = {
            "py_limited_api": False,
            "name": "cythondict",
            "sources": ["lookdi.pyx"],
            "include_dirs": [numpyincludefolder],
            "define_macros": [
                ("NPY_NO_DEPRECATED_API", 1),
                ("NPY_1_7_API_VERSION", 1),
                ("CYTHON_USE_DICT_VERSIONS", 1),
                ("CYTHON_FAST_GIL", 1),
                ("CYTHON_USE_PYLIST_INTERNALS", 1),
                ("CYTHON_USE_UNICODE_INTERNALS", 1),
                ("CYTHON_ASSUME_SAFE_MACROS", 1),
                ("CYTHON_USE_TYPE_SLOTS", 1),
                ("CYTHON_USE_PYTYPE_LOOKUP", 1),
                ("CYTHON_USE_ASYNC_SLOTS", 1),
                ("CYTHON_USE_PYLONG_INTERNALS", 1),
                ("CYTHON_USE_UNICODE_WRITER", 1),
                ("CYTHON_UNPACK_METHODS", 1),
                ("CYTHON_USE_EXC_INFO_STACK", 1),
                ("CYTHON_ATOMICS", 1),
            ],
            "undef_macros": [],
            "library_dirs": [],
            "libraries": [],
            "runtime_library_dirs": [],
            "extra_objects": [],
            "extra_compile_args": ["/O2", "/Oy"],
            "extra_link_args": [],
            "export_symbols": [],
            "swig_opts": [],
            "depends": [],
            "language": "c",
            "optional": None,
        }
        compiler_directives = {
            "binding": True,
            "boundscheck": False,
            "wraparound": False,
            "initializedcheck": False,
            "nonecheck": False,
            "overflowcheck": False,
            "overflowcheck.fold": True,
            "embedsignature": False,
            "embedsignature.format": "c",  # (c / python / clinic)
            "cdivision": True,
            "cdivision_warnings": False,
            "cpow": True,
            "always_allow_keywords": False,
            "c_api_binop_methods": False,
            "profile": False,
            "linetrace": False,
            "infer_types": True,
            "language_level": 3,  # (2/3/3str)
            "c_string_type": "bytes",  # (bytes / str / unicode)
            "c_string_encoding": "default",  # (ascii, default, utf-8, etc.)
            "type_version_tag": False,
            "unraisable_tracebacks": True,
            "iterable_coroutine": True,
            "annotation_typing": True,
            "emit_code_comments": True,
            "cpp_locals": False,
            "legacy_implicit_noexcept": False,
            "optimize.use_switch": True,
            "optimize.unpack_method_calls": True,
            "warn.undeclared": False,  # (default False)
            "warn.unreachable": True,  # (default True)
            "warn.maybe_uninitialized": False,  # (default False)
            "warn.unused": False,  # (default False)
            "warn.unused_arg": False,  # (default False)
            "warn.unused_result": False,  # (default False)
            "warn.multiple_declarators": True,  # (default True)
            "show_performance_hints": True,  # (default True)
        }

        compile_cython_code(
            name="lookdi",
            configdict=configdict,
            optionsdict=optionsdict,
            cmd_line_args=compiler_directives,
            cwd=os.getcwd(),
            shell=True,
            env=os.environ.copy(),
        )

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cycompi-0.13.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

cycompi-0.13-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file cycompi-0.13.tar.gz.

File metadata

  • Download URL: cycompi-0.13.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for cycompi-0.13.tar.gz
Algorithm Hash digest
SHA256 280394ea337d54c9f1e40afe4c7e1ee96b61b0d6c8f6026b2f66a24e24c6946b
MD5 2cb89e9d241764674aef3599f8881586
BLAKE2b-256 2ae6c2e3f18ebfa1ce3932df9bc03498aab74e034f657eccb43d14ac4231e6e7

See more details on using hashes here.

File details

Details for the file cycompi-0.13-py3-none-any.whl.

File metadata

  • Download URL: cycompi-0.13-py3-none-any.whl
  • Upload date:
  • Size: 8.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for cycompi-0.13-py3-none-any.whl
Algorithm Hash digest
SHA256 ecd05b3b1710cd6a940cd45b3288e49b2e16684de545df5ca489c4e738533f95
MD5 4de641db468a30e1419d7ecd32f3d9db
BLAKE2b-256 cc03358be56415a6a38f810b21c2178264d4dd833e0d2101cfaf637604fc874d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page