1. 程式人生 > >python 練習一

python 練習一

python

#

#題目鏈接:https://github.com/Yixiaohan/show-me-the-code

#

1.備份文件

[[email protected] ~]# cat backup_file.py 
import time
import os
source=[‘/root/py/‘,‘/root/tx‘]
target="/tmp/"
today_dir=target+time.strftime(‘%Y%m%d‘)
time_dir=time.strftime(‘%H%M%S‘)
touch =today_dir+os.sep+time_dir+‘.zip‘
command_touch ="zip -qr "+touch+‘ ‘+‘ ‘.join(source)
print(command_touch)
if os.path.exists(today_dir)==False:
    os.mkdir(today_dir)
if os.system(command_touch)==0:
    print(‘Success backup UP‘)
else:
    print(‘Failed back‘)
[[email protected]
/* */ ~]#


0000 題解

#參考鏈接:PIL示例

from PIL import Image,ImageFont,ImageDraw
import os
def Modify_Image(Image_Path,num=1):
    img=Image.open(Image_Path)
    mod_img=ImageDraw.Draw(img)
    myfont=ImageFont.truetype(r"C:\Windows\Fonts\Verdana.ttf",36)
    mod_img.text((img.size[1]*3/4,0),str(num),fill=(255,0,0),font=myfont)
    save_path=os.path.dirname(Image_Path)+os.sep+"modify"+os.path.splitext(Image_Path)[1]
    img.save(save_path)
if __name__=="__main__":
    path=r"C:\Users\chauncey\Desktop\c.jpg"
    Modify_Image(path,4)

技術分享


0001 題解:

import uuid
def create_code(num,length):
    result=[]
    while True:
        uid_4=uuid.uuid4()
        uid_1=uuid.uuid1()
        x=str(uid_1).replace(‘-‘,‘‘)
        y=str(uid_4).replace(‘-‘,‘‘)
        result.append((x+y)[:length])
        if len(result)==num:
            break
    for code in result:
        print(code)
if __name__==‘__main__‘:
    create_code(200,21)
================= RESTART: C:/Users/chauncey/Desktop/0001.py
=================
5e742c9c7ccd11e79a0ef
5e742c9d7ccd11e7b164f
5e742c9e7ccd11e7a0baf
5e742c9f7ccd11e79768f
5e742ca07ccd11e79c95f
5e742ca17ccd11e79d15f
5e742ca27ccd11e7b8d2f
5e742ca37ccd11e79cd6f
5e742ca47ccd11e794d7f
5e742ca57ccd11e79356f
5e742ca67ccd11e7851cf
5e742ca77ccd11e790fbf
5e7453ae7ccd11e7983ef
5e7453af7ccd11e78775f
5e7453b07ccd11e79590f
5e7453b17ccd11e7a4f6f
......



0002 題解 參考: pymysql示例


#mysql
mysql> delete from py_uuid_0002;
Query OK, 0 rows affected (0.00 sec)

mysql> describe py_uuid_0002;
+-----------+-------------+------+-----+---------+-------+
| Field     | Type        | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| uuid_name | varchar(64) | YES  |     | NULL    |       |
+-----------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

mysql> select count(*) from py_uuid_0002;
+----------+
| count(*) |
+----------+
|      201 |
+----------+
1 row in set (0.00 sec)




#python

import uuid
import pymysql
def create_code(num,length):
    result=[]
    while True:
        uid_4=uuid.uuid4()
        uid_1=uuid.uuid1()
        x=str(uid_1).replace(‘-‘,‘‘)
        y=str(uid_4).replace(‘-‘,‘‘)
        result.append((x+y)[:length])
        if len(result)==num:
            break
    return result
def mysql_conn(host="192.168.174.131",Port=3307,user="root",passwd="123456",dbname="test"):
    db=pymysql.connect(host,user,passwd,dbname,port=Port)
    cursor=db.cursor()
    return db,cursor
def mysql_exec(db,cursor,result):
    for uuid_code in result:
        sql="insert into py_uuid_0002 values(‘"+uuid_code+"‘);"
        try:
            cursor.execute(sql)
            db.commit()
        except:
            db.rollback()
        
if __name__==‘__main__‘:
    result=create_code(201,21)
    db,cursor=mysql_conn()
    mysql_exec(db,cursor,result)
    db.close()



0003 題解: 參考鏈接: redis 搭建 redis list

import uuid
import redis
def create_code(num,length):
    result=[]
    while True:
        uid_4=uuid.uuid4()
        uid_1=uuid.uuid1()
        x=str(uid_1).replace(‘-‘,‘‘)
        y=str(uid_4).replace(‘-‘,‘‘)
        result.append((x+y)[:length])
        if len(result)==num:
            break
    return result
def save_redis(result):
    r=redis.Redis(host=‘192.168.174.128‘,port=7000,db=0)
    for uuid_code in result:
        r.lpush(‘uuid_code1‘,uuid_code)
if __name__==‘__main__‘:
    result=create_code(200,21)
    save_redis(result)
[[email protected] bin]# ./redis-cli -p 7000
127.0.0.1:7000> LRANGE ‘uuid_code1‘ 5 7
1) "c2df0cfc7d0211e7b85ef"
2) "c2df0cfb7d0211e78e3ff"
3) "c2df0cfa7d0211e79ce3f"
127.0.0.1:7000>




本文出自 “Chauncey” 博客,請務必保留此出處http://cqwujiang.blog.51cto.com/10808946/1954954

python 練習一