cooodecooo


Namecooodecooo JSON
Version 0.10 PyPI version JSON
download
home_pagehttps://github.com/hansalemaos/cooodecooo
SummaryA decorator that compiles C/C++ functions with Clang in Python
upload_time2023-04-16 00:33:15
maintainer
docs_urlNone
authorJohannes Fischer
requires_python
licenseMIT
keywords c++ c compile decorator
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # A decorator that compiles C/C++ functions with Clang in Python

#### Like Numba, but Jerry-built and pre-alpha :) 

## pip install cooodecooo

#### Tested against Windows 10 / Python 3.10 / Anaconda


### Here are 2 examples:


#### Find RGB colors 


```python
import cv2
import numpy as np
from cooodecooo import ccoo

# The name of the variable needs to be `clangpath`, if you change it, it won't work
clangpath = r"C:\Program Files\LLVM\bin\clang.exe"

# The names of the 2 functions (Python / C) need to be equal (colorsearch2 - colorsearch2)
@ccoo
def colorsearch2(**kwargs): # put here always **kwargs
    # The variable that contains the C source code must be named - c_source_code -
    c_source_code = r"""
    __declspec(dllexport) void colorsearch2(unsigned char *pic, unsigned char *colors, int width, int totallengthpic, int totallengthcolor, int *outputx, int *outputy, int *lastresult) {
        int counter = 0;
        for (int i = 0; i <= totallengthcolor; i += 3) {
            int r = i;
            int g = i + 1;
            int b = i + 2;
            for (int j = 0; j <= totallengthpic; j += 3) {
                if ((colors[r] == pic[j]) && (colors[g] == pic[j + 1]) && (colors[b] == pic[j + 2])) {
                    int dividend = j / 3;
                    int quotient = dividend / width;
                    int remainder = dividend % width;
                    int upcounter = counter;
                    outputx[upcounter] = quotient;
                    outputy[upcounter] = remainder;
                    lastresult[0] = upcounter;
                    counter++;
                }
            }
        }
    }
    """
    # Where do you want to save the shared library? The name of the variable cannot be changed! It needs to be - c_save_path -
    c_save_path = "cloop.so"
	# That's it, no return value! Scroll down for more details.

def get_pixelcolor(pic, colors):
    totallenghtpic = (pic.shape[0] * pic.shape[1] * pic.shape[2]) - 1
    totallengthcolor = (colors.shape[0] * colors.shape[1]) - 1
    outputx = np.zeros(totallenghtpic, dtype=np.int32)
    outputy = np.zeros(totallenghtpic, dtype=np.int32)
    lastresult = np.zeros(1, dtype=np.int32)

    # Use the same signature as in the C function # unsigned char *pic, unsigned char *colors, int width, int totallengthpic, int totallengthcolor, int *outputx, int *outputy, int *lastresult
    # Pointers only work when numpy arrays are passed to the function
    varstopass = {
        "pic": pic,
        "colors": colors,
        "width": pic.shape[1],
        "totallengthpic": totallenghtpic,
        "totallengthcolor": totallengthcolor,
        "outputx": outputx,
        "outputy": outputy,
        "lastresult": lastresult,
    }

    # The compiled function doesn't return anything, it writes the results in  given numpy arrays by using pointers
    # In this example: outputx/outputy/lastresult
    _ = colorsearch2(**varstopass)
    return np.dstack([outputx[: lastresult[0] + 1], outputy[: lastresult[0] + 1]])[0]


picx = r"C:\Users\hansc\Downloads\pexels-alex-andrews-2295744.jpg"
pic = cv2.imread(picx)
colors = 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,
)
resu = get_pixelcolor(pic, colors)



```




### Levenshtein Distance


```python
import numpy as np
from cooodecooo import ccoo

clangpath = r"C:\Program Files\LLVM\bin\clang.exe"


@ccoo
def levenshtein_distance(**kwargs):
    c_source_code = r"""
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int minim(int a, int b, int c)
{
    int m = a;
    if (b < m)
        m = b;
    if (c < m)
        m = c;
    return m;
}

__declspec(dllexport) void levenshtein_distance(char *s1, char *s2, int len1, int len2, int *resultsx)
{
    //printf("%d %d\n", len1, len2);
    int *d = (int *)malloc((len1 + 1) * (len2 + 1) * sizeof(int));
    int i, j;

    for (i = 0; i <= len1; i++)
        d[i * (len2 + 1) + 0] = i;

    for (j = 0; j <= len2; j++)
        d[0 * (len2 + 1) + j] = j;

    for (i = 1; i <= len1; i++)
    {
        for (j = 1; j <= len2; j++)
        {
            //printf("%c %c\n", s1[i - 1], s2[j - 1]);
            //fflush(stdout);
            int cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1;
            d[i * (len2 + 1) + j] = minim((d[(i - 1) * (len2 + 1) + j] + 1),
                                          (d[i * (len2 + 1) + (j - 1)] + 1),
                                          (d[(i - 1) * (len2 + 1) + (j - 1)] + cost));
        }
    }

    resultsx[0] = d[len1 * (len2 + 1) + len2];
    printf("%d\n", resultsx[0]);
    free(d);
}
    """
    c_save_path = "levete.so"


s01 = ("kitten").encode()
s02 = ("sitting").encode()
s1 = np.array(list(s01), dtype=np.uint8)
s2 = np.array(list(s02), dtype=np.uint8)
len1 = len(s1)
len2 = len(s2)
resultsx = np.array([0], dtype=np.int32)
_ = levenshtein_distance(s1=s1, s2=s2, len1=len1, len2=len2, resultsx=resultsx)
print(resultsx)
```

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/hansalemaos/cooodecooo",
    "name": "cooodecooo",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "c++,c,compile,decorator",
    "author": "Johannes Fischer",
    "author_email": "aulasparticularesdealemaosp@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/c9/55/09555cf62d49c9f71fe4b2a0be0fdac9d3be5bf031e4e2f4271f2d945c76/cooodecooo-0.10.tar.gz",
    "platform": null,
    "description": "# A decorator that compiles C/C++ functions with Clang in Python\r\n\r\n#### Like Numba, but Jerry-built and pre-alpha :) \r\n\r\n## pip install cooodecooo\r\n\r\n#### Tested against Windows 10 / Python 3.10 / Anaconda\r\n\r\n\r\n### Here are 2 examples:\r\n\r\n\r\n#### Find RGB colors \r\n\r\n\r\n```python\r\nimport cv2\r\nimport numpy as np\r\nfrom cooodecooo import ccoo\r\n\r\n# The name of the variable needs to be `clangpath`, if you change it, it won't work\r\nclangpath = r\"C:\\Program Files\\LLVM\\bin\\clang.exe\"\r\n\r\n# The names of the 2 functions (Python / C) need to be equal (colorsearch2 - colorsearch2)\r\n@ccoo\r\ndef colorsearch2(**kwargs): # put here always **kwargs\r\n    # The variable that contains the C source code must be named - c_source_code -\r\n    c_source_code = r\"\"\"\r\n    __declspec(dllexport) void colorsearch2(unsigned char *pic, unsigned char *colors, int width, int totallengthpic, int totallengthcolor, int *outputx, int *outputy, int *lastresult) {\r\n        int counter = 0;\r\n        for (int i = 0; i <= totallengthcolor; i += 3) {\r\n            int r = i;\r\n            int g = i + 1;\r\n            int b = i + 2;\r\n            for (int j = 0; j <= totallengthpic; j += 3) {\r\n                if ((colors[r] == pic[j]) && (colors[g] == pic[j + 1]) && (colors[b] == pic[j + 2])) {\r\n                    int dividend = j / 3;\r\n                    int quotient = dividend / width;\r\n                    int remainder = dividend % width;\r\n                    int upcounter = counter;\r\n                    outputx[upcounter] = quotient;\r\n                    outputy[upcounter] = remainder;\r\n                    lastresult[0] = upcounter;\r\n                    counter++;\r\n                }\r\n            }\r\n        }\r\n    }\r\n    \"\"\"\r\n    # Where do you want to save the shared library? The name of the variable cannot be changed! It needs to be - c_save_path -\r\n    c_save_path = \"cloop.so\"\r\n\t# That's it, no return value! Scroll down for more details.\r\n\r\ndef get_pixelcolor(pic, colors):\r\n    totallenghtpic = (pic.shape[0] * pic.shape[1] * pic.shape[2]) - 1\r\n    totallengthcolor = (colors.shape[0] * colors.shape[1]) - 1\r\n    outputx = np.zeros(totallenghtpic, dtype=np.int32)\r\n    outputy = np.zeros(totallenghtpic, dtype=np.int32)\r\n    lastresult = np.zeros(1, dtype=np.int32)\r\n\r\n    # Use the same signature as in the C function # unsigned char *pic, unsigned char *colors, int width, int totallengthpic, int totallengthcolor, int *outputx, int *outputy, int *lastresult\r\n    # Pointers only work when numpy arrays are passed to the function\r\n    varstopass = {\r\n        \"pic\": pic,\r\n        \"colors\": colors,\r\n        \"width\": pic.shape[1],\r\n        \"totallengthpic\": totallenghtpic,\r\n        \"totallengthcolor\": totallengthcolor,\r\n        \"outputx\": outputx,\r\n        \"outputy\": outputy,\r\n        \"lastresult\": lastresult,\r\n    }\r\n\r\n    # The compiled function doesn't return anything, it writes the results in  given numpy arrays by using pointers\r\n    # In this example: outputx/outputy/lastresult\r\n    _ = colorsearch2(**varstopass)\r\n    return np.dstack([outputx[: lastresult[0] + 1], outputy[: lastresult[0] + 1]])[0]\r\n\r\n\r\npicx = r\"C:\\Users\\hansc\\Downloads\\pexels-alex-andrews-2295744.jpg\"\r\npic = cv2.imread(picx)\r\ncolors = np.array(\r\n    [\r\n        (66, 71, 69),\r\n        (62, 67, 65),\r\n        (144, 155, 153),\r\n        (52, 57, 55),\r\n        (127, 138, 136),\r\n        (53, 58, 56),\r\n        (51, 56, 54),\r\n        (32, 27, 18),\r\n        (24, 17, 8),\r\n    ],\r\n    dtype=np.uint8,\r\n)\r\nresu = get_pixelcolor(pic, colors)\r\n\r\n\r\n\r\n```\r\n\r\n\r\n\r\n\r\n### Levenshtein Distance\r\n\r\n\r\n```python\r\nimport numpy as np\r\nfrom cooodecooo import ccoo\r\n\r\nclangpath = r\"C:\\Program Files\\LLVM\\bin\\clang.exe\"\r\n\r\n\r\n@ccoo\r\ndef levenshtein_distance(**kwargs):\r\n    c_source_code = r\"\"\"\r\n#include <stdio.h>\r\n#include <stdlib.h>\r\n#include <string.h>\r\n\r\nint minim(int a, int b, int c)\r\n{\r\n    int m = a;\r\n    if (b < m)\r\n        m = b;\r\n    if (c < m)\r\n        m = c;\r\n    return m;\r\n}\r\n\r\n__declspec(dllexport) void levenshtein_distance(char *s1, char *s2, int len1, int len2, int *resultsx)\r\n{\r\n    //printf(\"%d %d\\n\", len1, len2);\r\n    int *d = (int *)malloc((len1 + 1) * (len2 + 1) * sizeof(int));\r\n    int i, j;\r\n\r\n    for (i = 0; i <= len1; i++)\r\n        d[i * (len2 + 1) + 0] = i;\r\n\r\n    for (j = 0; j <= len2; j++)\r\n        d[0 * (len2 + 1) + j] = j;\r\n\r\n    for (i = 1; i <= len1; i++)\r\n    {\r\n        for (j = 1; j <= len2; j++)\r\n        {\r\n            //printf(\"%c %c\\n\", s1[i - 1], s2[j - 1]);\r\n            //fflush(stdout);\r\n            int cost = (s1[i - 1] == s2[j - 1]) ? 0 : 1;\r\n            d[i * (len2 + 1) + j] = minim((d[(i - 1) * (len2 + 1) + j] + 1),\r\n                                          (d[i * (len2 + 1) + (j - 1)] + 1),\r\n                                          (d[(i - 1) * (len2 + 1) + (j - 1)] + cost));\r\n        }\r\n    }\r\n\r\n    resultsx[0] = d[len1 * (len2 + 1) + len2];\r\n    printf(\"%d\\n\", resultsx[0]);\r\n    free(d);\r\n}\r\n    \"\"\"\r\n    c_save_path = \"levete.so\"\r\n\r\n\r\ns01 = (\"kitten\").encode()\r\ns02 = (\"sitting\").encode()\r\ns1 = np.array(list(s01), dtype=np.uint8)\r\ns2 = np.array(list(s02), dtype=np.uint8)\r\nlen1 = len(s1)\r\nlen2 = len(s2)\r\nresultsx = np.array([0], dtype=np.int32)\r\n_ = levenshtein_distance(s1=s1, s2=s2, len1=len1, len2=len2, resultsx=resultsx)\r\nprint(resultsx)\r\n```\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A decorator that compiles C/C++ functions with Clang in Python",
    "version": "0.10",
    "split_keywords": [
        "c++",
        "c",
        "compile",
        "decorator"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ed13d56f63773154cec371b02975356fe2d0a2c45f5732c50c30c36a95233d53",
                "md5": "cf9b8b0c2f8edefab81777e1fd4aa4cb",
                "sha256": "d8d786990680242160939b50f75c146e8cc999857cae260d71974f9737b4f025"
            },
            "downloads": -1,
            "filename": "cooodecooo-0.10-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "cf9b8b0c2f8edefab81777e1fd4aa4cb",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 26299,
            "upload_time": "2023-04-16T00:33:13",
            "upload_time_iso_8601": "2023-04-16T00:33:13.340130Z",
            "url": "https://files.pythonhosted.org/packages/ed/13/d56f63773154cec371b02975356fe2d0a2c45f5732c50c30c36a95233d53/cooodecooo-0.10-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c95509555cf62d49c9f71fe4b2a0be0fdac9d3be5bf031e4e2f4271f2d945c76",
                "md5": "1c62d687df26d215fb8dbde549dbe0d9",
                "sha256": "402fba6adcaba1750609e5eca2611848b8ae303f58676e141b639c8582cbe76a"
            },
            "downloads": -1,
            "filename": "cooodecooo-0.10.tar.gz",
            "has_sig": false,
            "md5_digest": "1c62d687df26d215fb8dbde549dbe0d9",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 25537,
            "upload_time": "2023-04-16T00:33:15",
            "upload_time_iso_8601": "2023-04-16T00:33:15.486104Z",
            "url": "https://files.pythonhosted.org/packages/c9/55/09555cf62d49c9f71fe4b2a0be0fdac9d3be5bf031e4e2f4271f2d945c76/cooodecooo-0.10.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-04-16 00:33:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "hansalemaos",
    "github_project": "cooodecooo",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "cooodecooo"
}
        
Elapsed time: 0.05552s