Reverse string-formatting (maybe?)
Dustan
DustanGroups at gmail.com
Sun Oct 15 15:17:58 EDT 2006
> Only you know what anomalies will be found in your data-sets. If
> you know/assert that
>
> -the only stuff in the formatting string is one set of characters
>
> -that stuff in the replacement-values can never include any of
> your format-string characters
>
> -that you're not using funky characters/formatting in your format
> string (such as "%%" possibly followed by an "s" to get the
> resulting text of "%s" after formatting, or trying to use other
> formatters such as the aforementioned "%f" or possibly "%i")
>
> then you should be safe. It could also be possible (with my
> original replacement of "(.*)") if your values will never include
> any substring of your format string. If you can't guarantee
> these conditions, you're trying to make a cow out of hamburger.
> Or a pig out of sausage. Or a whatever out of a hotdog. :)
>
> Conventional wisdom would tell you to create a test-suite of
> format-strings and sample values (preferably worst-case funkiness
> in your expected format-strings/values), and then have a test
> function that will assert that the unformatting of every
> formatted string in the set returns the same set of values that
> went in. Something like
>
> tests = {
> 'I was %s but now I am %s' : [
> ('hot', 'cold'),
> ('young', 'old'),
> ],
> 'He has 3 %s and 2 %s' : [
> ('brothers', 'sisters'),
> ('cats', 'dogs')
> ]
> }
>
> for format_string, values in tests:
> unformatter = format.replace('%s', '(.*)')
> for value_tuple in values:
> formatted = format_string % value_tuple
> unformatted = unformatter.search(formatted).groups()
> if unformatted <> value_tuple:
> print "%s doesn't match %s when unformatting %s" % (
> unformatted,
> value_tuple
> format_string)
>
> -tkc
Thanks for all your help. I've gotten the idea.
More information about the Python-list
mailing list