Skip to content

Commit ea198c4

Browse files
try 24
1 parent be8103a commit ea198c4

File tree

8 files changed

+435
-123
lines changed

8 files changed

+435
-123
lines changed

24-bak.py

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
from PIL import Image
2+
import logging
3+
4+
5+
def main():
6+
logging.basicConfig(filename="maze/24.log", filemode="w", level=logging.DEBUG,
7+
format='%(asctime)s=>%(message)s')
8+
file_path = "maze/maze.png"
9+
im = first_step(file_path)
10+
im_size = im.size
11+
width = im_size[0] # 641
12+
height = im_size[1] # 641
13+
# second_step(im, height)
14+
start = (639, 0)
15+
third_step(im, start, width, height)
16+
17+
18+
def first_step(file_path):
19+
im = Image.open(file_path)
20+
return im
21+
22+
23+
def second_step(im, height):
24+
y = 0
25+
for x in range(height):
26+
pixel = im.getpixel((x, y))
27+
if pixel != (255, 255, 255, 255) and pixel != (127, 127, 127, 255):
28+
print("x={}, y={}, pixel={}".format(x, y, pixel))
29+
30+
31+
def third_step(im, start, width, height):
32+
x = start[0]
33+
y = start[1]
34+
point_ary = [(x, y)]
35+
way = "0"
36+
ways = {way: point_ary}
37+
go_to_end(im, x, y, point_ary, way, ways)
38+
39+
40+
def router(im, points, way, ways):
41+
logging.info("router start: ways={}".format(ways))
42+
tmp_way_value = ways[way]
43+
logging.info("delting way={}".format(way))
44+
del ways[way]
45+
for i in range(len(points)):
46+
point = points[i]
47+
next_way = str(int(way) + 1 + i)
48+
logging.info("next_way={}".format(next_way))
49+
ways[next_way] = tmp_way_value
50+
x = point[0]
51+
y = point[1]
52+
next_point_ary = ways[next_way]
53+
next_point_ary.append((x, y))
54+
tmp_ways = ways
55+
tmp_ways_keys = tmp_ways.keys()
56+
for key in tmp_ways_keys:
57+
now_points = tmp_ways[key]
58+
now_points_tail = now_points[-1]
59+
x = now_points_tail[0]
60+
y = now_points_tail[1]
61+
go_to_end(im, x, y, now_points, key, ways)
62+
logging.info("router end: ways={}".format(ways))
63+
64+
65+
def go_to_end(im, x, y, point_ary, way, ways):
66+
logging.info("go_to_end start: ways={}".format(ways))
67+
points = find_next_point(im, x, y, point_ary)
68+
if points is None:
69+
logging.info("delting way={}".format(way))
70+
del ways[way]
71+
else:
72+
len_points = len(points)
73+
if len_points == 1:
74+
x = points[0][0]
75+
y = points[0][1]
76+
point_ary.append((x, y))
77+
go_to_end(im, x, y, point_ary, way, ways)
78+
else:
79+
router(im, points, way, ways)
80+
81+
82+
def find_next_point(im, x, y, ok_points):
83+
logging.info("now is ({}, {}) finding next_point".format(x, y))
84+
right_x = x + 1
85+
left_x = x - 1
86+
up_y = y - 1
87+
down_y = y + 1
88+
if right_x > 640:
89+
right_x_pixel = None
90+
else:
91+
right_x_pixel = im.getpixel((right_x, y))
92+
if left_x < 0:
93+
left_x_pixel = None
94+
else:
95+
left_x_pixel = im.getpixel((left_x, y))
96+
if up_y < 0:
97+
up_y_pixel = None
98+
else:
99+
up_y_pixel = im.getpixel((x, up_y))
100+
if down_y > 640:
101+
down_y_pixel = None
102+
else:
103+
down_y_pixel = im.getpixel((x, down_y))
104+
points = []
105+
right_point = right_x, y
106+
left_point = left_x, y
107+
up_point = x, up_y
108+
down_point = x, down_y
109+
next_director = {}
110+
if right_x_pixel is not None and right_x_pixel != (255, 255, 255, 255):
111+
# logging.info("right_x={}, y={}, right_x_pixel={}".format(right_x, y, right_x_pixel))
112+
points.append(right_point)
113+
if left_x_pixel is not None and left_x_pixel != (255, 255, 255, 255):
114+
# logging.info("left_x={}, y={}, left_x_pixel={}".format(left_x, y, left_x_pixel))
115+
points.append(left_point)
116+
if up_y_pixel is not None and up_y_pixel != (255, 255, 255, 255):
117+
# logging.info("x={}, up_y={}, up_y_pixel={}".format(x, up_y, up_y_pixel))
118+
points.append(up_point)
119+
if down_y_pixel is not None and down_y_pixel != (255, 255, 255, 255):
120+
# logging.info("x={}, down_y={}, down_y_pixel={}".format(x, down_y, down_y_pixel))
121+
points.append(down_point)
122+
for point in points:
123+
now_index = ok_points.index((x, y))
124+
before = ok_points[now_index - 1]
125+
if before == point:
126+
logging.info("removing {}".format(point))
127+
points.remove(point)
128+
if len(points) < 1:
129+
logging.info("return None")
130+
return None
131+
logging.info("return points={}".format(points))
132+
points2 = [points[0]]
133+
return points2
134+
135+
136+
if __name__ == "__main__":
137+
main()

24-test.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from queue import Queue
2+
3+
def main():
4+
first_step()
5+
6+
7+
def first_step():
8+
q = Queue()
9+
q.put('a')
10+
q.put('b')
11+
print(q.get())
12+
13+
14+
if __name__ == "__main__":
15+
main()

24-test2.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
from threading import Thread
2+
import queue
3+
4+
num_worker_threads = 10
5+
queue = queue.Queue(maxsize=0)
6+
7+
8+
def process(item):
9+
print("doing work for %(item)s" % locals())
10+
11+
12+
def worker():
13+
while True:
14+
item = queue.get()
15+
process(item)
16+
queue.task_done()
17+
18+
19+
for i in range(num_worker_threads):
20+
thread = Thread(target=worker)
21+
thread.daemon = True
22+
thread.start()
23+
24+
for item in range(10):
25+
queue.put(item)
26+
27+
queue.join()
28+
29+
print("finished")

24-test3.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import concurrent.futures
2+
import urllib.request
3+
4+
URLS = ['http://www.foxnews.com/',
5+
'http://www.cnn.com/',
6+
'http://europe.wsj.com/',
7+
'http://www.bbc.co.uk/',
8+
'http://some-made-up-domain.com/']
9+
10+
# Retrieve a single page and report the URL and contents
11+
def load_url(url, timeout):
12+
with urllib.request.urlopen(url, timeout=timeout) as conn:
13+
return conn.read()
14+
15+
# We can use a with statement to ensure threads are cleaned up promptly
16+
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
17+
# Start the load operations and mark each future with its URL
18+
future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
19+
for future in concurrent.futures.as_completed(future_to_url):
20+
url = future_to_url[future]
21+
try:
22+
data = future.result()
23+
except Exception as exc:
24+
print('%r generated an exception: %s' % (url, exc))
25+
else:
26+
print('%r page is %d bytes' % (url, len(data)))

24-test4.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from concurrent import futures
2+
3+
4+
def foo(x, y):
5+
print("{}-{}={}".format(y, x, y - x ))
6+
return y - x
7+
8+
9+
def main():
10+
with futures.ProcessPoolExecutor() as pool:
11+
pool.map(foo, [1, 2, 3], [4, 7, 1000])
12+
13+
14+
if __name__ == '__main__':
15+
main()

24-test5.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ def remove_dic(dic, key):
2323

2424
def start_operation(dic, pool):
2525
pool.submit(insert_dic, dic, "hello", "world")
26+
pool.submit(remove_dic, dic, "hello")
27+
pool.submit(insert_dic, dic, "liu", "bei")
28+
pool.submit(insert_dic, dic, "hi", "happy")
2629

2730

2831
def main():
29-
pool = ThreadPoolExecutor(max_workers=3)
32+
pool = ThreadPoolExecutor(max_workers=10000)
3033
# req(pool)
3134
dic = {}
3235
start_operation(dic, pool)

24.py

+4-122
Original file line numberDiff line numberDiff line change
@@ -1,134 +1,16 @@
11
from PIL import Image
2-
import logging
32

43

54
def main():
6-
logging.basicConfig(filename="maze/24.log", filemode="w", level=logging.DEBUG,
7-
format='%(asctime)s=>%(message)s')
85
file_path = "maze/maze.png"
9-
im = first_step(file_path)
10-
im_size = im.size
11-
width = im_size[0] # 641
12-
height = im_size[1] # 641
13-
# second_step(im, height)
14-
start = (639, 0)
15-
third_step(im, start, width, height)
6+
first_step(file_path)
167

178

189
def first_step(file_path):
1910
im = Image.open(file_path)
20-
return im
21-
22-
23-
def second_step(im, height):
24-
y = 0
25-
for x in range(height):
26-
pixel = im.getpixel((x, y))
27-
if pixel != (255, 255, 255, 255) and pixel != (127, 127, 127, 255):
28-
print("x={}, y={}, pixel={}".format(x, y, pixel))
29-
30-
31-
def third_step(im, start, width, height):
32-
x = start[0]
33-
y = start[1]
34-
point_ary = [(x, y)]
35-
way = "0"
36-
ways = {way: point_ary}
37-
go_to_end(im, x, y, point_ary, way, ways)
38-
39-
40-
def router(im, points, way, ways):
41-
logging.info("router start: ways={}".format(ways))
42-
tmp_way_value = ways[way]
43-
logging.info("delting way={}".format(way))
44-
del ways[way]
45-
for i in range(len(points)):
46-
point = points[i]
47-
next_way = str(int(way) + 1 + i)
48-
logging.info("next_way={}".format(next_way))
49-
ways[next_way] = tmp_way_value
50-
x = point[0]
51-
y = point[1]
52-
next_point_ary = ways[next_way]
53-
next_point_ary.append((x, y))
54-
ways_keys = ways.keys()
55-
for key in ways_keys:
56-
now_points = ways[key]
57-
now_points_tail = now_points[-1]
58-
x = now_points_tail[0]
59-
y = now_points_tail[1]
60-
go_to_end(im, x, y, now_points, key, ways)
61-
logging.info("router end: ways={}".format(ways))
62-
63-
64-
def go_to_end(im, x, y, point_ary, way, ways):
65-
logging.info("go_to_end start: ways={}".format(ways))
66-
points = find_next_point(im, x, y, point_ary)
67-
if points is None:
68-
logging.info("delting way={}".format(way))
69-
del ways[way]
70-
else:
71-
len_points = len(points)
72-
if len_points == 1:
73-
x = points[0][0]
74-
y = points[0][1]
75-
point_ary.append((x, y))
76-
go_to_end(im, x, y, point_ary, way, ways)
77-
else:
78-
router(im, points, way, ways)
79-
80-
81-
def find_next_point(im, x, y, ok_points):
82-
logging.info("now is ({}, {}) finding next_point".format(x, y))
83-
right_x = x + 1
84-
left_x = x - 1
85-
up_y = y - 1
86-
down_y = y + 1
87-
if right_x > 640:
88-
right_x_pixel = None
89-
else:
90-
right_x_pixel = im.getpixel((right_x, y))
91-
if left_x < 0:
92-
left_x_pixel = None
93-
else:
94-
left_x_pixel = im.getpixel((left_x, y))
95-
if up_y < 0:
96-
up_y_pixel = None
97-
else:
98-
up_y_pixel = im.getpixel((x, up_y))
99-
if down_y > 640:
100-
down_y_pixel = None
101-
else:
102-
down_y_pixel = im.getpixel((x, down_y))
103-
points = []
104-
right_point = right_x, y
105-
left_point = left_x, y
106-
up_point = x, up_y
107-
down_point = x, down_y
108-
next_director = {}
109-
if right_x_pixel is not None and right_x_pixel != (255, 255, 255, 255):
110-
logging.info("right_x={}, y={}, right_x_pixel={}".format(right_x, y, right_x_pixel))
111-
points.append(right_point)
112-
if left_x_pixel is not None and left_x_pixel != (255, 255, 255, 255):
113-
logging.info("left_x={}, y={}, left_x_pixel={}".format(left_x, y, left_x_pixel))
114-
points.append(left_point)
115-
if up_y_pixel is not None and up_y_pixel != (255, 255, 255, 255):
116-
logging.info("x={}, up_y={}, up_y_pixel={}".format(x, up_y, up_y_pixel))
117-
points.append(up_point)
118-
if down_y_pixel is not None and down_y_pixel != (255, 255, 255, 255):
119-
logging.info("x={}, down_y={}, down_y_pixel={}".format(x, down_y, down_y_pixel))
120-
points.append(down_point)
121-
for point in points:
122-
now_index = ok_points.index((x, y))
123-
before = ok_points[now_index - 1]
124-
if before == point:
125-
logging.info("removing {}".format(point))
126-
points.remove(point)
127-
if len(points) < 1:
128-
logging.info("return None")
129-
return None
130-
logging.info("return points={}".format(points))
131-
return points
11+
size = im.size
12+
width = size[0]
13+
height = size[1]
13214

13315

13416
if __name__ == "__main__":

0 commit comments

Comments
 (0)