Skip to content

Instantly share code, notes, and snippets.

@Tuhin-thinks
Last active March 9, 2024 17:53
Show Gist options
  • Save Tuhin-thinks/8fe85920b784caf5a1209e503f9beb65 to your computer and use it in GitHub Desktop.
Save Tuhin-thinks/8fe85920b784caf5a1209e503f9beb65 to your computer and use it in GitHub Desktop.
Function to print header along with list of list in tabular format.
import re
def print_formatted(_header, _items, padding=10, alignment="^"):
"""
Function to print header along with list of list in tabular format.
:param alignment: ^ -center alignment; < -left alignment; > -right alignment
:param _header: header strings for the table
:param _items: list of list, representing collection of rows
:param padding: padding count
:return:
"""
header_formatter = ""
for _ in _header:
header_formatter += "{:_" + alignment + str(padding) + "}|"
print("| " + header_formatter.format(*[_head for _head in _header]))
# after header horizontal line for separation in markdown
print("|", end=" ")
for _ in _header:
print(("-" * padding), end="|")
print()
for item_info in _items:
format_row = ("{:" + alignment + str(padding) + "}|") * len(item_info)
print("| " + format_row.format(*item_info))
print()
def parse_list_from_string(input_string):
"""
Function to parse the list from the given string.
:param input_string: input string
:return: list of items
"""
lines = input_string.split("\n")
items = [re.split(r'\s', line.strip()) for line in lines]
return items
def accept_multiline_input():
string = ""
while True:
try:
input_string = input()
if input_string != "end":
string += "\n" + input_string
else:
return string.strip()
except Exception as e:
print(f"Error: {e}")
if __name__ == '__main__':
# headers = ['Item no', 'Item name', 'Price', 'Unit']
# items_list = ["01", "Banana", 5.00, "unit", "02", "Mango", 20.00, "kg", " 03", "Apple", 15.00, "kg", " 04",
# "Papaya", 25.00, "unit", "5", "Guava", 15.00]
#
# row_width = 4 # max number of items in each row
# clustered_price_list = [items_list[n:n + row_width] for n in range(0, len(items_list), row_width)]
headers = input("Enter the header: ").split()
clustered_list = parse_list_from_string(accept_multiline_input())
print(clustered_list)
print_formatted(_items=clustered_list, _header=headers, padding=20)
# Example:
# print_formatted(_items=clustered_price_list, _header=header, padding=20)
#
# Output:
# ______Item no____________Item name_____________Price________________Unit________
# 01 Banana 5.0 unit
# 02 Mango 20.0 kg
# 03 Apple 15.0 kg
# 04 Papaya 25.0 unit
# 5 Guava 15.0
# Output:
# | _______Start________|________End_________|________Time________|
# | --------------------|--------------------|--------------------|
# | 1 | 2000 |0.0032053990000804333|
# | 100 | 10000 |0.013571235999961573|
# | 20000 | 80000 |0.21431415500001094 |
# | 100000 | 2000000 | 16.89189469400003 |
# | 2000000 | 9000000 | 108.93620239800009 |
# | 10000000 | 100000000 | 38.168661233999956 |
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment