Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
bytes f-string ?
#6
I have a less ugly solution using two functions ub() and bu() which stand respectively for 'unicode bytes' and 'bytes unicode'. It gives code like
>>> foo = b'spam'
>>> s = bu(f'{ub(foo)}bar')
>>> s
b'spambar'
Converting a bytes or a str with the ub() function returns a unicode string that contains only characters which ord is lower than 256. For str, it raises an exception if it is not possible, for example ub('€') fails. Converting a bytes or a str with bu() converts it to bytes but it will fail for str that contain unicode characters beyond 256. The first letter u or b mnemotechnically indicates if the function returns unicode or bytes, thus ub() returns unicode and bu() returns bytes.

Here is the code defining these functions
from functools import singledispatch

__version__ = '2021.06.11'


class _Ub(str):
    """A subtype of str that can contain only chars with ord < 256
    """
    __slots__ = ()
    
    def __new__(cls, s):
        instance = str.__new__(cls, s)
        instance.encode('latin-1') # fail if there is a char beyond 256
        return instance

    def __bytes__(self):
        return self.encode('latin-1')

@singledispatch
def ub(s):
    """Convert argument to 'unicode bytes' a subclass of str
    
    Returns an instance of a subclass of str that contains only
    unicode characters with ord < 256.

    'ub' stands for 'unicode bytes'
    """
    return _Ub(s)

@ub.register(bytes)
@ub.register(bytearray)
def _(s):
    return _Ub(s.decode('latin-1'))

@ub.register(_Ub)
def _(s):
    return s

@singledispatch
def bu(s):
    """Convert to bytes an object which str() has only characters < 256.
    
    'bu' stands for 'bytes unicode'
    """
    return bytes(ub(s))

@bu.register(bytes)
def _(s):
    return s

@bu.register(bytearray)
@bu.register(_Ub)
def _(s):
    return bytes(s)


def main():
    x = 'hello'
    print(ub(x))
    y = b'world'
    print(ub(y))
    print(bytes(y))
    z = bytearray(b'nice')
    print(ub(z))
    print(str(z))
    print(bytes(z))
    
    foo = b'spam'
    s = bu(f'{ub(foo)}bar')
    print(s)
    
if __name__ == '__main__':
    main()
Reply


Messages In This Thread
bytes f-string ? - by Skaperen - Jun-08-2021, 10:43 PM
RE: bytes f-string ? - by bowlofred - Jun-09-2021, 01:40 AM
RE: bytes f-string ? - by Gribouillis - Jun-09-2021, 06:47 AM
RE: bytes f-string ? - by Skaperen - Jun-09-2021, 06:27 PM
RE: bytes f-string ? - by DeaD_EyE - Jun-10-2021, 06:07 AM
RE: bytes f-string ? - by Gribouillis - Jun-10-2021, 10:21 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Error on import: SyntaxError: source code string cannot contain null bytes kirkwilliams2049 7 6,875 Aug-03-2023, 06:00 PM
Last Post: Gribouillis
  TypeError: int() argument must be a string, a bytes-like object or a number, not 'Non Anldra12 2 5,231 May-02-2021, 03:45 PM
Last Post: Anldra12
  how to do a bytes f-string? Skaperen 4 11,146 Jul-16-2020, 11:25 PM
Last Post: Skaperen
  Why, TypeError: expected string or bytes-like object ? JohnnyCoffee 3 18,637 May-08-2020, 04:26 AM
Last Post: bowlofred
  printing a bytes string Skaperen 2 2,373 Jul-21-2019, 03:42 AM
Last Post: Skaperen
  replace bytes with other byte or bytes BigOldArt 1 10,657 Feb-02-2019, 11:00 PM
Last Post: snippsat
  portion of the bytes to string ricardons 1 2,626 Mar-02-2018, 12:00 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020