python-code-16

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-code-16相关的知识,希望对你有一定的参考价值。

模拟实现一个ATM + 购物商城程序

  1. 额度 15000或自定义
  2. 实现购物商城,买东西加入 购物车,调用信用卡接口结账
  3. 可以提现,手续费5%
  4. 每月22号出账单,每月10号为还款日,过期未还,按欠款总额 万分之5 每日计息
  5. 支持多账户登录
  6. 支持账户间转账
  7. 记录每月日常消费流水
  8. 提供还款接口
  9. ATM记录操作日志 
  10. 提供管理接口,包括添加账户、用户额度,冻结账户等。。。
  11. 用户认证用装饰器

import os

BASE_DIR = os.path.dirname(os.path.dirname(__file__))
DB_PATH = os.path.join(BASE_DIR, \'db\')
BASE_LOG =os.path.join(BASE_DIR,\'log\')

standard_format = \'[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]\' \\
                  \'[%(levelname)s][%(message)s]\'  # 其中name为getlogger指定的名字

simple_format = \'[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s\'

id_simple_format = \'[%(levelname)s][%(asctime)s] %(message)s\'

# 定义日志输出格式 结束

# 如果不存在定义的日志目录就创建一个
if not os.path.isdir(BASE_LOG):
    os.mkdir(BASE_LOG)

# log文件的全路径
logfile_path = os.path.join(BASE_LOG, \'log.log\')

# log配置字典
LOGGING_DIC = {
    \'version\': 1,
    \'disable_existing_loggers\': False,
    \'formatters\': {
        \'standard\': {
            \'format\': standard_format
        },
        \'simple\': {
            \'format\': simple_format
        },
    },
    \'filters\': {},
    \'handlers\': {
        # 打印到终端的日志
        \'console\': {
            \'level\': \'DEBUG\',
            \'class\': \'logging.StreamHandler\',  # 打印到屏幕
            \'formatter\': \'simple\'
        },
        # 打印到文件的日志,收集info及以上的日志
        \'default\': {
            \'level\': \'DEBUG\',
            \'class\': \'logging.handlers.RotatingFileHandler\',  # 保存到文件
            \'formatter\': \'standard\',
            \'filename\': logfile_path,  # 日志文件
            \'maxBytes\': 1024 * 1024 * 5,  # 日志大小 5M
            \'backupCount\': 5,
            \'encoding\': \'utf-8\',  # 日志文件的编码,再也不用担心中文log乱码了
        },


    },
    \'loggers\': {
        # logging.getLogger(__name__)拿到的logger配置
        \'\': {
            \'handlers\': [\'default\', \'console\'],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
            \'level\': \'INFO\',
            \'propagate\': True,  # 向上(更高level的logger)传递
        },
    },
}
conf-setting
from interface import user
from lib import common
from interface import bank
from interface import shop

user_data = {\'name\':None}

def login():
    if user_data[\'name\']:
        print(\'登录状态\')
        return
    print(\'登录\')
    count = 0
    while True:
        name = input(\'用户名:\').strip()
        if name == \'q\': break
        user_dic = user.get_userinfo_interface(name)
        if user_dic:
            if not user_dic[\'locked\']:
                pwd = input(\'密码:\').strip()
                if pwd == user_dic[\'pwd\']:
                    print(\'登录成功\')
                    user_data[\'name\'] = name
                    break
                else:
                    print(\'密码错误\')
                    count += 1
                    if count == 3:
                        msg = user.lock_user_interface(name)
                        print(msg)
                        break
            else:
                print(\'用户锁定状态\')
                break
        else:
            print(\'用户名不存在\')

def register():
    if user_data[\'name\']:
        print(\'登录状态\')
        return
    print(\'注册\')
    while True:
        name = input(\'用户名(q to quit):\').strip()
        if name == \'q\':break
        if user.get_userinfo_interface(name):
            print(\'用户名已被注册\')
            continue
        else:
            pwd = input(\'密码:\').strip()
            conf_pwd = input(\'核对密码:\').strip()
            if pwd == conf_pwd:
                msg = user.register_interface(name,pwd)
                print(msg)
                break
            else:
                print(\'两次密码不一致\')

@common.auth
def check_balance():
    print(\'查看余额\')
    msg = bank.check_balance_interface(user_data[\'name\'])
    print(msg)

@common.auth
def transfer():
    print(\'转账\')
    to_user = input(\'转入账户:\').strip()
    transfer_account = input(\'转账金额:\').strip()
    flag,msg = bank.transfer_interface(user_data[\'name\'],to_user,transfer_account)
    if flag:
        print(msg)
    else:
        print(msg)

@common.auth
def repay():
    print(\'还款\')
    account = input(\'输入还款金额:\').strip()
    flag,msg = bank.repay_interface(user_data[\'name\'],account)
    if flag:
        print(msg)
    else:
        print(msg)

@common.auth
def withdraw():
    print(\'取款\')
    account = input(\'取款金额:\').strip()
    flag,msg = bank.withdraw_interface(user_data[\'name\'],account)
    if flag:
        print(msg)
    else:
        print(msg)

@common.auth
def check_records():
    res = bank.check_records_interface(user_data[\'name\'])
    for i in res:
        print(i)

@common.auth
def shopping():
    product_list = [[\'Iphone7\', 5800],
                    [\'Coffee\', 30],
                    [\'疙瘩汤\', 10],
                    [\'Python Book\', 99],
                    [\'Bike\', 199],
                    [\'ViVo X9\', 2499],

                    ]
    shopping_cart = {}
    while True:
        for k,v in enumerate(product_list):
            info = \'编号:%s 商品:%s 价格:%s\' %(k,product_list[k][0],product_list[k][1])
            print(info)
        choice = input(\'输入商品编号:\').strip()
        if choice.isdigit():
            choice = int(choice)
            if 0 <= choice < len(product_list):
                product_name = product_list[choice][0]
                product_price = product_list[choice][1]
            else:
                print(\'超出编号范围\')
                continue
        else:
            print(\'输入非法\')
            continue

        product_count = input(\'输入购买数量:\').strip()
        if product_count.isdigit():
            product_count = int(product_count)
        else:
            print(\'输入非法\')

        for name in shopping_cart:
            if name == product_name:
                shopping_cart[product_name][\'count\'] += product_count
                break
        else:
            shopping_cart[product_name] = {\'price\':product_price,\'count\':product_count}

        cmd = input(\'结账:Y/y 任意键继续>>: \').strip()
        if cmd == \'y\' or cmd == \'Y\':
            # name shopping_cart
            print(shopping_cart)
            flag,msg = shop.shop_interface(user_data[\'name\'],shopping_cart)
            if flag:
                print(msg)
                break
            else:
                print(msg)
                break
        else:
            print(shopping_cart)

@common.auth
def check_shopping_cart():
    res = shop.check_cart_interface(user_data[\'name\'])
    for i in res:
        print(i)

def logout():
    user_data[\'name\'] = None

func_dic = {
    \'1\':login,
    \'2\':register,
    \'3\':check_balance,
    \'4\':transfer,
    \'5\':repay,
    \'6\':withdraw,
    \'7\':check_records,
    \'8\':shopping,
    \'9\':check_shopping_cart,
    \'0\':logout
}

msg = \'\'\'
    1 登录
    2 注册
    3 查看余额
    4 转账
    5 还款
    6 提现
    7 查看流水
    8 购物
    9 查看购买商品
    0 退出当前用户
\'\'\'

def run():
    while True:
        print(msg)
        choice = input(\'选择功能>>: \').strip()
        if choice in func_dic:
            func_dic[choice]()
core-src
import os
import json
from conf import setting


def select(name):
    user_path = os.path.join(setting.DB_PATH, \'%s.json\' % name)
    if os.path.exists(user_path):
        with open(user_path,encoding=\'utf-8\') as f:
            user_dic = json.load(f)
        return user_dic
    else:
        return False

def update(user_dic):
    user_path = os.path.join(setting.DB_PATH, \'%s.json\' %user_dic[\'name\'])
    with open(user_path,\'wt\',encoding=\'utf-8\') as f:
        json.dump(user_dic,f)
        f.flush()
db-db_handler
from db import db_handler
from lib import common

logger_bank = common.get_logger(\'bank\')

def check_balance_interface(name):
    user_dic = db_handler.select(name)
    return \'%s 余额为:%s\' %(name,user_dic[\'account\'])

def transfer_interface(from_user,to_user,account):
    to_user_dic = db_handler.select(to_user)
    from_user_dic = db_handler.select(from_user)
    if from_user != to_user:
        if to_user_dic:
            if account.isdigit():
                account = int(account)
                if from_user_dic[\'account\'] >= account:
                    from_user_dic[\'account\'] -= account
                    to_user_dic[\'account\'] += account
                    from_user_dic[\'bankflow\'].append(\'%s 转给 %s %s元\' % (from_user, to_user, account))
                    to_user_dic[\'bankflow\'].append(\'%s 收到 %s %s元\' % (to_user, from_user, account))
                    db_handler.update(from_user_dic)
                    db_handler.update(to_user_dic)
                    logger_bank.info(\'转账成功:%s 转给 %s %s元\' % (from_user, to_user, account))
                    return True,\'转账成功\'
                else:
                    return False,\'余额不足\'
            else:
                return False,\'金额必须是整数\'
        else:
            return False,\'转入账户不存在\'
    else:
        return False,\'不能转给自己\'

def repay_interface(name,account):
    user_dic = db_handler.select(name)
    if account.isdigit():
        account = int(account)
        user_dic[\'account\'] += account
        user_dic[\'bankflow\'].append(\'%s 还款 %s\' %(name,account))
        db_handler.update(user_dic)
        logger_bank.info(\'%s 还款 %s\' %(name,account))
        return True,\'还款成功\'
    else:
        return False,\'还款金额必须是整数\'

def withdraw_interface(name,account):
    user_dic = db_handler.select(name)
    if account.isdigit():
        account = int(account)
        if user_dic[\'account\'] >= account:
            user_dic[\'account\'] -= account
            user_dic[\'bankflow\'].append(\'%s 取款 %s元\' %(name,account))
            logger_bank.info(\'%s 取款 %s元\' %(name,account))
            return True,\'取款成功\'
        else:
            return False,\'余额不足\'
    else:
        return False,\'取款金额必须是整数\'

def check_records_interface(name):
    user_dic = db_handler.select(name)
    return user_dic[\'bankflow\']
interface-bank
from db import db_handler
from lib import common

logger_shop = common.get_logger(\'shop\')

def shop_interface(name,cart):
    cost = 0
    user_dic = db_handler.select(name)
    for v in cart.values():
        cost += v[\'price\'] * v[\'count\']
    if user_dic[\'account\'] >= cost:
        user_dic[\'account\'] -= cost
        user_dic[\'shopping_cart\'].append(\'%s 购物信息 %s\' %(name,cart))
        db_handler.update(user_dic)
        logger_shop.info(\'%s 购物信息 %s\' %(name,cart))
        return True,\'购物完成\'
    else:
        return False,\'余额不足\'

def check_cart_interface(name):
    user_dic = db_handler.select(name)
    return user_dic[\'shopping_cart\']
interface-shop
import os
from conf import setting
from db import db_handler
from lib import common
from core import src

logger_user = common.get_logger(\'user\')

def get_userinfo_interface(name):
    user_dic = db_handler.select(name)
    return user_dic

def register_interface(name,pwd,account=15000):
    user_dic = {\'name\': name, \'pwd\': pwd, \'locked\': False, \'account\': account, \'creidt\': account, \'bankflow\': [],
                \'shopping_cart\': []}
    db_handler.update(user_dic)
    logger_user.info(\'%s 注册成功\' %name)
    return \'注册成功\'


def lock_user_interface(name):
    user_dic = db_handler.select(name)
    user_dic[\'locked\'] = True
    db_handler.update(user_dic)
    logger_user.info(\'%s 被锁定\' %name)
    return \'错误3次,锁定用户\'
interface-user
import os
from conf import setting
from db import db_handler
from lib import common
from core import src

logger_user = common.get_logger(\'user\')

def get_userinfo_interface(name):
    user_dic = db_handler.select(name)
    return user_dic

def register_interface(name,pwd,account=15000):
    user_dic = {\'name\': name, \'pwd\': pwd, \'locked\': False, \'account\': account, \'creidt\': account, \'bankflow\'VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数