dadadada

1|0用面向对象的思路来写


1|1个人版


bin/starts.py

分享图片
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title></title>
        <style type="text/css">
            #cnblogs_post_body p img:hover{
                cursor: pointer;
            }
            /*#cnblogs_post_body p img[style~=grab]{
                width: 800px;
            }*/
            li{
                list-style: ;
            }
        </style>
    </head>
    
    <body>
        <h1>1</h1>
        <h1>2</h1>
        <h1>3</h1>
        <h1>4</h1>
        <h1>5</h1>
        <h1>6</h1>
        <h1>7</h1>
        <div id="post_body">
            <p>
                <img src="img/头像.jpg" data-lightbox="roadtrip" alt="" />
            </p>
            <p>
                <img src="img/头像.jpg" data-lightbox="roadtrip" alt="" />
            </p>
            <p>
                <img src="img/头像.jpg" data-lightbox="roadtrip" alt="" />
            </p>
            <p>
                <img src="img/头像.jpg" alt="" />
            </p>
            <p>
                <img src="img/头像.jpg" alt="" />
            </p>
            <p>
                <img src="img/头像.jpg" alt="" />
            </p>
            <p>
                <img src="img/头像.jpg" data-lightbox="roadtrip" alt="" />
            </p>
            <p>
                <img src="img/头像.jpg" data-lightbox="roadtrip" alt="" />
            </p>
        </div>
        
<!--    <script src="https://cdn.bootcss.com/zooming/2.1.1/zooming.js"></script>-->
    <!--<script type="text/javascript">
        var zooming = new Zooming({
            "bgColor":"#000",
            "bgOpacity": "0.8",
            //bgOpacity
            });
        zooming.listen(‘#cnblogs_post_body p img‘);
        // #000
    </script>-->
    <!--<script src="https://cdn.bootcss.com/lightbox2/2.11.1/js/lightbox-plus-jquery.min.js"></script>
    <link href="https://cdn.bootcss.com/lightbox2/2.11.1/css/lightbox.css" rel="stylesheet">
    <script src="https://cdn.bootcss.com/lightbox2/2.11.1/js/lightbox.js"></script>
    <script>
        lightbox.option({
          ‘resizeDuration‘: 200,
          ‘wrapAround‘: true
        })
    </script>-->
    <!--<script src="http://www.suwanbin.com/my_zooming.js"></script>-->
    <!--<script src="js/new_file.js" type="text/javascript" charset="utf-8"></script>-->
    <script src="https://common.cnblogs.com/scripts/jquery-2.2.0.min.js"></script>
    <script type="text/javascript">
        $(window).load(function(){
            $("h1").each(function(index){
                console.log($(this));
                preStyle = "" + (index + 1);
                this.innerText = preStyle + this.innerText;
            });
        });
    </script>
        <script src="https://cdn.bootcss.com/zooming/2.1.1/zooming.js"></script>
<script type="text/javascript">
    var zooming = new Zooming({
        "bgColor":"#000",
        "bgOpacity": "0.8",
        "enableGrab": false,
        // 放大后的回调函数
        "onOpen": function(e){
            console.log(e,  before);
            e.style.position = "absolute";
            e.style.width = "800px";
            console.log(e.style.postion,  before);
        },
        // 关闭后的回调函数
        "onClose": function(e){
            console.log(e,  after);
            e.style.position = "none";
            e.style.width = "200px";
        },
    });
    zooming.listen(#cnblogs_post_body p img);
</script>
    
    </body>
    
</html>
这是博客园的代码 vue.html

 

views/main.py
分享图片
from views import admin from views import teacher from views import student from lib import common func_list = { " 1 " : student.run, " 2 " : teacher.run, " 3 " : admin.run, } def run(): menu = """ 视图列表如下: 1.学生视图 2.老师视图 3.管理员视图 q.退出 """ common.choose_func(menu, func_list)
views/main.py

views/admin.py

分享图片
from lib import common from interface import admin def login(): username = input( " 请输入用户名>>>: " ).strip() password = input( " 请输入密码>>>: " ).strip() if username and password: res = admin.login(username, password) if not res: print( " 您的账号或密码有误 " ) else : print( " 登录成功! " ) else : print( " 您的账号或密码有误 " ) def create_school(): school_name = input( " 请输入校区名称>>>: " ).strip() address = input( " 请输入学校地址>>>: " ).strip() if not school_name and address: print( " 学校名或地址不能为空! " ) else : res = admin.create_school(school_name, address) if not res: print( " 校区已存在,请换一个试试 " ) else : print( " 校区创建成功! " ) def create_teacher(): # 先选择校区,再选择老师 res = get_all_school_and_print() if not res: return False school_name = res teacher = input( " 请输入老师姓名>>>: " ).strip() password = input( " 请输入老师账号密码>>>: " ).strip() if not teacher and password: print( " 您的输入有误! " ) else : res = admin.create_teacher(school_name, teacher, password) if res: print( " 老师创建成功! " ) else : print( " 老师创建失败! " ) def create_course(): pass def create_class(): pass def arrange_teacher_to_class(): pass func_list = { " 1 " : login, " 2 " : create_school, " 3 " : create_teacher, " 4 " : create_course, " 5 " : create_class, " 6 " : arrange_teacher_to_class, } def run(): menu = """ 请选择功能编号 1.登录 2.创建校区 3.创建讲师 4.创建课程 5.创建班级 6.给老师指定班级 """ common.choose_func(menu, func_list) def get_all_school_and_print(): all_school = admin.get_all_school() while True: for item in all_school: print(f " {all_school.index(item) + 1}. {item} " ) choice = input( " 请输入编号选择校区>>>: " ).strip() if choice in [ q , exit ]: return False elif choice.isdigit() and int(choice) - 1 in range(0, len(all_school)): return all_school[int(choice)-1 ] else : print( " 您的输入有误! ")
views/admin.py

interface/admin.py

分享图片
from db.models import Admin from db.models import School from db.models import Teacher from lib import common def login(username, password): res = Admin.get_obj(username) if not res: return False password = common.get_md5(password) admin_obj = res if password == admin_obj.password: return True def register(username, password): # 判断是否已存在 res = Admin.get_obj(username) if res: return False password = common.get_md5(password) Admin(username, password) return True def create_school(school_name, address): # 判断学校是否存在,不存在则创建 res = School.get_obj(school_name) if res: return False School(school_name, address) return True def create_teacher(school_name, teacher, password): # 判断老是是否存在 res = Teacher.get_obj(teacher) if not res: Teacher(school_name, teacher, password) return True if __name__ == __main__ : register( jason , 123 ) register( tank , 123 ) register( jerry , 123 ) print (Admin.get_all_obj_name()) def get_all_school(): res = School.get_all_obj_name() return res
interface/admin.py

db/models.py

分享图片
from db import db_handler class BaseClass: @classmethod def get_obj(cls, name): class_name = cls. __name__ .lower() res = db_handler.select(class_name, name) return res def save(self): class_name = self. __class__. __name__ .lower() res = db_handler.save(class_name, self) if res: return True @classmethod def get_all_obj_name(cls): class_name = cls. __name__ .lower() res = db_handler.select_all_name(class_name) if res: return res class Admin(BaseClass): def __init__ (self, name, password): self.name = name self.password = password self.save() # 初始化完成代表着对象已经创建好了,所以要把它给存起来 def create_school(self, school_name, address): pass class Student(BaseClass): def __init__(self, name, password, school_name): # 把创建学生时需要的信息写进来,不需要的默认赋值就行了 self.name = name self.password = password self.school_name = school_name self.class_name = None self.is_pay = False self.score = None self.save() class School(BaseClass): def __init__ (self, name, address): self.name = name self.address = address self.save() class Teacher(BaseClass): def __init__ (self, school_name, name, password): self.school_name = school_name self.name = name self.password = password self.save() # ... # 省略n多对象
db/models.py

db/db_handler.py

分享图片
import pickle from typing import List, Union from conf import settings import os def select(class_name, name): # 文件夹路径 DB_PATH + class_name + self.name dir_path = os.path.join(settings.DB_PATH, class_name, name) if not os.path.exists(dir_path): return False with open(dir_path, rb ) as f: obj = pickle.load(f) return obj def save(class_name, obj): dir_path = os.path.join(settings.DB_PATH, class_name) if not os.path.exists(dir_path): os.makedirs(dir_path) obj_path = os.path.join(dir_path, obj.name) with open(obj_path, wb ) as f: pickle.dump(obj, f) return True def select_all_name(class_name): dir_path = os.path.join(settings.DB_PATH, class_name) if not os.path.exists(dir_path): return False obj_name_list = os.listdir(dir_path) return obj_name_list
db/db_handler.py

conf/settings.py

分享图片
import os BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath( __file__ ))) DB_PATH = os.path.join(BASE_PATH, db , datas )
conf/settings.py

lib/common.py

分享图片
import hashlib def choose_func(menu, func_list): while True: print (menu) choice = input( " 请选择对应编号>>>: " ).strip() if choice in [ q , exit ]: break elif choice in func_list: func_list[choice]() else : print( " 您的输入有误,请重试! " ) def get_md5(todo_str): md = hashlib.md5() salt = xkxt .encode( utf-8 ) md.update(salt) md.update(todo_str.encode( utf-8 )) md.update(salt) # 双重加盐,玩的就是骚 return md.hexdigest()

1|2tank版


start.py

分享图片
import os import sys from core import src sys.path.append(os.path.dirname( __file__ )) if __name__ == __main__ : src.run()
start.py

core/src.py

分享图片
from core import admin, student, teacher func_dic = { 1 : admin.admin_view, 2 : student.student_view, 3 : teacher.teacher_view, } # 主程序视图 def run(): while True: print( ‘‘‘ 1.管理员视图 2.学生视图 3.老师视图 q.退出 ‘‘‘ ) choice = input( 请选择用户功能: ).strip() if choice == q : break if choice not in func_dic: print( 输入有误! ) continue func_dic[choice]()
core/src.py

core/admin.py

分享图片
from interface import admin_interface, common_interface from lib import common admin_info = { name : None } def register(): while True: username = input( 请输入用户名: ).strip() password = input( 请输入密码 ).strip() re_password = input( 请确认密码 ).strip() if password == re_password: flag, msg = admin_interface.admin_register(username, password) if flag: print (msg) break else : print (msg) def login(): while True: username = input( 请输入用户名: ).strip() password = input( 请输入密码 ).strip() flag, msg = common_interface.login_interface(username, password, admin ) if flag: print (msg) admin_info[ name ] = username break else : print (msg) @common.login_auth( admin ) def create_school(): while True: school_addr = input( 请输入学校地址: ).strip() school_name = input( 请输入学校名称: ).strip() flag, msg = admin_interface.create_school_interface(admin_info[ name ], school_name, school_addr) if flag: print (msg) break else : print (msg) @common.login_auth( admin ) def create_teacher(): while True: teacher_name = input( 请输入老湿的名字: ).strip() flag, msg = admin_interface.create_teacher_interface(admin_info[ name ], teacher_name) if flag: print (msg) break else : print (msg) @common.login_auth( admin ) def create_course(): while True: school_list = common_interface.check_all_file( school ) for index, school in enumerate(school_list): print (index, school) choice = input( 请选择学校编号: ).strip() if choice.isdigit(): choice = int(choice) if choice >= 0 and choice < len(school_list): school_name = school_list[choice] course_name = input( 请选择课程: ).strip() flag, msg = admin_interface.create_course_interface(admin_info[ name ], school_name, course_name) if flag: print (msg) break else : print (msg) else : print( 请输入正确的编号 ) else : print( 请输入数字 ) admin_dic = { 1 : register, 2 : login, 3 : create_school, 4 : create_teacher, 5 : create_course, } def admin_view(): while True: print( ‘‘‘ 1.注册 2.登录 3.创建学校 4.创建老师 5.创建课程 q.退出 ‘‘‘ ) choice = input( 请选择功能: ).strip() if choice == q : break if choice not in admin_dic: print( 输入有误! ) continue admin_dic[choice]()
core/admin.py

interface/admin_interface.py

分享图片
from db import models import os from conf import settings # 注册接口 def admin_register(user, pwd): # admin对象 admin_obj = models.Admin.select(user) # cls, user if admin_obj: return False, 用户已存在! # 调用类 自动触发类内部的 __init__() models.Admin(user, pwd) # self, user, pwd return True, %s 创建成功! % user # 创建学校接口 def create_school_interface(admin_name, school_name, school_addr): school_obj = models.School.select(school_name) if not school_obj: admin_obj = models.Admin.select(admin_name) admin_obj.add_school(school_name, school_addr) return True, %s 学校创建成功! % school_name # 创建老师接口 def create_teacher_interface(admin_name, teacher_name, pwd= 123 ): tea_obj = models.Teacher.select(teacher_name) if tea_obj: return False, 老湿已存在 admin_obj = models.Admin.select(admin_name) admin_obj.add_teacher(teacher_name, pwd) return True, %s 老湿创建成功! % teacher_name # 创建课程解耦 def create_course_interface(admin_name, school_name, course_name): school_obj = models.School.select(school_name) if not school_obj: return False, 学校不存在! admin_obj = models.Admin.select(admin_name) admin_obj.add_course(course_name) school_list = school_obj.school_course_list print (school_list) if course_name not in school_list: school_obj.school_course_list.append(course_name) school_obj.save() return True, %s 课程创建成功! % course_name else : return False, 课程也存在!
interface/admin_interface.py

db/models.py

分享图片
import pickle from conf import settings import os from db import db_hanlder class Base: def save(self): # admin db_hanlder.db_save(self) @classmethod def select(cls, name): obj = db_hanlder.db_select(cls, name) return obj class Admin(Base): def __init__ (self, name, pwd): self.name = name self.pwd = pwd self.save() # admin_obj def add_school(self, school_name, school_addr): School(school_name, school_addr) def add_teacher(self, teacher_name, pwd): Teacher(teacher_name, pwd) def add_course(self, course_name): Course(course_name) class School(Base): def __init__ (self, school_name, school_addr): self.name = school_name self.school_addr = school_addr self.school_course_list = [] self.save() class Teacher(Base): def __init__ (self, teacher_name, pwd): self.name = teacher_name self.pwd = pwd self.teacher_course_list = [] self.save() def teacher_choose_course(self, course_name): self.teacher_course_list.append(course_name) self.save() def check_course_student(self, course_name): course_obj = Course.select(course_name) return course_obj.student_list def change_score(self, course_name, student_name, score): student_obj = Student.select(student_name) student_obj.score[course_name] = score student_obj.save() class Course(Base): def __init__ (self, course_name): self.name = course_name self.student_list = [] self.save() def add_student(self, student_name): self.student_list.append(student_name) self.save() class Student(Base): def __init__ (self, student_name, student_pwd, ): self.name = student_name self.pwd = student_pwd self.school = None self.student_course_list = [] self.score = {} self.save() def choose_school(self, school_name): self.school = school_name self.save() def choose_course(self, course_name): self.student_course_list.append(course_name) self.score[course_name] = 0 self.save()
db/models.py

db/db_hanlder.py

分享图片
import os from conf import settings import pickle def db_save(obj): # admin path = os.path.join(settings.DB_PATH, obj. __class__. __name__ .lower()) if not os.path.isdir(path): os.mkdir(path) user_path = os.path.join(path, obj.name) with open(user_path, wb ) as f: pickle.dump(obj, f) f.flush() def db_select(cls, name): # 文件夹的路径 path = os.path.join(settings.DB_PATH, cls. __name__ .lower()) # 判断文件夹是否存在 if not os.path.isdir(path): os.mkdir(path) # 用户文件 user_path = os.path.join(path, name) if not os.path.exists(user_path): return False with open(user_path, rb ) as f: user_obj = pickle.load(f) if user_obj: return user_obj else : return False
db/db_hanlder.py

conf/settings.py

分享图片
import os BASE_PATH = os.path.dirname(os.path.dirname( __file__ )) DB_PATH = os.path.join(BASE_PATH, db )
conf/settings.py

lib/common.py

分享图片
def login_auth(user_type): from core import admin, student, teacher def auth(func): def inner(*args, ** kwargs): if user_type == admin : if admin.admin_info[ name ]: return func(*args, ** kwargs) else : admin.login() elif user_type == student : if student.student_info[ name ]: return func(*args, ** kwargs) else : student.login() elif user_type == teacher : if teacher.teacher_info[ name ]: return func(*args, ** kwargs) else : teacher.login() return inner return auth
lib/common.py

 


__EOF__

作  者Suwanbin
出  处https://www.cnblogs.com/suwanbin/p/11279832.html
关于博主:希望我的博客对你能有所帮助,如果内容有什么错误,欢迎评论指出哦,或者直接私信
版权声明声明如果是整文转载请在文章开头或者结尾处 注明 原文地址 如果是大段参考备注 参考链接
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!

相关文章
相关标签/搜索