[Python-ideas] Re: Support more conversions in format string

2021-05-05 Thread Cameron Simpson
On 06May2021 03:43, Dennis Sweeney wrote: >Maybe I'm missing something, but why do you need the SimpleNamespace at all? >Why not make your own mapping as in > >class StringMapper: >... >def __getitem__(self, s): ># Whatever arbitrary behavior you want >

[Python-ideas] Re: Support more conversions in format string

2021-05-05 Thread Dennis Sweeney
Maybe I'm missing something, but why do you need the SimpleNamespace at all? Why not make your own mapping as in class StringMapper: ... def __getitem__(self, s): # Whatever arbitrary behavior you want # Process suffixes, etc here, for example:

[Python-ideas] Re: Support more conversions in format string

2021-05-05 Thread Cameron Simpson
On 25Apr2021 10:54, Cameron Simpson wrote: >On 24Apr2021 22:35, Stephen J. Turnbull >wrote: >[...] >> > My use case is presupplied strings, eg a command line supplied >> > format string. >> >>In that case the format string is user input, and x is a variable in >>the program that the user can hav

[Python-ideas] Re: Support more conversions in format string

2021-04-27 Thread Stephen J. Turnbull
Cameron Simpson writes: > First up, I have somehow missed this (":format_name") in the > semirecursive mess which is the python format-and-friends descriptions. > (object.__format__? str.format? str.formap_map? f''? the format > mini-language? all in separate places, for reasonable rea

[Python-ideas] Re: Support more conversions in format string

2021-04-27 Thread Stephen J. Turnbull
Serhiy Storchaka writes: > Because it converts value to string, and string formatting does not > support "g". Converters !s, !r and !a are separated from format > specifier, and it is old and widely used feature. And poorly documented, IMO. I'll see if I can do better. > but if we are going

[Python-ideas] Re: Support more conversions in format string

2021-04-24 Thread Cameron Simpson
On 25Apr2021 01:01, Chris Angelico wrote: >On Sat, Apr 24, 2021 at 11:36 PM Stephen J. Turnbull > wrote: >> Assuming that *exact* use case, wouldn't >> >> >>> class LowerableStr(str): >> ... def __format__(self, fmt): >> ... if fmt == 'lc': >> ...return self.lower() >> .

[Python-ideas] Re: Support more conversions in format string

2021-04-24 Thread Cameron Simpson
On 24Apr2021 22:35, Stephen J. Turnbull wrote: >Cameron Simpson writes: > > On 23Apr2021 18:25, Stephen J. Turnbull > > wrote: > > >I don't understand how this is supposed to work. It looks to me > > >like !code is a preprocessor: [...] > > >If so, > > > > > >'{x} is {x!lc:foo} in lowerca

[Python-ideas] Re: Support more conversions in format string

2021-04-24 Thread Chris Angelico
On Sat, Apr 24, 2021 at 11:36 PM Stephen J. Turnbull wrote: > Assuming that *exact* use case, wouldn't > > >>> class LowerableStr(str): > ... def __format__(self, fmt): > ... if fmt == 'lc': > ...return self.lower() > ... else: > ...return str.__format__(self,

[Python-ideas] Re: Support more conversions in format string

2021-04-24 Thread Stephen J. Turnbull
Cameron Simpson writes: > On 23Apr2021 18:25, Stephen J. Turnbull > wrote: > >I don't understand how this is supposed to work. It looks to me like > >!code is a preprocessor: [...] > >If so, > > > >'{x} is {x!lc:foo} in lowercase' > > > >will fail because str doesn't implement the

[Python-ideas] Re: Support more conversions in format string

2021-04-24 Thread Serhiy Storchaka
21.04.21 12:14, Paul Moore пише: > I don't have a particularly strong opinion here, other than to say I'm > not sure I like the upper case "I". It looks far too much like a lower > case "L" in the font I'm using here, which makes me think of C's > "long", so it's easy to confuse. So of the two opti

[Python-ideas] Re: Support more conversions in format string

2021-04-24 Thread Serhiy Storchaka
23.04.21 12:22, Stephen J. Turnbull пише: > Serhiy Storchaka writes: > > > Currently format strings (and f-string expressions) support three > > conversions: !s -- str, !r -- repr and !a for ascii. > > It's not clear to me what these are good for, to be honest. Why not > just have s, r, and a

[Python-ideas] Re: Support more conversions in format string

2021-04-23 Thread Eric V. Smith
On 4/23/2021 5:22 AM, Stephen J. Turnbull wrote: But we could change int.__format__ to allow 's' as a format code[1], automagically calling str(), just as 'efg' are allowed and automagically call float(). ... [1] I recognize this may imply many changes, since some (all?) of the builtin types s

[Python-ideas] Re: Support more conversions in format string

2021-04-23 Thread Cameron Simpson
On 23Apr2021 18:25, Stephen J. Turnbull wrote: >Cameron Simpson writes: > > I would _frequently_ like to be able to provide custom > > conversions. At present I'm using elaborate hacks based on > > __getattr__ etc to recognise things like this: > > > > '{x} is {x_lc} in lowercase' > > > > whe

[Python-ideas] Re: Support more conversions in format string

2021-04-23 Thread Chris Angelico
On Fri, Apr 23, 2021 at 7:26 PM Stephen J. Turnbull wrote: > > Cameron Simpson writes: > > > I would _frequently_ like to be able to provide custom > > conversions. At present I'm using elaborate hacks based on > > __getattr__ etc to recognise things like this: > > > > '{x} is {x_lc} in l

[Python-ideas] Re: Support more conversions in format string

2021-04-23 Thread Chris Angelico
On Fri, Apr 23, 2021 at 7:24 PM Stephen J. Turnbull wrote: > > Serhiy Storchaka writes: > > > Currently format strings (and f-string expressions) support three > > conversions: !s -- str, !r -- repr and !a for ascii. > > It's not clear to me what these are good for, to be honest. Why not > just

[Python-ideas] Re: Support more conversions in format string

2021-04-23 Thread Stephen J. Turnbull
Cameron Simpson writes: > I would _frequently_ like to be able to provide custom > conversions. At present I'm using elaborate hacks based on > __getattr__ etc to recognise things like this: > > '{x} is {x_lc} in lowercase' > > where the _lc suffix is caught and a value computed from

[Python-ideas] Re: Support more conversions in format string

2021-04-21 Thread Rob Cliffe via Python-ideas
But IIUC once a conversion was added, it could not be removed without breaking backward compatibility.  So I think we should be cautious about building what is in effect a Domain Specific Language and take care to get it (as) right (as possible) first time. Rob Cliffe On 21/04/2021 22:44, Cam

[Python-ideas] Re: Support more conversions in format string

2021-04-21 Thread Cameron Simpson
On 21Apr2021 10:14, Paul Moore wrote: >On Wed, 21 Apr 2021 at 09:01, Serhiy Storchaka wrote: >> Do we need support of more standard conversions? Do we want to >> support >> custom conversions (registered globally as encodings and error >> handlers). re.escape, html.escape and shlex.quote could b

[Python-ideas] Re: Support more conversions in format string

2021-04-21 Thread Paul Moore
On Wed, 21 Apr 2021 at 09:01, Serhiy Storchaka wrote: > > Currently format strings (and f-string expressions) support three > conversions: !s -- str, !r -- repr and !a for ascii. > > I propose to add support of additional conversions: for int, float and > operator.index. It will help to convert au