[Pykafe-commits] SF.net SVN: pykafe: [81] trunk/pykafe/server/currencyformat.py
Status: Pre-Alpha
Brought to you by:
jnmbk
|
From: <jn...@us...> - 2007-06-03 10:07:26
|
Revision: 81
http://pykafe.svn.sourceforge.net/pykafe/?rev=81&view=rev
Author: jnmbk
Date: 2007-06-03 03:07:28 -0700 (Sun, 03 Jun 2007)
Log Message:
-----------
currency thing from python 2.5
Added Paths:
-----------
trunk/pykafe/server/currencyformat.py
Added: trunk/pykafe/server/currencyformat.py
===================================================================
--- trunk/pykafe/server/currencyformat.py (rev 0)
+++ trunk/pykafe/server/currencyformat.py 2007-06-03 10:07:28 UTC (rev 81)
@@ -0,0 +1,93 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2007, pyKafe Development Team
+#
+# This program is free software; you can redistribute it and/or modify it under
+# the terms of the GNU General Public License as published by the Free
+# Software Foundation; either version 2 of the License, or (at your option)
+# any later version.
+#
+# Please read the COPYING file.
+#
+
+import locale
+
+#taken from python 2.5 sources
+#TODO: Remove these when Python 2.5 is widely available
+def format(percent, value, grouping=False, monetary=False, *additional):
+ """Returns the locale-aware substitution of a %? specifier
+ (percent).
+
+ additional is for format strings which contain one or more
+ '*' modifiers."""
+ # this is only for one-percent-specifier strings and this should be checked
+ if percent[0] != '%':
+ raise ValueError("format() must be given exactly one %char "
+ "format specifier")
+ if additional:
+ formatted = percent % ((value,) + additional)
+ else:
+ formatted = percent % value
+ # floats and decimal ints need special action!
+ if percent[-1] in 'eEfFgG':
+ seps = 0
+ parts = formatted.split('.')
+ if grouping:
+ parts[0], seps = locale._group(parts[0], monetary=monetary)
+ decimal_point = locale.localeconv()[monetary and 'mon_decimal_point'
+ or 'decimal_point']
+ formatted = decimal_point.join(parts)
+ while seps:
+ sp = formatted.find(' ')
+ if sp == -1: break
+ formatted = formatted[:sp] + formatted[sp+1:]
+ seps -= 1
+ elif percent[-1] in 'diu':
+ if grouping:
+ formatted = locale._group(formatted, monetary=monetary)[0]
+ return formatted
+
+def currency(val, symbol=True, grouping=False, international=False):
+ """Formats val according to the currency settings
+ in the current locale."""
+ conv = locale.localeconv()
+
+ # check for illegal values
+ digits = conv[international and 'int_frac_digits' or 'frac_digits']
+ if digits == 127:
+ raise ValueError("Currency formatting is not possible using "
+ "the 'C' locale.")
+
+ s = format('%%.%if' % digits, abs(val), grouping, monetary=True)
+ # '<' and '>' are markers if the sign must be inserted between symbol and value
+ s = '<' + s + '>'
+
+ if symbol:
+ smb = conv[international and 'int_curr_symbol' or 'currency_symbol']
+ precedes = conv[val<0 and 'n_cs_precedes' or 'p_cs_precedes']
+ separated = conv[val<0 and 'n_sep_by_space' or 'p_sep_by_space']
+
+ if precedes:
+ s = smb + (separated and ' ' or '') + s
+ else:
+ s = s + (separated and ' ' or '') + smb
+
+ sign_pos = conv[val<0 and 'n_sign_posn' or 'p_sign_posn']
+ sign = conv[val<0 and 'negative_sign' or 'positive_sign']
+
+ if sign_pos == 0:
+ s = '(' + s + ')'
+ elif sign_pos == 1:
+ s = sign + s
+ elif sign_pos == 2:
+ s = s + sign
+ elif sign_pos == 3:
+ s = s.replace('<', sign)
+ elif sign_pos == 4:
+ s = s.replace('>', sign)
+ else:
+ # the default if nothing specified;
+ # this should be the most fitting sign position
+ s = sign + s
+
+ return s.replace('<', '').replace('>', '')
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|