Title: Optimize textwrap.indent() · Issue #107369 · python/cpython · GitHub
Open Graph Title: Optimize textwrap.indent() · Issue #107369 · python/cpython
X Title: Optimize textwrap.indent() · Issue #107369 · python/cpython
Description: Current code: def indent(text, prefix, predicate=None): """Adds 'prefix' to the beginning of selected lines in 'text'. If 'predicate' is provided, 'prefix' will only be added to the lines where 'predicate(line)' is True. If 'predicate' i...
Open Graph Description: Current code: def indent(text, prefix, predicate=None): """Adds 'prefix' to the beginning of selected lines in 'text'. If 'predicate' is provided, 'prefix' will only be added to the lines where 'pr...
X Description: Current code: def indent(text, prefix, predicate=None): """Adds 'prefix' to the beginning of selected lines in 'text'. If 'predicate' is provided, 'prefix...
Opengraph URL: https://github.com/python/cpython/issues/107369
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Optimize textwrap.indent()","articleBody":"Current code:\r\n\r\n```py\r\ndef indent(text, prefix, predicate=None):\r\n \"\"\"Adds 'prefix' to the beginning of selected lines in 'text'.\r\n\r\n If 'predicate' is provided, 'prefix' will only be added to the lines\r\n where 'predicate(line)' is True. If 'predicate' is not provided,\r\n it will default to adding 'prefix' to all non-empty lines that do not\r\n consist solely of whitespace characters.\r\n \"\"\"\r\n if predicate is None:\r\n def predicate(line):\r\n return line.strip()\r\n\r\n def prefixed_lines():\r\n for line in text.splitlines(True):\r\n yield (prefix + line if predicate(line) else line)\r\n return ''.join(prefixed_lines())\r\n```\r\n\r\n* `predicate = str.strip` is faster than `def predicate(line)`\r\n* `''.join(x)` converts input iterable to sequence. Using generator just makes overhead.\r\n* creating temporary `prefix + line` is avoidable.\r\n\r\n\u003c!-- gh-linked-prs --\u003e\r\n### Linked PRs\r\n* gh-107374\r\n* gh-131923\n\u003c!-- /gh-linked-prs --\u003e\r\n","author":{"url":"https://github.com/methane","@type":"Person","name":"methane"},"datePublished":"2023-07-28T05:39:20.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":7},"url":"https://github.com/107369/cpython/issues/107369"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:93ef8b31-594b-8507-08c1-4ac7a25e5706 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D77C:164E7F:236FE12:2EAFAFA:696B14B5 |
| html-safe-nonce | 571b7d1a66f2484a7425d8c7d40d0bcf0070c10eade16b26e83bf5a8fea93fdb |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzdDOjE2NEU3RjoyMzZGRTEyOjJFQUZBRkE6Njk2QjE0QjUiLCJ2aXNpdG9yX2lkIjoiMTUxMDAyNzE2Nzc5MTc4MTA0NSIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | a40ed7187d5585460b7ecd0e4e23292db89bc96ffbae2525238c49a1b95f5514 |
| hovercard-subject-tag | issue:1825738630 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/107369/issue_layout |
| twitter:image | https://opengraph.githubassets.com/d12092a5218b500f8f9255bd5adcd9e33929c694ff6117174b62c1ab7fc76d29/python/cpython/issues/107369 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/d12092a5218b500f8f9255bd5adcd9e33929c694ff6117174b62c1ab7fc76d29/python/cpython/issues/107369 |
| og:image:alt | Current code: def indent(text, prefix, predicate=None): """Adds 'prefix' to the beginning of selected lines in 'text'. If 'predicate' is provided, 'prefix' will only be added to the lines where 'pr... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | methane |
| hostname | github.com |
| expected-hostname | github.com |
| None | 5f99f7c1d70f01da5b93e5ca90303359738944d8ab470e396496262c66e60b8d |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 82560a55c6b2054555076f46e683151ee28a19bc |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width