F-string support

sebastian

New member
I also suggest you consider something as an alternative to f-strings, I use them a lot and 11l just hates them right now (doesn't recognize them at all).
Perhaps you could use something with std::format in c++, and maybe have them pass either unaffected to 11l or use std::format or some alternative there. Keep in mind that std::format is not a perfect crossover, and as such there may be some f-string capabilities that can't cross over.

Thank you again for your time.
 

alextretyak

Administrator
Staff member
f-strings, I use them a lot
Can you bring some examples (maybe links to the source code, if it's open-source)?

Currently, Python → 11l transpiler supports Python % formatting (e.g. '%02d: %s' % (1, 's')) and str.format (e.g. '{:02}: {}'.format(1, 's') or '{n:02}: {s}'.format(n = 1, s = 's'), which translates into ‘#02: #.’.format(1, ‘s’)).

But if add support of f-strings into Python → 11l transpiler, then there are 3 possible ways to do it:
  1. Translate f"x = {x}" into ‘x = #.’.format(x).
  2. Translate f"x = {x}" into ‘x = ’x.
  3. Translate f"x = {x}" into f:‘x = {x}’ (11l currently doesn't support this).
The second way looks nicer than others for my taste:
f"({a} {y} {b}) {x} ({c} {z} {d})" (Python f-string)
vs
‘(’a‘ ’y‘ ’b‘) ’x‘ (’c‘ ’z‘ ’d‘)’ (11l)
but there is a problem with format specifiers, e.g. f'{x:02}'.
Therefore, I need some code examples to understand which way of supporting f-strings is better.
 
Last edited:

sebastian

New member
This function uses a lot of f-strings to output a list nicely. Here is the code:
Python:
def return_exp_list_as_str(obj: list[Exponent], fancy: bool = False, brackets: str = "[]", just_print: bool = False) -> str:
    output = ""
    if fancy:
        try:
            output += f"{brackets[0]}"
        except IndexError:
            pass
        output += f"{obj[0]}, \n"
        for exp in obj[1:-1]:
            output += f" {exp},\n"
        try:
            output += f" {obj[-1]}{brackets[1]}\n"
        except IndexError:
            pass
    elif just_print:
        lst = []
        for exp in obj:
            lst.append(str(exp.b) + superscript(exp.p))
        for i in lst[:-1]:
            output += i
            output += " * "
        output += lst[-1]
    else:
        try:
            output += f"{brackets[0]}"
        except IndexError:
            pass
        output += f"{obj[0]}, \n"
        for exp in obj[1:-1]:
            output += f" {exp}, \n"
        try:
            output += f" {obj[-1]}{brackets[1]}\n"
        except IndexError:
            pass
    return output
 

alextretyak

Administrator
Staff member
As I can see, there are no format specifiers in this code at all.
Can you look through all of your code, which you wish to compile with Python → 11l → C++ transpiler, and say are there any format specifiers inside f-strings? [Format specifier is a part of replacement field (expression delimited by curly braces {}) after the colon (:), e.g. alignment, width, precision, or type.]
 
Last edited:

alextretyak

Administrator
Staff member
Also I think that f-strings in this code are slightly overused. For example, f"{brackets[0]}" can be written as str(brackets[0]), or just brackets[0].
 

alextretyak

Administrator
Staff member
So, I decided to proceed the second way if possible {because it is possible in many cases, including your code}, and proceed the third way otherwise.
 
Top