Skip to content

Commit c7d2321

Browse files
committed
Add get_output_filename helper function
1 parent 0ddc4e0 commit c7d2321

File tree

2 files changed

+39
-46
lines changed

2 files changed

+39
-46
lines changed

tests/test_helper.py

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -395,63 +395,53 @@ def test_get_output_row(mocker):
395395
assert csv_row == ['bitcoin', '2010-01-01', False, True, 'absolute', 1, False, True, 1, 0]
396396

397397

398-
def test_write_csv_output(mocker):
399-
get_metrics_mock = mocker.patch('tokenomics_decentralization.helper.get_metrics')
400-
get_metrics_mock.return_value = ['hhi']
401-
398+
def test_get_output_filename(mocker):
402399
get_output_directories_mock = mocker.patch('tokenomics_decentralization.helper.get_output_directories')
403400
get_output_directories_mock.return_value = [pathlib.Path(__file__).resolve().parent]
404-
405-
get_clustering_mock = mocker.patch('tokenomics_decentralization.helper.get_clustering_flag')
406401
get_exclude_contracts_mock = mocker.patch('tokenomics_decentralization.helper.get_exclude_contracts_flag')
407-
get_exclude_below_fees_mock = mocker.patch('tokenomics_decentralization.helper.get_exclude_below_fees_flag')
408-
get_exclude_below_usd_cent_mock = mocker.patch('tokenomics_decentralization.helper.get_exclude_below_usd_cent_flag')
409-
get_top_limit_type_mock = mocker.patch('tokenomics_decentralization.helper.get_top_limit_type')
410-
get_top_limit_value_mock = mocker.patch('tokenomics_decentralization.helper.get_top_limit_value')
411-
412-
get_clustering_mock.return_value = True
413402
get_exclude_contracts_mock.return_value = False
403+
get_exclude_below_fees_mock = mocker.patch('tokenomics_decentralization.helper.get_exclude_below_fees_flag')
414404
get_exclude_below_fees_mock.return_value = False
405+
get_exclude_below_usd_cent_mock = mocker.patch('tokenomics_decentralization.helper.get_exclude_below_usd_cent_flag')
415406
get_exclude_below_usd_cent_mock.return_value = False
407+
get_top_limit_type_mock = mocker.patch('tokenomics_decentralization.helper.get_top_limit_type')
416408
get_top_limit_type_mock.return_value = 'absolute'
409+
get_top_limit_value_mock = mocker.patch('tokenomics_decentralization.helper.get_top_limit_value')
417410
get_top_limit_value_mock.return_value = 0
418411

419-
hlp.write_csv_output([
420-
['bitcoin', '2010-01-01', True, False, 'absolute', 0, False, False, 100],
421-
['ethereum', '2010-01-01', True, False, 'absolute', 0, False, False, 200],
422-
])
423-
with open(pathlib.Path(__file__).resolve().parent / 'output.csv') as f:
424-
lines = f.readlines()
425-
assert lines[0] == ','.join(['ledger', 'snapshot_date', 'clustering', 'exclude_contract_addresses',
426-
'top_limit_type', 'top_limit_value', 'exclude_below_fees',
427-
'exclude_below_usd_cent', 'hhi']) + '\n'
428-
assert lines[1] == ','.join(['bitcoin', '2010-01-01', 'True', 'False', 'absolute', '0', 'False', 'False',
429-
'100']) + '\n'
430-
assert lines[2] == ','.join(['ethereum', '2010-01-01', 'True', 'False', 'absolute', '0', 'False', 'False',
431-
'200']) + '\n'
432-
os.remove(pathlib.Path(__file__).resolve().parent / 'output.csv')
412+
output_filename = hlp.get_output_filename()
413+
assert output_filename == pathlib.Path(__file__).resolve().parent / 'output.csv'
433414

434-
get_clustering_mock.return_value = False
435415
get_exclude_contracts_mock.return_value = True
436416
get_exclude_below_fees_mock.return_value = True
437417
get_exclude_below_usd_cent_mock.return_value = True
438-
get_top_limit_type_mock.return_value = 'absolute'
439418
get_top_limit_value_mock.return_value = 10
440419

420+
output_filename = hlp.get_output_filename()
421+
assert output_filename == pathlib.Path(__file__).resolve().parent / 'output-exclude_contract_addresses-absolute_10-exclude_below_fees-exclude_below_usd_cent.csv'
422+
423+
424+
def test_write_csv_output(mocker):
425+
get_metrics_mock = mocker.patch('tokenomics_decentralization.helper.get_metrics')
426+
get_metrics_mock.return_value = ['hhi']
427+
428+
get_output_filename_mock = mocker.patch('tokenomics_decentralization.helper.get_output_filename')
429+
get_output_filename_mock.return_value = pathlib.Path(__file__).resolve().parent / 'output.csv'
430+
441431
hlp.write_csv_output([
442-
['bitcoin', '2010-01-01', False, False, 'absolute', 0, False, False, 100],
443-
['ethereum', '2010-01-01', False, False, 'absolute', 0, False, False, 200],
432+
['bitcoin', '2010-01-01', True, False, 'absolute', 0, False, False, 100],
433+
['ethereum', '2010-01-01', True, False, 'absolute', 0, False, False, 200],
444434
])
445-
with open(pathlib.Path(__file__).resolve().parent / 'output-no_clustering-exclude_contract_addresses-absolute_10-exclude_below_fees-exclude_below_usd_cent.csv') as f:
435+
with open(pathlib.Path(__file__).resolve().parent / 'output.csv') as f:
446436
lines = f.readlines()
447437
assert lines[0] == ','.join(['ledger', 'snapshot_date', 'clustering', 'exclude_contract_addresses',
448438
'top_limit_type', 'top_limit_value', 'exclude_below_fees',
449439
'exclude_below_usd_cent', 'hhi']) + '\n'
450-
assert lines[1] == ','.join(['bitcoin', '2010-01-01', 'False', 'False', 'absolute', '0', 'False', 'False',
440+
assert lines[1] == ','.join(['bitcoin', '2010-01-01', 'True', 'False', 'absolute', '0', 'False', 'False',
451441
'100']) + '\n'
452-
assert lines[2] == ','.join(['ethereum', '2010-01-01', 'False', 'False', 'absolute', '0', 'False', 'False',
442+
assert lines[2] == ','.join(['ethereum', '2010-01-01', 'True', 'False', 'absolute', '0', 'False', 'False',
453443
'200']) + '\n'
454-
os.remove(pathlib.Path(__file__).resolve().parent / 'output-no_clustering-exclude_contract_addresses-absolute_10-exclude_below_fees-exclude_below_usd_cent.csv')
444+
os.remove(pathlib.Path(__file__).resolve().parent / 'output.csv')
455445

456446

457447
def test_get_active_source_keywords(mocker):

tokenomics_decentralization/helper.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -485,24 +485,17 @@ def get_output_row(ledger, date, metrics):
485485
return csv_row
486486

487487

488-
def write_csv_output(output_rows):
488+
def get_output_filename():
489489
"""
490-
Produces the output csv file for the given data.
491-
:param output_rows: a list of lists, where each list corresponds to a line in the output csv file
490+
Produces the name (full path) of the output file.
491+
:returns output_filename: a pathlib path of the output file
492492
"""
493-
header = ['ledger', 'snapshot_date', 'clustering', 'exclude_contract_addresses', 'top_limit_type',
494-
'top_limit_value', 'exclude_below_fees', 'exclude_below_usd_cent']
495-
header += get_metrics()
496-
497-
clustering = get_clustering_flag()
498493
exclude_contract_addresses_flag = get_exclude_contracts_flag()
499494
top_limit_type = get_top_limit_type()
500495
top_limit_value = get_top_limit_value()
501496
exclude_below_fees_flag = get_exclude_below_fees_flag()
502497
exclude_below_usd_cent_flag = get_exclude_below_usd_cent_flag()
503498
output_filename = 'output'
504-
if not clustering:
505-
output_filename += '-no_clustering'
506499
if exclude_contract_addresses_flag:
507500
output_filename += '-exclude_contract_addresses'
508501
if top_limit_value:
@@ -512,9 +505,19 @@ def write_csv_output(output_rows):
512505
if exclude_below_usd_cent_flag:
513506
output_filename += '-exclude_below_usd_cent'
514507
output_filename += '.csv'
508+
return get_output_directories()[0] / output_filename
509+
510+
511+
def write_csv_output(output_rows):
512+
"""
513+
Produces the output csv file for the given data.
514+
:param output_rows: a list of lists, where each list corresponds to a line in the output csv file
515+
"""
516+
header = ['ledger', 'snapshot_date', 'clustering', 'exclude_contract_addresses', 'top_limit_type',
517+
'top_limit_value', 'exclude_below_fees', 'exclude_below_usd_cent']
518+
header += get_metrics()
515519

516-
output_dir = get_output_directories()[0]
517-
with open(output_dir / output_filename, 'w') as f:
520+
with open(get_output_filename(), 'w') as f:
518521
csv_writer = csv.writer(f)
519522
csv_writer.writerow(header)
520523
csv_writer.writerows(output_rows)

0 commit comments

Comments
 (0)