{"id":15046873,"url":"https://github.com/robinvandernoord/comptime","last_synced_at":"2025-03-03T14:10:29.522Z","repository":{"id":187710490,"uuid":"676972325","full_name":"robinvandernoord/comptime","owner":"robinvandernoord","description":"\"Zig into Python's speed lane with Comptime – it's not a sprint; it's a compile!\"","archived":false,"fork":false,"pushed_at":"2023-08-15T13:30:56.000Z","size":43,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-14T11:53:22.694Z","etag":null,"topics":["ast","compiler","comptime","python","zig"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/comptime","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/robinvandernoord.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-08-10T12:45:47.000Z","updated_at":"2024-08-28T22:45:26.000Z","dependencies_parsed_at":null,"dependency_job_id":"6c3caa64-34cc-4cc9-ba8b-9d636a48f1ca","html_url":"https://github.com/robinvandernoord/comptime","commit_stats":{"total_commits":6,"total_committers":2,"mean_commits":3.0,"dds":"0.16666666666666663","last_synced_commit":"d5b38477ae9959baba34c25c4a7f0dffe457cebf"},"previous_names":["robinvandernoord/comptime"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinvandernoord%2Fcomptime","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinvandernoord%2Fcomptime/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinvandernoord%2Fcomptime/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/robinvandernoord%2Fcomptime/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/robinvandernoord","download_url":"https://codeload.github.com/robinvandernoord/comptime/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241678157,"owners_count":20001682,"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":["ast","compiler","comptime","python","zig"],"created_at":"2024-09-24T20:53:41.458Z","updated_at":"2025-03-03T14:10:29.491Z","avatar_url":"https://github.com/robinvandernoord.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# comptime\n\n\u003cdiv align=\"center\"\u003e\n    \u003ca href=\"https://pypi.org/project/comptime\"\u003e\u003cimg alt=\"PyPI - Version\" src=\"https://img.shields.io/pypi/v/comptime.svg\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://pypi.org/project/comptime\"\u003e\u003cimg alt=\"PyPI - Python Version\" src=\"https://img.shields.io/pypi/pyversions/comptime.svg\"/\u003e\u003c/a\u003e\n    \u003cbr/\u003e\n    \u003ca href=\"https://github.com/psf/black\"\u003e\u003cimg alt=\"Code style: black\" src=\"https://img.shields.io/badge/code%20style-black-000000.svg\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://opensource.org/licenses/MIT\"\u003e\u003cimg alt=\"License: MIT\" src=\"https://img.shields.io/badge/License-MIT-yellow.svg\"/\u003e\u003c/a\u003e\n    \u003cbr/\u003e\n    \u003ca href=\"https://github.com/robinvandernoord/comptime/actions\"\u003e\u003cimg alt=\"comptime checks\" src=\"https://github.com/robinvandernoord/comptime/actions/workflows/comptime.yml/badge.svg?branch=development\"/\u003e\u003c/a\u003e\n    \u003ca href=\"https://github.com/robinvandernoord/comptime/actions\"\u003e\u003cimg alt=\"Coverage\" src=\"coverage.svg\"/\u003e\u003c/a\u003e\n\u003c/div\u003e\n\n\"Comptime\" accelerates Python code by precomputing complex calculations, turning them into simple lookups for faster\nexecution.\n\n-----\n\n**Table of Contents**\n\n- [Installation](#installation)\n- [Usage](#usage)\n- [License](#license)\n\nComptime is inspired by the concept of compile-time computation found in languages\nlike [Zig](https://ziglang.org/documentation/master/#comptime), and it brings this powerful\nfeature into the world of Python, an interpreted language. By utilizing special decorators, you can mark functions whose\nreturn values should be precomputed. This enables you to separate computationally expensive parts of your code,\nperforming those calculations once, and embedding the results directly into your source code. The result is Python code\nthat executes faster by turning complex calculations into simple lookups. Whether you're optimizing critical performance\nbottlenecks or exploring new ways to structure your code, Comptime offers a novel approach to accelerate your Python\ndevelopment.\n\n**\"Zig into Python's speed lane with Comptime – it's not a sprint; it's a compile!\"**\n\n## :warning: Warning: this is only a proof-of-concept\n\nThis code was only created to test the concept. It should NOT be used in a production environment, as I can not\nguarantee at this time that the semantics of the outputted code are correct.\nFurthermore, initial performance tests show that for relatively simple calculations, this does NOT actually improve\nperformance:\n\n```bash\npython examples/perf.py # pre-comptime:\n# Function executed in: 2.0992 seconds total; avg of 209.92 ns. per execution.\npython examples/perf_match.py # comptime with match-case strategy:\n# Function executed in: 21.5824 seconds total; avg of 2158.24 ns. per execution.\npython examples/perf_dict.py # comptime with dict lookup strategy:\n# Function executed in: 152.4098 seconds total; avg of 15240.98 ns. per execution.\n```\n\nWhile this package could be useful in cases where the calculation is actually heavy, it could also negatively impact\nyour performance!\n\n## Installation\n\n```console\npip install comptime\n```\n\n## Usage\n\n```python\n# src_raw/main.py: before comptime\nfrom comptime import comptime\n\n\n@comptime.skip\ndef nonpure_method():\n    # this method has side effects so should only be used at runtime\n    print(\"e.g. sending an email\")\n\n\n@comptime\ndef my_method():\n    ...\n    # some expensive calculations\n    nonpure_method()  # not executed due to @comptime.skip\n    return 41 + 1\n\n\n@comptime(\"users\", \"posts\")\ndef call_api(endpoint):\n    # api get value for endpoint\n    ...\n    return value\n\n\ncall_api(\"other\")  # possible comptime warning due to unsupported argument?\n```\n\n```bash\ncomptime --input src_raw --output src_compiled\n```\n\n```python\n# src_compiled/main.py: after comptime\nimport typing\n\n\ndef nonpure_method():\n    # this method has side effects so should only be used at runtime\n    print(\"e.g. sending an email\")\n\n\ndef my_method():\n    # computed by comptime \n    return 42\n\n\ndef call_api(endpoint: typing.Literal[\"users\", \"posts\"]):\n    # computed by comptime\n    match endpoint:\n        case \"users\":\n            return ['user1']\n        case \"posts\":\n            return ['post1']\n        case _:\n            raise ValueError(\"Uncompiled variant {endpoint}\")\n    return value\n```\n\n## Acknowledgments\n\nThis project owes its inspiration and certain elements of its design to various sources:\n\n- **Zig Programming Language:** The concept of compile-time computation in Comptime is inspired\n  by [Zig](https://github.com/ziglang/zig), a language that emphasizes safety, performance, and readability. A special\n  thank you to Zig's creators and community for their innovative approach to programming.\n\n- **GPT-4 by OpenAI:** Assistance with the project boilerplate, creative brainstorming, and crafting the memorable\n  slogan \"Zig into Python's speed lane with Comptime – it's not a sprint; it's a compile!\" was provided by GPT-4. It\n  also wrote this section.\n\nPlease note that the author of comptime is not affiliated with Zig, OpenAI, or any other entities mentioned\nabove. The acknowledgments are expressions of gratitude and inspiration and do not imply any formal association or\nendorsement by these parties.\n\n## License\n\n`comptime` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobinvandernoord%2Fcomptime","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobinvandernoord%2Fcomptime","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobinvandernoord%2Fcomptime/lists"}