[Python-3000] String formating operations in python 3k
Crutcher Dunnavant
crutcher at gmail.com
Mon Apr 3 19:44:38 CEST 2006
On 4/3/06, Barry Warsaw <barry at python.org> wrote
> On Mon, 2006-04-03 at 11:13 -0500, Ian Bicking wrote:
>
> > assert path.startswith(prefix), (
> > "%r should start with %r" % (path, prefix))
> > assert path.startswith(prefix), (
> > $"${repr(path)} should start with ${repr(prefix)}")
> > assert path.startswith(prefix), (
> > "$path should start with $prefix".substitute(
> > path=repr(path), prefix=repr(prefix))
>
> The second example is why I added sys._getframe(). In my i18n code, I
> wanted to do things like this (rewritten for $-string style):
>
> print _("$user is not a member of the $listname mailing list")
>
> and it would Just Work. Now, the trick is that in the same function,
> just before the print line, you'd have code like this (pseudocoded):
>
> user = utils.websafe(form.get('user'))
> listname = mlist.get_name()
>
> So _() does a sys._getframe() to dig the locals out of the calling
> function, and does the substitutions from there. So, if your
> applications isn't i18n, I could see the use for a format() global, but
> while I'd probably want the behavior I just described, you might not, so
> I'm not sure we could write a format() builtin that would be useful to
> us both.
>
Well, what if we added '%{expression}s' as a formating type?
This would make your example:
print _("%{user}s is not a member of the %{listname}s mailing list")
or even:
print _("%{utils.websafe(form.get('user'))}s is not a member of the
%{mlist.get_name()}s mailing list")
> -Barry
>
>
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.2.2 (GNU/Linux)
>
> iQCVAwUARDFUjHEjvBPtnXfVAQJsWQP/Rk7WNMv3aduLhjVbRApgcpjv78Cv0gXG
> M1096pL9J2jvj29D/pb9SeHTlGG/cZTNoRNsFHUYJ8yMHT57VUiPcuz7VIWCeqPP
> 4RaMP9AvdGufFULfGmT13mKTSZ0EG33f8aq74lf4GEUnmyWa/p2goUoe/lBxKwSP
> u9fjNwBxRrg=
> =mUlb
> -----END PGP SIGNATURE-----
>
>
> _______________________________________________
> Python-3000 mailing list
> Python-3000 at python.org
> http://mail.python.org/mailman/listinfo/python-3000
> Unsubscribe: http://mail.python.org/mailman/options/python-3000/crutcher%40gmail.com
>
>
>
--
Crutcher Dunnavant <crutcher at gmail.com>
littlelanguages.com
monket.samedi-studios.com
More information about the Python-3000
mailing list