{ "info": { "author": "Ryan Morshead", "author_email": "ryan.morshead@gmail.com", "bugtrack_url": null, "classifiers": [ "Intended Audience :: Developers" ], "description": "# fstr\n\n\"Build\n\"PyPI\"\n\"Code\n\"License:\n\n**1. Use f-string syntax in Python 2:**\n\n```python\nimport fstr\n\nx = 1\ny = 2\n\ntemplate = fstr(\"{x} + {y} = {x + y}\")\n\nprint(template.evaluate())\n```\n\n```\n1 + 2 = 3\n```\n\n**2. Use f-string syntax instead of `str.format` in both Python 2 and 3:**\n\n```python\nimport fstr\n\ncommon_error_message = fstr(\"function {function.__name__!r} failed because {error}\")\n\ndef add(x, y):\n try:\n return x + y\n except Exception as e:\n msg = common_error_message.format(function=add, error=e)\n print(msg)\n\ndef sub(x, y):\n try:\n return x + y\n except Exception as e:\n msg = common_error_message.format(function=sub, error=e)\n print(msg)\n\nadd(1, \"2\")\nsub(\"5\", 3)\n```\n\n```\nfunction 'add' failed because unsupported operand type(s) for +: 'int' and 'str'\nfunction 'sub' failed because can only concatenate str (not \"int\") to str\n```\n\n\n# Full [PEP-498](https://www.python.org/dev/peps/pep-0498) Compliance\n\nOther backward compatibility libraries for f-string syntax in Python 2 only implement some of the capabilities defined in the PEP's [specification](https://www.python.org/dev/peps/pep-0498/#specification). The test cases for `fstr` were even lifted (with minor changes) from [CPython's test suite](https://github.com/python/cpython/blob/master/Lib/test/test_fstring.py).\n\n\n## Format Specifiers\n\nFormat specifiers may contain evaluated expressions.\n\n```python\nimport fstr\nimport decimal\n\nwidth = 10\nprecision = 4\nvalue = decimal.Decimal('12.34567')\n\nfstr(\"result: {value:{width}.{precision}}\").evaluate()\n```\n\n```\n'result: 12.35'\n```\n\nOnce expressions in a format specifier are evaluated (if necessary), format specifiers are not interpreted by the f-string evaluator. Just as in `str.format()`, they are merely passed in to the `__format__()` method of the object being formatted.\n\n\n## Lambdas In Expressions\n\n```python\nimport fstr\n\nfstr(\"{(lambda x: x*2)(3)}\").format()\n```\n\n```\n'6'\n```\n\n## Error Handling\n\nExact messages will vary depending on whether you are using Python<3.6 or not.\n\n---\n\n```python\nimport fstr\n\nfstr(\"x={x\")\n```\n\n```\nFile \"fstr\", line 1\n x={x\n ^\nSyntaxError: Mismatched braces in f-string.\n```\n\n---\n\n```python\nimport fstr\n\nfstr(\"x={!x}\")\n```\n\n```\nFile \"fstr\", line 1\n x={!x}\n ^\nSyntaxError: Empty expresion not allowed.\n```\n\n\n# Performance Considerations\n\n`fstr` is not meant to be a replacement for python's f-string syntax. Rather it serves primarily as a slightly slower, but more convenient way to do string formatting in the\ncases where you might otherwise use `str.format`. Additionally Python's f-string syntax is able to make performance optimizations at compile time that are not afforded to either `str.format` or `fstr.format`. Given this we only compare `fstr.format` to `str.format`.\n\nThe performance of `fstr` differs depending on whether you:\n\n+ Use Python<3.6 or not.\n+ Define your f-string template ahead of time.\n\nFor example, this will be **significantly** slower\n\n```python\nfor i in range(10):\n s = fstr(\"{i}**2 = {i**2}\").format(i=i)\n```\n\nthan if you define your template outside the loop:\n\n```python\ntemplate = fstr(\"{i}**2 = {i**2}\")\n\nfor i in range(10):\n s = template.format(i=i)\n```\n\n## `str.format` vs `fstr.format`\n\n```python\nfrom timeit import timeit\n\nstr_setup = \"template = '{x}' * 10\"\nfstr_setup = \"import fstr\\ntemplate = fstr('{x}' * 10)\"\n\nstr_result = timeit(\"template.format(x=1)\", setup=str_setup, number=1000000)\nfstr_result = timeit(\"template.format(x=1)\", setup=fstr_setup, number=1000000)\n\nprint(\"str.format() : %s seconds\" % str_result)\nprint(\"fstr.format() : %s seconds\" % fstr_result)\n```\n\n### Python < 3.6\n\n```\nstr.format() : 0.741672992706 seconds\nfstr.format() : 6.77992010117 seconds\n```\n\n### Python >= 3.6\n\n```\nstr.format: 0.7007193689933047 seconds\nfstr.format: 0.9083925349987112 seconds\n```\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/rmorshea/fstr", "keywords": "fstring,f-string", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "fstr", "package_url": "https://pypi.org/project/fstr/", "platform": "Linux", "project_url": "https://pypi.org/project/fstr/", "project_urls": { "Homepage": "https://github.com/rmorshea/fstr" }, "release_url": "https://pypi.org/project/fstr/0.1.0a2/", "requires_dist": [ "six (==1.11.0)" ], "requires_python": "", "summary": "A library for performing delayed f-string evaluation.", "version": "0.1.0a2" }, "last_serial": 4692026, "releases": { "0.1.0a1": [ { "comment_text": "", "digests": { "md5": "1895d915260092cd386ccee1383f741b", "sha256": "b51a008afacdec85273009700cb5f0f88da3678531037ed19ad7037af73d8705" }, "downloads": -1, "filename": "fstr-0.1.0a1-py2-none-any.whl", "has_sig": false, "md5_digest": "1895d915260092cd386ccee1383f741b", "packagetype": "bdist_wheel", "python_version": "py2", "requires_python": null, "size": 5008, "upload_time": "2019-01-13T20:36:12", "url": "https://files.pythonhosted.org/packages/cc/e2/4d16ea4374db2d0ce3569072e7010f0774798f39765f92ed72689dde17c2/fstr-0.1.0a1-py2-none-any.whl" }, { "comment_text": "", "digests": { "md5": "7ce99fa8ca7f2ad01987a9dbbdc72545", "sha256": "25025ac74d6a304797f06c4278bae063adb47e5e3f52d68255848bd203c88a50" }, "downloads": -1, "filename": "fstr-0.1.0a1.tar.gz", "has_sig": false, "md5_digest": "7ce99fa8ca7f2ad01987a9dbbdc72545", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 4523, "upload_time": "2019-01-13T20:36:15", "url": "https://files.pythonhosted.org/packages/2a/3e/1f740be2282e344e19aa5afffccfe385f93859afe61e28d8af6b43423012/fstr-0.1.0a1.tar.gz" } ], "0.1.0a2": [ { "comment_text": "", "digests": { "md5": "81d98732c6cc0ad969e6ae2971425186", "sha256": "bd4b6e3b8fe7ff33a761fec109165ca990704b4a4c164171749a6842e9122488" }, "downloads": -1, "filename": "fstr-0.1.0a2-py3-none-any.whl", "has_sig": false, "md5_digest": "81d98732c6cc0ad969e6ae2971425186", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6930, "upload_time": "2019-01-13T23:04:15", "url": "https://files.pythonhosted.org/packages/61/21/57edec2a033ca59065e3bb51fa7364d10b2e4ccf4a15d5e61e3c0424b4dd/fstr-0.1.0a2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "51a24be7bd3e97331587aeb20f46972e", "sha256": "c9ea83f18ad7e10e3f3d0b53a18fb2d9134ccd9c6c9f4998bde9022dd783ad9b" }, "downloads": -1, "filename": "fstr-0.1.0a2.tar.gz", "has_sig": false, "md5_digest": "51a24be7bd3e97331587aeb20f46972e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6831, "upload_time": "2019-01-13T23:04:17", "url": "https://files.pythonhosted.org/packages/e8/19/d3f61d1ff62559f4fce0abcfe969d6d71865f391f1890305d0292664a7a8/fstr-0.1.0a2.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "81d98732c6cc0ad969e6ae2971425186", "sha256": "bd4b6e3b8fe7ff33a761fec109165ca990704b4a4c164171749a6842e9122488" }, "downloads": -1, "filename": "fstr-0.1.0a2-py3-none-any.whl", "has_sig": false, "md5_digest": "81d98732c6cc0ad969e6ae2971425186", "packagetype": "bdist_wheel", "python_version": "py3", "requires_python": null, "size": 6930, "upload_time": "2019-01-13T23:04:15", "url": "https://files.pythonhosted.org/packages/61/21/57edec2a033ca59065e3bb51fa7364d10b2e4ccf4a15d5e61e3c0424b4dd/fstr-0.1.0a2-py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "51a24be7bd3e97331587aeb20f46972e", "sha256": "c9ea83f18ad7e10e3f3d0b53a18fb2d9134ccd9c6c9f4998bde9022dd783ad9b" }, "downloads": -1, "filename": "fstr-0.1.0a2.tar.gz", "has_sig": false, "md5_digest": "51a24be7bd3e97331587aeb20f46972e", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 6831, "upload_time": "2019-01-13T23:04:17", "url": "https://files.pythonhosted.org/packages/e8/19/d3f61d1ff62559f4fce0abcfe969d6d71865f391f1890305d0292664a7a8/fstr-0.1.0a2.tar.gz" } ] }