@@ -836,20 +836,20 @@ Hashlib
836
836
Threading
837
837
---------
838
838
``` python
839
- import threading
839
+ from threading import Thread, RLock
840
840
```
841
841
842
842
### Thread
843
843
``` python
844
- thread = threading. Thread(target = < function> , args = (< first_arg> , ))
844
+ thread = Thread(target = < function> , args = (< first_arg> , ))
845
845
thread.start()
846
846
...
847
847
thread.join()
848
848
```
849
849
850
850
### Lock
851
851
``` python
852
- lock = threading. Rlock()
852
+ lock = Rlock()
853
853
lock.acquire()
854
854
...
855
855
lock.release()
@@ -998,43 +998,48 @@ Eval
998
998
----
999
999
### Basic
1000
1000
``` python
1001
- >> > import ast
1002
- >> > ast. literal_eval(' 1 + 1' )
1001
+ >> > from ast import literal_eval
1002
+ >> > literal_eval(' 1 + 1' )
1003
1003
2
1004
- >> > ast. literal_eval(' [1, 2, 3]' )
1004
+ >> > literal_eval(' [1, 2, 3]' )
1005
1005
[1 , 2 , 3 ]
1006
1006
```
1007
1007
1008
1008
### Detailed
1009
1009
``` python
1010
1010
import ast
1011
+ from ast import Num, BinOp, UnaryOp
1011
1012
import operator as op
1012
1013
1013
1014
# Supported operators
1014
- operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul,
1015
- ast.Div: op.truediv, ast.Pow: op.pow, ast.BitXor: op.xor,
1016
- ast.USub: op.neg}
1017
-
1018
- def eval_expr (expr ):
1019
- return eval_(ast.parse(expr, mode = ' eval' ).body)
1020
-
1021
- def eval_ (node ):
1022
- if isinstance (node, ast.Num): # <number>
1015
+ operators = {ast.Add: op.add,
1016
+ ast.Sub: op.sub,
1017
+ ast.Mult: op.mul,
1018
+ ast.Div: op.truediv,
1019
+ ast.Pow: op.pow,
1020
+ ast.BitXor: op.xor,
1021
+ ast.USub: op.neg}
1022
+
1023
+ def evaluate (expr ):
1024
+ return eval_node(ast.parse(expr, mode = ' eval' ).body)
1025
+
1026
+ def eval_node (node ):
1027
+ if isinstance (node, Num): # <number>
1023
1028
return node.n
1024
- elif isinstance (node, ast. BinOp): # <left> <operator> <right>
1025
- return operators[type (node.op)](eval_ (node.left), eval_ (node.right))
1026
- elif isinstance (node, ast. UnaryOp): # <operator> <operand> e.g., -1
1027
- return operators[type (node.op)](eval_ (node.operand))
1029
+ elif isinstance (node, BinOp): # <left> <operator> <right>
1030
+ return operators[type (node.op)](eval_node (node.left), eval_node (node.right))
1031
+ elif isinstance (node, UnaryOp): # <operator> <operand> e.g., -1
1032
+ return operators[type (node.op)](eval_node (node.operand))
1028
1033
else :
1029
1034
raise TypeError (node)
1030
1035
```
1031
1036
1032
1037
``` python
1033
- >> > eval_expr (' 2^6' )
1038
+ >> > evaluate (' 2^6' )
1034
1039
4
1035
- >> > eval_expr (' 2**6' )
1040
+ >> > evaluate (' 2**6' )
1036
1041
64
1037
- >> > eval_expr (' 1 + 2*3**(4^5) / (6 + -7)' )
1042
+ >> > evaluate (' 1 + 2*3**(4^5) / (6 + -7)' )
1038
1043
- 5.0
1039
1044
```
1040
1045
@@ -1115,10 +1120,10 @@ Curses
1115
1120
------
1116
1121
``` python
1117
1122
# $ pip3 install curses
1118
- import curses
1123
+ from curses import wrapper
1119
1124
1120
1125
def main ():
1121
- curses. wrapper(draw)
1126
+ wrapper(draw)
1122
1127
1123
1128
def draw (screen ):
1124
1129
screen.clear()
@@ -1197,7 +1202,7 @@ Web
1197
1202
``` python
1198
1203
# $ pip3 install bottle
1199
1204
import bottle
1200
- import urllib
1205
+ from urllib.parse import unquote
1201
1206
```
1202
1207
1203
1208
### Run
@@ -1217,7 +1222,7 @@ def send_image(image):
1217
1222
``` python
1218
1223
@route (' /<sport>' )
1219
1224
def send_page (sport ):
1220
- sport = urllib.parse. unquote(sport).lower()
1225
+ sport = unquote(sport).lower()
1221
1226
page = read_file(sport)
1222
1227
return template(page)
1223
1228
```
@@ -1227,7 +1232,7 @@ def send_page(sport):
1227
1232
@post (' /p/<sport>' )
1228
1233
def p_handler (sport ):
1229
1234
team = bottle.request.forms.get(' team' )
1230
- team = urllib.parse. unquote(team).lower()
1235
+ team = unquote(team).lower()
1231
1236
1232
1237
db = sqlite3.connect(< db_path> )
1233
1238
home_odds, away_odds = get_p(db, sport, team)
0 commit comments