def generate_text(n: int, align: str = 'full') -> str: """ Generate a fixed width text of n lines. :param n: number of lines of the text :param align: alignment style ie. "full", "left" or "right", default full :return: generated text """ f = { 'full': generate_line_full, 'left': generate_line_left, 'right': generate_line_right }[align] dp = DataParser.factory() text = "" if dp.include_header is True: for i in range(len(dp.column_names)): width = dp.offsets[i] name = dp.column_names[i] if align == "right": text += ' ' * (width - len(name)) + name else: text += name + ' ' * (width - len(name)) text += '\n' for i in range(n - 1): text += f(dp.offsets) + '\n' text += f(dp.offsets) return text
""" Test cases """ from dataparser import DataParser, SPEC_FILE dp = DataParser.factory(SPEC_FILE) print("Test input file with full width fields", end='') dp.parse("tests/test_input1.txt", "tests/_temp_output1.txt", delimiter=',') with open("tests/_temp_output1.txt", 'r') as f: derived_output = f.read() with open("tests/test_output1.txt", 'r') as f: expected_output = f.read() assert derived_output == expected_output print(" ==> passed!") print("Test input file with left aligned fields and blank fields", end='') dp.parse("tests/test_input2.txt", "tests/_temp_output2.txt", delimiter=',') with open("tests/_temp_output2.txt", 'r') as f: derived_output = f.read() with open("tests/test_output2.txt", 'r') as f: expected_output = f.read() assert derived_output == expected_output print(" ==> passed!") print("Test input file with right aligned fields and blank fields", end='') dp.parse("tests/test_input3.txt", "tests/_temp_output3.txt", delimiter=',') with open("tests/_temp_output3.txt", 'r') as f: derived_output = f.read() with open("tests/test_output3.txt", 'r') as f: