jin5011 发表于 2018-8-10 08:56:49

python博客园示例,重点使用装饰器

# -*- coding:utf-8 -*-  
import time
  

  
login_state = False
  
user_dict = {'username': None}
  

  

  
def register():# 注册函数
  
    while True:
  
      username = input("请输入注册账号:").strip()
  
      password = input("请输入注册密码:").strip()
  
      with open("register", encoding="UTF-8")as reg:
  
            for i in reg:
  
                list_reg = i.strip().split(',')
  
                if username == list_reg:
  
                  print("用户名已经存在,请重新输入")
  
                  break
  
            else:
  
                with open("register", encoding="UTF-8", mode="a")as ligo:
  
                  ligo.write('\n{},{}'.format(username, password))
  
                  print("注册成功")
  
                  return True
  

  

  
def login():# 登陆函数
  
    global login_state
  
    global user_dict
  
    i = 0# 计数器
  
    while i < 3:# 超过3次后,登陆失败
  
      username = input(&quot;请输入您的账号:&quot;).strip()
  
      password = input(&quot;请输入您的密码:&quot;).strip()# 去除空格及换号符
  
      with open('register', encoding='UTF-8')as f1:
  
            for line in f1:# 循环读取注册文件中的内容
  
                line_list = line.strip().split(',')
  
                if username == line_list and password == line_list:
  
                  print(&quot;*******登陆成功*******&quot;)
  
                  login_state = True
  
                  user_dict = line_list
  
                  return True
  
            else:
  
                print(&quot;账户或密码输入错误&quot;)
  
            i += 1
  

  

  
def log(e):
  
    def loge():
  
      log_time = time.strftime(&quot;%Y-%m-%d %H:%M:%S&quot;, time.localtime())
  
      with open(&quot;log&quot;, encoding=&quot;UTF-8&quot;, mode=&quot;a&quot;) as logg:
  
            logg.write('\n{},{}登陆了,执行了{}函数'.format(log_time, user_dict, e.__name__))
  
      e()
  

  
    return loge
  

  

  
def jian_ce(f):# 认证函数,检测用户是否登陆
  
    def inner():
  
      global login_state
  
      if login_state == False:
  
            print(&quot;您尚未登陆,请先登陆后在执行程序&quot;)
  
            login()
  
      else:
  
            f()
  

  
    return inner
  

  

  
def logoff():# 注销函数
  
    global login_state
  
    if login_state == True:
  
      login_state = False
  
      print(&quot;注销成功&quot;)
  
      return login_state
  
    else:
  
      print(&quot;您尚未登陆,不需要注销&quot;)
  

  

  
@jian_ce# 语法糖,装饰器
  
@log
  
def wen_zhang():
  
    print(&quot;这是文章页面&quot;)
  

  

  
@jian_ce# 语法糖,装饰器
  
@log
  
def ri_ji():
  
    print(&quot;这是日记页面&quot;)
  

  

  
@jian_ce# 语法糖,装饰器
  
@log
  
def ping_lun():
  
    print(&quot;这是评论页面&quot;)
  

  

  
@jian_ce# 语法糖,装饰器
  
@log
  
def shou_cang():
  
    print(&quot;这是收藏页面&quot;)
  

  

  
xu_dict = {# 定义了一个字典,存放序列号对应函数
  
    1: login,
  
    2: register,
  
    3: wen_zhang,
  
    4: ri_ji,
  
    5: ping_lun,
  
    6: shou_cang,
  
    7: logoff
  
}
  
while True:
  
    print('''-------欢迎来到博客园-------
  
      1:请登陆
  
      2:请注册
  
      3:文章页面
  
      4:日记页面
  
      5:评论页面
  
      6:收藏页面
  
      7:注销
  
      8:退出程序
  
      ''')
  
    xu_hao = input(&quot;请输入序列号:&quot;).strip()
  
    if xu_hao.isdigit():
  
      xu_hao = int(xu_hao)
  
      if xu_hao > 0 and xu_hao <= len(xu_dict):
  
            xu_dict()
  
      elif xu_hao == 8:
  
            break
  
            print(&quot;再见!退出程序成功&quot;)
  
      else:
  
            print(&quot;你输入的序号不存在&quot;)
  
    else:
  
      print(&quot;您输入的序列号存在非法字符&quot;)
页: [1]
查看完整版本: python博客园示例,重点使用装饰器