[Python-checkins] r46218 - sandbox/trunk/stringbench/stringbench.py

richard.tew python-checkins at python.org
Thu May 25 17:57:04 CEST 2006


Author: richard.tew
Date: Thu May 25 17:56:59 2006
New Revision: 46218

Modified:
   sandbox/trunk/stringbench/stringbench.py
Log:
Add a string formatting test.

Modified: sandbox/trunk/stringbench/stringbench.py
==============================================================================
--- sandbox/trunk/stringbench/stringbench.py	(original)
+++ sandbox/trunk/stringbench/stringbench.py	Thu May 25 17:56:59 2006
@@ -738,6 +738,34 @@
         s_replace(from_str, to_str)
 
 
+# CCP does a lot of this, for internationalisation of ingame messages.
+_format = "The %(thing)s is %(place)s the %(location)s."
+_format_dict = { "thing":"THING", "place":"PLACE", "location":"LOCATION", }
+_format_unicode = unicode(_format)
+_format_dict_unicode = dict([ (unicode(k), unicode(v)) for (k,v) in _format_dict.iteritems() ])
+
+def _get_format(STR):
+    if STR is unicode:
+        return _format_unicode
+    if STR is str:
+        return _format
+    raise AssertionError
+
+def _get_format_dict(STR):
+    if STR is unicode:
+        return _format_dict_unicode
+    if STR is str:
+        return _format_dict
+    raise AssertionError
+
+# Formatting.
+ at bench('"The %(k1)s is %(k2)s the %(k3)s."%{"k1":"x","k2":"y","k3":"z",}',
+       'formatting a string type with a dict', 1000)
+def format_with_dict(STR):
+    s = _get_format(STR)
+    d = _get_format_dict(STR)
+    for x in _RANGE_1000:
+        s % d
 
 # end of benchmarks
 


More information about the Python-checkins mailing list