[Python-ideas] Re: Config file template motivation for PEP 463 or an update to format string spec

2022-04-02 Thread Kale Kundert
Have you considered using a templating library like Jinja2 or Mako?  That's my first thought whenever I want something that's too complicated for `str.format()`.  In addition to handling default values, these libraries would also give you the ability to encode some simple logic in your files, which

[Python-ideas] Re: Config file template motivation for PEP 463 or an update to format string spec

2022-04-01 Thread Eric Fahlgren
My first thought was that this is a job for string.Template, which has always struck me as subclassing-friendly. Took about a half hour of wrangling the regex to get here: from string import Template, _sentinel_dict class TemplateWithDefaults(Template): braceidpattern = r'([_a-z][_a-z0-9]*)

[Python-ideas] Re: Config file template motivation for PEP 463 or an update to format string spec

2022-04-01 Thread Brian McCall
Oh, I didn't even know about `format_map`! Or `__format__` for that matter. This makes everything much easier. For my purposes, I don't even need to use `__format__` since I am not using format strings. In a show of appreciation, here is a URL to a GIF of James Corden bowing: https://media.giphy

[Python-ideas] Re: Config file template motivation for PEP 463 or an update to format string spec

2022-04-01 Thread Chris Angelico
On Fri, 1 Apr 2022 at 23:51, 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