Exemplo n.º 1
0
def parse_query(input_string):
    if not input_string:
        return 'Please enter something ... type \'help\' if you need help'
    string_list = input_string.split()
    # top return_column in column : key
    start = string_list.pop(0).lower()
    if start == 'top':
        return_column = read_until(string_list, 'in').lower()
        column = read_until(string_list, ':').lower()
        key = read_until(string_list, '')
        if return_column not in q.userInput:
            return 'Return column does not exist or there is no \'in\'  statement'
        elif column not in q.userInput:
            return 'Column does not exist or there is no \':\'  statement'
        elif key == '':
            return 'There is no \':\' after the column or no key was entered'
        else:
            return q.QueryTop10(key, column, return_column)
    # crimes in column : key
    elif start == 'crimecount':
        junk = read_until(string_list, 'in')
        column = read_until(string_list, ":").lower()
        key = read_until(string_list, '')
        if column not in q.userInput:
            return 'Column does not exist or there is no \'in\'  statement'
        elif key == '':
            return 'There is no \':\' after the column or no key was entered'
        else:
            return q.QueryNumCrimes(key, column)
    # return_column in column : key
    elif start in q.userInput:
        return_column = start.lower() + ' ' + read_until(string_list, 'in')
        column = read_until(string_list, ':').lower()
        key = read_until(string_list, '')
        if column not in q.userInput:
            return 'Column does not exist or there is no \'in\'  statement'
        elif key == '':
            return 'There is no \':\' after the column or no key was entered'
        else:
            return q.QueryOne(key, column, return_column.strip())
    # refresh
    elif start == 'refresh':
        return dbCreate.connect_database()
    elif start == 'exit':
        return 'Exiting...'
    elif start == 'help':
        return 'TOP 10 DATA POINTS SYNTAX:\n' \
               'top return column in column : key \n\n' \
               'CRIME COUNT SYNTAX:\n' \
               'crimecount in column : key\n\n' \
               '1 DATAPOINT SYNTAX:\n' \
               'return column in column : key\n\n' \
               'OTHER COMMANDS: \n' \
               'help - access this text\n' \
               'exit - close this program\n' \
               'columns - list possible query columns\n' \
               'refresh - load database from crime_final.csv & offense_codes_final.csv files'
    elif start == 'columns':
        return q.dbKey()
    else:
        return 'Data request must start with \'top\', \'crimecount\', or a column name\n' \
               'Type \'help\' for more info'