Python需要配置檔案嗎?
I’ve never understood why people design systems in python to use config files. IMO there are two types of data and we can handle them both in dead simple ways:
First: Read/write data, like UI/user preferences. I say, just use python dictionaries, and serialize them however the hell you want (json, yaml, pickle, cPickle, I don’t care). I don’t understand why anyone would build anything more complex (except maybe a wrapper over this functionality), and especially why anyone would bother using a non-serialized format like ini or cfg. Can there be a good reason?
Second: Read-only data, like application configuration (nothing that is edited by your app). This, too, is very commonly in config files with special like xml or ini. Well in this case I say, why bother even with serialized data at all? Just make it code!
This data, by definition, does not have to be statically known and serialized, it is only needed at runtime. So why not make it a python module, have your code import/exec the python file, and access attributes from the python module?
There’s a good deal of data we may only know at runtime, or we cannot statically represent, so we end up making the systems that consume this configuration data much more complex. (For an example, see Sphinx’s config, I think it does it right).
An example of where I think python-as-configuration has huge power is with complex configuration data like for a plugin system. Instead of relying on some static functionality, the configuration can even be callables that return a plugin instance- so now you don’t have to refer to globals inside of your plugins, you can have your own initializer with a much larger signature, and the plugin system can have its well-defined initialization interface.
I’m not sure I did a great job explaining that example- it deserves its own post. We’ve used it extensively for new tools and systems we’ve developed and it seems to work great (and results in zero globals or singletons used by the plugins, which is an artifact of most plugin systems I’ve seen).
These are my opinions and practices and I know there are plenty good modules which rely on ini/xml/config files. Other than legacy decisions or to be compatible with pre-existing systems, why would someone choose a config file/format over 1) serialized python objects and/or 2) python code?
First: Read/write data, like UI/user preferences. I say, just use python dictionaries, and serialize them however the hell you want (json, yaml, pickle, cPickle, I don’t care). I don’t understand why anyone would build anything more complex (except maybe a wrapper over this functionality), and especially why anyone would bother using a non-serialized format like ini or cfg. Can there be a good reason?
Second: Read-only data, like application configuration (nothing that is edited by your app). This, too, is very commonly in config files with special like xml or ini. Well in this case I say, why bother even with serialized data at all? Just make it code!
This data, by definition, does not have to be statically known and serialized, it is only needed at runtime. So why not make it a python module, have your code import/exec the python file, and access attributes from the python module?
There’s a good deal of data we may only know at runtime, or we cannot statically represent, so we end up making the systems that consume this configuration data much more complex. (For an example, see Sphinx’s config, I think it does it right).
An example of where I think python-as-configuration has huge power is with complex configuration data like for a plugin system. Instead of relying on some static functionality, the configuration can even be callables that return a plugin instance- so now you don’t have to refer to globals inside of your plugins, you can have your own initializer with a much larger signature, and the plugin system can have its well-defined initialization interface.
I’m not sure I did a great job explaining that example- it deserves its own post. We’ve used it extensively for new tools and systems we’ve developed and it seems to work great (and results in zero globals or singletons used by the plugins, which is an artifact of most plugin systems I’ve seen).
These are my opinions and practices and I know there are plenty good modules which rely on ini/xml/config files. Other than legacy decisions or to be compatible with pre-existing systems, why would someone choose a config file/format over 1) serialized python objects and/or 2) python code?
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/301743/viewspace-734199/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【Python】配置檔案配置路徑Python
- python讀配置檔案配置資訊Python
- python 配置檔案如何使用Python
- Python配置檔案使用教程Python
- mac需要裝python嗎MacPython
- python pyyaml操作yaml配置檔案PythonYAML
- python ini 配置檔案處理Python
- 做nagios分散式需要修改的配置檔案iOS分散式
- linux下為什麼每次修改完配置檔案之後都需要重新載入配置檔案Linux
- python怎麼讀取配置檔案Python
- Python之ini配置檔案詳解Python
- python 修改jenkins的配置檔案PythonJenkins
- 學習Python需要培訓嗎?可以自學嗎?Python
- Python讀寫檔案你真的瞭解嗎?Python
- 專案管理需要懂業務嗎?專案管理
- Structs的配置檔案都在一個檔案(struct-config.xml)中嗎?StructXML
- 安裝wordpress後開啟,需要建立一個配置檔案
- 透過python讀取ini配置檔案Python
- Python 中常見的配置檔案寫法Python
- 如何在python中讀取配置檔案Python
- Python_16 配置檔案與封裝Python封裝
- python讀取yaml配置檔案的方法PythonYAML
- NAS需要配置的專案
- Git配置配置檔案Git
- 新手學Python可行嗎?需要什麼基礎嗎?Python
- 香港伺服器需要備案嗎?伺服器
- Python學習需要多久?自學可以嗎?Python
- Python中常見的配置檔案寫法分享!Python
- python config配置檔案的讀寫Python
- jboss中的幾個配置檔案,你搞清楚了嗎?
- 域名為什麼需要備案?購買 GoDaddy域名需要備案嗎?Go
- 8.4.4 配置檔案
- vim配置檔案
- Maven配置檔案Maven
- 配置檔案vimrc
- MySQL配置檔案MySql
- shell配置檔案
- mysql 配置檔案MySql