配置文件-2
阿新 • • 發佈:2018-07-15
read 屬性 配置文件 配置 app Coding add name reg
# 使用配置文件
# why :給程序提供一些默認的或個性化的全局參數
# what: 分塊kv存儲,默認形式有 ini,conf, cfg
# how : configparse, http://devdocs.io/python~3.6/library/configparser
#[DEFAULT] # section 章節 特殊的章節
import configparser base_dir = r‘D:\python全站‘ config = configparser.ConfigParser() config[‘DEFAULT‘] = {‘base_dir‘:‘D:\python全站‘} config_path = os.path.join(base_dir, ‘common.ini‘) with open(config_path, ‘w‘) as f: config.write(f)
--------------------------------------------------------------------------- NameError Traceback (most recent call last) <ipython-input-2-9e2c1545cc34> in <module>() 3 config = configparser.ConfigParser() 4 config[‘DEFAULT‘] = {‘base_dir‘:‘D:\python全站‘} ----> 5 config_path = os.path.join(base_dir, ‘common.ini‘) 6 with open(config_path, ‘w‘) as f: 7 config.write(f) NameError: name ‘os‘ is not defined
import configparser import os config = configparser.ConfigParser() config[‘DEFAULT‘] = {‘base_dir‘:‘D:\python全站‘} config[‘DEFAULT‘][‘db_type‘] = ‘db‘ config[‘DEFAULT‘][‘db_path‘] = ‘${base_dir}/data.db‘ config[‘DEFAULT‘][‘max_items‘] = ‘1000‘ config[‘DEFAULT‘][‘auto_save‘] = ‘True‘ config[‘coop‘] = {} config[‘coop‘][‘db_type‘] = ‘db‘ config[‘coop‘][‘db_path‘] = ‘${base_dir}/data.db‘ config_path = os.path.join(base_dir, ‘common.ini‘) with open(config_path, ‘w‘) as f: config.write(f) # 將f寫入到config中?!
config.sections() # 只顯示 coop 方法sections() DEFAULT的特殊地方,類似於類的繼承,現在coop繼承了DEFAULT的屬性
[‘coop‘]
config[‘coop‘][‘db_type‘] # 如上面所言
‘db‘
config[‘coop‘][‘auto_save‘] # 配置文件裏面都是字符串
‘True‘
bool(config[‘coop‘][‘auto_save‘]) # 轉換
True
for key in config[‘coop‘]: # 繼承 printt的是字典的key,而不是裏面的子集:字典
print(key)
db_type
db_path
base_dir
max_items
auto_save
coop = config[‘coop‘] # sections
coop.getboolean(‘auto_save‘) # 獲取真的bool值
True
coop.getint(‘max_items‘) # 獲取的是整數getint()
1000
coop?
coop.get(‘db_path‘) #字典的方法dic.get()
‘${base_dir}/data.db‘
config.read(config_path) #config的方法config.read() 讀取的是文件
[‘D:\\python全站\\common.ini‘]
config.read?
#Signature: config.read(filenames, encoding=None)
Docstring:
Read and parse a filename or a list of filenames.
Files that cannot be opened are silently ignored; this is
designed so that you can specify a list of potential
configuration file locations (e.g. current directory, user‘s
home directory, systemwide directory), and all existing
configuration files in the list will be read. A single
filename may also be given.
config.add_section(‘fang‘) # 向config中增加section:config.add_section()
config.set(‘fang‘, ‘db_name‘, ‘ccoop.pkl‘) #設置‘fang’的子集
with open(config_path, ‘w‘) as f:
config.write(f)
config = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
config.read(config_path) # 獲取的是完整的路徑
[‘D:\\python全站\\common.ini‘]
config[‘coop‘][‘db_path‘] ######\全路徑
‘D:\\python全站/data.db‘
# 面向對象三步走
# 1 規劃函數
def write_config():
pass
def config_read():
pass
def config_check():
pass
def add_config():
pass
# 面向對象三步走
# 1 規劃函數
import os
import pickle
import configparser
base_dir = r‘D:\python全站‘
config_path = os.path.join(base_dir, ‘demo.ini‘)
data = {
‘DEFAULT‘:{
‘base_dir‘:‘D:\python全站‘, ‘db_type‘:‘db‘, ‘db_path‘:‘${base_dir}/data.db‘, ‘max_items‘:‘1000‘, ‘auto_save‘:‘True‘
},
‘coop‘:{
‘db_type‘:‘db‘,‘db_path‘:‘${base_dir}/data.db‘
}
}
# base_dir = input(‘please input you base dir:‘)
# suffix = input(‘please input your suffix:‘)
# config_path = os.path.join(base_dir, suffix)
config = configparser.ConfigParser()
def write_config(data:dict): #必須是dict,四個字母
# config = configparser.ConfigParser()
for k, v in data.items():
config[k] = v
config_path = os.path.join(base_dir, ‘demo.ini‘)
with open(config_path, ‘w‘) as f:
config.write(f)
def config_read(config_path):
config = configparser.ConfigParser()
config.read(config_path)
for k in config.sections():
for m in config[k]:
print(m)
# for m, n in k.items():
# print(m, n)
# return config.read(config_path)
def config_check(sect):
pass
def add_config(sect, k, v):
# config = configparser.ConfigParser()
config.add_section(sect)
config.set(sect, k, v)
with open(config_path, ‘w‘) as f:
config.write(f)
write_config(data)
add_config(‘fang‘, ‘db_type‘, ‘db‘)
# coop = config[‘fang‘]
# coop[‘fang‘] #config_read()的時候到了
config_read(config_path)
config.sections() #如何print出所有的demo.db中多有內容
import re
re_gen = re.compile(r‘(\n)‘)
with open(config_path, ‘r‘) as f:
aa = f.readlines()
print(aa)
bb = []
for i in aa:
cc = re_gen.sub(‘ ‘, i)
bb.append(cc)
print(bb)
db_type
db_path
base_dir
max_items
auto_save
db_type
base_dir
db_path
max_items
auto_save
[‘[DEFAULT]\n‘, ‘base_dir = D:\\python全站\n‘, ‘db_type = db\n‘, ‘db_path = ${base_dir}/data.db\n‘, ‘max_items = 1000\n‘, ‘auto_save = True\n‘, ‘\n‘, ‘[coop]\n‘, ‘db_type = db\n‘, ‘db_path = ${base_dir}/data.db\n‘, ‘\n‘, ‘[fang]\n‘, ‘db_type = db\n‘, ‘\n‘]
[‘[DEFAULT] ‘, ‘base_dir = D:\\python全站 ‘, ‘db_type = db ‘, ‘db_path = ${base_dir}/data.db ‘, ‘max_items = 1000 ‘, ‘auto_save = True ‘, ‘ ‘, ‘[coop] ‘, ‘db_type = db ‘, ‘db_path = ${base_dir}/data.db ‘, ‘ ‘, ‘[fang] ‘, ‘db_type = db ‘, ‘ ‘]
re_gen.sub?
print?
# 使用configadmin為項目51備忘錄擴展功能
#針對上次作業的MemoAdmin類
# 1,添加註冊和登陸功能,用戶密碼使用dict保存為:users.pkl
# 2. 添加配置文件,為備忘錄數據指定路徑,類型和文件名
# 比如coop,則數據文件可以為coop.pkl或者coop.db
# 3.註冊時,相應數據文件根據用戶名在配置文件保存為新的section,
# 如coop,則有新的section 叫做[coop]
# 4 .啟動程序先提示登陸每次登陸時候,先根據配置文件讀取用戶信息,找不到,提示註冊
class MemoAdmin:
def __init__(self, config):
self.config = config
def register(self):
pass
def login(self):
pass
配置文件-2