一起學Python吧~Day08

#!/bin/env python3
# -*- coding:utf8 -*-
# #學python3的第八天
# #time時間模塊
# #表示方式:
# # 時間戳(1970-1-1 0:0:0到某一時間點之間的秒數)
# # UTC時間(世界協調時,以英國格林威治所在的縱切面作爲標準時區 每隔15°是一個時區 共24個時區)
# # 9元組:(年月日時分秒,一週中的第幾天,一年中的第幾天,是否爲夏時制)
# #當前時間的時間戳
# import  time
# a = time.time()
# print(
#     a
# )
#
# #計算1+到10000001用多長時間
# import time
#
# result = 0
#
# start = time.time()
# for i in range(
#         1,10000001
# ):
#     result += i
# end = time.time()
#
# print(
#     result
# )
# print(
#     end - start
# )
#
# #字符串表示的UTC時間
# t = time.ctime()
# print(
#     t
# )
# t = time.ctime(0) #0是時間戳
# print(
#     t
# )
# #時間格式化
# t = time.strftime(
#     '%Y-%m-%d %H:%M:%S'
# )
# print(
#     t
# )
# print(
#     time.strftime(
#         '%Y-%m-%d %a %H:%M:%S'
#     )
# )
# #9元組時間
# t =  time.localtime()
# print(
#     t
# )
# print(
#     t.tm_year
# ) #從九元組裏截取年份
# tt = time.strptime(
#     '2019-11-11 00:00:00','%Y-%m-%d %H:%M:%S'
# )
# print(
#     tt
# )
#
# 截取時間段
# import time
#
# fname = '/home/student/PycharmProjects/untitled/my.log'
# t9 = time.strptime(
#     '2019-5-15 09:00:00', '%Y-%m-%d %H:%M:%S'
# )
# t12 =time.strptime(
#     '2019-05-15 12:00:00','%Y-%m-%d %H:%M:%S'
# )
#
# with open(
#         fname
# ) as fobj:
#     for line  in fobj:
#         t =time.strptime(
#             line[:19],'%Y-%m-%d %H:%M:%S'
#         )
#         if t9 <= t <= t12:
#             print(
#                 line,
#                 end=''
#             )
# 優化
# import time
#
# fname = '/home/student/PycharmProjects/untitled/my.log'
# t9 =time.strptime(
#     '2019-05-15 09:00:00','%Y-%m-%d %H:%M:%S'
# )
# t12 =time.strptime(
#     '2019-05-15 12:00:00','%Y-%m-%d %H:%M:%S'
# )
#
# with open(
#         fname
# )as fobj:
#     for line  in fobj:
#         t =time.strptime(
#             line[:19],'%Y-%m-%d %H:%M:%S'
#         )
#         if t > t12:
#             break
#         if t >= t9:
#             print(
#                 line,
#                 end=''
#             )
# #time.sleep#實現睡眠
# import time
#
# start = time.time(
#
# )
# time.sleep(3)
# end = time.time(
#
# )
#
# print(
#     start - end
# )
# #datetime模塊
# #取出當前時間 分別爲年月日時分秒毫秒
# from datetime import datetime
# t1 = datetime.now(
#
# )
# print(
#     t1
# )
# print(
#     t1.year,t1.month,t1.day,
#     sep='-'
# )
# print(
#     t1.hour,t1.minute,t1.second,t1.microsecond
# )
# #創建指定時間,沒有指定的,默認爲0
# t2 = datetime(
#     2019,11,11
# )
# print(
#     t2
# )
# #返回指定格式字符串
# print(
#     t1.strftime(
#         '%Y-%m-%d' '%H:%M:%S'
#     )
# )
# #將指定字符串轉爲datetime對象
# t3 = datetime.strptime('2019-11-11 08:00:00', '%Y-%m-%d %H:%M:%S')
# print(
#     t3
# )
# 計算時間差
# from datetime import  datetime,timedelta
#
# t1 = datetime.now(
#
# )
# print(
#     t1
# )
#
# days = timedelta(
#     days=50,hours=1
# )
#
# h = t1 - days #50天1小時之前
# print(
#     h
# )
#
# h = t1 + days #50天1小時之後
# print(
#     h
# )
# #datetime改版
# from datetime import  datetime,timedelta
#
# fname = '/home/student/PycharmProjects/untitled/my.log'
#
# t9 =datetime.strptime(
#     '2019-05-15 09:00:00','%Y-%m-%d %H:%M:%S'
# )
# t12 =datetime.strptime(
#     '2019-05-15 12:00:00','%Y-%m-%d %H:%M:%S'
# )
#
# with open(
#         fname
# )as fobj:
#     for line  in fobj:
#         t =datetime.strptime(
#             line[:19],'%Y-%m-%d %H:%M:%S'
#         )
#         if t > t12:
#             break
#         if t >= t9:
#             print(
#                 line,
#                 end=''
#             )
# #異常處理:避免遇到錯誤程序崩潰導致終止 使得程序繼續運行
#
# """常見的報錯:
# NameError     //
# SyntaxError   //
# ...
# """
#
#
# # #***try處理
# # try:
# #     有可能發生異常的語句
# # except (異常1,異常2):
# #     發生異常時執行的代碼
# # except (異常4,異常3):
# #     發生異常時執行的代碼
# # else:
# #     不發生異常才執行的代碼
# #例子
#
# try:
#     n = int(
#         input(
#             'number: '
#         )
#     )
#     result = 100/n
#
# except (
#         ValueError,
#         FileExistsError
# ):  #錯誤輸出(多個可以加括號)
#     print(
#         'Invalid input.Try again.'
#     )
# except \
#         ZeroDivisionError:
#     print(
#         'Invalid input.Try again.'
#     )
# except \
#         KeyboardInterrupt:
#     print(
#         '\nBye-bye'
#     )
# except EOFError as echo: #將異常保存到變量echo中
#     print(
#         '\nBye-bye: ',
#         echo
#     )
# else:                     #正確纔會輸出
#     print(
#         result
#     )
# finally:                  #不管錯不錯都會輸出
#     print(
#         '***oooxxx***'
#     )
# print(
#     'Done'  #不管怎麼樣都會執行的Done
# )
# #*******************show time*******************
# import time
#
# t9 = time.strptime(
#     '2019-5-15 09:00:00','%Y:%m:%d %H:%M:%S'
# )
# t12 = time.strptime(
#     '2019-5-15 12:00:00','%Y:%m:%d %H:%M:%S'
# )
#
# with open(
#         'my.log'
# ) as fobj:
#     for line in fobj:
#         t = time.strptime(
#             line[:19],'%Y-%m-%d %H:%M:%S'
#         )
#         if t > t12:  #避免不必要的循環重複
#             break
#         if t >= t9:
#             print(
#                 line,end=''
#             )
#
# with open(
#         'my.log'
# ) as fobj:
#     for line in fobj:
#         t = time.strptime(
#             line[:19],'%Y-%m-%d %H:%M:%S'
#         )
#         if t9 <= t <= t12:
#             print(
#                 line,end=''
#             )
#
# from datetime import  datetime
#
# t9 = datetime.strptime(
#     '2019-5-15 09:00:00','%Y-%m-%d %H:%M:%S'
# )
# t12 = datetime.strptime(
#     '2019-5-15 12:00:00','%Y-%m-%d %H:%M:%S'
# )
#
# with open(
#         'my.log'
# ) as fobj:
#     for line in fobj:
#         t = datetime.strptime(
#             line[:19],'%Y-%m-%d %H:%M:%S'
#         )
#         if t > t12:
#             break
#         if t >= t9:
#             print(
#                 line,end=''
#             )
#
# import time
#
# result = 0
#
# start = time.time(
#
# )
# for i in range(
#         1,10000001
# ):
#     result += i
# end = time.time(
#
# )
#
# print(
#     result
# )
# print(
#     end - start
# )
#
# try:
#     n = int(
#         input(
#         'number: '
#     )
#     )
#     result = 100/n
#     print(
#         result
#     )
#     print(
#         'Done'
#     )
# except (
#         ValueError #值錯
# ):
#     print(
#         'Invalid input.Try again.'
#     )
# except (
#         ZeroDivisionError #100/0
# ):
#     print(
#         'Invalid input.Try again.'
#     )
# except (
#         KeyboardInterrupt #ctrl + c
# ):
#     print(
#         '\nBye-bye'
#     )
# except(
#     EOFError
# ):
#     print(
#         '\nBye-bye'
#     )
#
# try:
#     n = int(
#         input(
#             'number: '
#         )
#     )
#     result = 100 / n
#     print(
#         result
#     )
#     print(
#         'Done'
#     )
# #將常量保存到變量echo中
# except (
#     ValueError,
#     ZeroDivisionError #ctrl + d
# ) as echo:
#     print(
#         'Invalid input,Try again.',
#         echo
#     )
# except (
#     KeyboardInterrupt,
#     EOFError
# ):
#     print(
#         '\nBye-bye'
#     )
# #完整步驟
# try:
#     n = (
#         int(
#             input(
#                 'number: '
#             )
#         )
#     )
#     result = 100 / n
# except (
#     ValueError,
#     ZeroDivisionError
# ) as echo:
#     print(
#         'Invalid input.Try again.'
#     )
# except (
#     KeyboardInterrupt,
#     EOFError
# ):
#     print(
#         '\nBye-bye'
#     )
#     exit()
# else:
#     print(
#         result
#     )
# finally:
#     print(
#         'End'
#     )
#
# print(
#     'Done'
# )
# ***********************************************************
# #主動出發異常
# #使用raise制定觸發
#
# def set_age(name,age):
#     if not 0 < age < 120:
#         raise ValueError('Age out of range!') #指定異常
#     print('%s is %d years old'% (name,age))
#
# def set_age2(name,age):
#     assert 0 < age < 120, 'Age out of range!'
#     print('%s is %d years old'% (name,age))
#
# if __name__ == '__main__':
#     set_age('NB',20)
#     set_age2('NB',20)
# os模塊(系統命令)
import os
# print(
#     os.getcwd(
#
#     ) #pwd
# )
# print(
#     os.mkdir(
#         '/tmp/wxk'
#     ) #mkdir /wxk
# )
# print(
#     os.makedirs(
#         '/ak/wxk'
#     ) #mkdir -p /ak/wxk
# )
# print(
#     os.listdir(
#
#     ) #ls
# )
# print(
#     os.chdir(
#         '/tmp/nsd1906/demo' #cd /tmp/nsd1906/demo
#     )
# )
# print(
#     os.mknod(
#         'myfile.txt' #touch myfile.txt
#     )
# )
# print(
#     os.symlink(
#         '/etc/hosts',
#         'zhuji' #ln -s /etc/hosts zhuji
#     )
# )
# print(
#     os.stat(
#         '/etc/hosts'
#     )
# )
# hosts = os.stat(
#     '/etc/hosts' #fileinfo
# )
# print(
#     hosts.st_size
# ) #size
# print(
#     os.chmod(
#         'myfile.txt',
#         0o644 #linux權限是八進制數 chmod 644 myfile.txt(python中默認是十進制數)
#     )
# )
# print(
#     os.remove(
#         'zhuji' #rm zhuji
#     )
# )
# print(
#     os.path.abspath('python3') #取出絕對路徑
# )
# print(
#     os.path.dirname(
#         '/tmp/nsd1906/deno/myfile.txt' #名字
#     )
# )
# print(
#     os.path.split(
#         '/tmp/nsd196/demo/myfile.txt' #分離
#     )
# )
# print(
#     os.path.join(
#         '/tmp/nsd1906/demo','myfile.txt' #拼接
#     )
# )
# print(
#     os.path.isfile(
#         '/etc/hosts' #存在並且是文件麼
#     )
# )
# print(
#     os.path.ismount(
#         '/etc' #/etc是掛載點麼??
#     )
# )
# print(
#     os.path.isdir(
#         '/' #存在並且是目錄麼?
#     )
# )
# print(
#     os.path.islink(
#         '/etc/grub2.cfg' #存在並且是軟連接麼?
#     )
# )
# print(
#     os.path.exists(
#         '/etc' #存在麼?
#     )
# )
# os.walk使用
# 分析.result列表共有五項,每一項內容其結構完全一樣
# 字符串:路徑
# 如result[0](字符 列表 列表)
# 第一個列表:路徑下的目錄
# 第二個列表:路徑下的文件
# result = list(
#     os.walk(
#         '/etc/security'
#     )
# )
# print(
#     len(
#         result
#     )
# )
# 實現ls -R /etc/security功能
# import sys
# import os
#
#
# def lsdir_R(
#         directory
# ):
#     for (
#             path, folder, files
#     ) in os.walk(
#         directory
#     ):
#         print(
#             '%s:'
#             %
#             path
#         )
#         for d in folder:
#             print(
#                 '\033[34;1m%s\t\t\033[0m'
#                 %
#                 d,
#                 end=''
#             )
#         print(
#
#         )
#         for file in files:
#             print(
#                 '%s\t\t' % file,
#                 end=''
#             )
#         # print('\n')
#         print(
#
#         )
#         print(
#
#         )
#
#
# if __name__ == '__main__':
#     lsdir_R(
#         sys.argv[
#             1
#         ]
#     )

# #進度條(給程序加進度條)****process_bar
# from tqdm import tqdm
# import  time
#
# for i in tqdm(
#         range(
#             10
#         )
# ):
#     time.sleep(
#         0.2
#     )
#
# print(
#
# )
# #copy文件帶進度條
# import \
#     os
# import \
#     sys
# from \
#     tqdm\
#     import\
#     tqdm
#
# def \
#         copy\
#                 (
#                 src_fname,dst_fname,length=4096
#         ):
#     size = os.stat(
#         src_fname
#     ).st_size
#     times,extra = divmod(
#         size,
#         length
#     )
#     if extra:
#         times += 1
#
#     with open(
#             src_fname,
#             'rb'
#     )as src_fobj:
#         with open(
#                 dst_fname,'wb'
#         )as dst_fobj:
#             for\
#                     i\
#                     in\
#                     tqdm(
#                     range(
#                         times
#                     )
#             ):
#                 data = src_fobj.read(
#                     length
#                 )
#                 dst_fobj.write(
#                     data
#                 )
#
# if __name__ == '__main__':
#     copy(
#         sys.argv[
#             1
#         ],
#         sys.argv[
#             2
#         ]
#     )
#pickle模塊:能夠無損的取出
#存入的字典取出還是字典
# import pickle
#
# adict = {
#     'name':'bob',
#     'age':20
# }
# f =\
#     open(
#     '/tmp/a.data','wb'
#     )
# pickle.dump(
#     adict,f
# ) #將字典寫入文件,如果用wirte只能寫str類型
# f.close(
#
# )
# quit(
#
# )
#
# import picke
# f = open(
#     '/tmp/a.data','rb'
# )
# bdict = pickle.load(
#     f
# )
# f.close(
#
# )
# print(
#     bdict
# )
#記賬程序
import time


adict = {} #小字典
records = []  #總表
int_data = time.strftime('%Y-%m-%d') #date

def save_it(save=0): #存入餘額
    try:
        if save != 0:
            save = input('\033[33;1mre-save: \033[0m').strip()[0]
    except ValueError as prompt:
        print('\033[36;1mInvalid input,Try again: %s\033[0m' % prompt)
    except (KeyboardInterrupt,EOFError):
        print('\033[32;1m\nBye-bye!\033[0m')
    else:
        print('\033[32;1mSave successfully!\033[0m')
        return save


def cost_it(cost=0): #支出餘額
    try:
        if cost != 0:
            cost = input('\033[33;1mre-cost: \033[0m').strip()[0]
    except ValueError as prompt:
        print('\033[36;1mInvalid input,Try again: %s\033[0m' % prompt)
    except (KeyboardInterrupt,EOFError):
        print('\033[32;1m\nBye-bye!\033[0m')
    else:
        print('\033[32;1mCost successfully!\033[0m')
        return cost


def comment_it(): #零錢明細
    comment = input('\033[33;1re-mcomment: \033[0m').strip()[0]
    return comment

def view_it(): #賬單查詢
    print(records)

def write_list(): #寫入總表
    records.append(adict)

def show_menu():
    cmds = {'0': save_it,'1': cost_it,'2': view_it}
    prompt = """\033[33;1m(0)save
(1)cost
(2)view
(3)exit
請選擇(0/1/2/3/4): \033[0m """
    while True:
        choice = input(prompt).strip()[0]
        if choice == '0':
            save = input('\033[33;1msave: \033[0m').strip()[0]
            save_it(int(save))
            cost_it()
            comment_it()
            adict.clear()
            adict.update(
                {
                    'date': int_data,
                    'save': save_it(),
                    'cost': cost_it(),
                    'balance': counter,
                    'comment': comment_it()
                }
            )
            write_list()
        elif choice == '1':
            cost = input('\033[33;1mcost: \033[0m').strip()[0]
            cost_it(int(cost))
            save_it()
            comment_it()
            adict.clear()
            adict.update(
                {
                    'date': int_data,
                    'save': save_it(),
                    'cost': cost_it(),
                    'balance': counter,
                    'comment': comment_it()
                }
            )
            write_list()
        elif choice == '2':
            view_it()
        else:
            print('\nBye-bye')
            break

    cmds[choice]()


if __name__ == '__main__':
    if not records == []:
        counter = records[-1][-2] + save_it() if not save_it() > 0 else records[-1][-2] - cost_it()
    show_menu()





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章