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

curly brackets in string format #20

Open
Winand opened this issue Apr 14, 2018 · 2 comments
Open

curly brackets in string format #20

Winand opened this issue Apr 14, 2018 · 2 comments
Labels
bug Something isn't working

Comments

@Winand
Copy link
Contributor

Winand commented Apr 14, 2018

pscript gives right result if I use two opening brackets and one closing bracket (3rd line)
Python gives the same result with two opening and two closing brackets (2nd line)

_pymeth_format.call('{"search":"{0}","source_lang": "{1}", "target_lang":"{2}"}', "phrase1", "lang1", "lang2");
"undefined","source_lang": "lang1", "target_lang":"lang2"}"

_pymeth_format.call('{{"search":"{0}","source_lang": "{1}", "target_lang":"{2}"}}', "phrase1", "lang1", "lang2");
"{"search":"phrase1","source_lang": "lang1", "target_lang":"lang2"}}"

_pymeth_format.call('{{"search":"{0}","source_lang": "{1}", "target_lang":"{2}"}', "phrase1", "lang1", "lang2");
"{"search":"phrase1","source_lang": "lang1", "target_lang":"lang2"}"
@almarklein almarklein added the bug Something isn't working label Apr 16, 2018
@almarklein
Copy link
Member

Thanks!

@Winand
Copy link
Contributor Author

Winand commented Oct 11, 2018

This code doesn't format text, it just puts field contents into square brackets and parses }} as expected. Just an idea of algorithm.

def formats(pt, *args, **kw):
    ret = ""
    p = 0
    ln = len(pt)
    while True:
        op = pt.find("{", p)
        cl = pt.find("}", p)
        if op > -1 and (op < cl or cl == -1):  # {
            if op == ln - 1:  # opening { is the last char
                raise ValueError("Single '{' encountered in format string") 
            elif pt[op+1] == "{":  # double {
                ret += pt[p: op+1]
                p = op + 2 # {{
            elif cl > -1:  # a field
                ret += pt[p: op]
                ret += "[" + pt[op+1: cl] + "]"  # FIELD
                p = cl + 1
            else:  # no closing }
                raise ValueError("Single '{' encountered in format string") 
        elif cl > -1:
            if pt[cl+1] == "}":
                ret += pt[p: cl+1]
                p = cl + 2 # }}
            else:
                raise ValueError("Single '}' encountered in format string") 
        else:
            ret += pt[p:]
            break
    return ret

Suddenly i've found out that there's a special case: '{:{width}.{prec}f}'.format(2.7182, width=5, prec=2)
In Python you can put field inside format part (after :) of another field. Only 1 level of nesting is supported. But it makes things more complicated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants