A
Andre Alexander Bell
Hello,
I'm used to write in Python something like
and then have a dictionary like
and get the formatted output like this:
Occasionally I want to extract the field names from the template string.
I was used to write a class like
class Extractor(object):
def __init__(self):
self.keys = []
def __getitem__(self, key):
self.keys.append(key)
return ''
and use it like this:
['hello']
Now Python has the format method for string formatting with the more
advanced handling. So I could as well write
My question is, if I do have a string template which uses the newer
format string syntax, how do I best extract the field information?
I found the str._formatter_parser() method which I could use like this:
keys = []
for (a, key, c, d) in s._formatter_parser():
if key:
keys.append(key)
Is there a more elegant solution?
What are a, c, d?
Where can I find additional information on this method?
Should one use a method that actually starts with an _?
Couldn't this one change any time soon?
Thanks for any help
Andre
I'm used to write in Python something like
and then have a dictionary like
and get the formatted output like this:
Occasionally I want to extract the field names from the template string.
I was used to write a class like
class Extractor(object):
def __init__(self):
self.keys = []
def __getitem__(self, key):
self.keys.append(key)
return ''
and use it like this:
['hello']
Now Python has the format method for string formatting with the more
advanced handling. So I could as well write
My question is, if I do have a string template which uses the newer
format string syntax, how do I best extract the field information?
I found the str._formatter_parser() method which I could use like this:
keys = []
for (a, key, c, d) in s._formatter_parser():
if key:
keys.append(key)
Is there a more elegant solution?
What are a, c, d?
Where can I find additional information on this method?
Should one use a method that actually starts with an _?
Couldn't this one change any time soon?
Thanks for any help
Andre