如何解析Python的三引号f字符串? [英] How can I parse Python's triple-quote f-strings?

查看:360
本文介绍了如何解析Python的三引号f字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码可以解析和处理普通的"f-string"模板字符串(有关示例,请参见下面的用法部分):

I have this code that parses and processes normal "f-string" template strings (See the usage part below for an example):

from string import Formatter
import sys


_conversions = {'a': ascii, 'r': repr, 's': str}

def z(template, locals_=None):
    if locals_ is None:
        previous_frame = sys._getframe(1)
        previous_frame_locals = previous_frame.f_locals
        locals_ = previous_frame_locals
        # locals_ = globals()
    result = []
    parts = Formatter().parse(template)
    for part in parts:
        literal_text, field_name, format_spec, conversion = part
        if literal_text:
            result.append(literal_text)
        if not field_name:
            continue
        value = eval(field_name, locals_) #.__format__()
        if conversion:
            value = _conversions[conversion](value)
        if format_spec:
            value = format(value, format_spec)
        else:
            value = str(value)
        result.append(value)
    res = ''.join(result)
    return res

用法:

a = 'World'
b = 10
z('Hello {a} --- {a:^30} --- {67+b} --- {a!r}')
# "Hello World ---             World              --- 77 --- 'World'"

但是,如果模板字符串是这样的话,它将不起作用:

But it doesn't work if the template string is something like this:

z('''
echo monkey {z("curl -s https://www.poemist.com/api/v1/randompoems | jq --raw-output '.[0].content'")} end | sed -e 's/monkey/start/'
echo --------------
''')

出现此错误:

  File "<string>", line 1
    z("curl -s https
                   ^
SyntaxError: EOL while scanning string literal

如果通常无法实现,我甚至愿意从Python的源代码中复制代码以使其正常工作.

I am willing to even copy code from Python's source code to get this to work, if it's not possible normally.

推荐答案

感谢@ForceBru的技巧,我完成了此操作.以下代码解析并处理源三元引用的f字符串:(忽略过程部分)

Thanks to the tip by @ForceBru, I finished this. The following code parses and processes source tripe-quote f-strings: (Ignore the process parts)

_conversions = {'a': ascii, 'r': repr, 's': str}

def zstring(self, template, locals_=None, getframe=1):
    if locals_ is None:
        previous_frame = sys._getframe(getframe)
        previous_frame_locals = previous_frame.f_locals
        locals_ = previous_frame_locals

    def asteval(astNode):
        if astNode is not None:
            return eval(compile(ast.Expression(astNode), filename='<string>', mode='eval'), locals_)
        else:
            return None

    def eatFormat(format_spec, code):
        res = False
        if format_spec:
            flags = format_spec.split(':')
            res = code in flags
            format_spec = list(filter(lambda a: a != code,flags))
        return ':'.join(format_spec), res


    p = ast.parse(f"f'''{template}'''")
    result = []
    parts = p.body[0].value.values
    for part in parts:
        typ = type(part)
        if typ is ast.Str:
            result.append(part.s)
        elif typ is ast.FormattedValue:
            # print(part.__dict__)

            value = asteval(part.value)
            conversion = part.conversion
            if conversion >= 0:
                # parser doesn't support custom conversions
                conversion = chr(conversion)
                value = self._conversions[conversion](value)

            format_spec = asteval(part.format_spec) or ''
            # print(f"orig format: {format_spec}")
            format_spec, fmt_eval = eatFormat(format_spec, 'e')
            format_spec, fmt_bool = eatFormat(format_spec, 'bool')
            # print(f"format: {format_spec}")
            if format_spec:
                value = format(value, format_spec)
            if fmt_bool:
                value = boolsh(value)

            value = str(value)
            if not fmt_eval:
                value = self.zsh_quote(value)
            result.append(value)
    cmd = ''.join(result)
    return cmd

这篇关于如何解析Python的三引号f字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆