Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Berry added f-strings as an alternative to string formatting #18937

Merged
merged 1 commit into from
Jun 22, 2023

Conversation

s-hadinger
Copy link
Collaborator

Description:

Berry add support for f-strings (similar to python). f-strings are converted internally by lexer to their format equivalent and then parsed as normally. It works as a transpiler, syntactically rewriting code before parsing.

Principle:
f"begin { <expr> : <format>} end" becomes format("begin %<format> end", expr

Example:
f"2 + 2 equals {2+2:i}" becomes format("2 + 2 equals %i", 2+2)

If the format is unspecified, %s is inserted:
f'"foo" + "bar" equals {"foo"+"bar"} becomes format("\"foo\" + \"bar\" equals %s", "foo"+bar")

If you accidentally insert % in the format, it is ignored:
f"1/3 equals {1.0 / 3.0 :%.3f}" becomes format("1/3 equals %.3f", 1.0 / 3.0)

To output { use double brackets: (note that % are automatically doubled)
f"{{ 1% }}" becomes format("{ 1%% }")

Use the = (equal sign) to also output the expression:
f"{id=:i} {name=}" becomes format("id=%i name=%s", id, name)

Note: f-strings takes less than 1KB of flash memory in the lexer

Checklist:

  • The pull request is done against the latest development branch
  • Only relevant files were touched
  • Only one feature/fix was added per PR and the code change compiles without warnings
  • The code change is tested and works with Tasmota core ESP8266 V.2.7.4.9
  • The code change is tested and works with Tasmota core ESP32 V.2.0.10
  • I accept the CLA.

NOTE: The code change must pass CI tests. Your PR cannot be merged unless tests pass

@s-hadinger s-hadinger merged commit 85f3570 into arendst:development Jun 22, 2023
@barbudor
Copy link
Contributor

Berry is more and more pythonic 🤣
Next step, remove end 😵‍💫

@s-hadinger
Copy link
Collaborator Author

Nah...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants