def test_format_to_columns(self):
     res = [
         'foo              bar              fish             ',
         'stapler          anchovies        falcon-heavy     ',
         'saffron          waterfall        ',
     ]
     self.assertEqual(formatToLines(arr), res)
Beispiel #2
0
 def test_format_to_columns(self):
     res = [
         'foo              2                ',
         'bar              2                ',
         'fish             0                ',
         'stapler          3                ',
     ]
     self.assertEqual(formatToLines(arr, 2), res)
Beispiel #3
0
def run(args):
    options = dealArgs(args).to_object()
    words = getAndFilter(options)
    stats = None

    # if stats being requested
    if options['stats']:
        stats = getStats(words)
    # format (incompatible with stats)
    elif options['format']:
        words = formatToLines(words)

    # if outputting, just print JSON for one
    if options['output']:
        # CSV
        if options['csv']:
            columns = ['Name']
            if stats:
                columns = ['Name', 'Count']
            writeCSV(stats, options['output'], columns)

        # Not CSV
        else:
            if stats:
                for_print = json.dumps(stats, indent=4)
            else:
                for_print = json.dumps(words, indent=4)
            writeFile(for_print, options['output'])
        print(
            f"\nThe requested data has been written to file {options['output']}."
        )
    # if printing to screen
    else:
        for_print = '\n'
        if stats:
            if len(stats) <= 1:
                for_print += 'No stats to print.'
            else:
                for_print += 'STATS:\n'
                for stat in stats:
                    for_print += f'\n{stat}: {stats[stat]}'
        else:
            if len(words) <= 1:
                for_print += 'No words to print.'
            else:
                for_print += 'WORDS:\n'
                for word in words:
                    for_print += f'\n{word}'
        print(for_print)
def run(args, db_name=settings['default_db'], table_name=settings['default_table']):
    options = dealArgsRead(args)
    conn = connect_db(db_name, table_name)
    if 'word' in options:
        count = get_word(conn, options['word'], table_name)
        rtn = f"\nWord {options['word']} occurs {count} times.\n"
    else:
        counts = get_words(conn, table_name)
        rtn = ''
        lines = formatToLines(counts)
        rtn += '\nWord         Count\n'
        for line in lines:
            rtn += f'{line}\n'
    close_db(conn)
    return rtn
def run(args,
        db_name=settings['default_db'],
        table_name=settings['default_table']):
    options = dealArgsRead(args)
    conn = connect_db(db_name, table_name)
    rtn = ''
    if 'word' in options:
        counts = get_word(conn, options['word'], table_name)
    else:
        counts = get_words(conn, table_name)
    # Shut the database
    close_db(conn)
    # If no results
    if len(counts) == 0:
        rtn = '\nThere are no results to show.\n'
    # If there are results
    else:
        counts = [('WORD', 'ASSOC', 'DISTANCE', 'COUNT')] + counts
        lines = formatToLines(counts, 4)
        for line in lines:
            rtn += f'{line}\n'
    return rtn
 def test_format_to_columns_of_single_word(self):
     res = [
         'foo              ',
     ]
     self.assertEqual(formatToLines(['foo']), res)
        'finish': '',
    }
    for a in args:
        a = a.split('=')
        if a[0] == '-file':
            rtn['file'] = a[1]
        elif a[0] == '-start':
            rtn['start'] = a[1]
        elif a[0] == '-stop':
            rtn['stop'] = a[1]
        elif a[0] == '-finish':
            rtn['finish'] = a[1]
    return rtn


def getAndFilter(args):
    options = dealArgs(args)
    words = getFile(options['file'])
    arr = WalkArray(words, options)
    arr.filter()
    return arr.words


if __name__ == '__main__':
    args = sys.argv
    words = getAndFilter(args)
    if '-f' in args:
        words = formatToLines(words)
    for word in words:
        print(word)