-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·315 lines (221 loc) · 7.39 KB
/
main.py
File metadata and controls
executable file
·315 lines (221 loc) · 7.39 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#!/usr/bin/env python3
"""
Features present in Python 3. Some may h
Source: <http://docs.python.org/3.0/whatsnew/3.0.html>
"""
import sys
if "##print function":
"""
Python 3.x removes the print statement and introduces the print function.
This is a saner than having an statement just for that,
but still less sane than Java's class strucutring `System.out.print`.
As part of increased sanity, extra options are available via standard
keyword arguments.
"""
print(1, 2, 3, sep=' ', end='\n', file=sys.stdout)
print(1, 2, 3, sep=' ', end='\n', file=sys.stderr)
if "#new syntax":
# Integer division breaks the ancestral C int floor type conversion.
# Use explicit floor division for that instead.
assert 1 / 2 == 0.5
assert 1 // 2 == 0
# Octal literals change for uniformity with hexadecimal and binary literals.
assert 0o12 == 10
# Extended unpacking:
a, *bs, c = range(0, 4)
assert a == 0
assert bs == [1, 2]
assert c == 3
if "##args":
if "Keyword argument after args":
def f(*args, after_args):
assert args == (0, 1)
assert after_args == 2
f(0, 1, after_args=2)
try:
f(0, 1, 2)
except TypeError:
pass
else:
assert False
if "empty args to ignore args":
def f(a, *, b):
assert a == 0
assert b == 1
# TODO stopped working
#f(0, 10, 11, b=1)
if "##annotations":
'''
Complement docstrings for doc generation.
'''
def foo(a: 'x', b: 5 + 6, c: list) -> max(2, 9):
pass
def f(a: "doc a",
b: "doc b",
c: 1,
d: 1 + 1,
e: [1, 2],
has_default: "doc has_default" = "default_val",
*args: 'doc args',
**kwargs: 'doc kwargs'
) -> 'doc return':
pass
assert f.__annotations__ == {
'a': 'doc a',
'b': 'doc b',
'c': 1,
'd': 2,
'e': [1, 2],
'args': 'doc args',
'kwargs': 'doc kwargs',
'has_default': 'doc has_default',
'return': 'doc return',
}
if "##view objects":
'''
dict keys, values and items return view instead of lists.
Those views are updated with the dict.
'''
d = {0: 'zero', 1: 'one'}
keys = d.keys()
assert set(keys) == {0, 1}
d[2] = 'two'
assert set(keys) == {0, 1, 2}
if "##nonlocal":
# Allows for closures. Closures are functions + a context.
def outer():
x = 1
def inner():
nonlocal x
x += 1
return x
return inner
inner1 = outer()
inner2 = outer()
assert inner1() == 2
assert inner1() == 3
assert inner1() == 4
# `inner1` and `inner2` have two separate versions of x:
assert inner2() == 2
# The minimal example of nonlocal usage is with a funciton inside a function.
#nonlocal i
# Gives:
# SyntaxError: nonlocal declaration not allowed at module level
# Globals cannot be declared as nonlocal:
i = 0
#def f(): nonlocal i
# Gives:
# SyntaxError: no binding for nonlocal 'i' found
# The nonlocal variable can come after the nonlocal statement:
def outer():
def inner():
nonlocal x
x += 1
return x
# This comes after the nonlocal:
x = 1
return inner
inner1 = outer()
inner2 = outer()
assert inner1() == 2
assert inner1() == 3
assert inner1() == 4
if "##built-in types":
if "##numbers":
'''
long does not exist anymore.
int is the same as long.
'''
#1L # SyntaxError
try:
long(1)
except NameError:
pass
else:
assert False
if "##sequence built-ins":
if "##str ##unicode ##bytes":
'''
The default source encoding is UTF-8. No need to add the
infamous `-*- coding: utf-8 -*-`, unless you want yet another encoding
(which you don't do you? =)).
'''
assert u'\u4E2D\u6587' == u'中文'
'''
In Python 3, the unicode situation changes drastically:
- Python 2 `unicode` becomes Python 3 `str`
- Python 3 `str` (Python 2 `unicode`) literals are writen either like Python 2 `str`: `'\uAAAA'`.
or old Python 2 `unicode` literals: `u'\uAAAA'`
- Python 2 `str` is Python 3 `byte`.
This is more intuitive, since in natural language terms,
a string like `é` is considered to contain one character only.
It is also more coherent with the `bytearray` type.
- Python 3 `byte` literals start with `b`: `b'\xAA\xBB'`
'''
assert '中'.encode('UTF-8') == b'\xE4\xB8\xAD'
assert '中' == b'\xE4\xB8\xAD'.decode('UTF-8')
s = str('\u4E2D\u6587')
s2 = '\u4E2D\u6587'
s3 = u'\u4E2D\u6587'
assert s == s2
assert s == s3
assert s[0] == '\u4E2D'
'''
TODO what do u escapes mean inside `b` literals? `\u0000` and `\U00000000` escapes.
'''
u = bytes(b'\u4E2D')
#print("%x" % u[0])
#assert u[0] == b'\x4E'
# `[]` takes and gives integer or `TypeError` is raised.
ba = b'a'
try:
ba[0] = b'b'
except TypeError:
pass
else:
assert False
# `bytes` is immutable like `str`.
b = b'a'
try:
b[0] = ord(b'b')
except TypeError:
pass
else:
assert False
'%d' % 1
if "##range ##xrange":
'''
In Python 3, the Python 2 `xrange` gloabal function and built-in types disappear
and are replaced by the `range()` function and Range `built-in` type.
Old Python 2 `range()` method disappears. It can be easily emulated via `list(range())`.
Since this object is iterable, it can be used in the similar situations as `xrange` in Python 2,
but it also supports more operations than simple iterators, for example efficient
presence test for integers.
Objects of type range have the advantage that the memory size of the object
does not depend on the range size since only the edges and the step need to be stored.
'''
assert list(range(0, 6, 2)) == [0, 2, 4]
'''
Inclusion tests for integers is `O(1)` since they can be made in via modulos.
'''
assert 4 in range(0, 6, 2)
'''
You can make a range out of anything that implements TODO
'''
class Myrange:
def __init__(self, i):
self.i = i
if "##truth value testing for objects":
'''
What if evaluates to True depends now only on the `__bool__()` value of an object.
In Python 2 this was a mixture of `__nonzero__` and `__len__`, much less neat.
'''
if "##super without args":
class A(object):
def __init__(self):
self.member = 0
class B(A):
def __init__(self):
super().__init__()
b = B()
assert b.member == 0