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
- python 修改jenkins的配置檔案PythonJenkins
- python ini 配置檔案處理Python
- python pyyaml操作yaml配置檔案PythonYAML
- python config配置檔案的讀寫Python
- Python之ini配置檔案詳解Python
- python怎麼讀取配置檔案Python
- linux下為什麼每次修改完配置檔案之後都需要重新載入配置檔案Linux
- mac需要裝python嗎MacPython
- Python 使用ConfigParser操作ini配置檔案教程。Python
- python讀取yaml配置檔案的方法PythonYAML
- Python_16 配置檔案與封裝Python封裝
- 透過python讀取ini配置檔案Python
- Python 中常見的配置檔案寫法Python
- 如何在python中讀取配置檔案Python
- Python讀寫檔案你真的瞭解嗎?Python
- 安裝wordpress後開啟,需要建立一個配置檔案
- Git配置配置檔案Git
- 用python生成oracle goldengate複製配置檔案PythonOracleGo
- Python中常見的配置檔案寫法分享!Python
- NAS需要配置的專案
- 【python介面自動化】- ConfigParser配置檔案的使用Python
- 使用配置檔案方式記錄Python程式日誌Python
- vim配置檔案
- 8.4.4 配置檔案
- nginx配置檔案Nginx
- MySQL配置檔案MySql
- 【SpringBoot】配置檔案Spring Boot
- 配置檔案vimrc
- Nginx 配置檔案Nginx
- gitignore 檔案配置Git
- Maven 配置檔案Maven
- Springboot配置檔案Spring Boot
- mysql 配置檔案MySql
- docker 配置檔案Docker
- Maven配置檔案Maven