<commit_before>import boto3
from boto3.dynamodb.conditions import Key


def launch_response():
    text_item = {"type": "PlainText",
                 "text": "Welcome to Helsinki buses. " +
                         "We can tell you departures on Helsinki area " +
                         "bus stops." +
                         "Just utter the four digit stop code for" +
                         "next departures. " +
                         "Try for example: Helsinki 1001. " +
                         "Ask to repeat for updates on same stop"}
    card_item = {"type": "Simple", "title": "Helsinki bus stops",
                 "content": "Helsinki bus stops can tell you next departures on" +
                            " any Helsinki metropolitan area bus stop."}
    reprompt = {
        "outputSpeech": {"text": "Which stop do you want to know about?",
                         "type": "PlainText"}}
    response = {"version": "1.0",
                "response": {"outputSpeech": text_item, "card": card_item},
                "shouldEndSession": False}

    return response


def response_creator(text, card):
    text_item = {"type": "PlainText", "text": text}
    card_item = {"type": "Simple", "title": "Stop Info", "content": card}
    reprompt = {
        "outputSpeech": {"text": "Which stop do you want to know about?",
                         "type": "PlainText"}}
    response = {"version": "1.0",
                "response": {"outputSpeech": text_item, "card": card_item,
                             "reprompt": reprompt,
                             "shouldEndSession": True}}

    return response


class DynamoStorage(object):
    def __init__(self, request):
        self.request_json = request
        self.dynamodb = boto3.resource('dynamodb', region_name="us-east-1")
        self.table = self.dynamodb.Table("HslBotUsers")

    def get_user_id(self):
        return self.request_json["session"]["user"]["userId"]

    def write_previous_stop(self, stop_code):
        try:
            userId = self.get_user_id()
        except KeyError:
            return
        response = self.table.query(
            KeyConditionExpression=Key('userkey').eq(self.get_user_id()))
        count = response.get("Count", 0)
        if count > 0:
            cresp = self.table.put_item(
                Item={"userkey": "test1", "stopcode": stop_code})
        else:
            cresp = self.table.update_item(Key={"userkey": "test1"},
                                           UpdateExpression="set stopcode = :s",
                                           ExpressionAttributeValues={
                                               ':s': stop_code},
                                           ReturnValues="UPDATED_NEW")<commit_msg>proper user key stored into dynamodb<commit_after>import boto3
from boto3.dynamodb.conditions import Key


def launch_response():
    text_item = {"type": "PlainText",
                 "text": "Welcome to Helsinki buses. " +
                         "We can tell you departures on Helsinki area " +
                         "bus stops." +
                         "Just utter the four digit stop code for" +
                         "next departures. " +
                         "Try for example: Helsinki 1001. " +
                         "Ask to repeat for updates on same stop"}
    card_item = {"type": "Simple", "title": "Helsinki bus stops",
                 "content": "Helsinki bus stops can tell you next departures on" +
                            " any Helsinki metropolitan area bus stop."}
    reprompt = {
        "outputSpeech": {"text": "Which stop do you want to know about?",
                         "type": "PlainText"}}
    response = {"version": "1.0",
                "response": {"outputSpeech": text_item, "card": card_item},
                "shouldEndSession": False}

    return response


def response_creator(text, card):
    text_item = {"type": "PlainText", "text": text}
    card_item = {"type": "Simple", "title": "Stop Info", "content": card}
    reprompt = {
        "outputSpeech": {"text": "Which stop do you want to know about?",
                         "type": "PlainText"}}
    response = {"version": "1.0",
                "response": {"outputSpeech": text_item, "card": card_item,
                             "reprompt": reprompt,
                             "shouldEndSession": True}}

    return response


class DynamoStorage(object):
    def __init__(self, request):
        self.request_json = request
        self.dynamodb = boto3.resource('dynamodb', region_name="us-east-1")
        self.table = self.dynamodb.Table("HslBotUsers")

    def get_user_id(self):
        return self.request_json["session"]["user"]["userId"]

    def write_previous_stop(self, stop_code):
        try:
            userId = self.get_user_id()
        except KeyError:
            return
        response = self.table.query(
            KeyConditionExpression=Key('userkey').eq(self.get_user_id()))
        count = response.get("Count", 0)
        username = userId
        if count > 0:
            cresp = self.table.put_item(
                Item={"userkey": username, "stopcode": stop_code})
        else:
            cresp = self.table.update_item(Key={"userkey": username},
                                           UpdateExpression="set stopcode = :s",
                                           ExpressionAttributeValues={
                                               ':s': stop_code},
                                           ReturnValues="UPDATED_NEW")
{'fraction_non_alphanumeric': 0.11803928635790233, 'fraction_numerical': 0.005406379527842855, 'mean_word_length': 1.6772793053545587, 'pattern_counts': {'":': 60, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7438642', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1424, 'n_words': 388}