<commit_before>#! /usr/bin/python3

# * GetQuote.py                                   * #
# * <NAME>                               * #
# * --------------------------------------------- * #
# * Provides user interface for Get Quote         * #
# * DataBase Management                           * #
# * --------------------------------------------- * #

import shelve


def PrintDB(quotes_DB):
    for key, value in quotes_DB.items():
        print('{0} : '.format(key))
        print(value)
        print('-' * 45)


def DeleteQuote(quotes_DB):
    userDict = {}
    dbKeys = list(quotes_DB.keys())

    # Creates a dict of quoteDB keys as values with easier to enter
    # keys mapped to them
    for i in range(len(dbKeys)):
        userDict[str(i)] = dbKeys[i]

    userKeys = [int(x) for x in list(userDict.keys())]
    userKeys.sort()

    print('Num | Author/Datetime Stamp')
    print('-' * 45)
    for key in userKeys:
        title = userDict.get(str(key))
        print('{0}: {1}'.format(key, title))

    choice = input('Please select the number of the quote to delete: ')

    while choice not in userDict.keys():
        choice = input('Input not found.\
                       Please enter the number of the quote to delete: ')

    # Get quote to delete
    quoteKey = userDict.get(choice)
    quote = quotes_DB.get(quoteKey)

    # Print quote to delete
    print('{}: '.format(quoteKey))
    print(quote)
    print('-' * 45)
    confirm = input('Are you sure you want to delete this quote (y/n): ')

    if confirm == 'y' or confirm == 'Y':
        del quotes_DB[quoteKey]
        print('Deleted\n')


def SearchQuote(quotes_DB):
    matches = []

    while True:
        to_search = input('Please enter an author to search for: ')

        for key in quotes_DB.keys():
            if to_search in key:
                matches.append(quotes_DB[key])

        if not matches:
            print('Sorry, did not find {} in the database.'.format(to_search))

        else:
            print('Found the following quotes by {}'.format(to_search))
            print('-' * 45)
            for quote in matches:
                print(quote)
                print('-' * 45)

        matches = []  # Reset list
        choice = input('Would you like you search again? (y/n): ')
        if choice == 'n' or choice == 'N':
            break


def DumpQuotes(quotes_DB):
    fileName = input('Please enter the filename to save the quotes to: ')

    with open(fileName, 'w') as f:
        for key, value in quotes_DB.items():
            f.write('{0}: {1}\n'.format(key, value))
            f.write('-' * 90 + '\n')

    print('Done! Your quotes can be found in {}'.format(fileName))

def Main():
    quotes_DB = shelve.open('.Quotes.db')
    flag = True

    while flag:
        print('Please enter a choice:')
        print('1. Print all Quotes')
        print('2. Delete a Quote')
        print('3. Search for author')
        print('4. Dump Database to text file')
        print('5. [EXIT]')
        choice = input('> ')

        try:<commit_msg>Added check to ensure that fileName ends with .txt<commit_after>#! /usr/bin/python3

# * GetQuote.py                                   * #
# * <NAME>                               * #
# * --------------------------------------------- * #
# * Provides user interface for Get Quote         * #
# * DataBase Management                           * #
# * --------------------------------------------- * #

import shelve


def PrintDB(quotes_DB):
    for key, value in quotes_DB.items():
        print('{0} : '.format(key))
        print(value)
        print('-' * 45)


def DeleteQuote(quotes_DB):
    userDict = {}
    dbKeys = list(quotes_DB.keys())

    # Creates a dict of quoteDB keys as values with easier to enter
    # keys mapped to them
    for i in range(len(dbKeys)):
        userDict[str(i)] = dbKeys[i]

    userKeys = [int(x) for x in list(userDict.keys())]
    userKeys.sort()

    print('Num | Author/Datetime Stamp')
    print('-' * 45)
    for key in userKeys:
        title = userDict.get(str(key))
        print('{0}: {1}'.format(key, title))

    choice = input('Please select the number of the quote to delete: ')

    while choice not in userDict.keys():
        choice = input('Input not found.\
                       Please enter the number of the quote to delete: ')

    # Get quote to delete
    quoteKey = userDict.get(choice)
    quote = quotes_DB.get(quoteKey)

    # Print quote to delete
    print('{}: '.format(quoteKey))
    print(quote)
    print('-' * 45)
    confirm = input('Are you sure you want to delete this quote (y/n): ')

    if confirm == 'y' or confirm == 'Y':
        del quotes_DB[quoteKey]
        print('Deleted\n')


def SearchQuote(quotes_DB):
    matches = []

    while True:
        to_search = input('Please enter an author to search for: ')

        for key in quotes_DB.keys():
            if to_search in key:
                matches.append(quotes_DB[key])

        if not matches:
            print('Sorry, did not find {} in the database.'.format(to_search))

        else:
            print('Found the following quotes by {}'.format(to_search))
            print('-' * 45)
            for quote in matches:
                print(quote)
                print('-' * 45)

        matches = []  # Reset list
        choice = input('Would you like you search again? (y/n): ')
        if choice == 'n' or choice == 'N':
            break


def DumpQuotes(quotes_DB):
    fileName = input('Please enter the filename to save the quotes to: ')

    if '.txt' not in fileName:
        fileName += '.txt'

    with open(fileName, 'w') as f:
        for key, value in quotes_DB.items():
            f.write('{0}: {1}\n'.format(key, value))
            f.write('-' * 90 + '\n')

    print('Done! Your quotes can be found in {}'.format(fileName))

def Main():
    quotes_DB = shelve.open('.Quotes.db')
    flag = True

    while flag:
        print('Please enter a choice:')
        print('1. Print all Quotes')
        print('2. Delete a Quote')
        print('3. Search for author')
        print('4. Dump Database to text file')
        print('5. [EXIT]')
        choice = input('> ')

        try:
{'fraction_non_alphanumeric': 0.15774193548387097, 'fraction_numerical': 0.007419354838709678, 'mean_word_length': 1.7794710891976693, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '703098', 'n_tokens_mistral': 1874, 'n_tokens_neox': 1793, 'n_words': 566}