Exercises - Configuration#
1) Using different plot styles#
There are many pre-defined matplotlib styles as illustrated at the Python Graph Gallery.
Add a new option
--styletoplotcount.pythat allows the user to pick a style from the list of pre-defined matplotlib styles.
Hint: Use the choices parameter discussed in Section @ref(config-command-line)
to define the valid choices for the new --style option.
Re-generate the plot of the Jane Eyre word count distribution using a bunch of different styles to decide which you like best.
Matplotlib style sheets are designed to be composed together. (See the style sheets tutorial for details.) Use the
nargsparameter to allow the user to pass any number of styles when using the--styleoption.
2) Saving configurations#
Add an option
--saveconfig filenametoplotcounts.pythat writes all of its configuration to a file. Make sure this option saves all of the configuration, including any defaults that the user hasn’t changed.Add a new target
test-saveconfigto theMakefilecreated in Chapter @ref(automate) to test that the new option is working.How would this new
--saveconfigoption make your work more reproducible?
3) Using INI syntax#
If we used Windows INI format instead of YAML
for our plot parameters configuration file
(i.e., plotparams.ini instead of plotparams.yml)
that file would read as follows:
[AXES]
axes.labelsize=x-large
[TICKS]
xtick.labelsize=large
ytick.labelsize=large
The configparser library can be used to read and write INI files.
Install that library by running pip install configparser at the command line.
Using configparser, rewrite the set_plot_params function in plotcounts.py to
handle a configuration file in INI rather than YAML format.
Which file format do you find easier to work with?
What other factors should influence your choice of a configuration file syntax?
Note: the code modified in this exercise is not required for the rest of the book.
4) Configuration consistency#
In order for a data processing pipeline to work correctly, some of the configuration parameters for Program A and Program B must be the same. However, the programs were written by different teams, and each has its own configuration file. What steps could you take to ensure the required consistency?