Best Python code snippet using localstack_python
app.py
Source:app.py
1import os2import sys3from flask import Flask, jsonify, request, abort, send_file4from dotenv import load_dotenv5from linebot import LineBotApi, WebhookParser6from linebot.exceptions import InvalidSignatureError7from linebot.models import MessageEvent, TextMessage, TextSendMessage8from fsm import TocMachine9from utils import send_text_message10load_dotenv()11machine = TocMachine(12 states=["user", "home", "fsm","information","peko","menu","search","recommand","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],13 transitions=[14 {15 "trigger": "advance",16 "source": "user",17 "dest": "home",18 "conditions": "is_going_to_home",19 },20 {21 "trigger": "advance",22 "source": "user",23 "dest": "fsm",24 "conditions": "is_going_to_fsm",25 },26 {27 "trigger": "advance",28 "source": "user",29 "dest": "information",30 "conditions": "is_going_to_information",31 },32 {33 "trigger": "advance",34 "source": "information",35 "dest": "menu",36 "conditions": "is_going_to_menu",37 },38 39 {40 "trigger": "advance",41 "source": "information",42 "dest": "home",43 "conditions": "is_going_to_home",44 },45 {46 "trigger": "advance",47 "source": "information",48 "dest": "peko",49 "conditions": "is_going_to_peko",50 },51 {52 "trigger": "advance",53 "source": "user",54 "dest": "menu",55 "conditions": "is_going_to_menu",56 },57 {58 "trigger": "advance",59 "source": "menu",60 "dest": "search",61 "conditions": "is_going_to_search",62 },63 64 {65 "trigger": "advance",66 "source": "menu",67 "dest": "recommand",68 "conditions": "is_going_to_recommand",69 },70 71 {72 "trigger": "advance",73 "source": "search",74 "dest": "a",75 "conditions": "is_going_to_a",76 },77 {78 "trigger": "advance",79 "source": "search",80 "dest": "b",81 "conditions": "is_going_to_b",82 },83 {84 "trigger": "advance",85 "source": "b",86 "dest": "menu",87 "conditions": "is_going_to_menu",88 },89 {90 "trigger": "advance",91 "source": "b",92 "dest": "home",93 "conditions": "is_going_to_home",94 },95 {96 "trigger": "advance",97 "source": "search",98 "dest": "c",99 "conditions": "is_going_to_c",100 },101 {102 "trigger": "advance",103 "source": "search",104 "dest": "d",105 "conditions": "is_going_to_d",106 },107 {108 "trigger": "advance",109 "source": "d",110 "dest": "menu",111 "conditions": "is_going_to_menu",112 },113 {114 "trigger": "advance",115 "source": "d",116 "dest": "home",117 "conditions": "is_going_to_home",118 },119 {120 "trigger": "advance",121 "source": "search",122 "dest": "e",123 "conditions": "is_going_to_e",124 },125 {126 "trigger": "advance",127 "source": "e",128 "dest": "menu",129 "conditions": "is_going_to_menu",130 },131 {132 "trigger": "advance",133 "source": "e",134 "dest": "home",135 "conditions": "is_going_to_home",136 },137 {138 "trigger": "advance",139 "source": "search",140 "dest": "f",141 "conditions": "is_going_to_f",142 },143 {144 "trigger": "advance",145 "source": "f",146 "dest": "menu",147 "conditions": "is_going_to_menu",148 },149 {150 "trigger": "advance",151 "source": "f",152 "dest": "home",153 "conditions": "is_going_to_home",154 },155 {156 "trigger": "advance",157 "source": "search",158 "dest": "g",159 "conditions": "is_going_to_g",160 },161 {162 "trigger": "advance",163 "source": "search",164 "dest": "h",165 "conditions": "is_going_to_h",166 },167 {168 "trigger": "advance",169 "source": "search",170 "dest": "i",171 "conditions": "is_going_to_i",172 },173 {174 "trigger": "advance",175 "source": "search",176 "dest": "j",177 "conditions": "is_going_to_j",178 },179 {180 "trigger": "advance",181 "source": "j",182 "dest": "menu",183 "conditions": "is_going_to_menu",184 },185 {186 "trigger": "advance",187 "source": "j",188 "dest": "home",189 "conditions": "is_going_to_home",190 },191 {192 "trigger": "advance",193 "source": "search",194 "dest": "k",195 "conditions": "is_going_to_k",196 },197 {198 "trigger": "advance",199 "source": "search",200 "dest": "l",201 "conditions": "is_going_to_l",202 },203 {204 "trigger": "advance",205 "source": "search",206 "dest": "m",207 "conditions": "is_going_to_m",208 },209 {210 "trigger": "advance",211 "source": "search",212 "dest": "n",213 "conditions": "is_going_to_n",214 },215 {216 "trigger": "advance",217 "source": "search",218 "dest": "o",219 "conditions": "is_going_to_o",220 },221 {222 "trigger": "advance",223 "source": "search",224 "dest": "p",225 "conditions": "is_going_to_p",226 },227 {228 "trigger": "advance",229 "source": "search",230 "dest": "q",231 "conditions": "is_going_to_q",232 },233 {234 "trigger": "advance",235 "source": "q",236 "dest": "menu",237 "conditions": "is_going_to_menu",238 },239 {240 "trigger": "advance",241 "source": "q",242 "dest": "home",243 "conditions": "is_going_to_home",244 },245 {246 "trigger": "advance",247 "source": "search",248 "dest": "r",249 "conditions": "is_going_to_r",250 },251 {252 "trigger": "advance",253 "source": "search",254 "dest": "s",255 "conditions": "is_going_to_s",256 },257 {258 "trigger": "advance",259 "source": "search",260 "dest": "t",261 "conditions": "is_going_to_t",262 },263 {264 "trigger": "advance",265 "source": "search",266 "dest": "u",267 "conditions": "is_going_to_u",268 },269 {270 "trigger": "advance",271 "source": "search",272 "dest": "v",273 "conditions": "is_going_to_v",274 },275 {276 "trigger": "advance",277 "source": "v",278 "dest": "menu",279 "conditions": "is_going_to_menu",280 },281 {282 "trigger": "advance",283 "source": "v",284 "dest": "home",285 "conditions": "is_going_to_home",286 },287 {288 "trigger": "advance",289 "source": "search",290 "dest": "w",291 "conditions": "is_going_to_w",292 },293 {294 "trigger": "advance",295 "source": "search",296 "dest": "x",297 "conditions": "is_going_to_x",298 },299 {300 "trigger": "advance",301 "source": "x",302 "dest": "menu",303 "conditions": "is_going_to_menu",304 },305 {306 "trigger": "advance",307 "source": "x",308 "dest": "home",309 "conditions": "is_going_to_home",310 },311 {312 "trigger": "advance",313 "source": "search",314 "dest": "y",315 "conditions": "is_going_to_y",316 },317 {318 "trigger": "advance",319 "source": "search",320 "dest": "z",321 "conditions": "is_going_to_z",322 },323 {324 "trigger": "advance",325 "source": "z",326 "dest": "menu",327 "conditions": "is_going_to_menu",328 },329 {330 "trigger": "advance",331 "source": "z",332 "dest": "home",333 "conditions": "is_going_to_home",334 },335 {"trigger": "go_back", "source": ["home","fsm","peko","recommand","a","c","g","h","i","k","l","m","n","o","p","r","s","t","u","w","y"], "dest": "user"},336 ],337 initial="user",338 auto_transitions=False,339 show_conditions=True,340)341app = Flask(__name__, static_url_path="")342# get channel_secret and channel_access_token from your environment variable343channel_secret = os.getenv("LINE_CHANNEL_SECRET", None)344channel_access_token = os.getenv("LINE_CHANNEL_ACCESS_TOKEN", None)345if channel_secret is None:346 print("Specify LINE_CHANNEL_SECRET as environment variable.")347 sys.exit(1)348if channel_access_token is None:349 print("Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.")350 sys.exit(1)351line_bot_api = LineBotApi(channel_access_token)352parser = WebhookParser(channel_secret)353@app.route("/callback", methods=["POST"])354def callback():355 signature = request.headers["X-Line-Signature"]356 # get request body as text357 body = request.get_data(as_text=True)358 app.logger.info("Request body: " + body)359 # parse webhook body360 try:361 events = parser.parse(body, signature)362 except InvalidSignatureError:363 abort(400)364 # if event is MessageEvent and message is TextMessage, then echo text365 for event in events:366 if not isinstance(event, MessageEvent):367 continue368 if not isinstance(event.message, TextMessage):369 continue370 line_bot_api.reply_message(371 event.reply_token, TextSendMessage(text=event.message.text)372 373 )374 return "OK"375@app.route("/webhook", methods=["POST"])376def webhook_handler():377 signature = request.headers["X-Line-Signature"]378 # get request body as text379 body = request.get_data(as_text=True)380 app.logger.info(f"Request body: {body}")381 # parse webhook body382 try:383 events = parser.parse(body, signature)384 #print(events)385 except InvalidSignatureError:386 abort(400)387 # if event is MessageEvent and message is TextMessage, then echo text388 for event in events:389 if not isinstance(event, MessageEvent):390 continue391 if not isinstance(event.message, TextMessage):392 continue393 if not isinstance(event.message.text, str):394 continue395 print(f"\nFSM STATE: {machine.state}")396 print(f"REQUEST BODY: \n{body}")397 response = machine.advance(event)398 if response == False:399 send_text_message(event.reply_token, "Enter home or press the button for help ")400 return "OK"401@app.route("/show-fsm", methods=["GET"])402def show_fsm():403 machine.get_graph().draw("fsm.png", prog="dot", format="png")404 return send_file("fsm.png", mimetype="image/png")405if __name__ == "__main__":406 port = os.environ.get("PORT", 8000)...
draw_fsm.py
Source:draw_fsm.py
1from fsm import TocMachine2machine = TocMachine(3 states=["user", "home", "fsm","information","peko","menu","search","recommand","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],4 transitions=[5 {6 "trigger": "advance",7 "source": "user",8 "dest": "home",9 "conditions": "is_going_to_home",10 },11 {12 "trigger": "advance",13 "source": "user",14 "dest": "fsm",15 "conditions": "is_going_to_fsm",16 },17 {18 "trigger": "advance",19 "source": "user",20 "dest": "information",21 "conditions": "is_going_to_information",22 },23 {24 "trigger": "advance",25 "source": "information",26 "dest": "menu",27 "conditions": "is_going_to_menu",28 },29 30 {31 "trigger": "advance",32 "source": "information",33 "dest": "home",34 "conditions": "is_going_to_home",35 },36 {37 "trigger": "advance",38 "source": "information",39 "dest": "peko",40 "conditions": "is_going_to_peko",41 },42 {43 "trigger": "advance",44 "source": "user",45 "dest": "menu",46 "conditions": "is_going_to_menu",47 },48 {49 "trigger": "advance",50 "source": "menu",51 "dest": "search",52 "conditions": "is_going_to_search",53 },54 55 {56 "trigger": "advance",57 "source": "menu",58 "dest": "recommand",59 "conditions": "is_going_to_recommand",60 },61 62 {63 "trigger": "advance",64 "source": "search",65 "dest": "a",66 "conditions": "is_going_to_a",67 },68 {69 "trigger": "advance",70 "source": "search",71 "dest": "b",72 "conditions": "is_going_to_b",73 },74 {75 "trigger": "advance",76 "source": "b",77 "dest": "menu",78 "conditions": "is_going_to_menu",79 },80 {81 "trigger": "advance",82 "source": "b",83 "dest": "home",84 "conditions": "is_going_to_home",85 },86 {87 "trigger": "advance",88 "source": "search",89 "dest": "c",90 "conditions": "is_going_to_c",91 },92 {93 "trigger": "advance",94 "source": "search",95 "dest": "d",96 "conditions": "is_going_to_d",97 },98 {99 "trigger": "advance",100 "source": "d",101 "dest": "menu",102 "conditions": "is_going_to_menu",103 },104 {105 "trigger": "advance",106 "source": "d",107 "dest": "home",108 "conditions": "is_going_to_home",109 },110 {111 "trigger": "advance",112 "source": "search",113 "dest": "e",114 "conditions": "is_going_to_e",115 },116 {117 "trigger": "advance",118 "source": "e",119 "dest": "menu",120 "conditions": "is_going_to_menu",121 },122 {123 "trigger": "advance",124 "source": "e",125 "dest": "home",126 "conditions": "is_going_to_home",127 },128 {129 "trigger": "advance",130 "source": "search",131 "dest": "f",132 "conditions": "is_going_to_f",133 },134 {135 "trigger": "advance",136 "source": "f",137 "dest": "menu",138 "conditions": "is_going_to_menu",139 },140 {141 "trigger": "advance",142 "source": "f",143 "dest": "home",144 "conditions": "is_going_to_home",145 },146 {147 "trigger": "advance",148 "source": "search",149 "dest": "g",150 "conditions": "is_going_to_g",151 },152 {153 "trigger": "advance",154 "source": "search",155 "dest": "h",156 "conditions": "is_going_to_h",157 },158 {159 "trigger": "advance",160 "source": "search",161 "dest": "i",162 "conditions": "is_going_to_i",163 },164 {165 "trigger": "advance",166 "source": "search",167 "dest": "j",168 "conditions": "is_going_to_j",169 },170 {171 "trigger": "advance",172 "source": "j",173 "dest": "menu",174 "conditions": "is_going_to_menu",175 },176 {177 "trigger": "advance",178 "source": "j",179 "dest": "home",180 "conditions": "is_going_to_home",181 },182 {183 "trigger": "advance",184 "source": "search",185 "dest": "k",186 "conditions": "is_going_to_k",187 },188 {189 "trigger": "advance",190 "source": "search",191 "dest": "l",192 "conditions": "is_going_to_l",193 },194 {195 "trigger": "advance",196 "source": "search",197 "dest": "m",198 "conditions": "is_going_to_m",199 },200 {201 "trigger": "advance",202 "source": "search",203 "dest": "n",204 "conditions": "is_going_to_n",205 },206 {207 "trigger": "advance",208 "source": "search",209 "dest": "o",210 "conditions": "is_going_to_o",211 },212 {213 "trigger": "advance",214 "source": "search",215 "dest": "p",216 "conditions": "is_going_to_p",217 },218 {219 "trigger": "advance",220 "source": "search",221 "dest": "q",222 "conditions": "is_going_to_q",223 },224 {225 "trigger": "advance",226 "source": "q",227 "dest": "menu",228 "conditions": "is_going_to_menu",229 },230 {231 "trigger": "advance",232 "source": "q",233 "dest": "home",234 "conditions": "is_going_to_home",235 },236 {237 "trigger": "advance",238 "source": "search",239 "dest": "r",240 "conditions": "is_going_to_r",241 },242 {243 "trigger": "advance",244 "source": "search",245 "dest": "s",246 "conditions": "is_going_to_s",247 },248 {249 "trigger": "advance",250 "source": "search",251 "dest": "t",252 "conditions": "is_going_to_t",253 },254 {255 "trigger": "advance",256 "source": "search",257 "dest": "u",258 "conditions": "is_going_to_u",259 },260 {261 "trigger": "advance",262 "source": "search",263 "dest": "v",264 "conditions": "is_going_to_v",265 },266 {267 "trigger": "advance",268 "source": "v",269 "dest": "menu",270 "conditions": "is_going_to_menu",271 },272 {273 "trigger": "advance",274 "source": "v",275 "dest": "home",276 "conditions": "is_going_to_home",277 },278 {279 "trigger": "advance",280 "source": "search",281 "dest": "w",282 "conditions": "is_going_to_w",283 },284 {285 "trigger": "advance",286 "source": "search",287 "dest": "x",288 "conditions": "is_going_to_x",289 },290 {291 "trigger": "advance",292 "source": "x",293 "dest": "menu",294 "conditions": "is_going_to_menu",295 },296 {297 "trigger": "advance",298 "source": "x",299 "dest": "home",300 "conditions": "is_going_to_home",301 },302 {303 "trigger": "advance",304 "source": "search",305 "dest": "y",306 "conditions": "is_going_to_y",307 },308 {309 "trigger": "advance",310 "source": "search",311 "dest": "z",312 "conditions": "is_going_to_z",313 },314 {315 "trigger": "advance",316 "source": "z",317 "dest": "menu",318 "conditions": "is_going_to_menu",319 },320 {321 "trigger": "advance",322 "source": "z",323 "dest": "home",324 "conditions": "is_going_to_home",325 },326 {"trigger": "go_back", "source": ["home","fsm","peko","recommand","a","c","g","h","i","k","l","m","n","o","p","r","s","t","u","w","y"], "dest": "user"},327 ],328 initial="user",329 auto_transitions=False,330 show_conditions=True,331)...
test_resources_condition.py
Source:test_resources_condition.py
1import json2from datetime import datetime3from google.fhir.r4.json_format import json_fhir_string_to_proto4from proto.google.fhir.proto.r4.core.resources.bundle_and_contained_resource_pb2 import ( # noqa: E5015 Bundle,6)7from fhir_helpers.resources.lpr import LPR8from .helpers import (9 get_example_bundle,10 get_example_bundle_by_filename,11 get_example_bundle_proto,12)13def test_condition_find() -> None:14 lpr_dict = LPR(get_example_bundle())15 lpr_proto = LPR(get_example_bundle_proto())16 assert len(lpr_dict.conditions) == 1217 assert len(lpr_proto.conditions) == 1218 conditions_dict = lpr_dict.conditions.find_by_coding("232353008")19 conditions_proto = lpr_proto.conditions.find_by_coding("232353008")20 assert len(conditions_dict) == 121 assert len(conditions_proto) == 122 three_years_ago = datetime(year=2018, month=3, day=6)23 conditions_dict = lpr_dict.conditions.find_after_date(three_years_ago)24 conditions_proto = lpr_proto.conditions.find_after_date(three_years_ago)25 assert len(conditions_dict) == 826 assert len(conditions_proto) == 827 conditions_dict = lpr_dict.conditions.find_before_date(three_years_ago)28 conditions_proto = lpr_proto.conditions.find_before_date(three_years_ago)29 assert len(conditions_dict) == 430 assert len(conditions_proto) == 431def test_condition_search() -> None:32 lpr_dict = LPR(get_example_bundle())33 lpr_proto = LPR(get_example_bundle_proto())34 assert len(lpr_dict.conditions.find_by_text_match("rhinitis")) == 135 assert len(lpr_proto.conditions.find_by_text_match("rhinitis")) == 136def test_procedure_sort() -> None:37 lpr_dict = LPR(get_example_bundle())38 lpr_proto = LPR(get_example_bundle_proto())39 sorted_dict = lpr_dict.conditions.sort_by_date()40 sorted_proto = lpr_proto.conditions.sort_by_date()41 sorted_dict_reverse = lpr_dict.conditions.sort_by_date(reverse=True)42 sorted_proto_reverse = lpr_proto.conditions.sort_by_date(reverse=True)43 dates = sorted([co.recorded_date for co in sorted_dict if co.recorded_date])44 dates_reverse = sorted(dates, reverse=True)45 for i in range(len(dates)):46 assert sorted_dict.get(i).recorded_date == dates[i]47 assert sorted_proto.get(i).recorded_date == dates[i]48 assert sorted_dict_reverse.get(i).recorded_date == dates_reverse[i]49 assert sorted_proto_reverse.get(i).recorded_date == dates_reverse[i]50def test_condtion_date() -> None:51 bundle_dict = get_example_bundle_by_filename("synthea_condition_date.json")52 lpr_dict = LPR(bundle_dict)53 lpr_proto = LPR(json_fhir_string_to_proto(json.dumps(bundle_dict), Bundle))54 condition_dict = lpr_dict.conditions.first()55 constion_proto = lpr_proto.conditions.first()56 assert condition_dict and condition_dict.onset_datetime...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!