On Fri, 1 Apr 2022 at 23:51, <brian.patrick.mcc...@gmail.com> wrote: > > One way that I like to create templates for config files is to write the > config file in a way that can be used with `str.format` if I read in the > entire config file as a string before re-saving it or sending it somewhere.* > The config file contains a half dozen or dozen variables. Another several > dozen variables are pretty much permanent. This approach works for me in the > sense that batch submits over several combinations of variables is just a few > lines of code, and when I revisit a job after quite some time, the curly > braces in the config file template remind me what settings (might) require my > attention. The problem lies in what to do about defaults. Including a > formatted variable in the template file requires that the variable be > included in my call to `format` otherwise I'll get a `KeyError`. For my part, > I can work around this by including a companion '.py' file that includes a > `dict` of default values. What I pine for is the ability to discard this > extra '.py' file, and its W YS > INWYG heresy by enabling a way to enable defaults in the processing of > formatted strings. One such way would be to modify the format string spec. I > am not sure how this might be done, so I cannot make a smart proposal on that > one. Another way, although it might not be the most popular, is to allow for > inline exception handling of the sort proposed in PEP 463. > > To me, the following does not look so bad: > Config file: > ``` > ... > BAUD: {baud except 9600} > ... > ``` > > Template updating code snippets: > ``` > with open('config.yaml', 'r') as f: > txt = f.read() > options = {'baud': 19200} > new_txt1 = txt.format(options) # BAUD: 19200 is now in new_txt1 > options = {} > new_txt2 = txt.format(options) # BAUD: 9600 is now in new_txt2 > ``` > > > Curious to know anyone's thoughts on the subject, particularly anyone else > who is in the habit of hastily writing config files. Apologies if bringing up > a rejected PEP on my first thread causes any irritation, but I swear I would > not have done it if string formatting weren't so useful. I thank you for my > cookie and now I would like some milk. >
Nothing wrong with bringing up rejected PEPs, either to reference when discussing another feature (like this), or to specifically revisit the original proposal (although in that case you'd have to respond to the reasons for rejection). But in this case, I think exception handling is kinda overkill for defaults. What you really want is a way to say "put this thing, and use this default". It might be easier to abuse the format string for this. I'll leave you to polish this (for starters, the options should really be contained inside Options, not referenced as a global), but here's a bit of an idea: >>> class Defaultable(str): ... def __format__(self, fmt): ... return self ... >>> class Default: ... def __format__(self, fmt): ... return fmt ... >>> class Options: ... def __getitem__(self, key): ... if key in options: ... return Defaultable(options[key]) ... return Default() ... >>> options = {'baud': 19200} >>> "BAUD: {baud:9600}".format_map(Options()) 'BAUD: 19200' >>> options = {} >>> "BAUD: {baud:9600}".format_map(Options()) 'BAUD: 9600' >>> The basic idea is: Regardless of the "format string", return the string unchanged, but if we don't have a string to return, use the format string instead. If you need format strings as well, the easiest way would probably be to do something like "{baud:9600:10s}" and handle the subdivision in your own code. This is all a bit of an abuse of notation, though, so do whatever makes the most sense for you. ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/QIRGD6K7AZBNBDMWIPDRFBON2IGLQBAN/ Code of Conduct: http://python.org/psf/codeofconduct/