六月十八号作业

# 一:编写函数,(函数执行的时间用time.sleep(n)模拟)
# import time
# def index(x, y):
#     time.sleep(2)
#     print('index===>', x, y)
# index(1,2)
#
# 二:编写装饰器,为函数加上统计时间的功能
# from functools import wraps
# import time
#
# def outter(func):
#     @wraps(func)
#     def wrapper(*args, **kwargs):
#         stop = time.time()
#         res = func(*args, **kwargs)
#         start = time.time()
#         print('共%s秒'%(start-stop))
#         return res
#
#     return wrapper
#
# @outter
# def index(x, y):
#     time.sleep(2)
#     print('index===>', x, y)
# index(1,2)
#
# 三:编写装饰器,为函数加上认证的功能
# from  functools import wraps
# def outter(func):
#     @wraps(func)
#     def wrapper(*args, **kwargs):
#         while True:
#             user = input('请输入账号')
#             pwd = input('请输入密码')
#             if user == 'a' and pwd == '123':
#                 break
#         res = func(*args, **kwargs)
#         return res
#
#     return wrapper
# @outter
# def index(x, y):
#     time.sleep(2)
#     print('index===>', x, y)
# index(1,2)
#
#
#
# 四:编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件),要求登录成功一次,后续的函数都无需再输入用户名和密码
# 注意:从文件中读出字符串形式的字典,可以用eval('{"name":"egon","password":"123"}')转成字典格式
#
# 五:编写装饰器,为多个函数加上认证功能,要求登录成功一次,在超时时间内无需重复登录,超过了超时时间,则必须重新登录
#
# 六:编写装饰器,为多个函数加上记录日志的功能:函数一旦运行则按照下述格式记录日志
#     函数开始执行的时间  函数名  返回值
#     2020-06-18 12:13:38  index  456
#     2020-06-18 12:13:39  home  123
#
#  试写传说中三大大山之一的的购物车
user = None
gwc = {}
import time
login_time = None
from functools import wraps
# 装饰器
# 登陆时记录时间
def lt_outter(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        res = func(*args, **kwargs)
        global login_time
        login_time = time.time()
        return res
    return wrapper

# 超时计算
def nt_outter(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        now_time = time.time()
        s_time = now_time - login_time
        s = 600 - s_time
        s = int(s)
        if s_time > 600:
            global user
            user = None
            print('使用超时')
            return '使用超时'
        else:
            print('剩余%ss'%s)
            res = func(*args, **kwargs)
            return res
    return wrapper

# 日志记录
def tail(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        res = func(*args, **kwargs)
        t = time.strftime('%Y-%m-%d %H:%M:%S')
        fn = func.__name__
        with open('tail',mode='at',encoding='utf-8') as f0:
            f0.write('%s  %6s  %6s  %6s\n'%(t,fn,user,res))
        return res


    return wrapper



#从文件取用户密码
def eval():
    e = {}
    with open('eval', mode='rt', encoding='utf-8') as f:
        for l in f:
            k , v = l.strip().split(':')
            e[k] = v
    return e

# 文件提取钱包账户
def wallet():
    q = {}
    with open('wallet', mode='rt', encoding='utf-8') as f:
        for l in f:
            k, v = l.strip().split(':')
            q[k] = v
    return q


#登录
@tail
@lt_outter
def dl():
    while True:
        user1 = input('请输入账号:')
        pwd = input('请输入密码:')
        eval()
        if user1 in eval() and eval()[user1] == pwd:
            global user
            user = user1
            print('登录成功')
            return '登录'
        else:
            print('登录失败')

#注册
def zc():
    eval()
    while True:
        us = input('请输入用户名:')
        if us in eval():
            print('用户名已存在')
        else:
            while True:
                pwd1 = input('请输入密码:')
                pwd2 = input('请确认密码:')
                if pwd1 == pwd2:
                    with open('eval',mode='at',encoding='utf-8') as f1,\
                        open('wallet',mode='at',encoding='utf-8') as f2:
                        f1.write('%s:%s\n'%(us,pwd1))
                        f2.write('%s:%s\n' % (us, int(0)))
                        a = input('注册成功!\n1 以当前身份进入\n回车退出\n')
                        if a == '1':
                            global user
                            user = us
                            return '注册并登录'
                        else:
                            return '注册'
                else:
                    print('密码不相同')

#退出
def tc():
    return


# 一级目录
catalogue = {
        '1':('登录',dl),
        '2':('注册',zc),
        '0':('退出',tc)
    }


# 文件提取商品目录
def catalog():
    c = {}
    with open('catalog', mode='rt', encoding='utf-8') as f:
        for l in f:
            k, v = l.strip().split(':')
            c[k] = v
    return c


# 余额查询
@tail
@nt_outter
def yecx():
    ye = wallet()[user]
    print('%s余额%s元'%(user,ye))
    return '余额查询'



#商城操作
@tail
@nt_outter
def gsc():
    while True:
        for k , v in catalog().items():
            print('%s,%s元'%(k,v))
        sp = input('按0退出\n请输入商品名称:').strip()
        if sp == '0':
            return '退出商城'
        elif sp not in catalog().keys():
            print('商品不存在')
        else:
            print('%s,%s元'%(sp,catalog()[sp]))
            gs = input('购买多少').strip()
            gwc[sp] = gs
            print('%s,%s个加入购物车'%(sp,gs))
            return ('%s,%s个加入购物车'%(sp,gs))


# 购物车操作
@tail
@nt_outter
def gw():
    if gwc == {}:
        print('购物车空空如也')
        return '查看购物车'
    else:
        zj = 0
        for l in  gwc.items():
            sp , gs = l
            jg = int(catalog()[sp]) * int(gs)
            zj += jg
            print('%s,%s个,%s元。'%(sp,gs,jg))
        a = input('1 删除商品\n2 更改个数\n3 购买\n其他 返回\n')
        if a == '1':
            while True:
                b = input('删除商品名称:').strip()
                if b in gwc:
                    gwc.pop(b)
                    print('删除成功')
                    return ('删除%s'%b)
                else:
                    print('商品不存在')
        elif a == '2':
            while True:
                b = input('修改商品名称:').strip()
                if b in gwc:
                    c = input('改为几个:').strip()
                    if c.isdigit():
                        gwc[b] = c
                        print('删除成功')
                        return ('更改%s,%s个'%(b,c))
                    else:
                        print('输入有误')
                else:
                    print('商品不存在')
        elif a == '3':
            if user in wallet():
                ye = wallet()[user]
                if int(ye) >= zj:
                    import os
                    with open('wallet', mode='rt', encoding='utf-8') as read_f, \
                            open('.wallet', mode='wt', encoding='utf-8') as write_f:
                        for line in read_f:
                            us, money = line.strip().split(':')
                            if us == user:
                                write_f.write('%s:%s\n' % (us, int(money) - int(zj)))
                            else:
                                write_f.write('%s:%s\n' % (us, money))
                    os.remove('wallet')
                    os.rename('.wallet', 'wallet')
                    with open('order',mode='at',encoding='utf-8') as f1:
                        f1.write("%s%s\n"%(user,gwc))
                        print('购买成功')
                        gwc.clear()
                        return '购买成功'
                else:
                    print('余额不足')
                    return '购买失败'
            else:
                print('请先充值')
                return '余额不足'

# 充值操作
@tail
@nt_outter
def cz():
    a = input('充值数目').strip()
    if a.isdigit() == True:
        import os
        with open('wallet', mode='rt', encoding='utf-8') as read_f,\
            open('.wallet',mode='wt',encoding='utf-8') as write_f:
            for line in read_f:
                us, money = line.strip().split(':')
                if us == user:
                    write_f.write('%s:%s\n' % (us, int(a) + int(money)))
                else:
                    write_f.write('%s:%s\n'%(us, money))
        os.remove('wallet')
        os.rename('.wallet','wallet')
        print('充值成功')
        return ('充值%s'%a)
    else:
        print('输入有误')

#提现操作
@tail
@nt_outter
def tx():
    a = input('提现数目').strip()
    if a.isdigit() == True:
        ye = wallet()[user]
        if ye >= a:
            import os
            with open('wallet', mode='rt', encoding='utf-8') as read_f, \
                    open('.wallet', mode='wt', encoding='utf-8') as write_f:
                for line in read_f:
                    us, money = line.strip().split(':')
                    if us == user:
                        write_f.write('%s:%s\n' % (us, int(money) - int(a)))
                    else:
                        write_f.write('%s:%s\n' % (us, money))
            os.remove('wallet')
            os.rename('.wallet', 'wallet')
            print('提现成功')
            return ('提现%s'%a)
        else:
            print('余额不足')
    else:
        print('输入有误')

# 转账操作
@tail
@nt_outter
def zz():
    while True:
        zu = input('回车退出\n请输入转入账号:').strip()
        if zu == '':
            return '取消转账'
        elif zu not in wallet():
                print('用户名不存在')
        else:
            z = input('转多少').strip()
            import os
            with open('wallet', mode='rt', encoding='utf-8') as read_f,\
                open('.wallet',mode='wt',encoding='utf-8') as write_f:
                for line in read_f:
                    us, money = line.strip().split(':')
                    if us == user:
                         write_f.write('%s:%s\n'%(us,int(money) - int(z)))
                    elif zu == us:
                        write_f.write('%s:%s\n' % (us, int(money) + int(z)))
                    else:
                        write_f.write('%s:%s\n' % (us, money))
            os.remove('wallet')
            os.rename('.wallet','wallet')
            print('转账成功')
            return ('%s向%s转%s'%(user,zu,z))

# 钱包(三级)操作
@tail
@nt_outter
def qb():
    while user != None:
        for k, v in catalogue3.items():
            print(k, v[0])
        a = input('请输入指令').strip()
        if a in catalogue3:
            if a == '0':
                break
            elif a in catalogue3:
                catalogue3[a][1]()
        else:
            print('指令不存在')

# 二级目录
catalogue2 = {
        '1':('逛商城',gsc),
        '2':('查看购物车',gw),
        '3':('打开钱包',qb),
        '0':('退出',tc)
    }

# 钱包三级目录
catalogue3 = {
        '1':('余额查询',yecx),
        '2':('充值',cz),
        '3':('提现',tx),
        '4':('转账',zz),
        '0':('退出',tc)
    }



# 一级操作
while user == None:
    for k , v in catalogue.items():
        print(k,v[0])
    a = input('请输入指令').strip()
    if a in catalogue:
        if a == '0':
            break
        elif a in catalogue:
            catalogue[a][1]()
    else:
        print('指令不存在')


# 二级操作
    while user != None:
        for k , v in catalogue2.items():
            print(k,v[0])
        a = input('请输入指令').strip()
        if a in catalogue2:
            if a == '0':
                user = None
                # break
            elif a in catalogue2:
                catalogue2[a][1]()
        else:
            print('指令不存在')

tail:

2020-06-18 23:05:46      dl       a      登录
2020-06-18 23:05:52    yecx       a    余额查询
2020-06-18 23:06:00      cz       a  充值5000
2020-06-18 23:06:02    yecx       a    余额查询
2020-06-18 23:06:21      tx       a  提现2000
2020-06-18 23:06:24    yecx       a    余额查询
2020-06-18 23:06:44      zz       a  a向w转1500

order:

a{'apple': '2', 'milk': '66'}
a{'banana': '5', 'apple': '12', 'milk': '4', 'melon': '4'}

catalog:

apple:5
banana:3
melon:4
milk:6

eval:

a:a
w:w
q:q

 

你可能感兴趣的:(六月十八号作业)