-
Notifications
You must be signed in to change notification settings - Fork 728
/
python
157 lines (127 loc) · 2.46 KB
/
python
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
def lex(code, lexer):
"""
Lex ``code`` with ``lexer`` and return an iterable of tokens.
"""
try:
return lexer.get_tokens(code)
except TypeError, err:
if isinstance(err.args[0], str) and \
'unbound method get_tokens' in err.args[0]:
raise TypeError('lex() argument must be a lexer instance, '
'not a class')
raise
# quotes in strings
def foo():
"it's working"
def foo():
'he said "hi"'
def foo():
"""it's working"""
"""he said "hi" """
def foo():
'''he said "hi"'''
'''it's working'''
# unicode docstrings
def foo():
ur"""unicode-raw"""
def bar():
u"""unicode"""
def baz():
r'raw'
def zap():
"""docstring"""
# escaped characters in string literals
def baz():
'\a\b\f\n\r\t\v\"\''
'\N{DEGREE SIGN}'
'\uaF09'
'\UaaaaAF09'
'\xaf\xAF\x09'
'\007'
'.*\[p00t_(d\d{4})\].*' # There are no escape sequences in this string
# escaped characters in raw strings
def baz():
r'\a\b\f\n\r\t\v\"\''
r'\N{DEGREE SIGN}'
r'\uaF09'
r'\UaaaaAF09'
r'\xaf\xAF\x09'
r'\007'
r'.*\[p00t_(d\d{4})\].*'
# line continuations
apple.filter(x, y)
apple.\
filter(x, y)
1 \
. \
__str__
from os import path
from \
os \
import \
path
import os.path as something
import \
os.path \
as \
something
class \
Spam:
pass
class Spam: pass
class Spam(object):
pass
class \
Spam \
(
object
) \
:
pass
def \
spam \
( \
) \
: \
pass
def the_word_strand():
a = b.strip()
a = strand
b = band
c = strand
b = error
c = stror
c = string
py2_long = -123L
# Python 3
def test():
raise Exception from foo
def exceptions():
try:
print("Hello")
except Exception as e:
print("Exception")
# PEP 515
integer_literals = [
123, -1_2_0, 0, 00, 0_0_00, 0b1, 0B1_0, 0b_1, 0b00_0,
0o1, 0O1_2, 0O_1, 0o00_0, 0x1, 0X1_2, 0x_1, 0x00_0
]
float_literals = [
0., 1., 00., 0_00., 1_0_0., 0_1., .0,
.1_2, 3.4_5, 1_2.3_4, 00_0.0_0_0, 0_0.1_2,
0_1_2j, 0_00J, 1.2J, 0.1j, 1_0.J,
0_00E+2_34_5, 0.e+1, 00_1.E-0_2, -100.e+20, 1e01,
0_00E+2_34_5j, 0.e+1J, 00_1.E-0_2j, -100.e+20J 1e01j
]
floats = (19.0, 19.)
# PEP 465
a = b @ c
x @= y
# PEP 498
f'{hello} world {int(x) + 1}'
f'{{ {4*10} }}'
f'result: {value:{width}.{precision}}'
# Unicode identifiers
α = 10
def coöperative(б):
return f"{б} is Russian"