From 444d44d8586c9ba1d69f4ebbe1fb201f1098d7a4 Mon Sep 17 00:00:00 2001 From: Prashu2020 <136256554+Prashu2020@users.noreply.github.com> Date: Wed, 16 Aug 2023 16:47:43 +0530 Subject: [PATCH 1/3] Add files via upload updated file after completing task1, 2 and 3. --- melli.py | 79 +++++++++++++++++++++++++------------------------------- 1 file changed, 35 insertions(+), 44 deletions(-) diff --git a/melli.py b/melli.py index d6854d2..983605a 100644 --- a/melli.py +++ b/melli.py @@ -12,12 +12,20 @@ @app.get("/task1/greet/{name}", tags=["Task 1"], summary="👋🇩🇪🇬🇧🇪🇸") -async def task1_greet(name: str) -> str: +async def task1_greet(name: str, language: str = "en") -> str: """Greet somebody in German, English or Spanish!""" - # Write your code below - ... - return f"Hello {name}, I am Melli." + supported_languages = ["de", "en", "es"] + + if language not in supported_languages: + return "Sorry, we don't speak that language." + + greetings = { + "de": f"Hallo {name}, ich bin Melli.", + "en": f"Hello {name}, I am Melli.", + "es": f"Hola {name}, soy Melli.", + } + return greetings[language] """ Task 2 - snake_case to cameCase @@ -28,16 +36,16 @@ async def task1_greet(name: str) -> str: def camelize(key: str): """Takes string in snake_case format returns camelCase formatted version.""" - # Write your code below - ... + words = key.split('_') + key = words[0] + ''.join(word.capitalize() for word in words[1:]) return key @app.post("/task2/camelize", tags=["Task 2"], summary="🐍➡️🐪") async def task2_camelize(data: dict[str, Any]) -> dict[str, Any]: - """Takes a JSON object and transfroms all keys from snake_case to camelCase.""" - return {camelize(key): value for key, value in data.items()} - + """Takes a JSON object and transforms all keys from snake_case to camelCase.""" + camelized_data = {camelize(key): value for key, value in data.items()} + return camelized_data """ Task 3 - Handle User Actions @@ -60,49 +68,32 @@ class ActionResponse(BaseModel): message: str -def handle_call_action(action: str): - # Write your code below - ... - return "🤙 Why don't you call them yourself!" - - -def handle_reminder_action(action: str): - # Write your code below - ... - return "🔔 I can't even remember my own stuff!" - +# Action handlers +def handle_call_action(username: str): + return f"📞 Sure, let me help you connect with your loved ones, {username}!" -def handle_timer_action(action: str): - # Write your code below - ... - return "⏰ I don't know how to read the clock!" +def handle_reminder_action(username: str): + return f"🔔 Absolutely, I'll remind you to have some tea with your friend, {username}!" +def handle_timer_action(username: str): + return f"⏰ Don't worry, I'll help you keep track of time for that tea with your friend, {username}!" -def handle_unknown_action(action: str): - # Write your code below - ... - return "🤬 #$!@" +def handle_unknown_action(): + return "🤔 I'm sorry, I didn't quite catch that. Could you please clarify?" @app.post("/task3/action", tags=["Task 3"], summary="🤌") def task3_action(request: ActionRequest): """Accepts an action request, recognizes its intent and forwards it to the corresponding action handler.""" - # tip: you have to use the response model above and also might change the signature - # of the action handlers - # Write your code below - ... - from random import choice - - # There must be a better way! - handler = choice( - [ - handle_call_action, - handle_reminder_action, - handle_timer_action, - handle_unknown_action, - ] - ) - return handler(request.action) + action = request.action.lower() + if "call" in action: + return handle_call_action(request.username) + elif "reminder" in action: + return handle_reminder_action(request.username) + elif "timer" in action: + return handle_timer_action(request.username) + else: + return handle_unknown_action() """ From d6fa7ad9e1f0ebaa5e64d53420dfd534b5d4bf77 Mon Sep 17 00:00:00 2001 From: Prashu2020 <136256554+Prashu2020@users.noreply.github.com> Date: Wed, 16 Aug 2023 17:17:45 +0530 Subject: [PATCH 2/3] Add files via upload Updated melli.py with task1,2 and 3 --- melli.py | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/melli.py b/melli.py index 983605a..0410673 100644 --- a/melli.py +++ b/melli.py @@ -12,7 +12,7 @@ @app.get("/task1/greet/{name}", tags=["Task 1"], summary="👋🇩🇪🇬🇧🇪🇸") -async def task1_greet(name: str, language: str = "en") -> str: +async def task1_greet(name: str, language: str = "de") -> str: """Greet somebody in German, English or Spanish!""" supported_languages = ["de", "en", "es"] @@ -69,29 +69,38 @@ class ActionResponse(BaseModel): # Action handlers -def handle_call_action(username: str): - return f"📞 Sure, let me help you connect with your loved ones, {username}!" +def handle_call_action(username: str, action:str): + if action.split()[-1][:-1] not in friends[username]: + return "{username}, I can't find this person in your contacts." + return f"🤙 Calling {username} ..." -def handle_reminder_action(username: str): - return f"🔔 Absolutely, I'll remind you to have some tea with your friend, {username}!" +def handle_reminder_action(username: str, action:str): + return f"🔔 Alright, I will remind you!" -def handle_timer_action(username: str): - return f"⏰ Don't worry, I'll help you keep track of time for that tea with your friend, {username}!" +def handle_timer_action(username: str, action:str): + return f"⏰ Alright, the timer is set!" def handle_unknown_action(): - return "🤔 I'm sorry, I didn't quite catch that. Could you please clarify?" + return "👀 Sorry , but I can't help with that!" @app.post("/task3/action", tags=["Task 3"], summary="🤌") def task3_action(request: ActionRequest): """Accepts an action request, recognizes its intent and forwards it to the corresponding action handler.""" + users = [] + users.append(friends.keys()) + users.extend(friends.values[0]) + users.extend(friends.values[1]) + if username not in users: + return f"Hi {username}, I don't know you yet. But I would love to meet you!" + action = request.action.lower() - if "call" in action: - return handle_call_action(request.username) - elif "reminder" in action: - return handle_reminder_action(request.username) - elif "timer" in action: - return handle_timer_action(request.username) + if "call" in action.lower(): + return handle_call_action(request.username, request.action) + elif "remind" in action.lower(): + return handle_reminder_action(request.username, request.action) + elif "timer" in action.lower(): + return handle_timer_action(request.username, request.action) else: return handle_unknown_action() From 4b570a59f6df60357cec9d0bfa755da7a95dcf73 Mon Sep 17 00:00:00 2001 From: Prashu2020 <136256554+Prashu2020@users.noreply.github.com> Date: Wed, 16 Aug 2023 17:27:05 +0530 Subject: [PATCH 3/3] Add files via upload Updated --- melli.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/melli.py b/melli.py index 0410673..1efa2ac 100644 --- a/melli.py +++ b/melli.py @@ -89,8 +89,6 @@ def task3_action(request: ActionRequest): """Accepts an action request, recognizes its intent and forwards it to the corresponding action handler.""" users = [] users.append(friends.keys()) - users.extend(friends.values[0]) - users.extend(friends.values[1]) if username not in users: return f"Hi {username}, I don't know you yet. But I would love to meet you!"