Python-izmなる勉強になるサイトを見つけたため、AWS Lambdaで試してみた。
基礎編
def lambda_handler(event, context): testnum = "123" testnum = testnum + "456" testnum = testnum + "789" testnum += "0" print(testnum) print(testnum.replace('1234567890','0123456789')) print(testnum.rjust(20,'0')) print(testnum.zfill(20)) teststr = 'this-message' print(teststr) print(teststr.startswith('this')) print(teststr.split('-')) ========================================================================== Response: null Request ID: "ebe00f2f-3fda-11e8-b0be-112860d9c610" Function Logs: START RequestId: ebe00f2f-3fda-11e8-b0be-112860d9c610 Version: $LATEST 1234567890 0123456789 00000000001234567890 00000000001234567890 this-message True ['this', 'message'] END RequestId: ebe00f2f-3fda-11e8-b0be-112860d9c610 REPORT RequestId: ebe00f2f-3fda-11e8-b0be-112860d9c610 Duration: 0.83 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
def lambda_handler(event, context): testint = 100.5 print(float(testint) + 100) testcomp = 100 + 5j print(testcomp.real) print(testcomp.imag) ========================================================================== Response: null Request ID: "98ecdea0-3fde-11e8-84a6-6331306e40e3" Function Logs: START RequestId: 98ecdea0-3fde-11e8-84a6-6331306e40e3 Version: $LATEST 200.5 100.0 5.0 END RequestId: 98ecdea0-3fde-11e8-84a6-6331306e40e3 REPORT RequestId: 98ecdea0-3fde-11e8-84a6-6331306e40e3 Duration: 0.42 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
import datetime import calendar def lambda_handler(event, context): today = datetime.date.today() todaydetail = datetime.datetime.today() print(today) print(todaydetail) print(todaydetail + datetime.timedelta(hours=9)) print(todaydetail.year) print(todaydetail.strftime("%Y/%m/%d %H:%M:%S")) print(calendar.isleap(2018)) ========================================================================== Response: null Request ID: "cb3365b8-3fdf-11e8-9ef3-f786a1acc5e8" Function Logs: START RequestId: cb3365b8-3fdf-11e8-9ef3-f786a1acc5e8 Version: $LATEST 2018-04-14 2018-04-14 12:31:45.698769 2018-04-14 21:31:45.698769 2018 2018/04/14 12:31:45 False END RequestId: cb3365b8-3fdf-11e8-9ef3-f786a1acc5e8 REPORT RequestId: cb3365b8-3fdf-11e8-9ef3-f786a1acc5e8 Duration: 9.95 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
import datetime def lambda_handler(event, context): test_tuple = get_today() print(test_tuple) print(test_tuple[0]) def get_today(): today = datetime.datetime.today() value = (today.year, today.month, today.day) return value ========================================================================== Response: null Request ID: "2e249bf9-3fe2-11e8-ac44-514731084dd0" Function Logs: START RequestId: 2e249bf9-3fe2-11e8-ac44-514731084dd0 Version: $LATEST (2018, 4, 14) 2018 END RequestId: 2e249bf9-3fe2-11e8-ac44-514731084dd0 REPORT RequestId: 2e249bf9-3fe2-11e8-ac44-514731084dd0 Duration: 28.05 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
def lambda_handler(event, context): test_list = ['this','-','is','-','test'] print(test_list) for i in test_list: print(i) test_list.append('.') print(test_list) print(test_list.index('test')) ========================================================================== Response: null Request ID: "126169e5-3fe3-11e8-8df7-4d4c2b06e81b" Function Logs: START RequestId: 126169e5-3fe3-11e8-8df7-4d4c2b06e81b Version: $LATEST ['this', '-', 'is', '-', 'test'] this - is - test ['this', '-', 'is', '-', 'test', '.'] 4 END RequestId: 126169e5-3fe3-11e8-8df7-4d4c2b06e81b REPORT RequestId: 126169e5-3fe3-11e8-8df7-4d4c2b06e81b Duration: 0.48 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
def lambda_handler(event, context): test_dict = {'year':'2018','mon':'04','day':'14'} for i in test_dict: print(i) print(test_dict[i]) print(test_dict.get('year','NOT FOUND')) print(test_dict.get('years','NOT FOUND')) print(test_dict.keys()) print(test_dict.values()) for key,value in test_dict.items(): print(key,":",value) ========================================================================== Response: null Request ID: "be57689a-3fe5-11e8-a508-7926ec266421" Function Logs: START RequestId: be57689a-3fe5-11e8-a508-7926ec266421 Version: $LATEST year 2018 mon 04 day 14 2018 NOT FOUND dict_keys(['year', 'mon', 'day']) dict_values(['2018', '04', '14']) year : 2018 mon : 04 day : 14 END RequestId: be57689a-3fe5-11e8-a508-7926ec266421 REPORT RequestId: be57689a-3fe5-11e8-a508-7926ec266421 Duration: 0.58 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
def lambda_handler(event, context): test_set = {'this','-','is','-','set'} print(test_set) for i in test_set: print(i) test_set.discard('-') print(test_set) test_set_frozon = frozenset({'can','not','add','and','discard'}) print(test_set_frozon) ========================================================================== Response: null Request ID: "84c22b12-3fe8-11e8-b96a-af1c8c10d84a" Function Logs: START RequestId: 84c22b12-3fe8-11e8-b96a-af1c8c10d84a Version: $LATEST {'is', 'set', '-', 'this'} is set - this {'is', 'set', 'this'} frozenset({'discard', 'can', 'not', 'add', 'and'}) END RequestId: 84c22b12-3fe8-11e8-b96a-af1c8c10d84a REPORT RequestId: 84c22b12-3fe8-11e8-b96a-af1c8c10d84a Duration: 0.43 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
def lambda_handler(event, context): test_list = ['this','is','python','slice','set'] print(test_list[:]) print(test_list[:4]) print(test_list[1:]) print(test_list[::2]) print(test_list[-1::]) print(test_list[::-1]) ========================================================================== Response: null Request ID: "1467b80a-3ffc-11e8-8f0b-d9cd7250656b" Function Logs: START RequestId: 1467b80a-3ffc-11e8-8f0b-d9cd7250656b Version: $LATEST ['this', 'is', 'python', 'slice', 'set'] ['this', 'is', 'python', 'slice'] ['is', 'python', 'slice', 'set'] ['this', 'python', 'set'] ['set'] ['set', 'slice', 'python', 'is', 'this'] END RequestId: 1467b80a-3ffc-11e8-8f0b-d9cd7250656b REPORT RequestId: 1467b80a-3ffc-11e8-8f0b-d9cd7250656b Duration: 0.40 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
class TestClass: def __init__(self): print('Create TestClass') def test_method(self,val): print('call test_method') print(val) ========================================================================== import testmod def lambda_handler(event, context): test_class = testmod.TestClass() test_class.test_method('1') ========================================================================== Response: null Request ID: "da5fbad7-3ffe-11e8-99e4-f124716fdbac" Function Logs: START RequestId: da5fbad7-3ffe-11e8-99e4-f124716fdbac Version: $LATEST Create TestClass call test_method 1 END RequestId: da5fbad7-3ffe-11e8-99e4-f124716fdbac REPORT RequestId: da5fbad7-3ffe-11e8-99e4-f124716fdbac Duration: 0.34 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
コマンドライン引数
Labmdaではeventを利用する。
def lambda_handler(event, context): print(event) print(event['msg']) ========================================================================== Response: null Request ID: "bd98380a-3fff-11e8-b8af-798937c861d6" Function Logs: START RequestId: bd98380a-3fff-11e8-b8af-798937c861d6 Version: $LATEST {'msg': 'Hello World'} Hello World END RequestId: bd98380a-3fff-11e8-b8af-798937c861d6 REPORT RequestId: bd98380a-3fff-11e8-b8af-798937c861d6 Duration: 0.38 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
import os def lambda_handler(event, context): directory = "/etc/python" file = "setting.ini" print(os.path.join(directory,file)) print(os.path.join(directory,"3.6",file)) ========================================================================== Response: null Request ID: "9176f4fd-4000-11e8-aa7d-f996117ce1ed" Function Logs: START RequestId: 9176f4fd-4000-11e8-aa7d-f996117ce1ed Version: $LATEST /etc/python/setting.ini /etc/python/3.6/setting.ini END RequestId: 9176f4fd-4000-11e8-aa7d-f996117ce1ed REPORT RequestId: 9176f4fd-4000-11e8-aa7d-f996117ce1ed Duration: 0.35 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
def lambda_handler(event, context): value = 1 if value == 1: print("value is 1") elif value == 2: print("value is 2") elif value == 3: pass else: print("value is not 1,2,3") ========================================================================== Response: null Request ID: "7c158508-4001-11e8-90fc-11102c6f7f98" Function Logs: START RequestId: 7c158508-4001-11e8-90fc-11102c6f7f98 Version: $LATEST value is 1 END RequestId: 7c158508-4001-11e8-90fc-11102c6f7f98 REPORT RequestId: 7c158508-4001-11e8-90fc-11102c6f7f98 Duration: 0.39 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB
def lambda_handler(event, context): counter = 0 while counter < 10: counter += 1 print(counter) # タイムアウトになるまで実行されてエラーメッセージが出力された while True: print("test") ========================================================================== Response: { "errorMessage": "2018-04-14T16:39:04.666Z 460c8d7e-4002-11e8-80d8-d3914a20416b Task timed out after 30.01 seconds" } Request ID: "460c8d7e-4002-11e8-80d8-d3914a20416b" Function Logs: test ....
def lambda_handler(event, context): for num in range(100): if num % 10: continue print(num) ========================================================================== Response: null Request ID: "1504b6c1-4003-11e8-a3ab-67ba43bfb4bb" Function Logs: START RequestId: 1504b6c1-4003-11e8-a3ab-67ba43bfb4bb Version: $LATEST 0 10 20 30 40 50 60 70 80 90 END RequestId: 1504b6c1-4003-11e8-a3ab-67ba43bfb4bb REPORT RequestId: 1504b6c1-4003-11e8-a3ab-67ba43bfb4bb Duration: 0.40 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 22 MB
import sys import traceback def lambda_handler(event, context): def except_test(value1,value2): result = 0 try: result = value1 + value2 except: print("計算できません") raise finally: print("計算終了") return result print(except_test(200,100)) try: print(except_test(200,'100')) except: print("Error") ========================================================================== Response: null Request ID: "deb4f5cb-4007-11e8-8bc4-fde8b1a9daf5" Function Logs: START RequestId: deb4f5cb-4007-11e8-8bc4-fde8b1a9daf5 Version: $LATEST 計算終了 300 計算できません 計算終了 Error END RequestId: deb4f5cb-4007-11e8-8bc4-fde8b1a9daf5 REPORT RequestId: deb4f5cb-4007-11e8-8bc4-fde8b1a9daf5 Duration: 0.31 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB