File: tuple_unpack_string.pyx

package info (click to toggle)
cython 3.0.11%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, 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 (106 lines) | stat: -rw-r--r-- 1,794 bytes parent folder | download | duplicates (10)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# mode: run
# tag: string, unicode, sequence unpacking, starexpr

def unpack_single_str():
    """
    >>> print(unpack_single_str())
    a
    """
    a, = 'a'
    return a

def unpack_str():
    """
    >>> a,b = unpack_str()
    >>> print(a)
    a
    >>> print(b)
    b
    """
    a,b = 'ab'
    return a,b

def star_unpack_str():
    """
    >>> a,b,c = star_unpack_str()
    >>> print(a)
    a
    >>> type(b) is list
    True
    >>> print(''.join(b))
    bbb
    >>> print(c)
    c
    """
    a,*b,c = 'abbbc'
    return a,b,c

def unpack_single_unicode():
    """
    >>> print(unpack_single_unicode())
    a
    """
    a, = u'a'
    return a

def unpack_unicode():
    """
    >>> a,b = unpack_unicode()
    >>> print(a)
    a
    >>> print(b)
    b
    """
    a,b = u'ab'
    return a,b

def star_unpack_unicode():
    """
    >>> a,b,c = star_unpack_unicode()
    >>> print(a)
    a
    >>> type(b) is list
    True
    >>> print(''.join(b))
    bbb
    >>> print(c)
    c
    """
    a,*b,c = u'abbbc'
    return a,b,c

# the following is not supported due to Py2/Py3 bytes differences

## def unpack_single_bytes():
##     """
##     >>> print(unpack_single_bytes().decode('ASCII'))
##     a
##     """
##     a, = b'a'
##     return a

## def unpack_bytes():
##     """
##     >>> a,b = unpack_bytes()
##     >>> print(a.decode('ASCII'))
##     a
##     >>> print(b.decode('ASCII'))
##     b
##     """
##     a,b = b'ab'
##     return a,b

## def star_unpack_bytes():
##     """
##     >>> a,b,c = star_unpack_bytes()
##     >>> print(a.decode('ASCII'))
##     a
##     >>> type(b) is list
##     True
##     >>> print(''.join([ch.decode('ASCII') for ch in b]))
##     bbb
##     >>> print(c.decode('ASCII'))
##     c
##     """
##     a,*b,c = b'abbbc'
##     return a,b,c