-
Notifications
You must be signed in to change notification settings - Fork 728
/
python
230 lines (184 loc) · 4.17 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
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}}'
f'{value!r}'
# Unicode identifiers
α = 10
def coöperative(б):
return f"{б} is Russian"
def __init__(self, input_dim: list, output_dim: int, **kwargs):
super(AverageEmbedding, self).__init__(**kwargs)
self.input_dim = input_dim
self.output_dim = output_dim
@abstractmethod
def evaluate(self, *args, **kwargs):
raise NotImplementedError
def _get_metadata(self, input_samples: list[InputSample]) -> Tuple[str, str, int]:
project_name, id = self._get_info()
class Spam:
pass
spam = Spam()
# Doctest
def factorial(n):
"""Return the factorial of n, an exact integer >= 0.
>>> [factorial(n) for n in range(6)]
[1, 1, 2, 6, 24, 120]
>>> for i in [factorial(n) for n in range(2)]:
... print(i)
1
1
>>> factorial(30)
265252859812191058636308480000000
>>> factorial(-1)
Traceback (most recent call last):
...
ValueError: n must be >= 0
Factorials of floats are OK, but the float must be an exact integer:
>>> factorial(30.1)
Traceback (most recent call last):
...
ValueError: n must be exact integer
>>> factorial(30.0)
265252859812191058636308480000000
It must also not be ridiculously large:
>>> factorial(1e100)
Traceback (most recent call last):
...
OverflowError: n too large
"""
print("hello world")
def do_nothing():
if False:
return
print("a...a")
with tracer.start_as_current_span('App Init'):
logger.info('OTEL Initialized...')
app = FastAPI()
FastAPIInstrumentor.instrument_app(app)
# Keyword shouldn't be read as function call
def take_first(pairs):
for (first, _) in pairs:
yield first
self.for('nonsense')
# Distinguish between builtins and method calls
self.filter(filter(lambda x: x == 1, items))