{"id":18747659,"url":"https://github.com/quantum5/optimize-later","last_synced_at":"2025-04-12T22:28:40.024Z","repository":{"id":49874303,"uuid":"100066944","full_name":"quantum5/optimize-later","owner":"quantum5","description":"Mark potentially slow blocks for notifications when it actually turns out too slow, so you can optimize it.","archived":false,"fork":false,"pushed_at":"2020-05-23T04:08:56.000Z","size":22,"stargazers_count":8,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-26T16:38:23.501Z","etag":null,"topics":["django","optimization","profiler","python","python-library","speed"],"latest_commit_sha":null,"homepage":"","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/quantum5.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":"2017-08-11T20:12:17.000Z","updated_at":"2024-11-11T20:52:14.000Z","dependencies_parsed_at":"2022-09-14T07:31:43.165Z","dependency_job_id":null,"html_url":"https://github.com/quantum5/optimize-later","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantum5%2Foptimize-later","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantum5%2Foptimize-later/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantum5%2Foptimize-later/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/quantum5%2Foptimize-later/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/quantum5","download_url":"https://codeload.github.com/quantum5/optimize-later/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248639289,"owners_count":21137817,"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":["django","optimization","profiler","python","python-library","speed"],"created_at":"2024-11-07T16:30:52.174Z","updated_at":"2025-04-12T22:28:40.000Z","avatar_url":"https://github.com/quantum5.png","language":"Python","readme":"# optimize-later [![Build Status](https://img.shields.io/travis/quantum5/optimize-later.svg)](https://travis-ci.org/quantum5/optimize-later) [![Coverage](https://img.shields.io/codecov/c/gh/quantum5/optimize-later.svg)](https://codecov.io/gh/quantum5/optimize-later) [![PyPI](https://img.shields.io/pypi/v/optimize-later.svg)](https://pypi.org/project/optimize-later/) [![PyPI - Format](https://img.shields.io/pypi/format/optimize-later.svg)](https://pypi.org/project/optimize-later/) [![PyPI - Django Version](https://img.shields.io/pypi/djversions/optimize-later.svg)](#django)\n\n\u003e Premature optimization is the root of all evil (or at least most of it) in programming.\n\u003e\n\u003e -- \u003ccite\u003eDonald Knuth\u003c/cite\u003e\n\nWouldn't it be nice to have something to tell you when optimization is really necessary?\n\nEnter `optimize-later`.\n\nInstead of trying to guess what code ought to be optimized, `optimize-later` times potentially\nslow blocks of code for you, and calls a user-specified function when it exceeds the specified\ntime limit. This way, you only have to optimize code when speed becomes a problem, saving you\nfrom both the evils of premature optimization, and the evils of slow code. \n\n## Usage\n\n```python\nfrom optimize_later import optimize_later, register_callback\n\n### Basic usage.\nwith optimize_later('test_block', 0.2):\n    # potentially slow block of code...\n    time.sleep(1)\n\n@register_callback\ndef my_report_function(report):\n    # Short one line description.\n    print(report.short())\n\n    # Long description with breakdown based on blocks.\n    print(report.long())\n    \n    # Details available in:\n    #   - report.name: block name\n    #   - report.limit: time limit\n    #   - report.delta: time consumed\n    #   - report.blocks: breakdown by blocks\n    #   - report.start, report.end: start and end time with an unspecified timer:\n    #     useful for building a relative timeline with blocks.\n\n### More advanced uses.\n# Automatic block names from file and source line (slightly slow).\nwith optimize_later(0.2):\n    # potentially slow block of code...\n    time.sleep(1)\n\n# Always warn. Good for exceptional cases that you suspect should not happen.\nwith optimize_later():\n    # potentially slow block of code...\n    time.sleep(1)\n\n# Also available as a decorator.\n@optimize_later('bad-function', 0.2)\ndef function_name():\n    # potentially slow function...\n    time.sleep(1)\n\n# Will use module:function as block name, if you do not specify a name.\n# There is no performance penalty this way, as the function name can be easily detected.\n@optimize_later(0.2)\ndef function_name():\n    # potentially slow function...\n    time.sleep(1)\n\n### Blocks.\nwith optimize_later() as o:\n    with o.block('block 1'):\n        # When the time limit of whole block is exceeded, your report will contain\n        # a detailed breakdown by sub-blocks executed. This allows you to pinpoint\n        # which exact block is the culprit.\n        time.sleep(1)\n    \n    # optimize-later will automatically generate a block name for you from file and\n    # line number, with a slightly performance penalty.\n    with o.block() as b:\n        # You can also nest blocks.\n        with b.block():\n            pass\n\n### Callbacks deregistration and contexts.\nfrom optimize_later import deregister_callback, optimize_context\n\nderegister_callback(my_report_function)\n\nwith optimize_context():\n    # Register a callback here.\n    register_callback(my_report_function)\n# Callback is not available here.\n\n@optimize_context\ndef function():\n    # This callback will be available for the duration of this function.\n    register_callback(my_report_function)\n\n# Remove global callbacks for this block.\nwith optimize_context(renew=True):\n    pass\n# or...\n@optimize_context(renew=True)\ndef function():\n    pass\n    \n# Shortcut registration syntax.\nwith optimize_context(my_report_function):\n    pass\n\n@optimize_context(my_report_function, renew=True)\ndef function():\n    pass\n```\n\nA sample short report:\n\n```Block 'tests.py@152' took 0.011565s (+0.011565s over limit)```\n\nA sample long report:\n\n```\nBlock 'tests.py@152' took 0.011565s (+0.011565s over limit), children:\n  - Block 'tests.py@153' took 0.006662s, children:\n      - Block 'tests.py@154' took 0.000002s\n      - Block 'tests.py@156' took 0.000002s\n  - Block 'tests.py@159' took 0.000001s\n```\n\n## Installation\n\nInstall the module with:\n\n```\n$ pip install optimize-later\n```\n\nOr if you want the latest bleeding edge version:\n\n```\n$ pip install -e git://github.com/quantum5/optimize-later.git\n```\n\nThat's it!\n\n### Django\n\nIf you are using Django, you might want to configure `optimize-later` in `settings.py` instead of\nadding callbacks directly.\n\nYou have to add `'optimize_later'` to `INSTALLED_APPS`.\n\nThen, the list of callbacks as dot-separated import paths can be specified in `'OPTIMIZE_LATER_CALLBACKS'`\nin `settings.py`. For example:\n\n```python\nOPTIMIZE_LATER_CALLBACKS = [\n    'myapp.optimize.report',\n    'otherapp.optimize.report',\n]\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquantum5%2Foptimize-later","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fquantum5%2Foptimize-later","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fquantum5%2Foptimize-later/lists"}