Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

finished 07 #7

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions 01/01.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: UTF-8 -*-
from PIL import Image
from PIL import ImageDraw,ImageFont




im=Image.open("./qq.png")
name="4"
print(im.format,im.size,im.mode)
ps=ImageDraw.Draw(im)
box=im.size
ps.ink= 255 + 0 * 256 + 0 * 256 * 256
ps.text((im.size[0]-35,5),name,font=ImageFont.truetype("C:\Windows\Fonts\simfang.ttf",50))

im.show()
im.save("./qq1.png")

Binary file added 01/qq.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 01/qq1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions 02/02
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
adsf
10 changes: 10 additions & 0 deletions 02/02.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import random
coupon=[]

for i in range(200):
coupon.append(random.randint(10000000,99999999))


print(coupon)


54 changes: 54 additions & 0 deletions 03/03
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
1.安装MYSQL

2.安装install mysql-connector #安装驱动或者pip install mysql-connector-python --allow-external mysql-connector-python

3.测试
# 导入MySQL驱动:
>>>import mysql.connector
#连接dexterliao
conn = mysql.connector.connect(user='root', password='', database='dexterliao')
cursor = conn.cursor()
#创建user表单
cursor.execute('create table user (`id` INT UNSIGNED AUTO_INCREMENT, `code` VARCHAR(40) NOT NULL,PRIMARY KEY ( `id` ))')

4.安装SQLAlchemy

5.底层处理
from sqlalchemy import create_engine
mysql+mysqlconnector://<user>:<password>@<host>[:<port>]/<dbname>
engine = create_engine('mysql+mysqlconnector://root:password@localhost:3306/test')
6.创建表
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import create_engine


engine = create_engine('mysql+mysqlconnector://root:password@localhost:3306/test')

# 创建对象的基类:
Base = declarative_base()

# 定义User对象:
class User(Base):
# 表的名字:
__tablename__ = 'user'

# 表的结构:
id = Column(String(20), primary_key=True)
code = Column(String(20))
# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)


# 创建session对象:
session = DBSession()
# 创建新User对象:
new_user = User(id='5', name='Bob')
# 添加到session:
session.add(new_user)
# 提交即保存到数据库:
session.commit()
# 关闭session:
session.close()

38 changes: 38 additions & 0 deletions 03/03.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import create_engine
import random
import mysql.connector
#连接dexterliao
conn = mysql.connector.connect(user='root', password='', database='dexterliao')
cursor = conn.cursor()
#创建user表单
cursor.execute('create table user (`id` INT UNSIGNED AUTO_INCREMENT, `code` VARCHAR(40) NOT NULL,PRIMARY KEY ( `id` ))')
#sqlalchemy 连接mysql
engine = create_engine('mysql+mysqlconnector://root:@localhost:3306/dexterliao')

# 创建对象的基类:
Base = declarative_base()

# 定义User对象:
class User(Base):
# 表的名字:
__tablename__ = 'user'

# 表的结构:
id = Column(Integer, primary_key=True)
code = Column(String(40))
# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)
# 创建session对象:
session = DBSession()
# 创建新User对象:
for i in range(200):
new_code = User(id=(i+1), code=random.randint(10000000,99999999))
session.add(new_code)


session.commit()
session.close()

30 changes: 30 additions & 0 deletions 03/QUERY.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, UniqueConstraint, Index
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy import create_engine


#sqlalchemy 连接mysql
engine = create_engine('mysql+mysqlconnector://root:@localhost:3306/dexterliao')
# 创建对象的基类:
Base = declarative_base()
# 定义User对象:
class User(Base):
# 表的名字:
__tablename__ = 'user'

# 表的结构:
id = Column(Integer, primary_key=True)
code = Column(String(40))

# 创建DBSession类型:
DBSession = sessionmaker(bind=engine)
# 创建Session:
session = DBSession()
# 创建Query查询,filter是where条件,最后调用one()返回唯一行,如果调用all()则返回所有行:
user = session.query(User).filter(User.id==5).one()
# 打印类型和对象的name属性:
print('type:', type(user))
print('name:', user.code)
# 关闭Session:
session.close()
16 changes: 16 additions & 0 deletions 05/05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import re

def count_the_words(path):
with open(path) as f:
text=f.read()
wordlist=re.findall(r'[a-zA-Z0-9]+', text)
print(wordlist)
count=len(wordlist)

return count
if __name__ == '__main__':
nums = count_the_words('.\\file.txt')
print (nums)



20 changes: 20 additions & 0 deletions 05/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
# **�� 0004 �⣺**��һ��Ӣ�ĵĴ��ı��ļ���ͳ�����еĵ��ʳ��ֵĸ�����
import re

fin = open('source/0004-text.txt', 'r')
str = fin.read()

reObj = re.compile('\b?(\w+)\b?')
words = reObj.findall(str)

wordDict = dict()

for word in words:
if word.lower() in wordDict:
wordDict[word.lower()] += 1
else:
wordDict[word] = 1

for key, value in wordDict.items():
print('%s: %s' % (key, value))
1 change: 1 addition & 0 deletions 06/06
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
29 changes: 29 additions & 0 deletions 06/06.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from PIL import Image
import os



def Re_size(path):
iPhone5_WIDTH = 1136
iPhone5_HEIGHT = 640
new_path=r"\\new\\"
name_list=os.listdir(path)
for name in name_list:
photo=Image.open(path+name)
w, h = photo.size
print(name)
if w>iPhone5_WIDTH:
w=iPhone5_WIDTH
h=h*iPhone5_WIDTH//w
if h>iPhone5_HEIGHT:
h=iPhone5_HEIGHT
w=w*iPhone5_HEIGHT//h
resize_photo=photo.resize((w,h),Image.ANTIALIAS)# ANTIALIAS滤镜缩放
resize_photo.save(os.getcwd()+new_path+name)
print(name)




if __name__ == '__main__':
Re_size(r".//photos//")
8 changes: 8 additions & 0 deletions 06/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from PIL import Image
import os
path='.//photos//'
name_list=os.listdir(path)
for name in name_list:
print(path+name)
print(name_list)
print(os.getcwd())
1 change: 1 addition & 0 deletions 07/01
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a
32 changes: 32 additions & 0 deletions 07/07.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import os
import re

def get_txt(path):
f_path=[]
for root, dirs, files in os.walk(path):
print(root,"\n",dirs,"\n",files)
for f in files:
if f.lower().endswith(".txt"):
f_path.append(os.path.join(root,f))
return f_path

def text_analysis(f_path):
word_dic = {}
f=open(f_path)
text=f.read()
word_list=re.findall(r'[a-zA-Z]+',text)
print(word_list)
for w in word_list:
if w in word_dic:
word_dic[w]+=1
else:
word_dic[w]= 1
sort_wordic=sorted(word_dic.items(),key=lambda item:item[1])
print(sort_wordic)
print("在文件%s中,关键词为%s,出现次数为%s"%(os.path.basename(f_path),sort_wordic[-1][0],sort_wordic[-1][1]))

if __name__ == '__main__':
for file in get_txt(os.getcwd()):
text_analysis(file)


33 changes: 33 additions & 0 deletions 07/file.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""
�� 0006 �⣺����һ��Ŀ¼��������һ���µ��ռǣ����� txt��Ϊ�˱���ִʵ����⣬
�������ݶ���Ӣ�ģ���ͳ�Ƴ�����Ϊÿƪ�ռ�����Ҫ�Ĵʡ�
"""

import os
import re

def walk_dir(path):
file_path = []
for root, dirs, files in os.walk(path):
for f in files:
if f.lower().endswith('txt'):
file_path.append(os.path.join(root, f))
return file_path

def find_key_word(filepath):
word_dic = {}
filename = os.path.basename(filepath)
with open(filepath) as f:
text = f.read()
words_list = re.findall(r'[A-Za-z]+', text.lower())
for w in words_list:
if w in word_dic:
word_dic[w] += 1
else:
word_dic[w] = 1
sorted_word_list = sorted(word_dic.items(), key=lambda d: d[1])
print u"��%s�ļ��У�%sΪ�ؼ��ʣ���������%s��" %(filename, sorted_word_list[-1][0], sorted_word_list[-1][1])

if __name__ == "__main__":
for file_path in walk_dir(os.getcwd()):
find_key_word(file_path)
1 change: 1 addition & 0 deletions 08/08
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
40 changes: 40 additions & 0 deletions 08/08.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#coding:utf-8
import os

def get_py(path):
f_path=[]
for root, dirs, files in os.walk(path):
print(root,"\n",dirs,"\n",files)
for f in files:
if f.lower().endswith(".py"):
f_path.append(os.path.join(root,f))
return f_path
def py_analysis(py_path,x,y,z):
f=open(py_path,encoding='UTF-8')
f=f.read()
f=f.split("\n")
N_um=0
zhushi_line=0
emptyline=0
for line in f:
N_um += 1
if line.startswith('#'):
zhushi_line +=1
if line.startswith('\'\'\''):
zhushi_line += 1
if line.startswith('\"\"\"'):
zhushi_line += 1
if line.startswith(''):
emptyline+=1
print("在文件%s中,总数为%s行,注释行为%s,空行为%s" %(os.path.basename(py_path), N_um, zhushi_line, emptyline))
return N_um,zhushi_line,emptyline
if __name__ == '__main__':
total_num=0
total_zhushi_line=0
total_emptyline=0
for file in get_py(os.getcwd()):
b,c,d=py_analysis(file,x=0,y=0,z=0)
total_num += b
total_zhushi_line += c
total_emptyline += d
print("写入总数为%s行,注释行为%s,空行为%s" % (total_num, total_zhushi_line,total_emptyline))
9 changes: 9 additions & 0 deletions 08/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
def secondvalue(a,b):
c=8
return ("s","s",c)


x= secondvalue('s',2)[0]
y = secondvalue('s',2)[1]
z = secondvalue('s',2)[2]
print ('x:',x,'y:',y,'z:',z)