-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
337 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
|
||
#mulitprocessing is the module for IPC communication. | ||
import multiprocessing | ||
from multiprocessing import Process, Queue | ||
import os, time, random | ||
|
||
def write(q): | ||
print('Process to write:%s' %os.getpid()) | ||
for value in ['a', 'b', 'c']: | ||
print('Put %s in queue' %value) | ||
q.put(value) | ||
time.sleep(random.random()) | ||
|
||
def read(q): | ||
print('Process to read:%s' %os.getpid()) | ||
while True: | ||
value = q.get(True) | ||
print('Get %s from queue.' %value) | ||
|
||
if __name__ == '__main__': | ||
#Print how may cores we have in this computer. | ||
print('This computer has %s cores' %multiprocessing.cpu_count()) | ||
q = Queue() | ||
pw = Process(target = write, args = (q,)) | ||
pr = Process(target = read, args = (q,)) | ||
pw.start() | ||
pr.start() | ||
pw.join() | ||
pr.terminate() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
|
||
import threading, time | ||
|
||
#The balance variable. | ||
balance = 0 | ||
#Add lock for more than one threads to access the global resource. | ||
lock = threading.Lock() | ||
|
||
def loop(): | ||
print('thread %s is running...' %threading.current_thread().name) | ||
n = 0 | ||
while n < 5: | ||
print('thread %s >>> %s' %(threading.current_thread().name, n)) | ||
time.sleep(1) | ||
n = n + 1 | ||
print('thread %s end.' %threading.current_thread().name) | ||
|
||
def change_it(n): | ||
global balance | ||
balance = balance + n | ||
balance = balance - n | ||
|
||
def run_thread(n): | ||
for i in range(100): | ||
#Before changing the global resource, we try to get the lock for this resource. | ||
lock.acquire() | ||
try: | ||
change_it(n) | ||
finally: | ||
lock.release() | ||
|
||
if __name__ == '__main__': | ||
print('thread %s is running...' %threading.current_thread().name) | ||
''' | ||
t = threading.Thread(target = loop, name = 'LoopThread') | ||
t.start() | ||
t.join() | ||
print('thread %s end.' %threading.current_thread().name) | ||
''' | ||
t1 = threading.Thread(target = run_thread, args = (5,)) | ||
t2 = threading.Thread(target = run_thread, args = (10,)) | ||
t1.start() | ||
t2.start() | ||
t1.join() | ||
t2.join() | ||
print(balance) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
|
||
import sys | ||
import os | ||
|
||
class Student(object): | ||
'''The student class''' | ||
|
||
def __init__(self, name, score): | ||
self.name = name | ||
self.score = score | ||
|
||
def print_score(self): | ||
print('%s: %s' %(self.name, self.score)) | ||
|
||
class University_Student(Student): | ||
'''The University_Student based on the Student class''' | ||
pass | ||
|
||
if __name__ == "__main__": | ||
'''The bleow segment is example of os.fork.''' | ||
print('Process %s start' %os.getpid()) | ||
pid = os.fork() | ||
if pid == 0: | ||
print('I am the child process (%s) and my parent process is %s' %(os.getpid(), os.getppid())) | ||
else: | ||
print('I (%s) just created a child process(%s)' %(os.getpid(), pid)) | ||
theo = Student('Theo', 90) | ||
theo.print_score() | ||
|
||
emma = Student('Emma', 98) | ||
emma.print_score() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
# This codes are for house trade infomation. | ||
|
||
# -*- coding:utf-8 -*- | ||
|
||
import requests | ||
import re | ||
import sys, os | ||
|
||
|
||
print(u'''每个区对应代码如下: | ||
0:显示上海所有区单独的平均房价; | ||
1:浦东新区; | ||
2:闵行区; | ||
3:徐汇区; | ||
4:长宁区; | ||
5:普陀区; | ||
6:静安区; | ||
7:卢湾区; | ||
8:黄浦区; | ||
9:闸北区; | ||
a:虹口区; | ||
b:杨浦区; | ||
c:宝山区; | ||
d:嘉定区; | ||
e:青浦区; | ||
f:松江区; | ||
g:金山区; | ||
h:奉贤区; | ||
i:南汇区; | ||
j:崇明区; | ||
k:上海周边; | ||
''') | ||
|
||
#各地区页面代码 | ||
num_area = { | ||
'1':'http://wap.ganji.com/sh/fang5/pudongxinqu/o', | ||
'2':'http://wap.ganji.com/sh/fang5/minhang/o', | ||
'3':'http://wap.ganji.com/sh/fang5/xuhui/o', | ||
'4':'http://wap.ganji.com/sh/fang5/changning/o', | ||
'5':'http://wap.ganji.com/sh/fang5/putuo/o', | ||
'6':'http://wap.ganji.com/sh/fang5/jingan/o', | ||
'7':'http://wap.ganji.com/sh/fang5/luwan/o', | ||
'8':'http://wap.ganji.com/sh/fang5/huangpu/o', | ||
'9':'http://wap.ganji.com/sh/fang5/zhabei/o', | ||
'a':'http://wap.ganji.com/sh/fang5/hongkou/o', | ||
'b':'http://wap.ganji.com/sh/fang5/yangpu/o', | ||
'c':'http://wap.ganji.com/sh/fang5/baoshan/o', | ||
'd':'http://wap.ganji.com/sh/fang5/jiading/o', | ||
'e':'http://wap.ganji.com/sh/fang5/qingpu/o', | ||
'f':'http://wap.ganji.com/sh/fang5/songjiang/o', | ||
'g':'http://wap.ganji.com/sh/fang5/jinshan/o', | ||
'h':'http://wap.ganji.com/sh/fang5/fengxian/o', | ||
'i':'http://wap.ganji.com/sh/fang5/nanhui/o', | ||
'j':'http://wap.ganji.com/sh/fang5/chongming/o', | ||
'k':'http://wap.ganji.com/sh/fang5/shanghaizhoubian/o' | ||
} | ||
|
||
#各地区显示代码 | ||
area = { | ||
'1':u'浦东新区', | ||
'2':u'闵行区', | ||
'3':u'徐汇区', | ||
'4':u'长宁区', | ||
'5':u'普陀区', | ||
'6':u'静安区', | ||
'7':u'卢湾区', | ||
'8':u'黄浦区', | ||
'9':u'闸北区', | ||
'a':u'虹口区', | ||
'b':u'杨浦区', | ||
'c':u'宝山区', | ||
'd':u'嘉定区', | ||
'e':u'青浦区', | ||
'f':u'松江区', | ||
'g':u'金山区', | ||
'h':u'奉贤区', | ||
'i':u'南汇区', | ||
'j':u'崇明区', | ||
'k':u'上海周边' | ||
} | ||
|
||
#Get the price of each district. | ||
def get_price(numb): | ||
sp_list = [] | ||
for n in range(1,50): #抓取前50页 | ||
url = num_area[numb]+str(n) | ||
urlpage = requests.get(url) | ||
urlpage.encoding = 'utf-8' | ||
urltx = urlpage.text | ||
|
||
#Save the html context for analysis. | ||
with open(os.path.abspath('.') + '/projects/SampleProject/python_learn_excrise/' + str(n),'w') as f: | ||
f.write(urltx) | ||
|
||
#Get the price from each web page. | ||
size_price = re.findall(u'(\d+)\u33a1.*?(\d+)\u4e07\u5143',urltx,re.S) | ||
for sp in size_price: | ||
sp_list.append(sp) | ||
|
||
priclist = [] | ||
sum_pric = 0 | ||
i = 0 | ||
|
||
#Cal the average price. | ||
for sizepri in sp_list: | ||
pric = round(float(sizepri[1])/float(sizepri[0]),2) | ||
#print pric | ||
priclist.append(pric) | ||
sum_pric = sum_pric + pric | ||
i = i + 1 | ||
|
||
if i != 0: | ||
print(area[numb]+u"共获取二手房数量:"+str(i)+u",平均房价为:"+str(round(float(sum_pric)/float(i),2))+u"万元每平方") | ||
else: | ||
print("获取失败!请使用浏览器检查%s是否能正常显示!" % num_area[numb]) | ||
print(urltx) | ||
|
||
#Get the district. | ||
def get_area(): | ||
print('') | ||
area_input = input('please input the area code:') | ||
print('DEBUG:the area input is %s' %area_input) | ||
areanum = area_input | ||
if str(0) in areanum:#输入有0则计算全部区域 | ||
print('您输入的区域为:上海所有区域') | ||
for numb in ('1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k'): | ||
get_price(numb) | ||
else:#输入无0则计算输入区域 | ||
output = u"您输入的区域为:" | ||
for numb in ('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k'): | ||
if str(numb) in areanum: | ||
output = output+str(area[numb])+" " | ||
print(output) | ||
for numb in ('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k'): | ||
if str(numb) in areanum: | ||
get_price(numb) | ||
|
||
if __name__ == "__main__": | ||
get_area() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#!/usr/bin/env python | ||
# -*- coding:utf-8 -*- | ||
|
||
import tushare as ts | ||
import matplotlib.pyplot as plt | ||
from mpl_toolkits.mplot3d import Axes3D | ||
import numpy as np | ||
|
||
#Scatter | ||
def show_scratter(): | ||
x = np.linspace(-np.pi, np.pi, 256) | ||
y = np.cos(x) | ||
plt.scatter(x, y, marker = '.', color = 'blue') | ||
#set the x_start, x_end, y_start, y_end. | ||
#plt.axis([0, 10, 0, 10]) | ||
plt.show() | ||
|
||
#Line | ||
def show_line(): | ||
x = list(range(1,11)) | ||
y = list(np.cos(y) for y in x) | ||
plt.plot(x, y, linestyle = '--', label = 'picture') | ||
plt.show() | ||
|
||
#Plot line of stock shanghai | ||
def show_shstock_line(): | ||
df = ts.get_hist_data('sh', start = '2016-01-01') | ||
df.to_excel('stock_sh.xlsx') | ||
df.close.plot() | ||
ax = plt.gca() | ||
ax.invert_xaxis() | ||
plt.show() | ||
|
||
#Show multiple figures in one picture | ||
def show_multiple_figures(): | ||
fig = plt.figure() | ||
|
||
x = list(range(10, 90)) | ||
y = list(np.sin(y) for y in x) | ||
|
||
p1 = fig.add_subplot(211) | ||
p1.plot(x, y) | ||
|
||
p2 = fig.add_subplot(212) | ||
p2.scatter(x, list(np.sin(y + np.random.randn()) for y in x)) | ||
|
||
plt.show() | ||
|
||
#Show two types figures in single picture | ||
def show_two_in_single(): | ||
x = np.linspace(0, 10, 1000) | ||
y = np.sin(x) | ||
z = np.cos(x**2) | ||
|
||
plt.figure(figsize = (8, 4)) | ||
plt.plot(x, y, label = '$sin(x)$', color = "red", linewidth = 2) | ||
plt.plot(x, z, "b--", label = "$cos(x^2)$") | ||
plt.xlabel("Time(s)") | ||
plt.ylabel("Volt") | ||
plt.title('PyPlot First Example') | ||
plt.ylim(-1.2, 1.2) | ||
plt.legend() | ||
plt.show() | ||
|
||
#3D | ||
def show_3D(): | ||
fig = plt.figure() | ||
ax = fig.add_subplot(111, projection = '3d') | ||
|
||
x = [1, 1, 2, 2] | ||
y = [3, 4, 4, 3] | ||
z = [1, 100, 1, 1] | ||
|
||
ax.plot_trisurf(x, y, z) | ||
plt.show() | ||
|
||
if __name__ == "__main__": | ||
#show_line() | ||
show_scratter() | ||
#show_shstock_line() | ||
#show_multiple_figures() | ||
#show_two_in_single() | ||
#show_3D() | ||
|
Binary file not shown.