8

I have a small script in python, that make use of locale to format a number from 1.000,00 to 1,000.00

import re, locale
locale.setlocale(locale.LC_ALL, 'es_PE.UTF-8')

locale.atof(number)

Then when I run this in Lambda, I get this error message:

unsupported locale setting

I know how to install the dependencies in my PC by executing this commands in the terminal:

export LC_ALL="en_US.UTF-8"
export LC_CTYPE="en_US.UTF-8"
sudo dpkg-reconfigure locales

source: https://stackoverflow.com/a/36257050/2513972

Romel Gomez
  • 325
  • 2
  • 19
  • How are AMIs, which are an EC2 feature, related to AWS Lambda here? – Mark B Feb 16 '22 at 18:02
  • Lambdas do have an AMI https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html But you are correct in pointing out that Lambda is more of a managed service and not really setup for you to be customing or installing things on. It does allow for limited customization though. – Matt Urtnowski Feb 16 '22 at 19:11

1 Answers1

0

For detereming the list of available locales see https://phrase.com/blog/posts/beginners-guide-to-locale-in-python/

for lang in locale.locale_alias.values():
    print(lang)

You won't be able to "install" anything in the default AWS Lambda Python Runtime Environment and setting up a custom runtime is probably more trouble than it's worth. You can see if there is something else you can use.

If you really are just converting 1.000,00 to 1,000.00 I recommend instead just running your own character replacement. A simple character check loop wouldn't be any slower than atof and would have less side effects.

If your curious about how atof works you can follow it's source code trace in https://github.com/python/cpython/blob/3.10/Lib/locale.py#L336

If you don't want to use your own character replace you can use format with a custom format_spec.

>>> '{:,.2f}'.format(10000)
'10,000.00'

This next line pretty much follows the same steps at atof. Delocalize with replacements then float.

>>> '{:,.2f}'.format(float('10.000,00'.replace('.','').replace(',','.')))
'10,000.00'

https://docs.python.org/3/library/functions.html#format https://docs.python.org/3/library/string.html#formatspec

If you you really want to customize your lambda runtime see the following:

https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html https://docs.aws.amazon.com/lambda/latest/dg/runtimes-custom.html https://docs.aws.amazon.com/lambda/latest/dg/configuration-layers.html https://docs.aws.amazon.com/lambda/latest/dg/runtimes-context.html

Matt Urtnowski
  • 2,556
  • 1
  • 18
  • 36