{"id":26917903,"url":"https://github.com/xjzh123/fstringizer","last_synced_at":"2025-04-01T20:50:56.346Z","repository":{"id":65077295,"uuid":"581715462","full_name":"xjzh123/fstringizer","owner":"xjzh123","description":"a tool to convert python string concatenation expressions into f-string expressions","archived":false,"fork":false,"pushed_at":"2022-12-24T04:28:29.000Z","size":14,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2023-05-12T12:25:46.865Z","etag":null,"topics":["f-strings","python","python3"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/xjzh123.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-12-24T03:48:34.000Z","updated_at":"2022-12-24T04:23:08.000Z","dependencies_parsed_at":"2023-01-11T16:01:05.570Z","dependency_job_id":null,"html_url":"https://github.com/xjzh123/fstringizer","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xjzh123%2Ffstringizer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xjzh123%2Ffstringizer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xjzh123%2Ffstringizer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/xjzh123%2Ffstringizer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/xjzh123","download_url":"https://codeload.github.com/xjzh123/fstringizer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246709894,"owners_count":20821298,"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":["f-strings","python","python3"],"created_at":"2025-04-01T20:50:56.274Z","updated_at":"2025-04-01T20:50:56.338Z","avatar_url":"https://github.com/xjzh123.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Fstringizer for python\n\nThis is a tool to convert python string concatenation expressions into f-string expressions.  \n这是一个把python的字符串拼接表达式转换成f-string表达式的工具。\n\nHowever, this tool is just to help convert simple expressions. It is **NOT** made to generate such high-level code as shown in \u003chttps://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals\u003e.  \n然而，这个工具只是转换简单的表达式。它**不是**用来生成\u003chttps://docs.python.org/zh-cn/3/reference/lexical_analysis.html#formatted-string-literals\u003e里展示的那些高级代码的。\n\nThis tool can not convert f-string into string concatenation, or convert string concatenation into `str.format()`. It can **ONLY** convert string concatenation into f-string.  \n这个工具不能把f-string转换成字符串拼接，也不能把字符串拼接转换成`str.format()`。它**只能**把字符串拼接转换成f-string。\n\nThis tool may give strange outputs, but this should only happen when the input is not correct string concatenation expression.  \n如果你输入的都不是正确的字符串拼接表达式，这个工具的输出确实会很奇怪。否则，它的输出应该会是合法的f-string。\n\n```console\n\u003e python fstringizer.py \"a a\" \nf'{aa}'\n```\n\nIf you find any bug, please raise an simple issue. (Limited by my coding ability level,) I may decide whether to fix it based on how simple the input which causes a bug is. If you can pull request, I will appreciate it much.  \n如果你找到bug，请提个简单的issue。（因为我技术也很差）我可能会根据造成bug的输入有多简单来决定是否修复这个bug。如果你可以pull request，我就更感激了。\n\nThe original purpose of this tool is to help beginners who didn't know f-string and wrote many string concatenation to switch to f-string so that their code will become more readable.  \n这个工具的本意是帮助不知道f-string然后写了很多字符串拼接的新手把自己的代码改成用f-string的，这样代码就会更易读。\n\nLet's check out examples:  \n让我们看看示例：\n\n```python\n\u003e\u003e\u003e from fstringizer import *\n\u003e\u003e\u003e fstringize('i')\n\"f'{i}'\"\n\u003e\u003e\u003e fstringize(\"date + time + ':' + nick + ' recorded to join ' + channel\")\n\"f'{date}{time}:{nick} recorded to join {channel}'\"\n\u003e\u003e\u003e fstringize(r\"'\\\"A \\'' + key + '\\' is a \\'so-called\\' ' + d[key] + '\\\" -- ' + name\")\n'f\\'\"A \\\\\\'{key}\\\\\\' is a \\\\\\'so-called\\\\\\' {d[key]}\" -- {name}\\''\n\u003e\u003e\u003e print(fstringize(r\"'\\\"A \\'' + key + '\\' is a \\'so-called\\' ' + d[key] + '\\\" -- ' + name\"))\nf'\"A \\'{key}\\' is a \\'so-called\\' {d[key]}\" -- {name}'\n```\n\nYou can use fstringizer in 3 ways:  \n你可以用三个方式使用fstringizer：\n\n1. import  \n   import\n\n    ```python\n    from fstringizer.py import fstringize\n    print(fstringize(input()))\n    ```\n\n2. run and input  \n   直接运行然后输入\n\n    ```console\n    \u003e python fstringizer.py\n    Input your expression: '\"A \\'' + key + '\\' is a \\'so-called\\' ' + d[key] + '\" -- ' + name\n    f'\"A \\'{key}\\' is a \\'so-called\\' {d[key]}\" -- {name}'\n    ```\n\n3. run with parameter  \n   带参数运行\n\n    ```console\n    \u003e python fstringizer.py \"'\\\"A \\'' + key + '\\' is a \\'so-called\\' ' + d[key] + '\\\" -- ' + name\" \n    f'\"A \\'{key}\\' is a \\'so-called\\' {d[key]}\" -- {name}'\n    ```\n\nThere is a demo program:  \n有一个演示程序：\n\n```python\n# demo.py\nfrom fstringizer import fstringize\n\ndef demo(exp):\n    print(exp, fstringize(exp), '', sep='\\n')\n\nprint(fstringize(\"'some text [' + time + '] some text ' + nick.str() + awa[0]\"))\nprint()\n\ndemo('i')\ndemo(\"date + time + ':' + nick + ' recorded to join ' + channel\")\ndemo(\"'a' + time + 'b' + nick + 'awa'\")\ndemo(\"varName\")\ndemo(\"'some text'\")\nwith open('test.py', 'r', encoding='UTF-8') as fp:\n    lines = fp.read().splitlines()\n    noBlank = list(filter(\n        lambda line:\n            len(line.split('#')[0].replace(' ', '').replace('\\t', '')) \u003e 0,\n        lines\n    ))\n    for line in noBlank:\n        demo(line)\n\n```\n\n```python\n#test.py\n# These expressions can not be written in string literals easily in python, so I put them in a file.\n\n'some \\' text \\''\n\n'\\'some \\' text'\n\n' Var ' + key + ' is ' + d[key]\n# f' Var {key} is {d[key]}'\n\n'\"A \\'' + key + '\\' is a \\'so-called\\' ' + d[key] + '\" -- ' + name\n# f'\"A \\'{key}\\' is a \\'so-called\\' {d[key]}\" -- {name}'\n```\n\nRun demo.py:  \n运行demo.py：\n\n```python\nf'some text [{time}] some text {nick.str()}{awa[0]}'\n\ni\nf'{i}'\n\ndate + time + ':' + nick + ' recorded to join ' + channel\nf'{date}{time}:{nick} recorded to join {channel}'\n\n'a' + time + 'b' + nick + 'awa'\nf'a{time}b{nick}awa'\n\nvarName\nf'{varName}'\n\n'some text'\nf'some text'\n\n'some \\' text \\''\nf\"some ' text '\"\n\n'\\'some \\' text'\nf\"'some ' text\"\n\n' Var ' + key + ' is ' + d[key]\nf' Var {key} is {d[key]}'\n\n'\"A \\'' + key + '\\' is a \\'so-called\\' ' + d[key] + '\" -- ' + name\nf'\"A \\'{key}\\' is a \\'so-called\\' {d[key]}\" -- {name}'\n```\n\nThat's all.  \n就这样。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxjzh123%2Ffstringizer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fxjzh123%2Ffstringizer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fxjzh123%2Ffstringizer/lists"}