summaryrefslogtreecommitdiffstats
path: root/Lib/locale.py
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-11-19 19:01:43 (GMT)
committerGuido van Rossum <guido@python.org>1997-11-19 19:01:43 (GMT)
commiteef1d4e8b16c7352cc748f19e0481275d4f78291 (patch)
tree6657239b556937c1627a08009118e6137f01a5f6 /Lib/locale.py
parent3df69bca0ac00aa413ab94d29624cd3d0a244448 (diff)
downloadcpython-eef1d4e8b16c7352cc748f19e0481275d4f78291.zip
cpython-eef1d4e8b16c7352cc748f19e0481275d4f78291.tar.gz
cpython-eef1d4e8b16c7352cc748f19e0481275d4f78291.tar.bz2
User-level locale module. A wrapper around _locale which adds
format(), str(), atof(), and atoi(). The last three are locale sensitive versions of the corresponding standard functions (only for numbers though); format() does general %[efg] formatting taking the locale into account, optionally with thousands grouping.
Diffstat (limited to 'Lib/locale.py')
-rw-r--r--Lib/locale.py75
1 files changed, 75 insertions, 0 deletions
diff --git a/Lib/locale.py b/Lib/locale.py
new file mode 100644
index 0000000..9935478
--- /dev/null
+++ b/Lib/locale.py
@@ -0,0 +1,75 @@
+"Support for number formatting using the current locale settings"
+
+from _locale import *
+import string
+
+#perform the grouping from right to left
+def _group(s):
+ conv=localeconv()
+ grouping=conv['grouping']
+ if not grouping:return s
+ result=""
+ while s and grouping:
+ # if grouping is -1, we are done
+ if grouping[0]==CHAR_MAX:
+ break
+ # 0: re-use last group ad infinitum
+ elif grouping[0]!=0:
+ #process last group
+ group=grouping[0]
+ grouping=grouping[1:]
+ if result:
+ result=s[-group:]+conv['thousands_sep']+result
+ else:
+ result=s[-group:]
+ s=s[:-group]
+ if s and result:
+ result=s+conv['thousands_sep']+result
+ return result
+
+def format(f,val,grouping=0):
+ """Formats a value in the same way that the % formatting would use,
+ but takes the current locale into account.
+ Grouping is applied if the third parameter is true."""
+ result = f % val
+ fields = string.splitfields(result,".")
+ if grouping:
+ fields[0]=_group(fields[0])
+ if len(fields)==2:
+ return fields[0]+localeconv()['decimal_point']+fields[1]
+ elif len(fields)==1:
+ return fields[0]
+ else:
+ raise Error,"Too many decimal points in result string"
+
+def str(val):
+ """Convert float to integer, taking the locale into account."""
+ return format("%.12g",val)
+
+def atof(str,func=string.atof):
+ "Parses a string as a float according to the locale settings."
+ #First, get rid of the grouping
+ s=string.splitfields(str,localeconv()['thousands_sep'])
+ str=string.join(s,"")
+ #next, replace the decimal point with a dot
+ s=string.splitfields(str,localeconv()['decimal_point'])
+ str=string.join(s,'.')
+ #finally, parse the string
+ return func(str)
+
+def atoi(str):
+ "Converts a string to an integer according to the locale settings."
+ return atof(str,string.atoi)
+
+def test():
+ setlocale(LC_ALL,"")
+ #do grouping
+ s1=format("%d",123456789,1)
+ print s1,"is",atoi(s1)
+ #standard formatting
+ s1=str(3.14)
+ print s1,"is",atof(s1)
+
+
+if __name__=='__main__':
+ test()