Created
March 26, 2012 12:30
-
-
Save AeroNotix/2204781 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def set_locale(locale_string): | |
| def __curr_conv_from_gb(string): | |
| major_num = ''.join(string.split(',')) | |
| return major_num | |
| __gb_delim = ',' | |
| def __curr_conv_from_de(string): | |
| major_num = string.replace('.', '').replace(',', '.') | |
| return major_num | |
| __de_delim = ';' | |
| def __curr_conv_default(string): | |
| major_num = ''.join(string.split(',')) | |
| return major_num | |
| __delim_default = ',' | |
| locale_data = { | |
| 'en_GB': { | |
| 'currency_conversion': __curr_conv_from_gb, | |
| 'delimiter' : __gb_delim | |
| }, | |
| 'de_DE': { | |
| 'currency_conversion': __curr_conv_from_de, | |
| 'delimiter' : __de_delim | |
| }, | |
| 'default': { | |
| 'currency_conversion': __curr_conv_default, | |
| 'delimiter' : __delim_default | |
| } | |
| } | |
| def wrapper(func): | |
| def inner(*args, **kwargs): | |
| inner_locale = locale_data.get(locale_string, locale_data['default']) | |
| return func(*args, locale=inner_locale) | |
| return inner | |
| return wrapper | |
| @set_locale('en_GB') | |
| def something(currency, locale=None): | |
| print locale['currency_conversion'](currency) | |
| @set_locale('de_DE') | |
| def something_else(currency, locale=None): | |
| print locale['currency_conversion'](currency) | |
| something("1,000,000.00") | |
| something_else("1.000.000,00") | |
| >>>1000000.00 | |
| >>>1000000.00 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This decorator came about because of the myriad of settings used on computers at work, various scripts would fail when parsing CSV files created on a different system. This decorator changes this by being able to set what these values are for the scope of the decorated function.
TODO: Remove looking the functions up using dictionary syntax.