def run(args, db_name='main.sqlite', table_name='main'): """ Takes arguments (usually from command-line), parses them, and retrieves the words, filters, and formats them. Returns a formatted string ready to print-to-screen. """ rtn = '' # Parse the arguments options = dealArgs(args).to_object() # Get and filter the words words = getAndFilter(options) word_pairs = getPairs(words) conn = connect_db(db_name, table_name) # If -c argument passed, clear out the table if options['clear']: removed_rows = empty_table(conn, table_name) rtn += f'\nRemoved {removed_rows} rows.' # Add the word-pairs & get the count modified_rows = add_words(conn, word_pairs, table_name) # Close the database close_db(conn) # If nothing saved, give a warning if modified_rows <= 0: rtn += f'\nFailed to save anything' # If something saved, state the count else: rtn += f'\nSuccessfully added {modified_rows} words' return rtn
def test_deal_args(self): res = { 'files': ['small_test.txt'], 'start': 'foo', 'stop': 'bar', 'finish': 'enough', 'clear': True, } options = dealArgs(args) self.assertEqual(res, options.to_object())
def test_deal_args(self): res = { 'file': 'small_test.txt', 'start': 'foo', 'stop': 'bar', 'finish': 'enough', 'format': False, 'output': 'output.json', # stats overrides format 'stats': True, 'csv': False, } options = dealArgs(args) self.assertEqual(res, options.to_object())
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='main.sqlite', table_name='main'): rtn = '' options = dealArgs(args).to_object() words = getAndFilter(options) word_pairs = getPairs(words, True) conn = connect_db(db_name, table_name) if options['clear']: removed_rows = empty_table(conn, table_name) rtn += f'\nRemoved {removed_rows} rows.' modified_rows = add_words(conn, word_pairs, table_name) close_db(conn) if modified_rows <= 0: rtn += f'\nFailed to save anything' else: rtn += f'\nSuccessfully added {modified_rows} words' return rtn
def test_change_stats_override(self): options = dealArgs(args) options.stats = 'foo' self.assertEqual(False, options.stats)
def test_change_stats(self): options = dealArgs(args) options.stats = True self.assertEqual(True, options.stats) self.assertEqual(False, options.format)
def test_change_csv_override(self): options = dealArgs(args) options.csv = True self.assertEqual(True, options.csv) self.assertEqual(False, options.format)
def test_change_output_override(self): options = dealArgs(args) options.output = 'foo' self.assertEqual('foo.json', options.output)
def test_change_file(self): options = dealArgs(args) options.file = 'foo.txt' self.assertEqual('foo.txt', options.file)
def test_change_format_override(self): options = dealArgs(args) options.format = 'foo' self.assertEqual(False, options.format)
def test_change_format(self): options = dealArgs(args) options.format = True self.assertEqual(True, options.format)
def test_change_clear(self): options = dealArgs(args) options.clear = True self.assertEqual(True, options.clear)
def test_change_file(self): options = dealArgs(args) options.files = ['foo.txt'] self.assertEqual(['foo.txt'], options.files)