Re: Need help with custom string formatter

2022-11-05 Thread MRAB
On 2022-11-05 11:07, Stefan Ram wrote: Robert Latest writes: result += ' ' *( length - len( result )) Nice, I didn't know that one could multiply strings by negative numbers without error. Thanks, but today I thought that maybe there might be a solution for getting a field of a fixed

Re: Need help with custom string formatter

2022-10-22 Thread Robert Latest via Python-list
Cameron Simpson wrote: > Stefan's code implements it's own format_field and falls back to the > original format_field(). That's standard subclassing practice, and worth > doing reflexively more of the time - it avoids _knowing_ that > format_field() just calls format(). > > So I'd take Stefan's

Re: Need help with custom string formatter

2022-10-21 Thread Cameron Simpson
On 21Oct2022 16:55, Stefan Ram wrote: I was not aware of "isdigit". There's also "isdecimal" and "isnumeric". They all have subtly different meanings :-) Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list

Re: Need help with custom string formatter

2022-10-21 Thread Cameron Simpson
On 21Oct2022 16:55, Stefan Ram wrote: Robert Latest writes: return super().format_field( value, format_string ) Why do you prefer super().format_field() over plain format()? The doc says: "format_field() simply calls format()." So I figured I might do the same. I am not aware of any

Re: Need help with custom string formatter

2022-10-21 Thread Robert Latest via Python-list
Stefan Ram wrote: [the solution] thanks, right on the spot. I had already figured out that format_field() is the one method I need, and thanks for the str.translate method. I knew that raking seven RE's across the same string HAD to be stupid. Have a nice weekend! --

Need help with custom string formatter

2022-10-21 Thread Robert Latest via Python-list
Hi all, I would like to modify the standard str.format() in a way that when the input field is of type str, there is some character replacement, and the string gets padded or truncated to the given field width. Basically like this: fmt = MagicString('<{s:6}>') print(fmt.format(s='Äußerst'))

Re: Need help with custom string formatter

2022-10-21 Thread Robert Latest via Python-list
Hi Stefan, I have now implemented a version of this, works nicely. I have a few minor questions / remarks: > result += ' ' *( length - len( result )) Nice, I didn't know that one could multiply strings by negative numbers without error. > def __init__( self ): >