首页 > 其他 > 详细

小说阅读功能

时间:2020-03-30 00:13:47      阅读:126      评论:0      收藏:0      [点我收藏+]

技术分享图片

 

 技术分享图片

 

 

技术分享图片
#启动运行代码

import os
import sys

#将项目的根目录,添加到sys.path中
sys.path.append(os.path.dirname(os.path.dirname(__file__)))



from core import src
if __name__ == __main__:
    src.run()
start.py
技术分享图片
#coding:UTF-8
‘‘‘
此处存放固定配置信息
‘‘‘
import os
BASE_PATH=os.path.dirname(os.path.dirname(__file__))

DB_PATH=os.path.join(BASE_PATH,db)

DB_TXT_PATH= os.path.join(DB_PATH, db.txt)

STORY_PATH=os.path.join(DB_PATH,story_class.txt)

FICTIONS_DIC=os.path.join(DB_PATH,fictions)

LOG_PATH=os.path.join(BASE_PATH,log,log.txt)
settings.py
技术分享图片
#coding:UTF-8
‘‘‘
此处存放核心业务逻辑代码
‘‘‘

from lib import common
from db import db_hanlder
import time
login_user=None
def register():
    print(注册功能执行中)
    while True:
        username=input(请输入用户名(输入q退出):).strip()
        if username == q:
            break
        user_date=db_hanlder.select(username)
        if user_date:
            print(用户已存在,请重新输入!)
            continue
        password=input(请输入密码:).strip()
        re_password=input(请确认密码:).strip()
        if password == re_password:
            db_hanlder.save(username,password)
            print(f用户{username}注册成功!)
            break
        else:
            print(两次密码输入不一致,请重新输入!)

        pass


def login():
    print(登录功能执行中)
    while True:
        username=input(请输入用户名(输入q退出):).strip()
        if username == q:
            break
        user_date=db_hanlder.select(username)
        if not user_date:
            print(当前输入的用户不存在,请重新输入!)
            continue
        password=input(请输入密码:).strip()
        if password == user_date[1]:
            global login_user
            login_user=username
            print(f用户{username}登录成功!)
            break
        else:
            print(密码错误,登录失败)



@common.login_auth
def recharge():
    print(充值功能执行中)
    while True:
        balance=input(请输入充值金额:).strip()
        if not balance.isdigit():
            print(请输入数字!)
            continue
        balance=int(balance)
        user,password,bal=db_hanlder.select(login_user)
        old_date=f{user}:{password}:{bal}
        bal=int(bal)
        bal+=balance
        new_date=f{user}:{password}:{bal}
        db_hanlder.update(old_date,new_date)
        print(f当前用户:{login_user}充值金额{balance}成功!)
        now_time=time.strftime(%Y-%m-%d %X)
        log_date=f时间:[{now_time}]用户名:[{login_user}]充值金额:[{balance}]
        print(log_date)
        common.append_log(log_date)
        break



@common.login_auth
def reader():
    story_dic=db_hanlder.get_all_story()
    if not story_dic:
        print(没有小说,请联系上传~~)
        return
    while True:
        print(‘‘‘
        ===欢迎来到阅读小说功能主页===
            0 玄幻武侠
            1 都市爱情
            2 高效养猪36技
        ========== end ============       
            ‘‘‘)
        choice1=input(请输入小说类型编号:).strip()
        if choice1 not in story_dic:
            print(输入有误,请重新输入!!!)
            continue
        fiction_dic=story_dic.get(choice1)
        for number,fiction_list in fiction_dic.items():
            name,price=fiction_list
            print(f小说编号:{number}小说名字:{name}小说价格:{price})
        while True:
            choice2=input(请输入要购买的小说编号:).strip()
            if choice2 not in fiction_dic:
                print(输入有误,请重新输入!!!)
                continue
            name,price=fiction_dic.get(choice2)
            choice3=input(f当前选择的小说名为:{name},商品单价为:{price},请输入y购买,或者退出程序:).strip()
            if choice3 == y:
                user,pwd,bal=db_hanlder.select(login_user)
                bal=int(bal)
                price=int(price)
                if bal < price:
                    print(穷鬼,请先充值~~~)
                    break
                old_date = f{user}:{pwd}:{bal}
                bal-=price
                new_date = f{user}:{pwd}:{bal}
                db_hanlder.update(old_date, new_date)
                print(当前小说购买成功,自动打开小说阅读~~~)
                fiction_date=db_hanlder.show_fiction_date(name)
                print(
                    f‘‘‘
                ===当前小说内容如下====
                {fiction_date}
                ‘‘‘)
                now_time = time.strftime(%Y-%m-%d %X)
                log_date = f时间:[{now_time}]用户名:[{login_user}]消费金额:[{price}]
                print(log_date)
                common.append_log(log_date)
                break

        break




func_dic={
    0:register,
    1:login,
    2:recharge,
    3:reader,
}




def run():
    while True:
        print(‘‘‘
=====小说阅读器欢迎您=====
        0 账号注册
        1 帐号登录
        2 充值功能
        3 阅读小说
=========  end  ========= 
        ‘‘‘)
        choice=input(请输入功能编号(温馨提示:输入q退出):).strip()
        if choice == q:
            break
        if choice not in func_dic:
            print(编号有误)
            continue

        # func_dic[choice]()
        func_dic.get(choice)()
src.py
技术分享图片
lqb:123:490
db.txt
技术分享图片
#coding:UTF-8
‘‘‘
用于存放操作数据的代码
‘‘‘
from conf import settings
import os
#查看数据
def select(username):
    with open(settings.DB_TXT_PATH,r,encoding=utf-8) as f:
        for line in f:
            if username in line:
                user_date=line.strip().split(:)
                return user_date
    return None

#保存数据
def save(username,password,balance=0):
    with open(settings.DB_TXT_PATH,a,encoding=utf-8) as f:
        f.write(f{username}:{password}:{balance}\n)
#修改数据
def update(old_date,new_date):
    import os
    new_path=os.path.join(
        settings.DB_PATH,new.txt
    )
    with open(settings.DB_TXT_PATH,r,encoding=utf-8) as r_f,        open(new_path,w,encoding=utf-8) as w_f:
        all_user_date=r_f.read()
        all_user_date=all_user_date.replace(old_date,new_date)
        w_f.write(all_user_date)
    os.remove(settings.DB_TXT_PATH)
    os.rename(new_path,settings.DB_TXT_PATH)
#获取小说字典
def get_all_story():
    with open(settings.STORY_PATH,r,encoding=utf-8) as f:
        story_dic=eval(f.read())
        return story_dic

#查看单本小说
def show_fiction_date(fiction_name):
    fiction_path=os.path.join(settings.FICTIONS_DIC,fiction_name)

    with open(fiction_path,r,encoding=utf-8) as f:
        fiction_date=f.read()

    return fiction_date
db_hanlder.py
技术分享图片
{
    "0":
    {
        "0":["倚天屠狗记.txt",3],
        "1":["沙雕英雄转.txt",10]
    },
    "1":
    {
        "0":["令人羞耻的爱.txt",6],
        "1":["二狗的妻子与大草原的故事.txt",5],
        2:[和大佬离婚当天我变小了.txt,500]
    },
    "2":
    {
        "0":["矮跟与富婆的故事.txt",250],
        "1":["矮跟落叶归根养猪转.txt",25]
    },
}
story_class.txt
技术分享图片
#coding:UTF-8
‘‘‘‘
此处存放公共功能
‘‘‘
from conf import settings


# # 登录认证装饰器
# def login_auth(func):
#     # 解决问题: 循环导入问题
#     from core import src
#
#     def inner(*args, **kwargs):
#         if src.login_user:
#             res = func(*args, **kwargs)
#             return res
#         else:
#             print(‘未登录,不允许使用特殊功能, 请先登录~~~‘)
#             src.login()
#
#     return inner


#登录认证装饰器
def login_auth(func):
    from core import src
    def inner(*args,**kwargs):
        if src.login_user:
            res=func(*args,**kwargs)
            return res
        else:
            print(未登录不允许享用特殊功能,请先登录~~~)
            src.login()

    return inner

#记录日志,应该放在公共功能中
def append_log(log_date):
    with open(settings.LOG_PATH,a,encoding=utf-8) as f:
        f.write(log_date+\n)
common.py
技术分享图片
时间:[2020-03-29 15:37:58]用户名:[lqb]充值金额:[1000]
时间:[2020-03-29 15:38:06]用户名:[lqb]消费金额:[500]
时间:[2020-03-29 19:41:11]用户名:[lqb]消费金额:[10]
log
技术分享图片
#软件的使用规范

一 软件的目录规范
    一bin
        start-->启动入口

    一conf
        setting.py-->配置

    一core
        src.pr-->核心业务逻辑代码

    一db-->用于存放数据文件与操作数据的代码文件
        db_file-->db.txt...
        db_hanlder.py-->操作数据的代码

    一lib
        common.py-->存放公共的功能

    一log
        log.txt-->存放日志的文件


二 一个项目开发前,有一份开发文档
项目:编写小说阅读程序实现下属功能
# 一:程序运行开始时显示
        0 账号注册
        1 帐号登录
        2 充值功能
        3 阅读小说

        (项目模板创建好)
                def register():
                    pass

                def login():
                    pass

                def recharge():
                    pass

                def reader():
                    pass


                func_dic={
                    0:register,
                    1:login,
                    2:recharge,
                    3:reader,
                }




                def run():
                    while True:
                        print(‘‘‘
                =====小说阅读器欢迎您=====
                        0 账号注册
                        1 帐号登录
                        2 充值功能
                        3 阅读小说
                =========  end  =========
                        ‘‘‘)
                        choice=input(请输入功能编号:).strip()


# 二: 针对文件db.txt,内容格式为:"用户名:密码:金额",完成下述功能
2.1、账号注册
2.2、登录功能
2.3、充值功能

# 三:文件story_class.txt存放类别与小说文件路径,如下,读出来后可用eval反解出字典
{"0":{
"0":["倚天屠狗记.txt",3],
"1":["沙雕英雄转.txt",10]
},
"1":{
"0":["令人羞耻的爱.txt",6],
"1":["二狗的妻子与大草原的故事.txt",5]
},
}

3.1、用户登录成功后显示如下内容,根据用户选择,显示对应品类的小说编号、小说名字、以及小说的价格
"""
0 玄幻武侠
1 都市爱情
2 高效养猪36技
"""

3.2、用户输入具体的小说编号,提示是否付费,用户输入y确定后,扣费并显示小说内容,如果余额不足则提示余额不足

# 四:为功能2.2、3.1、3.2编写认证功能装饰器,要求必须登录后才能执行操作

# 五:为功能2.2、3.2编写记录日志的装饰器,日志格式为:"时间 用户名 操作(充值or消费) 金额"



# 附加:
# 可以拓展作者模块,作者可以上传自己的作品
readme.txt

 

小说阅读功能

原文:https://www.cnblogs.com/lqb666/p/12595449.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!