Skip to content

Commit 2e026b4

Browse files
committed
Merge pull request Show-Me-the-Code#131 from PyBeaner/master
PyBeaner's Show-Me-the-Code
2 parents 6afdaeb + 52b5da0 commit 2e026b4

11 files changed

Lines changed: 194 additions & 0 deletions

File tree

PyBeaner/0001/coupon.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
__author__ = 'PyBeaner'
2+
from random import choice
3+
import string
4+
5+
chars = string.ascii_uppercase + string.digits
6+
7+
8+
def generate_coupons(count, coupon_length=5):
9+
coupons = []
10+
for i in range(count):
11+
coupon = generate_one_coupon(coupon_length=coupon_length)
12+
while coupon in coupons:
13+
coupon = generate_one_coupon(coupon_length)
14+
15+
coupons.append(coupon)
16+
17+
return coupons
18+
19+
20+
def generate_one_coupon(coupon_length=5):
21+
coupon = []
22+
for i in range(coupon_length):
23+
ch = choice(chars)
24+
coupon.append(ch)
25+
return "".join(coupon)
26+
27+
28+
if __name__ == '__main__':
29+
coupons = generate_coupons(200, 5)
30+
print(coupons)

PyBeaner/0002/save_to_mysql.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
__author__ = 'PyBeaner'
2+
import pymysql.cursors
3+
4+
5+
def save_to_mysql(coupons):
6+
try:
7+
connection = pymysql.connect(host='localhost',
8+
user='user',
9+
passwd='passwd',
10+
db='db',
11+
cursorclass=pymysql.cursors.DictCursor)
12+
13+
with connection.cursor() as cursor:
14+
select_sql = "SELECT coupon FROM coupons where coupon='%s'"
15+
insert_sql = "INSERT INTO coupons VALUES (%s);"
16+
for coupon in coupons:
17+
cursor.excute(select_sql, (coupon,))
18+
result = cursor.fetchone()
19+
if result:
20+
continue
21+
22+
cursor.excute(insert_sql, (coupon,))
23+
24+
connection.commit()
25+
finally:
26+
connection.close()

PyBeaner/0004/wc.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from collections import Counter
2+
import re
3+
4+
__author__ = "PyBeaner"
5+
6+
with open(r"F:\Program Files\Git\doc\git\html\RelNotes\1.5.0.1.txt") as f:
7+
word_pat = re.compile("^[A-Za-z]+$")
8+
file_words = [word for line in f for word in line.split()
9+
if len(word) > 1 and word_pat.match(word)]
10+
11+
print(Counter(file_words))

PyBeaner/0007/code_lines.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# coding=utf-8
2+
__author__ = 'PyBeaner'
3+
import os
4+
import fnmatch
5+
6+
total_lines = 0
7+
code_lines = 0
8+
empty_lines = 0
9+
comment_lines = 0
10+
11+
12+
def count_line(line):
13+
line = line.lstrip()
14+
global comment_lines, empty_lines, total_lines, code_lines
15+
16+
total_lines += 1
17+
if line.startswith("#"):
18+
comment_lines += 1
19+
elif not line:
20+
empty_lines += 1
21+
else:
22+
code_lines += 1
23+
24+
25+
def scan_dir(directory, suffix="*.py"):
26+
directory = os.path.abspath(directory)
27+
print("Scanning files in %s ..." % directory)
28+
for cur_dir, dirs, files in os.walk(directory):
29+
for file in files:
30+
if not fnmatch.fnmatch(file, suffix):
31+
continue
32+
file_path = os.path.join(cur_dir, file)
33+
with open(file_path, errors="replace") as f:
34+
for line in f:
35+
count_line(line)
36+
37+
38+
if __name__ == '__main__':
39+
scan_dir(r"../..")
40+
print("Total lines:%d" % total_lines)
41+
print("Code lines:%d" % code_lines)
42+
print("Empty lines:%d" % empty_lines)
43+
print("Comment lines:%d" % comment_lines)

PyBeaner/0008/text_in_html.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# coding=utf-8
2+
__author__ = 'PyBeaner'
3+
from bs4 import BeautifulSoup
4+
5+
6+
def get_text(html):
7+
soup = BeautifulSoup(html)
8+
return soup.text
9+
10+
11+
if __name__ == '__main__':
12+
import requests
13+
14+
r = requests.get("https://github.com/")
15+
html = r.text
16+
text = get_text(html)
17+
with open("html.txt", "w+", errors="replace") as f:
18+
print(text, file=f)
19+
f.seek(0)
20+
for line in f:
21+
print(line)

PyBeaner/0009/link_in_html.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# coding=utf-8
2+
__author__ = 'PyBeaner'
3+
from bs4 import BeautifulSoup
4+
5+
6+
def get_links(html):
7+
soup = BeautifulSoup(html)
8+
links = []
9+
for link in soup.find_all("a"):
10+
href = link["href"]
11+
if href.startswith("http"):
12+
links.append(href)
13+
return links
14+
15+
16+
if __name__ == '__main__':
17+
import requests
18+
19+
r = requests.get("https://github.com/")
20+
html = r.text
21+
links = get_links(html)
22+
print(links)

PyBeaner/0011/filter.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
__author__ = 'PyBeaner'
2+
3+
words = open("filtered_words.txt").read().split()
4+
5+
word = input("Please Input an word:")
6+
if word in words:
7+
print("Freedom")
8+
else:
9+
print("Human Rights")

PyBeaner/0011/filtered_words.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
����
2+
����Ա
3+
����Ա
4+
�쵼
5+
ţ��
6+
ţ��
7+
����
8+
����
9+
love
10+
sex
11+
jiangge

PyBeaner/0012/filter.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
__author__ = 'PyBeaner'
2+
words = open("filtered_words.txt").read().split()
3+
4+
user_input = input("Please Input an word:")
5+
6+
for word in words:
7+
user_input = user_input.replace(word, "*" * len(word))
8+
9+
print(user_input)

PyBeaner/0012/filtered_words.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
����
2+
����Ա
3+
����Ա
4+
�쵼
5+
ţ��
6+
ţ��
7+
����
8+
����
9+
love
10+
sex
11+
jiangge

0 commit comments

Comments
 (0)