File: bytes_formatting.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 19,092 kB
  • sloc: python: 83,539; ansic: 18,831; cpp: 1,402; xml: 1,031; javascript: 511; makefile: 403; sh: 204; sed: 11
file content (34 lines) | stat: -rw-r--r-- 668 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# mode: run
# tag: stringformat, bytesformat


import sys
IS_PY2 = sys.version_info[0] < 3


if IS_PY2:
    __doc__ = """
>>> print(format_bytes_with_str(u'abc'))
1 12170405abc6A
"""


def format_bytes():
    """
    >>> print(format_bytes())
    1 121704056A
    """
    cdef bytes result = b'%d%3i%x%02X%02.0f%g%c' % (
        1, 12, 23, 4, 5, 6, 65)
    assert type(result) is bytes
    return result.decode('ascii')


def format_bytes_with_str(s):
    """
    >>> print(format_bytes_with_str(b'abc'))
    1 12170405abc6A
    """
    result = b'%d%3i%x%02X%02.0f%s%g%c' % (
        1, 12, 23, 4, 5, s, 6, 65)
    return result if IS_PY2 else result.decode('ascii')