Python的三层架构(基础篇)

在Python的代码编写中,有一个简单的架构,即三层架构,其就是为了使程序达到高内聚低耦合。现在为了学习机器学习,对Python进行简单的学习,此博客作为学习的记录,如有错误,敬请指出。

三层架构分为:表现层(Presentation layer)、业务逻辑层(Business Logic Layer)、数据访问层(Data access layer)

表现层:即UI层,程序运行的入口,如果程序包括界面,其界面就是放在这一层中,直接调用业务逻辑层中封装好的方法。

业务逻辑层:完成程序的业务逻辑,对数据访问层进行调用,将从数据访问层中获取到的数据反馈给表现层。

数据访问层:直接操作数据库,对表进行增、删、改、查,其一般一个表对应一个程序文件。

 

三层架构的数据流向如下图所示:

      注意:一般表示层不会跨过业务逻辑层去访问数据访问层。

三层架构的基本实现:

 

源代码:

ps:使用到的数据库表结构如下图所示

       index.py(表现层)

'''
Created on 2019年7月8日

@author: Administrator 
'''

# 主文件,程序进来就执行的文件
from model.userinfo import UserInfo


def main():
    
    username = input("username:")
    pwd = input("password:")
    
    userinfo = UserInfo()
    result = userinfo.Check(username, pwd)
    
    if result is None:
        print("failed")
        return;
    else:
        print("success")
        return result
    
    
if __name__ == "__main__":
    
    '''helper = h.MysqlHelper()
    sql = "select * from userinfo where user <> %s"
    
    print(helper.Get_dict(sql, "ddd"))'''
    
    main()
    
    
    
    

      utility(业务逻辑层):sql_helper.py

'''
Created on 2019年7月8日

@author: Administrator
'''

import pymysql
from two import conf

class MysqlHelper(object):
    
    def __init__(self):
        self.__conn_dict = conf.conn_dict # 为conf中定义的一个字典
    
    def Get_dict(self,sql,params):
        
        conn = pymysql.connect(**self.__conn_dict ) # connect the database
        
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        
        cursor.execute(sql,params)
        
        data = cursor.fetchall()
        
        cursor.close()
        conn.close()
        
        return data
        
    def Get_one(self,sql,params):
        
        conn = pymysql.connect(**self.__conn_dict) # connect the database
        
        cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
        
        cursor.execute(sql,params)
        
        data = cursor.fetchone()
        
        cursor.close()
        conn.close()
        
        return data
    

    
    
        
    
    
    

       model(数据访问层):userinfo.py

'''
Created on 2019年7月8日

@author: Administrator
'''
from utility.sql_helper import MysqlHelper 

class UserInfo(object):
    
    def __init__(self):
        self.__helper = MysqlHelper()
        
    def getOne(self,name):
        
        sql = "select * from userinfo user = %s;"
        params = (name,)
        
        return self.__helper.Get_one(sql, params)
     
    def Check(self,username,pwd):
        sql = " select * from userinfo where user = %s and pwd = %s;" 
        
        params = (username,pwd)
        
        return self.__helper.Get_one(sql, params)
        
        

其他注意:

    

def t1(*a):

    t3(*a)

def t2(**k):

    t3(**k)

def t3(a, b):

    print(a,b)

t1(1,2)
t2(a=1,b=2)

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值