Day31 博客
Posted 呆萌小河马的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Day31 博客相关的知识,希望对你有一定的参考价值。
目录结构
choose_class--bin--start.py
--conf--settings.py
--src--core.py,models.py
目录结构start文件——程序的快捷方式,由其启动程序
import sys,os
BASE_DIR=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
from src import core
# from src.models import *
if __name__ == ‘__main__‘:
core.register()
settings文件——程序的配置信息
STUDENTDB_PATH=‘/Users/guoxing/Documents/学习笔记/day31/choose_class/db/student/‘
core文件——程序的核心
from src import models
import pickle
def register():
while True:
name=input(‘name:‘).strip()
if not name:
print(‘姓名必填‘)
continue
name_id=input(‘name_id:‘)
age=input(‘age:‘).strip()
sex=input(‘sex:‘).strip()
group=input(‘group:‘).strip()
break
obj=models.Student(name,name_id,age,sex,group)
obj.tell_info()
# with open(‘%s/%s‘%(settings.STUDENTDB_PATH,obj.name_id),‘wb‘) as f:
# pickle.dump(obj,f)
obj.save()
models——程序的模块
from conf import settings
import pickle
class Student:
def __init__(self,name,name_id,age,sex,group):
self.name=name
self.name_id=name_id
self.age=age
self.sex=sex
self.group=group
def walk(self):
print(‘%s is walking‘%self.name)
def study(self):
print(‘%s is study‘%self.name)
def tell_info(self):
print(‘‘‘
--------%s info--------
NAME:%s
ID:%s
AGE:%s
SEX:%s
GROUP:%s
‘‘‘%(self.name,self.name,self.name_id,self.age,self.sex,self.group))
def save(self):
with open(‘%s/%s‘ % (settings.STUDENTDB_PATH,self.name_id), ‘wb‘) as f:
pickle.dump(self, f)
以上是关于Day31 博客的主要内容,如果未能解决你的问题,请参考以下文章