"""AWS Lambda Container Experiment.""" import shlex import subprocess import time import cowsay CHARACTER = 'cow' def handler(event, context): """Entrypoint.""" start = time.time() if CHARACTER not in cowsay.char_names: raise Exception(f'{CHARACTER} not in {cowsay.char_names}') result = fibonacci(event['n']) getattr(cowsay, CHARACTER)(f'{result:,}') command = 'pip list --format=freeze --not-required' pip_result = subprocess.run( shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8' ) runtime = time.time() - start return { 'result': result, 'runtime': runtime, 'packages': pip_result.stdout.split('\n'), 'character': CHARACTER } def fibonacci(n): """Return fibinacci number in sequence.""" if n <= 0: raise Exception(f'{n} <= 0') elif n == 1: return 0 elif n == 2: return 1 else: return fibonacci(n - 1) + fibonacci(n - 2)