• python写小说阅读功能~


    一、代码格式规范readme.md

     1 -- 软件开发目录规范
     2     - bin
     3         - start  启动软件入口
     4     - conf
     5         - settings 配置文件
     6     - core
     7         - src  核心业务逻辑代码
     8     - db
     9         - 用于存放数据文件与操作数据的代码文件
    10         - db.file ----> db.txt
    11         - db_handler  操作数据的代码
    12     -lib 
    13         - common 存放公共的功能
    14     - log
    15         - log.txt  存放日志的文件

    二、正式代码:

     1 '''
     2 start.py
     3 项目启动入口
     4 '''
     5 import os
     6 import sys
     7 
     8 # 将项目的根目录,添加到sys.path中
     9 sys.path.append(
    10     os.path.dirname(os.path.dirname(__file__))
    11 )
    12 
    13 from core import src
    14 
    15 
    16 if __name__ == '__main__':
    17     src.run()
     1 '''
     2 setting.py
     3 配置文件路径
     4 '''
     5 import os
     6 
     7 # 获取根目录
     8 BASE_PATH = os.path.dirname(os.path.dirname(__file__))
     9 # 获取db目录
    10 DB_PATH = os.path.join(BASE_PATH,'db')
    11 # 获取db目录下文件
    12 DB_TXT_PATH = os.path.join(DB_PATH,"db.txt")
    13 
    14 # 获取db目录下小说的的路径
    15 STORY_PATH = os.path.join(DB_PATH,'story_class.txt')
    16 
    17 # 日志文件的路径
    18 LOG_PATH = os.path.join(BASE_PATH,"log","log.txt")
      1 '''
      2 src.py
      3 主逻辑代码
      4 '''
      5 
      6 from db import db_hanlder
      7 from lib import common
      8 import time
      9 
     10 login_user = None
     11 
     12 # 注册功能
     13 def register():
     14     print("注册!")
     15     while 1:
     16         user_name = input("请输入你要注册的账户:").strip()
     17         # 判断用户名是否存在
     18         user_data_or_None = db_hanlder.select(user_name)
     19 
     20         if user_data_or_None:
     21             print("账户名存在,请重新输入!")
     22             continue
     23         password = input("请输入注册账户的密码:").strip()
     24         re_password = input("请再次输入注册账户的密码:").strip()
     25 
     26         if password == re_password:
     27             print("用户【%s】注册成功!"%user_name)
     28             db_hanlder.save(user_name,password)
     29             break
     30         else:
     31             print("两次密码不一致,请从新输入")
     32 
     33 # 登录功能
     34 def login():
     35     print("登录!")
     36     while 1:
     37         user_name = input("请输入要登录的用户名:").strip()
     38         user_data= db_hanlder.select(user_name)
     39         if not user_data:
     40             print("用户不存在!")
     41             continue
     42         password = input("请输入用户名密码:").strip()
     43 
     44         if  password == user_data[1]:
     45             global login_user
     46             login_user = user_name
     47             print("【%s】登录成功!"%user_name)
     48             break
     49         else:
     50             print("用户名或密码错误!")
     51 
     52 # 充值功能
     53 @common.login_auth
     54 def recharge():
     55     print("充值!")
     56     while 1:
     57         balance = input("请输入要充值的金额:").strip()
     58         if not balance.isdigit():
     59             print("请输入数字!")
     60             continue
     61         balance = int(balance)
     62         user,pwd,bal =db_hanlder.select(login_user)
     63         old_data = '%s:%s:%s'%(user,pwd,bal)
     64         bal = int(bal)
     65         bal += balance
     66         new_data = '%s:%s:%s'%(user,pwd,bal)
     67         db_hanlder.update(old_data,new_data)
     68         print("当前用户【%s】充值金额【%s】元!"%(login_user,balance))
     69 
     70         now_time = time.strftime("%Y-%m-%d %X")
     71         login_data = "%s 用户名%s 充值金额:%s ...."%(now_time,login_user,balance)
     72         common.append_log(login_data)
     73         break
     74 
     75 # 阅读功能
     76 @common.login_auth
     77 def reader():
     78     print("阅读")
     79     story_dic = db_hanlder.get_all_story()
     80 
     81     if  not  story_dic:
     82         print("没有小说,联系管理员上传!")
     83         return
     84 
     85     while 1:
     86         # 打印小说目录
     87         print('''
     88         ====== 小说 ======
     89             0、玄幻小说
     90             1、都市爱情
     91             2、考试学习
     92         ''')
     93         choice = input("请输入要阅读的小说类别:").strip()
     94 
     95         if choice not in story_dic:
     96             print("输入有误,重新输入!")
     97             continue
     98         fiction_dic = story_dic.get(choice)
     99 
    100         for number,fiction_list in fiction_dic.items():
    101             name,price = fiction_list
    102             print("小说编号【%s】小说名字【%s】小说价格【%s】"%(number,name,price))
    103         while 1:
    104             choice2 = input("请输入要购买的小说:").strip()
    105             if choice2 not in fiction_dic:
    106                 print("输入有误,请重新输入")
    107                 continue
    108             name,price = fiction_dic.get(choice2)
    109             choice3 = input("是否购买当前小说【%s】价格是【%s】,请输入(Y/N):"%(name,price)).strip().upper()
    110             if choice3 == "Y":
    111                 # 读取用户数据
    112                 user,pwd,bal = db_hanlder.select(login_user)
    113                 bal = int(bal)
    114                 price = int(price)
    115                 if bal < price:
    116                     print("余额不足,请充值!!!")
    117                     break
    118                 old_data = '%s:%s:%s'%(user,pwd,bal)
    119                 bal -=price
    120                 new_data = '%s:%s:%s'%(user,pwd,bal)
    121                 db_hanlder.update(old_data,new_data)
    122                 print("小说购买成功!")
    123 
    124                 now_time = time.strftime("%Y-%m-%d %X")
    125                 login_data = "%s 用户名%s 购买小说:%s 消费金额:%s...." % (now_time, login_user, name,price)
    126                 common.append_log(login_data)
    127 
    128                 res = db_hanlder.show_fiction_data(name)
    129                 print(res)
    130 
    131                 break
    132 
    133             break
    134 
    135 
    136 # 函数字典
    137 func_dic = {
    138     "0":register,
    139     "1":login,
    140     "2":recharge,
    141     "3":reader
    142 }
    143 
    144 
    145 def run():
    146     '''
    147 
    148     :return: 程序运行开始显示:
    149             0、账号注册
    150             1、登录
    151             2、充值
    152             3、阅读小说
    153             4、退出
    154     '''
    155     while 1:
    156         print("""
    157         ======== 小说阅读器 ========
    158                 0、账号注册
    159                 1、登录
    160                 2、充值
    161                 3、阅读小说
    162                 4、退出
    163         """)
    164 
    165         choice =  input("请输入你要做的操作:").strip()
    166         if choice == "4":break
    167         if choice not in func_dic:
    168             print("你输入的有误,请重新输入!")
    169             continue
    170         func_dic[choice]()
     1 #!/usr/bin/python
     2 # -*- coding: UTF-8 -*-
     3 '''
     4 db_hanlder.py
     5 数据操作
     6 '''
     7 
     8 from conf import settings
     9 import os
    10 
    11 # 查询用户是否存在
    12 def select(name):
    13     with open(settings.DB_TXT_PATH,"r",encoding="utf8") as r_f:
    14         for line in r_f:
    15             if name in line:
    16                 use_data = line.strip().split(":")
    17                 return use_data
    18 
    19 # 注册写入功能
    20 def save(name,pwd,bal=0):
    21     with open(settings.DB_TXT_PATH,"a",encoding="utf8") as w_f:
    22         w_f.write("%s:%s:%s
    "%(name,pwd,bal))
    23 
    24 # 数据更新
    25 def update(old_data,new_data):
    26 
    27     new_path = os.path.join(settings.DB_PATH,"new.txt")
    28     with open(settings.DB_TXT_PATH,"r",encoding="utf-8") as r_f,
    29         open(new_path,"w",encoding="utf-8") as w_f:
    30 
    31         all_user_data = r_f.read()
    32         all_user_data = all_user_data.replace(old_data,new_data)
    33         w_f.write(all_user_data)
    34     os.remove(settings.DB_TXT_PATH)
    35     os.rename(new_path, settings.DB_TXT_PATH)
    36 
    37 
    38 # 读取小说文件中的字典
    39 def get_all_story():
    40     with open(settings.STORY_PATH,"r",encoding="utf8") as f:
    41         story_dic = eval(f.read())
    42         return story_dic
    43 
    44 def show_fiction_data(name):
    45     story_data_path = os.path.join(settings.DB_PATH,"fictions",name)
    46     with open(story_data_path,"r",encoding="utf8") as f:
    47         res = f.read()
    48         return res
     1 '''
     2 common.py
     3 公共模块
     4 '''
     5 from conf import settings
     6 from core import src
     7 import time,os
     8 
     9 def login_auth(func):
    10     def wrapper(*args,**kwargs):
    11         if src.login_user:
    12             res = func(*args,**kwargs)
    13             return res
    14         else:
    15             print("未登录,暂不支持此功能")
    16             src.login()
    17     return wrapper
    18 
    19 def append_log(log_data):
    20     with open(settings.LOG_PATH,"a",encoding="utf8") as f:
    21         f.write(log_data+'
    ')
  • 相关阅读:
    ELK Packetbeat 部署指南
    ELK beats平台介绍
    ELK Packetbeat 部署指南(15th)
    什么是staging server
    elasticsearch学习一、安装和配置
    How To Use Logstash and Kibana To Centralize Logs On CentOS 6
    Java字节码(.class文件)格式详解(一)
    JVM之字节码——Class文件格式
    如何获得JVM执行过程中调用的方法名
    ELK beats通用配置说明(12th)
  • 原文地址:https://www.cnblogs.com/Tang-Yuan/p/13228800.html
Copyright © 2020-2023  润新知