{"id":13501197,"url":"https://github.com/saurabh0719/constable","last_synced_at":"2026-02-22T23:49:17.042Z","repository":{"id":232679996,"uuid":"784920494","full_name":"saurabh0719/constable","owner":"saurabh0719","description":"Constable lets you be lazy by inserting prints directly into your AST for stateful debugging :man_juggling:","archived":false,"fork":false,"pushed_at":"2024-04-18T08:01:41.000Z","size":69,"stargazers_count":100,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-10-27T02:26:43.775Z","etag":null,"topics":["debug","debugging-tool","decorators","python"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/constable","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/saurabh0719.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-04-10T20:33:45.000Z","updated_at":"2025-07-06T18:22:39.000Z","dependencies_parsed_at":"2024-08-01T00:54:03.024Z","dependency_job_id":null,"html_url":"https://github.com/saurabh0719/constable","commit_stats":null,"previous_names":["saurabh0719/ast_debug","saurabh0719/constable"],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/saurabh0719/constable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saurabh0719%2Fconstable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saurabh0719%2Fconstable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saurabh0719%2Fconstable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saurabh0719%2Fconstable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/saurabh0719","download_url":"https://codeload.github.com/saurabh0719/constable/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/saurabh0719%2Fconstable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29731261,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-22T20:09:16.275Z","status":"ssl_error","status_checked_at":"2026-02-22T20:09:13.750Z","response_time":110,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["debug","debugging-tool","decorators","python"],"created_at":"2024-07-31T22:01:28.893Z","updated_at":"2026-02-22T23:49:17.023Z","avatar_url":"https://github.com/saurabh0719.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"\u003cp align=\"left\"\u003e\n    \u003cimg src=\"https://github.com/saurabh0719/constable/assets/127945292/80cf03c8-af53-4161-9a47-b9acbc9bb413\" width=500\u003e\n\u003c/p\u003e\n\n\u003chr\u003e\n\n\u003e :memo: This is an experimental project that tweaks the AST. Use at your own risk in mission-critical environments, or with unknown agents, as compiling and executing code during runtime can cause unwanted side effects. For all use cases that matter, use `pdb` instead.\n\u003cbr\u003e\n\nIf you find yourself aimlessly adding :sparkles: `print` :sparkles: statements while debugging your code, this is for you. :handshake:\n\nConstable inserts print statements directly into the AST at runtime to print variable assignments and other details.\n\nIt turns this 🔽 ....\n```python\n@constable.trace('a', 'b')\ndef do_something(a, b):\n    a = a + b\n```\n.... into this 🔽 during runtime\n```python\n# During runtime, print statements will be added for every assignment on 'a' \u0026 'b'.\n# Resulting in something like -\ndef do_something(a, b):\n    a = a + b\n    print(f\"wowww i wonder who put this print here! a = {a}\")\n```\n\nSee the [examples](#example) below\n\n```sh\n$ pip install constable\n```\n\n### How does it work?\n\nThe `constable.trace` decorator uses Python's Abstract Syntax Tree (AST) in much the same way we add `print`(s) to debug states. During runtime, it prepares and inserts `print` statements into the function's AST after every assignment operation (`ast.Assign`, `ast.AugAssign` and `ast.AnnAssign`), and then executes the modified code in a separate namespace with `exec`.\n\n#### Print variable assignments and execution info.\n\n\u003cspan id=\"example\"\u003e\u003c/span\u003e\n\nMonitor the state of specified variables at each assignment operation with a step-by-step view of variable changes!\n\n```python\nimport constable\n\n@constable.trace('a', 'b')\ndef example(a, b):\n    a = a + b\n    c = a\n    a = \"Experimenting with the AST\"\n    b = c + b\n    a = c + b\n    return a\n\nexample(5, 6)\n```\n\nOutput -\n\n```\nconstable: example: line 5\n    a = a + b\n    a = 11\n    type(a) = \u003cclass 'int'\u003e\n\nconstable: example: line 7\n    a = \"Experimenting with the AST\"\n    a = Experimenting with the AST\n    type(a) = \u003cclass 'str'\u003e\n\nconstable: example: line 8\n    b = c + b\n    b = 17\n    type(b) = \u003cclass 'int'\u003e\n\nconstable: example: line 9\n    a = c + b\n    a = 28\n    type(a) = \u003cclass 'int'\u003e\n\nconstable: example: line 3 to 10\n    args: (5, 6)\n    kwargs: {}\n    returned: 28\n    execution time: 0.00018480 seconds\n```\n\nYou can also use it on its own to track function execution info. \n\n```python\nimport constable\n\n@constable.trace()\ndef add(a, b):\n    return a + b\n\nadd(5, 6)\n```\n\nOutput - \n\n```\nconstable: add: line 3 to 5\n    args: (5, 6)\n    kwargs: {}\n    returned: 11\n    execution time: 0.00004312 seconds\n```\n\n\n#### @trace\nThe `trace` function is the decorator to add `print` statements into the AST.\n\n```python\n\ndef trace(\n    *variables,\n    exec_info=True,\n    verbose=True,\n    use_spaces=True,\n    max_len=None,\n):\n    \"\"\"\n    An experimental decorator for tracing function execution using AST.\n\n    Args:\n        variables (list): List of variable names to trace.\n        exec_info (bool, optional): Whether to print execution info.\n        verbose (bool, optional): Whether to print detailed trace info.\n        use_spaces (bool, optional): Whether to add empty lines for readability.\n        max_len (int, optional): Max length of printed values. Truncates if exceeded.\n\n    Returns:\n        function: Decorator for function tracing.\n    \"\"\"\n\n```\n\u003chr\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaurabh0719%2Fconstable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsaurabh0719%2Fconstable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsaurabh0719%2Fconstable/lists"}