Software is grown,not built.
bill de hóra
學(xué)習(xí)完基礎(chǔ)绘迁,按照進(jìn)度今天應(yīng)該是做一個(gè)自動(dòng)備份的編程富纸,但是這兩天邊上沒(méi)電腦黔衡,只能手機(jī)先摘記债蓝。雖然沒(méi)有在電腦上用markdown方便,但了勝于無(wú)鉴嗤。
主題:對(duì)所有重要文件(如coding)做備份
寫(xiě)軟件酷宵,總結(jié)有以下這些階段:
1.What(Analysis分析)
2.How(Design設(shè)計(jì))
3.Do it(Implementation實(shí)施)
4.Test(Testing or Debugging測(cè)試)
5.Use(Operation or Deployment使用)
6.Maintain(Refinement改善)
書(shū)中也是因此,特意用了四個(gè)vision來(lái)闡述躬窜,由粗到精,一步一步圍繞主題在不同階段分析怎么去編程去改善炕置。
分析:
- 文件和目錄備份在一個(gè)數(shù)列中荣挨;
- 備份必須儲(chǔ)存在一個(gè)主備份目錄下;
- 文件要打包成一個(gè)zip文件朴摊;
- zip檔案用當(dāng)前日期和時(shí)間來(lái)命名默垄;
- 用標(biāo)準(zhǔn)的zip命令,Linux和Unix下默認(rèn)可利用甚纲。(Windows用戶要從GnuWin32 project page下載安裝zip命令口锭,并將C:\Program Files\GnuWin32\bin添加到系統(tǒng)路徑環(huán)境變量中。
解決:
First version
import os
import time
#1.The files and directories to be backed up #are specified in a list.
source=['''C:\\My Documents''']
#Wo have to use double quotes inside a #string for names with space in it.Wo could #have also used a raw [r'C:\My Documents'].
#2.The backup must be stored in a main backup directory.
taget_dir='E:\\backup'
#3. The files are backed up into a zip file.
#4. The name of the zip archive is the current date and time
taget=taget_dir+os.sep+\time.strftime('%Y%m%d%H%m%S')+'.zip'
#Create target directory if it is not present
if not os.path.exists(target_dir):
os.mkdir(target_dir)
#5. We use the zip command to put the files in a zip archive
zip_command='zip -r{0}{1}'.format(target,''.jion(source))
# Run the buckup
print('Zip command is:')
print(zip_command)
print('Running:')
if os.system(zip_command)==():
print('Successful backup to',target)
else:
print('Backup FAILED')
Second version
make some refinement
一個(gè)更好的文件命名——在主備份目錄下介杆,用當(dāng)前日期創(chuàng)建目錄鹃操,里面文件用時(shí)間來(lái)命名
- 優(yōu)點(diǎn)1.備份顯得有層次,因而更容易管理
- 優(yōu)點(diǎn)2.文件名更短
- 優(yōu)點(diǎn)3.分開(kāi)的目錄有助于檢查是否每天都備份了春哨,看下有沒(méi)有當(dāng)天目錄就明了
Third Version&Forth Version
有時(shí)需要將較大的變更也添加到備份的文件名中荆隘,這樣更加一目了然。
第三和第四版本是解決同一設(shè)想赴背,但是前者有個(gè)bug椰拒,需要我們把它找出來(lái)。
簡(jiǎn)單一記凰荚,手機(jī)寫(xiě)還是有點(diǎn)不習(xí)慣燃观。
最后附一個(gè)斐波那契數(shù)列,第一便瑟、二數(shù)為0缆毁,1,之后的數(shù)為前面兩者之和胳徽。积锅。
Python簡(jiǎn)便爽彤,幾句就解決:
def fibs(num):
result=[0,1]
for i in range(num-2):
result.append(result[-2]+result[-1])
return result