Reading a Perl Config File in Python
Most interesting programs need some kind of configuration:
- Content Management Systems similar WordPress blogs, WikiMedia and Joomla need to store the information where the database server is (the hostname) and how to login (username and password)
- Proprietary software might demand to store if the software was registered already (the serial key)
- Scientific software could store the path to BLAS libraries
For very unproblematic tasks you might choose to write these configuration variables directly into the source code. Merely this is a bad idea when you upload the code to GitHub.
I volition explain some alternatives I got to know for Python.
I've created a module for configuration handling: cfg_load
Python Configuration File
The simplest way to write configuration files is to simply write a divide file that contains Python code. Y'all might want to call it something like databaseconfig.py. Then you could add the line *config.py to your .gitignore file to avert uploading information technology accidentally.
A configuration file could await like this:
#!/usr/bin/env python import preprocessing mysql = { "host" : "localhost" , "user" : "root" , "passwd" : "my clandestine password" , "db" : "write-math" , } preprocessing_queue = [ preprocessing . scale_and_center , preprocessing . dot_reduction , preprocessing . connect_lines , ] use_anonymous = True Within the actual code, you can utilise it similar this:
#!/usr/bin/env python import databaseconfig equally cfg connect ( cfg . mysql [ "host" ], cfg . mysql [ "user" ], cfg . mysql [ "password" ]) The way you include the configuration might feel very convenient at a first glance, just imagine what happens when you get more configuration variables. You definitely need to provide an example configuration file. And information technology is hard to resist the temptation to include code within the configuration file.
JSON
JSON is short for JavaScript Object Annotation. It is widespread and thus has skilful support for many programming languages.
The configuration might wait like this:
{ "mysql" :{ "host" : "localhost" , "user" : "root" , "passwd" : "my hugger-mugger password" , "db" : "write-math" }, "other" :{ "preprocessing_queue" :[ "preprocessing.scale_and_center" , "preprocessing.dot_reduction" , "preprocessing.connect_lines" ], "use_anonymous" : true } } You tin can read it like this:
import json with open up ( "config.json" ) as json_data_file : data = json . load ( json_data_file ) print ( information ) which outputs
{ "mysql" : { "db" : "write-math" , "host" : "localhost" , "passwd" : "my secret password" , "user" : "root" , }, "other" : { "preprocessing_queue" : [ "preprocessing.scale_and_center" , "preprocessing.dot_reduction" , "preprocessing.connect_lines" , ], "use_anonymous" : True , }, } Writing JSON files is also piece of cake. Just build up the dictionary and use
import json with open ( "config.json" , "w" ) as outfile : json . dump ( data , outfile ) YAML
YAML is a configuration file format. Wikipedia says:
YAML (rhymes with camel) is a human-readable data serialization format that takes concepts from programming languages such as C, Perl, and Python, and ideas from XML and the data format of electronic mail (RFC 2822). YAML was first proposed past Clark Evans in 2001, who designed information technology together with Ingy döt Net and Oren Ben-Kiki. It is available for several programming languages.
The file itself might look like this:
mysql : host : localhost user : root passwd : my clandestine password db : write-math other : preprocessing_queue : - preprocessing.scale_and_center - preprocessing.dot_reduction - preprocessing.connect_lines use_anonymous : yes You can read information technology like this:
import yaml with open ( "config.yml" , "r" ) as ymlfile : cfg = yaml . load ( ymlfile ) for section in cfg : print ( section ) print ( cfg [ "mysql" ]) print ( cfg [ "other" ]) It outputs:
other mysql { "passwd" : "my secret password" , "host" : "localhost" , "db" : "write-math" , "user" : "root" , } { "preprocessing_queue" : [ "preprocessing.scale_and_center" , "preprocessing.dot_reduction" , "preprocessing.connect_lines" , ], "use_anonymous" : Truthful , } There is a yaml.dump method, so y'all can write the configuration the aforementioned style. Simply build up a dictionary.
YAML is used past the Blender project.
Resources
- Documentation
INI
INI files expect like this:
[mysql] host = localhost user = root passwd = my secret password db = write-math [other] preprocessing_queue = ["preprocessing.scale_and_center", "preprocessing.dot_reduction", "preprocessing.connect_lines"] use_anonymous = yeah ConfigParser
Bones example
The file tin can exist loaded and used like this:
#!/usr/bin/env python import ConfigParser import io # Load the configuration file with open up ( "config.ini" ) as f : sample_config = f . read () config = ConfigParser . RawConfigParser ( allow_no_value = True ) config . readfp ( io . BytesIO ( sample_config )) # List all contents print ( "List all contents" ) for section in config . sections (): impress ( "Section: %south " % department ) for options in config . options ( department ): print ( "x %due south ::: %south ::: %s " % ( options , config . get ( section , options ), str ( type ( options ))) ) # Print some contents print ( " \n Print some contents" ) print ( config . go ( "other" , "use_anonymous" )) # Just get the value print ( config . getboolean ( "other" , "use_anonymous" )) # You know the datatype? which outputs
List all contents Department: mysql x host:::localhost:::<type 'str'> 10 user:::root:::<type 'str'> x passwd:::my underground password:::<type 'str'> ten db:::write-math:::<type 'str'> Department: other x preprocessing_queue:::["preprocessing.scale_and_center", "preprocessing.dot_reduction", "preprocessing.connect_lines"]:::<blazon 'str'> x use_anonymous:::yeah:::<type 'str'> Impress some contents yes True Equally you can see, yous can use a standard information format that is piece of cake to read and write. Methods like getboolean and getint allow you to get the datatype instead of a uncomplicated string.
Writing configuration
import bone configfile_name = "config.ini" # Check if there is already a configurtion file if not bone . path . isfile ( configfile_name ): # Create the configuration file every bit it doesn't exist yet cfgfile = open ( configfile_name , "due west" ) # Add content to the file Config = ConfigParser . ConfigParser () Config . add_section ( "mysql" ) Config . set ( "mysql" , "host" , "localhost" ) Config . set up ( "mysql" , "user" , "root" ) Config . gear up ( "mysql" , "passwd" , "my secret password" ) Config . set ( "mysql" , "db" , "write-math" ) Config . add_section ( "other" ) Config . set ( "other" , "preprocessing_queue" , [ "preprocessing.scale_and_center" , "preprocessing.dot_reduction" , "preprocessing.connect_lines" , ], ) Config . set ( "other" , "use_anonymous" , True ) Config . write ( cfgfile ) cfgfile . shut () results in
[mysql] host = localhost user = root passwd = my secret password db = write-math [other] preprocessing_queue = ['preprocessing.scale_and_center', 'preprocessing.dot_reduction', 'preprocessing.connect_lines'] use_anonymous = True XML
Seems not to exist used at all for configuration files by the Python customs. However, parsing / writing XML is piece of cake and there are plenty of possibilities to do then with Python. 1 is BeautifulSoup:
from BeautifulSoup import BeautifulSoup with open ( "config.xml" ) as f : content = f . read () y = BeautifulSoup ( content ) print ( y . mysql . host . contents [ 0 ]) for tag in y . other . preprocessing_queue : print ( tag ) where the config.xml might look similar this:
<config> <mysql> <host>localhost</host> <user>root</user> <passwd>my clandestine password</passwd> <db>write-math</db> </mysql> <other> <preprocessing_queue> <li>preprocessing.scale_and_center</li> <li>preprocessing.dot_reduction</li> <li>preprocessing.connect_lines</li> </preprocessing_queue> <use_anonymous value= "true" /> </other> </config> File Endings
File Endings requite the user and the system an indicator about the content of a file. Reasonable file endings for configuration files are
-
*config.pyfor Python files -
*.yamlor*.ymlif the configuration is done in YAML format -
*.jsonfor configuration files written in JSON format -
*.cfgor*.confto point that information technology is a configuration file -
*.inifor "initialization" are quite widespread (encounter Wiki) -
~/.[my_app_name]rcis a VERY mutual naming scheme for configuration files on Linux systems. RC is a reference to an one-time computer system and means "run common".
That said, I think I prefer *.conf. I think it is a choice that users understand.
Merely you might likewise consider that *.ini might get opened past standard in a text editor. For the other options, users might get asked which program they want to use.
Resources
- JSON Online Parser
- What is the deviation between YAML and JSON? When to prefer 1 over the other?
- Why practise and then many projects use XML for configuration files?
- YAML Lint
- TOML: Very similar to INI files.
Source: https://martin-thoma.com/configuration-files-in-python/
0 Response to "Reading a Perl Config File in Python"
Post a Comment