config.py
来自「PyChem是用Python语言编写的多元变量分析软件。它包括一个前端图形界面用」· Python 代码 · 共 133 行
PY
133 行
"""config.py adapted from GaussSum, gausssum.sf.net"""
import ConfigParser,os,sys
import pychem
whereami = pychem.__path__[0]
class Preferences(object):
def __init__(self):
"""Initialise the preferences."""
if sys.platform == "win32":
location = os.getenv("APPDATA")
else: # i.e. for Linux
location = os.getenv("HOME")
self.filename = os.path.join(location,".pychem")
if not os.path.isfile(self.filename):
self.setdefaults()
self.read()
def __getitem__(self,key):
return self.settings[key]
def __setitem__(self,key,value):
self.settings[key] = value
def setdefaults(self):
"""Set the defaults."""
self.settings = {
'main.wdir':os.path.join(whereami,"examples")
}
self.write()
def read(self):
"""Read the preferences from an .ini file.
Taken from the Python Cookbook 1st ed. (O'Reilly):
Uses the Python module ConfigParser for the complicated stuff.
"""
cp = ConfigParser.ConfigParser()
cp.read(self.filename)
config = {}
for sec in cp.sections():
name = sec.lower()
for opt in cp.options(sec):
fullname = ".".join([name,opt.lower()])
config[fullname] = cp.get(sec,opt).strip()
self.settings = config
def write(self):
"""Write the preferences to an .ini file."""
cp = ConfigParser.ConfigParser()
for key,value in self.settings.items():
sec = key.split('.')[0]
opt = key.split('.')[1]
if not cp.has_section(sec):
cp.add_section(sec)
cp.set(sec,opt,str(value))
out = open(self.filename,"w")
cp.write(out)
out.close()
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?