[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[技术讨论] 将这些Python代码用正则缩写为10行

用正则表达式将以下代码缩写为十行请问怎么做到,实在没法

  1. # My first python object,restructure 5 times,finish at 2019/11/29
  2. # 需求:
  3. #
  4. #
  5. #
  6. #
  7. #
  8. #
  9. #
  10. #
  11. #
  12. #
  13. import os
  14. import time
  15. path = 'C:\\Users\\hongj\\Desktop\\test.txt'  # 家 path
  16. # path = 'C:\\Users\\28158\\Desktop\\test.txt'  # 宿舍 path
  17. # path = 'C:\\Users\\Administrator.USER-20191115PO\\Desktop\\test.txt'  # 公司
  18. result = os.path.exists(path)
  19. if result is True:
  20.     pass
  21. else:
  22.     def preset_long():  # 长度上下限
  23.         global preset_user_longest, preset_user_minimum, preset_code_longest, preset_code_minimum  # 设定为全局变量
  24.         preset_user_longest = 8  # 账户长度上限
  25.         preset_user_minimum = 5  # 账户长度下限
  26.         preset_code_longest = 16  # 密码长度上限
  27.         preset_code_minimum = 8  # 密码长度下限
  28.         return
  29.     def test_long(u, c):
  30.         user_long = len(u)  # 计算帐号长度
  31.         code_long = len(c)  # 计算密码长度
  32.         if user_long > preset_user_longest:
  33.             print('帐号输入长度不得大于八位数。')
  34.             exit()
  35.         elif code_long > preset_code_longest:
  36.             print('密码输入长度不得超过十六位数。')
  37.             exit()
  38.         elif user_long < preset_user_minimum:
  39.             print('帐号输入长度不得小于五位数。')
  40.             exit()
  41.         elif code_long < preset_code_minimum:
  42.             print('密码输入长度不得小于八位数。')
  43.             exit()
  44.         else:
  45.             return
  46.     def test_code_safe(c):
  47.         q = 0
  48.         for item in c:
  49.             if (item > "a" and item < "z") or (item > "A" and item < "Z"):
  50.                 q += 1
  51.         if q <= 3:
  52.             print('请输入三位或以上任意大小写的字母。')
  53.             exit()
  54.         else:
  55.             return
  56.     register_user = (input('请输入账号:'))
  57.     if register_user == '':
  58.         print('账号输入不能为空。')
  59.         exit()
  60.     else:
  61.         register_code = (input('请输入密码:'))
  62.         if register_code == '':
  63.             print('密码输入不能为空。')
  64.             exit()
  65.         else:
  66.             preset_long()
  67.             test_long(register_user, register_code)
  68.             test_code_safe(register_code)
  69.             data = ('User:' + register_user + '\nCode:' + register_code)
  70.             with open(path, 'a')as f:
  71.                 f.write(data)
  72.             print('注册成功,请重新登录。')
  73.             exit()
  74.             with open(path, 'a') as f:
  75.                 f.write(data)
  76. def contrast(_items=6, _time=180, _end=3):
  77.     f = open(path)
  78.     count = len(f.readlines())
  79.     f = open(path)
  80.     a = {}
  81.     for line, i in zip(f, range(count)):
  82.         a[i] = line.strip('\n')
  83.         # a[0] = user a[1] = code
  84.     while _items > 0:
  85.         loding_user = (input('请输入登录帐号:'))
  86.         if loding_user == '':
  87.             print('帐号输入不能为空。')
  88.         else:
  89.             loding_user = 'User:' + loding_user
  90.             _list = [loding_user]
  91.             loding_code = (input('请输入登入密码:'))
  92.             if loding_code == '':
  93.                 print('密码输入不能为空。')
  94.             else:
  95.                 loding_code = 'Code:' + loding_code
  96.                 _list = [loding_user, loding_code]
  97.                 if (_list[0] == a[0]) and (_list[1] == a[1]):
  98.                     print('登录成功')
  99.                     exit()
  100.                 else:
  101.                     _items -= 1
  102.                     if _items <= 3:
  103.                         print('您输入的错误过多,程序将暂停您的操作,请在 ' + str(_time) + ' 秒后重试,您还有 ' + str(_end) + ' 次重试机会。')
  104.                         _end -= 1
  105.                         if _end == 0:
  106.                             print('出于安全考虑,您的电脑将在60秒后关机,请及时做好备份操作。')
  107.                             os.system('shutdown -s -t 60')
  108.                             exit()
  109.                         time.sleep(_time)
  110.                         _time += 180  # tmp
  111.                     else:
  112.                         print('帐号或密码输入错误,请重新输入。')
  113.                         print(_items, _time, _end)
  114. contrast()
复制代码
附件: 您需要登录才可以下载或查看附件。没有帐号?注册

怎么不直接让你的朋友帮你改成10行?
发贴,你自己的需求不说清楚? 就直接让别人给你给改,先读你的代码,提取出需求再改?个人觉得 发贴,自己的需求总要先说清楚。
正则表达式,的确是非常非常强大的,可以学一学。不过也没必要神吹,也不是万能的不是任何情况都适用的。
你这个用户名和密码的匹配,可以用正则,用了之后会精简很多的。关键还是需求没说清楚。

TOP

回复 2# cfwyy77_bat


    嗯嗯,听他说可以用十行能解决我懵了现在还没学会想看看论坛有没有大佬能写个例子看看的

TOP

返回列表