Software development specification

General code were classified in the following folder:

bin directory: store script execution
conf directory: to store configuration files
core directory: storage core logic
db directory: store the database file
log directory: store the log

 

 



As follows:
# =============> bin directory: store the execution of the script 
# start.py 
Import SYS, os

BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)

from core import core
from conf import my_log_settings

if __name__ == '__main__':
    my_log_settings.load_my_logging_cfg()
    core.run()

# =============> conf directory: to store configuration files 
# config.ini 
[the DEFAULT]
user_timeout = 1000

[Been]
password = 123
money = 10000000

[alex]
password = alex3714
money=10000000000

[yuanhao]
password = ysb123
money=10

#settings.py
import os
config_path=r'%s\%s' %(os.path.dirname(os.path.abspath(__file__)),'config.ini')
user_timeout=10
user_db_path=r'%s\%s' %(os.path.dirname(os.path.dirname(os.path.abspath(__file__))),\
                     'db')


#my_log_settings.py
"""
logging configuration
"""

import os
import logging.config

# Defines three formats for log output start 

standard_format = ' [% (the asctime) S] [% (threadName) S:% (Thread) D] [task_id:% (name) S] [% (filename) S:% (lineno ) D] ' \
                   ' [% (levelname) S] [% (Message) S] '  # where name is getlogger name specified 

simple_format = ' [% (levelname) S] [% (the asctime) S] [% (filename ) S:% (lineno) D]% (Message) S '

id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s'

# Define the format of the log output end 

logfile_dir = R & lt ' % S \ log ' % os.path.dirname (os.path.dirname (os.path.abspath with ( __FILE__ )))   # directory log file 

The logfile_name = ' all2.log '   # log file name

# Log directory does not exist if you create a defined 
IF  not os.path.isdir (logfile_dir):
    os.mkdir (LOGFILE_DIR)

# Full path to the log file 
logfile_path = os.path.join (logfile_dir, logfile_name)

# log配置字典
LOGGING_DIC = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': standard_format
        },
        'simple': {
            'format': simple_format
        },
    },
    ' Filters ' : {},
     ' handlers ' : {
         # print log to the terminal 
        ' Console ' : {
             ' Level ' : ' the DEBUG ' ,
             ' class ' : ' logging.StreamHandler ' ,   # printed to the screen 
            ' Formatter ' : ' the Simple '
        },
        # Print to a log file, the log collecting and above info 
        ' default ' : {
             ' Level ' : ' the DEBUG ' ,
             ' class ' : ' logging.handlers.RotatingFileHandler ' ,   # saved to a file 
            ' Formatter ' : ' Standard ' ,
             ' filename ' : logfile_path,   # log file 
            ' MaxBytes ' : 1024 * 1024 * 5,   # log size 5M 
            'backupCount' : 5 ,
             ' encoding ' : ' UTF-8 ' ,   # log file encoding, no longer have to worry about Chinese log garbled 
        }
    },
    ' Loggers ' : {
         # logging.getLogger (__ name__) get logger configuration 
        '' : {
             ' handlers ' : [ ' default ' , ' Console ' ],   # where the handler are defined above plus two, i.e., log both the data file is written to the screen printing and 
            ' level ' : ' the DEBUG ' ,
             ' Propagate ' : True,   # up (the higher level Logger) transmission 
        },
    },
}


def load_my_logging_cfg():
    logging.config.dictConfig (LOGGING_DIC)   # introduced defined above configuration logging 
    Logger = logging.getLogger ( the __name__ )   # generate a log example 
    logger.info ( ' It Works! ' )   # record operating state of the file

if __name__ == '__main__':
    load_my_logging_cfg()

# =============> Core Contents: storage core logic 
# core.py 
Import logging
 Import Time
 from conf Import Settings
 from lib Import read_ini

config=read_ini.read(settings.config_path)
logger=logging.getLogger(__name__)

current_user={'user':None,'login_time':None,'timeout':int(settings.user_timeout)}
def auth(func):
    def wrapper(*args,**kwargs):
        if current_user['user']:
            interval=time.time()-current_user['login_time']
            if interval < current_user['timeout']:
                return func(*args,**kwargs)
        name = input('name>>: ')
        password = input('password>>: ')
        if config.has_section(name):
            if password == config.get(name,'password'):
                logger.info ( ' Login successful ' )
                current_user['user']=name
                current_user['login_time']=time.time()
                return func(*args,**kwargs)
        else:
            logger.error ( ' username does not exist ' )

    return wrapper

@auth
def buy():
    print('buy...')

@auth
def run():

    print('''
shopping
Check your balance
Transfer
    ''')
    while True:
        choice = input('>>: ').strip()
        if not choice:continue
        if choice == '1':
            buy()



if __name__ == '__main__':
    run()

# =============> db directory: store the database file 
# alex_json 
# egon_json

# =============> lib directory: storage module and a custom package 
# read_ini.py 
Import ConfigParser
 DEF Read (config_file):
    config=configparser.ConfigParser()
    config.read(config_file)
    return config

# =============> log directory: store the log 
# all2.log 
[2017-07-29 00: 31: 40,272] [MainThread: 11692] [task_id: conf.my_log_settings] [my_log_settings .py: 75 ] [INFO] [It Works]!
[ 2017-07-29 00: 31: 41,789] [MainThread: 11692] [task_id: core.core] [core.py:25 ] [ERROR] [username does not exist]
[2017-07-29 00:31:46,394][MainThread:12348][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
[ 2017-07-29 00: 31: 47,629] [MainThread: 12348] [task_id: core.core] [core.py:25 ] [ERROR] [username does not exist]
[2017-07-29 00:31:57,912][MainThread:10528][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
[2017-07-29 00:32:03,340][MainThread:12744][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
[2017-07-29 00:32:05,065][MainThread:12916][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
[ 2017-07-29 00: 32: 08,181] [MainThread: 12916] [task_id: core.core] [core.py:25 ] [ERROR] [username does not exist]
[2017-07-29 00:32:13,638][MainThread:7220][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
[ 2017-07-29 00: 32: 23,005] [MainThread: 7220] [task_id: core.core] [core.py:20 ] [INFO] [successful login]
[ 2017-07-29 00: 32: 40,941] [MainThread: 7220] [task_id: core.core] [core.py:20 ] [INFO] [successful login]
[ 2017-07-29 00: 32: 47,222] [MainThread: 7220] [task_id: core.core] [core.py:20 ] [INFO] [successful login]
[ 2017-07-29 00: 32: 51,949] [MainThread: 7220] [task_id: core.core] [core.py:25 ] [ERROR] [username does not exist]
[ 2017-07-29 00: 33: 00,213] [MainThread: 7220] [task_id: core.core] [core.py:20 ] [INFO] [successful login]
[2017-07-29 00:33:50,118][MainThread:8500][task_id:conf.my_log_settings][my_log_settings.py:75][INFO][It works!]
[ 2017-07-29 00: 33: 55,845] [MainThread: 8500] [task_id: core.core] [core.py:20 ] [INFO] [successful login]
[ 2017-07-29 00: 34: 06,837] [MainThread: 8500] [task_id: core.core] [core.py:25 ] [ERROR] [username does not exist]
[ 2017-07-29 00: 34: 09,405] [MainThread: 8500] [task_id: core.core] [core.py:25 ] [ERROR] [username does not exist]
[ 2017-07-29 00: 34: 10,645] [MainThread: 8500] [task_id: core.core] [core.py:25] [ERROR] [username does not exist]

 A reference to the goddess Eva-J blog: https://www.cnblogs.com/Eva-J/articles/7292109.html#_label13

Guess you like

Origin www.cnblogs.com/hercules-chung/p/12388012.html