配置文件

# 使用配置文件
# 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'
    }

}
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, 'wb') as f:
        config.write(f)

def config_read():
    pass
def config_check():
    pass
def add_config():
    pass
# 使用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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章