如何在python 3中实现字节对象的sprintf样式格式化?

par*_*ty3 9 python templates incompatibility bytestring python-3.x

我想在python3上做sprintf但是使用原始字节对象,而不必为%s进行任何手动转换.因此,将一个bytes对象作为"模板",加上任意类型的任意数量的对象,并返回一个呈现的字节对象.这就是python 2的sprintf%运算符一直在运行的方式.

b'test %s %s %s' % (5, b'blah','strblah') # python3 ==> error
Traceback (most recent call last):
  File "<input>", line 1, in <module>
TypeError: %b requires bytes, or an object that implements __bytes__, not 'int'

def to_bytes(arg):
    if hasattr(arg,'encode'): return arg.encode()
    if hasattr(arg,'decode'): return arg
    return repr(arg).encode()

def render_bytes_template(btemplate : bytes, *args):
    return btemplate % tuple(map(to_bytes,args))

render_bytes_template(b'this is how we have to write raw strings with unknown-typed arguments? %s %s %s',5,b'blah','strblah')

# output: b'this is how we have to render raw string templates with unknown-typed arguments? 5 blah strblah'
Run Code Online (Sandbox Code Playgroud)

但在python 2中,它只是内置:

'example that just works %s %s %s' % (5,b'blah',u'strblah')
# output: 'example that just works 5 blah strblah'
Run Code Online (Sandbox Code Playgroud)

有没有办法在python 3中执行此操作但仍然实现与python 2相同的性能?请告诉我,我错过了什么.这里的后备是在cython中实现(或者有没有python 3的库有帮助吗?)但仍然没有看到为什么它被从标准库中删除而不是字符串对象的隐式编码.我们不能只添加像format_any()这样的字节方法吗?

顺便说一句,它并不像这个警察那样简单:

def render_bytes_template(btemplate : bytes, *args):
    return (btemplate.decode() % args).encode()
Run Code Online (Sandbox Code Playgroud)

我不仅不想做任何不必要的编码/解码,而且还重新编写字节args而不是注入raw.

mat*_*gan 1

Would something like this work for you? You just need to make sure that when you begin some bytes object you wrap it in the new B bytes-like object which overloads the % and %= operators:

class B(bytes):
    def __init__(self, template):
        self._template = template

    @staticmethod
    def to_bytes(arg):
        if hasattr(arg,'encode'): return arg.encode()
        if hasattr(arg,'decode'): return arg
        return repr(arg).encode()

    def __mod__(self, other):
        if hasattr(other, '__iter__') and not isinstance(other, str):
            ret = self._template % tuple(map(self.to_bytes, other))
        else: 
            ret = self._template % self.to_bytes(other)
        return ret

    def __imod__(self, other):
        return self.__mod__(other)

a = B(b'this %s good')
b = B(b'this %s %s good string')
print(a % 'is')
print(b % ('is', 'a'))

a = B(b'this %s good')
a %= 'is'
b = B(b'this %s %s good string')
b %= ('is', 'a')
print(a)
print(b)
Run Code Online (Sandbox Code Playgroud)

This outputs:

b'this is good'
b'this is a good string'
b'this is good'
b'this is a good string'
Run Code Online (Sandbox Code Playgroud)