Skip to content

Instantly share code, notes, and snippets.

@AeroNotix
Created March 26, 2012 12:30
Show Gist options
  • Select an option

  • Save AeroNotix/2204781 to your computer and use it in GitHub Desktop.

Select an option

Save AeroNotix/2204781 to your computer and use it in GitHub Desktop.
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
@AeroNotix

Copy link
Copy Markdown
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment