zoukankan      html  css  js  c++  java
  • Python 3 软件开发规范

    Python 3 软件开发规范

    参考链接 http://www.cnblogs.com/linhaifeng/articles/6379069.html#_label14

     

    对每个目录,文件介绍。

      1 #=============>bin目录:存放执行脚本
      2 
      3 #start.py
      4 
      5 import sys,os
      6 
      7  
      8 
      9 BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
     10 
     11 sys.path.append(BASE_DIR)
     12 
     13  
     14 
     15 from core import core
     16 
     17 from conf import my_log_settings
     18 
     19  
     20 
     21 if __name__ == '__main__':
     22 
     23     my_log_settings.load_my_logging_cfg()
     24 
     25     core.run()
     26 
     27  
     28 
     29 #=============>conf目录:存放配置文件
     30 
     31 #config.ini
     32 
     33 [DEFAULT]
     34 
     35 user_timeout = 1000
     36 
     37  
     38 
     39 [egon]
     40 
     41 password = 123
     42 
     43 money = 10000000
     44 
     45  
     46 
     47 [alex]
     48 
     49 password = alex3714
     50 
     51 money=10000000000
     52 
     53  
     54 
     55 [yuanhao]
     56 
     57 password = ysb123
     58 
     59 money=10
     60 
     61  
     62 
     63 #settings.py
     64 
     65 import os
     66 
     67 config_path=r'%s\%s' %(os.path.dirname(os.path.abspath(__file__)),'config.ini')
     68 
     69 user_timeout=10
     70 
     71 user_db_path=r'%s\%s' %(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),
     72 
     73                      'db')
     74 
     75  
     76 
     77  
     78 
     79 #my_log_settings.py
     80 
     81 """
     82 
     83 logging配置
     84 
     85 """
     86 
     87  
     88 
     89 import os
     90 
     91 import logging.config
     92 
     93  
     94 
     95 # 定义三种日志输出格式 开始
     96 
     97  
     98 
     99 standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' 
    100 
    101                   '[%(levelname)s][%(message)s]' #其中name为getlogger指定的名字
    102 
    103  
    104 
    105 simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
    106 
    107  
    108 
    109 id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s'
    110 
    111  
    112 
    113 # 定义日志输出格式 结束
    114 
    115  
    116 
    117 logfile_dir = r'%slog' %os.path.dirname(os.path.dirname(os.path.abspath(__file__)))  # log文件的目录
    118 
    119  
    120 
    121 logfile_name = 'all2.log'  # log文件名
    122 
    123  
    124 
    125 # 如果不存在定义的日志目录就创建一个
    126 
    127 if not os.path.isdir(logfile_dir):
    128 
    129     os.mkdir(logfile_dir)
    130 
    131  
    132 
    133 # log文件的全路径
    134 
    135 logfile_path = os.path.join(logfile_dir, logfile_name)
    136 
    137  
    138 
    139 # log配置字典
    140 
    141 LOGGING_DIC = {
    142 
    143     'version': 1,
    144 
    145     'disable_existing_loggers': False,
    146 
    147     'formatters': {
    148 
    149         'standard': {
    150 
    151             'format': standard_format
    152 
    153         },
    154 
    155         'simple': {
    156 
    157             'format': simple_format
    158 
    159         },
    160 
    161     },
    162 
    163     'filters': {},
    164 
    165     'handlers': {
    166 
    167         #打印到终端的日志
    168 
    169         'console': {
    170 
    171             'level': 'DEBUG',
    172 
    173             'class': 'logging.StreamHandler',  # 打印到屏幕
    174 
    175             'formatter': 'simple'
    176 
    177         },
    178 
    179         #打印到文件的日志,收集info及以上的日志
    180 
    181         'default': {
    182 
    183             'level': 'DEBUG',
    184 
    185             'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件
    186 
    187             'formatter': 'standard',
    188 
    189             'filename': logfile_path,  # 日志文件
    190 
    191             'maxBytes': 1024*1024*5,  # 日志大小 5M
    192 
    193             'backupCount': 5,
    194 
    195             'encoding': 'utf-8',  # 日志文件的编码,再也不用担心中文log乱码了
    196 
    197         },
    198 
    199     },
    200 
    201     'loggers': {
    202 
    203         #logging.getLogger(__name__)拿到的logger配置
    204 
    205         '': {
    206 
    207             'handlers': ['default', 'console'],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
    208 
    209             'level': 'DEBUG',
    210 
    211             'propagate': True,  # 向上(更高level的logger)传递
    212 
    213         },
    214 
    215     },
    216 
    217 }
    218 
    219  
    220 
    221  
    222 
    223 def load_my_logging_cfg():
    224 
    225     logging.config.dictConfig(LOGGING_DIC)  # 导入上面定义的logging配置
    226 
    227     logger = logging.getLogger(__name__)  # 生成一个log实例
    228 
    229     logger.info('It works!')  # 记录该文件的运行状态
    230 
    231  
    232 
    233 if __name__ == '__main__':
    234 
    235     load_my_logging_cfg()
    236 
    237  
    238 
    239 #=============>core目录:存放核心逻辑
    240 
    241 #core.py
    242 
    243 import logging
    244 
    245 import time
    246 
    247 from conf import settings
    248 
    249 from lib import read_ini
    250 
    251  
    252 
    253 config=read_ini.read(settings.config_path)
    254 
    255 logger=logging.getLogger(__name__)
    256 
    257  
    258 
    259 current_user={'user':None,'login_time':None,'timeout':int(settings.user_timeout)}
    260 
    261 def auth(func):
    262 
    263     def wrapper(*args,**kwargs):
    264 
    265         if current_user['user']:
    266 
    267             interval=time.time()-current_user['login_time']
    268 
    269             if interval < current_user['timeout']:
    270 
    271                 return func(*args,**kwargs)
    272 
    273         name = input('name>>: ')
    274 
    275         password = input('password>>: ')
    276 
    277         if config.has_section(name):
    278 
    279             if password == config.get(name,'password'):
    280 
    281                 logger.info('登录成功')
    282 
    283                 current_user['user']=name
    284 
    285                 current_user['login_time']=time.time()
    286 
    287                 return func(*args,**kwargs)
    288 
    289         else:
    290 
    291             logger.error('用户名不存在')
    292 
    293  
    294 
    295     return wrapper
    296 
    297  
    298 
    299 @auth
    300 
    301 def buy():
    302 
    303     print('buy...')
    304 
    305  
    306 
    307 @auth
    308 
    309 def run():
    310 
    311  
    312 
    313     print('''
    314 
    315 购物
    316 
    317 查看余额
    318 
    319 转账
    320 
    321     ''')
    322 
    323     while True:
    324 
    325         choice = input('>>: ').strip()
    326 
    327         if not choice:continue
    328 
    329         if choice == '1':
    330 
    331             buy()
    332 
    333  
    334 
    335  
    336 
    337  
    338 
    339 if __name__ == '__main__':
    340 
    341     run()
    342 
    343  
    344 
    345 #=============>db目录:存放数据库文件
    346 
    347 #alex_json
    348 
    349 #egon_json
    350 
    351  
    352 
    353 #=============>lib目录:存放自定义的模块与包
    354 
    355 #read_ini.py
    356 
    357 import configparser
    358 
    359 def read(config_file):
    360 
    361     config=configparser.ConfigParser()
    362 
    363     config.read(config_file)
    364 
    365     return config
    366 
    367  
    368 
    369 #=============>log目录:存放日志
    370 
    371 #all2.log
    372 
    373 [2017-07-29 00:31:40,272][MainThread:11692][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
    374 
    375 [2017-07-29 00:31:41,789][MainThread:11692][task_id:core.core][core.py:25][ERROR][用户名不存在]
    376 
    377 [2017-07-29 00:31:46,394][MainThread:12348][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
    378 
    379 [2017-07-29 00:31:47,629][MainThread:12348][task_id:core.core][core.py:25][ERROR][用户名不存在]
    380 
    381 [2017-07-29 00:31:57,912][MainThread:10528][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
    382 
    383 [2017-07-29 00:32:03,340][MainThread:12744][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
    384 
    385 [2017-07-29 00:32:05,065][MainThread:12916][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
    386 
    387 [2017-07-29 00:32:08,181][MainThread:12916][task_id:core.core][core.py:25][ERROR][用户名不存在]
    388 
    389 [2017-07-29 00:32:13,638][MainThread:7220][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
    390 
    391 [2017-07-29 00:32:23,005][MainThread:7220][task_id:core.core][core.py:20][INFO][登录成功]
    392 
    393 [2017-07-29 00:32:40,941][MainThread:7220][task_id:core.core][core.py:20][INFO][登录成功]
    394 
    395 [2017-07-29 00:32:47,222][MainThread:7220][task_id:core.core][core.py:20][INFO][登录成功]
    396 
    397 [2017-07-29 00:32:51,949][MainThread:7220][task_id:core.core][core.py:25][ERROR][用户名不存在]
    398 
    399 [2017-07-29 00:33:00,213][MainThread:7220][task_id:core.core][core.py:20][INFO][登录成功]
    400 
    401 [2017-07-29 00:33:50,118][MainThread:8500][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
    402 
    403 [2017-07-29 00:33:55,845][MainThread:8500][task_id:core.core][core.py:20][INFO][登录成功]
    404 
    405 [2017-07-29 00:34:06,837][MainThread:8500][task_id:core.core][core.py:25][ERROR][用户名不存在]
    406 
    407 [2017-07-29 00:34:09,405][MainThread:8500][task_id:core.core][core.py:25][ERROR][用户名不存在]
    408 
    409 [2017-07-29 00:34:10,645][MainThread:8500][task_id:core.core][core.py:25][ERROR][用户名不存在]
    View Code
  • 相关阅读:
    铁乐学Python_Day35_Socket模块3和hmac模块
    铁乐学Python_Day34_Socket模块2和黏包现象
    铁乐学Python_Day33_网络编程Socket模块1
    铁乐学python_day29_模块与包学习4
    铁乐学python_day28_模块学习3
    铁乐学python27_模块学习2
    铁乐学python_md5校验两个文件的一致性
    铁乐学python26_hashlib+configparser+logging模块
    Flask与Ajax
    Javascript与Ajax
  • 原文地址:https://www.cnblogs.com/qiangyuge/p/7286520.html
Copyright © 2011-2022 走看看