Python Operator Precedence

The upper operators have higher precedence than the lower.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Grouping			()
Function call			f()
Slicing				[index:index]
Array subscription		[]
Bitwise NOT			~x
Unary plus/minus		+, -
Mul/Div/Mod			*, /, %
Add/Sub				+, -
Bitwise shift			<<, >>
Bitwise AND			&
Bitwise XOR			^
Bitwise OR			|
Relation			==, !=, >, >=, <, <=, is, is not, in, not in
				not
				and
				or
				lambda

refer to:
https://discuss.codechef.com/t/operator-precedence-table/14545

start tiny web server with current folder as http root path

httpd.py

1
2
3
4
5
6
7
8
9
10
from http.server import HTTPServer, SimpleHTTPRequestHandler
 
handler = SimpleHTTPRequestHandler
handler.extensions_map.update({
	".js": "application/javascript",
})
 
server_address = ("", 8008)
server = HTTPServer(server_address, handler)
server.serve_forever()

how to run

1
python3 ./httpd.py

Fall-back from the nopython compilation path to the object mode compilation

出现此提示的原因是用@jit标注的函数内存在非数值运算语句。

比如下例中,time()函数和print()函数都不能出现在foo()函数中。正确写法为:

1
2
3
4
5
6
7
8
9
10
11
12
13
import numba as nb
import time
 
@nb.jit
def foo(x, y):
	s = 0
	for i in range(x, y):
		s += i
	return s
 
tt = time.time()
print(foo(1, 100000000))
print('Time used: {} secs'.format(time.time() - tt))

美化输出xml或json

xml

1
2
3
from xml.dom.minidom import *
with open('d:/xml.txt', 'r') as f:
    print(parseString(f.read()).toprettyxml())

json

1
python -m json.tool d:\json.txt

下载文件

1
2
3
4
import requests
resp = requests.get('http://file.url', headers={'User-Agent':'Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'})
with open("aa.dat", "wb") as f:
    f.write(resp.content)