cython

Форк
0
/
unicode_formatting.pyx 
76 строк · 1.4 Кб
1
# mode: run
2
# tag: stringformat
3

4
from __future__ import unicode_literals
5

6

7
def ascii_format(a, int b, list c):
8
    """
9
    >>> print(ascii_format('x', 2, [1]))
10
    -'x'-2-[1]-
11
    """
12
    return '-%a-%a-%a-' % (a, b, c)
13

14

15
def repr_format(a, int b, list c):
16
    """
17
    >>> print(repr_format('x', 2, [1]))
18
    -'x'-2-[1]-
19
    """
20
    return '-%r-%r-%r-' % (a, b, c)
21

22

23
def str_format(a, int b, list c):
24
    """
25
    >>> print(str_format('x', 2, [1]))
26
    -x-2-[1]-
27
    """
28
    return '-%s-%s-%s-' % (a, b, c)
29

30

31
def mix_format(a, int b, list c):
32
    """
33
    >>> print(mix_format('x', 2, [1]))
34
    -x-2-[1]-
35
    """
36
    return '-%s-%r-%a-' % (a, b, c)
37

38

39
class PySubtype(unicode):
40
    def __rmod__(self, other):
41
        return f'PyRMOD({self}, {other})'
42

43

44
cdef class ExtSubtype(unicode):
45
    def __rmod__(self, other):
46
        return f'ExtRMOD({self}, {other})'
47

48

49
def subtypes():
50
    """
51
    >>> py, ext = subtypes()
52
    >>> print(py)
53
    PyRMOD(PySub, -%s-)
54
    >>> print(ext)
55
    ExtRMOD(ExtSub, -%s-)
56
    """
57
    return [
58
        '-%s-' % PySubtype("PySub"),
59
        '-%s-' % ExtSubtype("ExtSub"),
60
    ]
61

62

63
def format_int(value):
64
    """
65
    >>> print(format_int(5))
66
    5
67
    >>> print(format_int(5.0))
68
    5
69
    >>> format_int(u"xyz")  # doctest: +ELLIPSIS
70
    Traceback (most recent call last):
71
    TypeError: ...int...
72
    >>> format_int([])  # doctest: +ELLIPSIS
73
    Traceback (most recent call last):
74
    TypeError: ...int...
75
    """
76
    return "%d" % (value,)
77

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.