Knowledge content:
1.luffy buy tesla
2. ATM + shopping mall
1. Luffy buys tesla
need:
1 1. Directory structure description 2 account 3 luffy.json --> store user account information { " money " : 1000000, " credit " : 300000 } 4 tesla.json --> store tesla account information { " money " : 0} 5 bin 6 start.py --> project entry file 7 core 8 withdraw.py --> project core file 9 logs 10 bank.log --> log file 11 12 2 . Function description 13 An interactive window appears when start.py is executed as follows: 14 ----Luffy Bank---- 15 1. Account information 16 2. Transfer 17 3. Withdrawal 18 (1 ) Select 1 Account information to display Luffy's current account balance and credit limit (stored in luffy.json) 19 (2 ) Choose 2 to transfer and directly deduct 750,000 and 5% of the tax in the luffy account and add 750,000 to the tesla account (stored in tesla.json) 20 (3) When choose 3 to withdraw, the withdrawal amount should be less than the credit limit, and the interest is 5% , the withdrawal amount is user-defined 21 (4) The user's transfer, login, and withdrawal operations are recorded in the bank.log through the logging module
Ideas and special skills:
1. Ideas
start.py is the main entrance of the program. It calls the choose_func function in withdraw to execute the code. The function of the choose_func function is similar to a menu. It connects all functions together. First, the three functions of user information display, user transfer and withdrawal are completed. Then, the user login authentication is implemented through the decorator, and the user only needs to log in once and then does not need to log in. Finally, the log record is written as a function. The parameters are the log level and output information. You can call this function where you need to output the log.
2. Special skills
How to make start.py call the function in withdraw.py, use the os module to get the root directory of the project and then dynamically add it to the system environment variable, the code is as follows:
1 def main(): 2 #Add dynamically 3 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath( __file__ )))) 4 #Import the core module file 5 from core import withdraw 6 withdraw.chose_func() 7 8 9 #Project main entry 10 if __name__ == ' __main__ ' : 11 main()
The decorator implements user login:
1 #Decorator function -> implement login 2 def login(func): 3 def wrapper(*args, ** kwargs): 4 global flag 5 if flag: #Login verification before logging in 6 print ( " Please log in first and then do the operation " ) 7 username = input( " username: " ) 8 password = input( " password: " ) 9 if username == _username and password ==_password: 10 # print("Login successful!") 11 logging_message( " info " , " %s login! " % username) 12 flag = False #If the login is successful, change the flag to False, and then use other functions without logging in 13 func(*args, ** kwargs) 14 else : 15 # print("Wrong username or password!") 16 logging_message( " warn " , " wrong username or wrong password! ") 17 else: #You don't need to verify if you have logged in 18 func(*args, ** kwargs) 19 20 return wrapper
Note: logging_message() is to call the log output function
Log output function:
1 # Implement log function Log location: /logs/bank.log 2 def logging_message(level, message): 3 route = os.path.dirname(os.path.dirname(os.path.abspath( __file__ )) ) #extract The absolute path of the project 4 route += r " \logs\bank.log " #Add the relative path of the project file 5 6 # create logger 7 logger = logging.getLogger(_username) #Get the logger object 8 logger.setLevel(logging. DEBUG) #Set a global log level 9 10 # create console handler and set level to debug 11 ch = logging.StreamHandler() #Print the log to the screen 12 ch.setLevel(logging.INFO) #Set the log level of the screen 13 14 # create file handler and set level to warning 15 fh = logging.FileHandler(route ) # Print the log to the file 16 fh.setLevel(logging.INFO) #Set the log level of the file 17 18 # create formatter Set the output format 19 formatter = logging.Formatter( ' %(asctime)s - %(name)s - % (levelname)s - %(message)s ' ) 20 21 # add formatter to ch and fh set the output format for screen and file 22 ch.setFormatter(formatter) 23 fh.setFormatter(formatter) 24 25 # add ch and fh to logger Print the log to the specified handler 26 logger.addHandler(ch) 27 logger.addHandler(fh) 28 29 # 'application' code 30 #Set information 31 if level == ' debug ' : 32 logger.debug(message) 33 if level == ' info ' : 34 logger.info(message) 35 if level == ' warn' : 36 logger.warning(message) 37 if level == ' error ' : 38 logger.error(message) 39 if level == ' critical ' : 40 logger.critical(message) 41 42 #Add the following sentence, in the record Remove handle after logging 43 logger.removeHandler(ch) 44 logger.removeHandler(fh)
Code:
The code is on github: https://github.com/15387062910/luffy_tesla/tree/first
2. ATM + shopping mall
need:
1 Simulate the realization of an ATM + shopping mall program 2 The functions are as follows: 3 The quota is 15000 or custom 4 Realize the shopping mall, add things to the shopping cart, call the credit card interface to check out The 10th of each month is the repayment day . If it is overdue, it will be calculated according to 5/10,000 of the total arrears. Daily interest calculation 7 Support multi -account login 8 Support transfer between accounts The log 12 provides management interfaces, including adding accounts, user quotas, freezing accounts, and the like. . . 13 Decorators for user authentication
Ideas and special skills:
Code: