{"id":15297466,"url":"https://github.com/minhtuan221/cython-npm","last_synced_at":"2025-10-12T02:48:37.355Z","repository":{"id":62566214,"uuid":"123369725","full_name":"minhtuan221/cython-npm","owner":"minhtuan221","description":"Cython project management like npm in nodejs","archived":false,"fork":false,"pushed_at":"2021-04-29T02:28:23.000Z","size":74,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-13T23:16:09.621Z","etag":null,"topics":["cython","cython-library","modules","npm","pip","pip-package","python","python-3","python3","python36"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/minhtuan221.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-03-01T02:18:50.000Z","updated_at":"2021-04-29T02:28:25.000Z","dependencies_parsed_at":"2022-11-03T16:15:57.454Z","dependency_job_id":null,"html_url":"https://github.com/minhtuan221/cython-npm","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minhtuan221%2Fcython-npm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minhtuan221%2Fcython-npm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minhtuan221%2Fcython-npm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/minhtuan221%2Fcython-npm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/minhtuan221","download_url":"https://codeload.github.com/minhtuan221/cython-npm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248794569,"owners_count":21162615,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cython","cython-library","modules","npm","pip","pip-package","python","python-3","python3","python36"],"created_at":"2024-09-30T19:17:45.756Z","updated_at":"2025-10-12T02:48:32.305Z","avatar_url":"https://github.com/minhtuan221.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cython-npm\nCython project management like npm in nodejs. This project is inspired by npm in nodejs.\n\n### Installation\n\nYou can easily install by:\n```\npip install cython-npm\n```\n\n### What problems does it solve ?\nWhen using cython, we face the problem of compile cython file. We can do it easily by:\n```python\nimport pyximport; pyximport.install()\n```\nBut that it is not recommended to let **pyximport** build code on end user side as it *hooks into their import system*. The best way to cater for end users is to provide pre-built binary packages.\nSo this project compiles .pyx file and provides pre-built binary packages for easy of use.\n\n### Quickstart:\nBasic use to Complie file or folder\n```python\nfrom cython_npm.cythoncompile import export\nexport('examplefile.pyx')\nexport('./examplefolder')\n# then import them to use\nimport examplefile\nfrom examplefolder import *\n```\nYou should do this code once time only.\n\n### Create install file like package.json\nYou can also compile many files or folders at once time. Create a file name `install.py` in the root of your project/package and write the code below:\n```python\nfrom cython_npm.cythoncompile import install\nManymodules = [\n    # put your modules list here\n    'examplefile.pyx',\n    './examplefolder'\n]\ninstall(Manymodules)\n```\nRun the file before start your project\n```\npython install.py\n```\nOr add first line `import install` in startup file of your project. Use install or export in parent folder will compile all .pyx file in subdirectories.\n### Using require('path') as nodejs\nYou can also relative or fullpath import in python by `require` function. For example:\n```python\nfrom cython_npm.cythoncompile import require\n\n# import .pyx file. Will cause error if it is not compiled by export() yet. \n# Default value of recompile is True, only apply for .py file. To import .pyx, change recompile=False\nexamplefile = require('../parentpackage', recompile=False) # import cython package from parent folder\nexamplefile.somefunction()\n\n# it also support relative import .py file\nexamplefile = require('../parentpackage')\nexamplefile.somefunction()\n```\n\nUsing requirepyx('path'): `requirepyx` is simillar to `require` except:\n* Use for cython file ('.pyx') only\n* Equivalent to export('.pyx file') and require('.pyx file')\nExample:\n```python\nfrom cython_npm.cythoncompile import export\nexport('examplefile')\nrequire('examplefile',recompile=False)\n# The code above is the same as:\nfrom cython_npm.cythoncompile import requirepyx\nrequirepyx('examplefile')\n```\n\n### Using typecheck\nAnother utils is typecheck support to raise error in typing module (from python 3.3):\n\n```python\nfrom cython_npm.typecheck import typecheck\n\n@type_check\ndef checkstr(s: Any)-\u003e(None, str):\n    return None, s\n\nx,y = checkstr('tuan')\nprint(x,y)\n\ntry:\n    checkstr(120)\nexcept Exception as error:\n    print(error)\n    traceback.print_exc()\n    \n# That will raise an error of TypeError\ncheckstr(200)\n\n```\n\n\n### Example: Cython vs speed test battle\nThis example compare the speed between cython vs python, Swift, Go and Code differences in doing a short calculation. Cython_npm is used in the test. This test is forked from 'marcinkliks', the original code and test is here: \n[Swift vs Go vs Python battle](http://www.marcinkliks.pl/2015/02/22/swift-vs-others/). Note: We use Swift and Go test results as pattern and do not retest them. Go to see in test folder in github for more examples\n\nTesting condition:\n* Python version: Python 3.6.3 :: Anaconda, Inc.\n\n* About computer: MacBook Pro (13-inch, 2016, Two Thunderbolt 3 ports), 2 GHz Intel Core i5, 256GB SSD\n\nHypothesis:  \n* Is Cython really fast (compare to other language) ? \n* How does Code differences affect performance ?\n\n#### Test process and results as shown below:\n0. Recall the speed of Swift: 0m0.416s, Go: 0m0.592s and Pypy: 0m2.633s\n\n1. Test pure python code:\n    ```python\n    sum = 0\n\n    for e in range(30):\n        sum = 0\n        x = []\n\n        for i in range(1000000):\n            x.append(i)\n\n        y = []\n        for i in range(1000000 - 1):\n            y.append(x[i] + x[i+1])\n\n        i = 0\n        for i in range(0, 1000000, 100):\n            sum += y[i]\n\n    print(sum)\n    ```\n    Speed test result is same/similar to original test\n    ```\n    time python test_python.py\n    9999010000\n\n    real    0m12.825s\n    user    0m11.721s\n    sys     0m1.061s\n    ```\n2. Test cython code: Create run.py with code: \n    ```python\n    from cython_npm.cythoncompile import export\n    export('test_cython.pyx') # will do once time\n    import test_cython\n    ```\n    Code in __test_cython.pyx__:\n    ```python\n    cdef long sum = 0\n    cdef int i\n    cdef int e\n    for e in range(30):\n        sum = 0\n        x = []\n\n        for i in range(1000000):\n            x.append(i)\n\n        y = []\n        for i in range(1000000 - 1):\n            y.append(x[i] + x[i+1])\n\n        i = 0\n        for i in range(0, 1000000, 100):\n            sum += y[i]\n\n    print(sum)\n    ```\n    Speed test result: time python run.py\n    ```\n    time python run.py\n    9999010000\n\n    real    0m5.803s\n    user    0m4.496s\n    sys     0m1.211s\n    ```\n3. Test cython code with list optimization and cache: create similar run.py. Code in __test_cythoncache.pyx__:\n    ```python\n    from functools import lru_cache\n    @lru_cache(maxsize=128)\n    def dotest():\n        cdef long mysum = 0\n        cdef int i\n        cdef int e\n        for e in range(30):\n            mysum = 0\n            x = [i for i in range(1000000)]\n\n            y = [x[i] + x[i+1] for i in range(1000000-1)]\n\n            i = 0\n            for i in range(0, 1000000, 100):\n                mysum += y[i]\n\n        print(mysum)\n    dotest()\n    ```\n    Speed test result:\n    ```\n    time python run.py\n    9999010000\n\n    real    0m3.373s\n    user    0m2.360s\n    sys     0m1.001s\n    ```\n4. Test cython code with cache and C array: create similar run.py. Code in **test_cythoncache.pyx**:\n    ```python\n    from functools import lru_cache\n    @lru_cache(maxsize=128)\n    def dotest():\n        cdef long mysum = 0\n        cdef int i\n        cdef int e\n        cdef int x[1000000]\n        cdef int y[1000000]\n        for e in range(30):\n            mysum = 0\n            for i in range(1000000):\n                x[i] = i\n\n            # y = []\n            for i in range(1000000 - 1):\n                y[i] = (x[i] + x[i+1])\n\n            i = 0\n            for i in range(0, 1000000, 100):\n                mysum += y[i]\n\n        print(mysum)\n    dotest()\n    ```\n    Speed test result:\n    ```\n    time python run.py\n    9999010000\n\n    real    0m0.085s\n    user    0m0.067s\n    sys     0m0.015s\n    ```\n\n#### Conclusions\n* With a slight change, Cython make pure python code faster by 2X time. But it is very slow compare to Swift and Go\n* Appling some optimal technical, Cython make python nearly 4X time faster than the original code. It may be the acceptable result. Pypy result seems very attractive too.\n* Using C array, Cython make the code become very fast. It consumes only 0.085s to complete as 4X time faster than Swift, 6X time faster than Go. It maybe the fastest but it is unusable in real life. \n* After all, i wish cython and cython_npm could give you more usefull options in coding\n\n\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fminhtuan221%2Fcython-npm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fminhtuan221%2Fcython-npm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fminhtuan221%2Fcython-npm/lists"}