Skip to content

Commonutils

File Name: commonUtils.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import pprint

def timeComplexity(value, desc):
    result = f'\n 🕒 Time Complexity: Approximately {value}'

    if (desc):
        result += f'\n    Description: {desc}'
    print(result)


def spaceComplexity(value, desc):
    result = f' 💾 Space Complexity: Approximately {value}'

    if (desc):
        result += f'\n    Description: {desc}'
    print(result)


def print_h1(text):
    print(f"\n{'=' * 40}\n{text}\n{'=' * 40}")


def print_h2(text):
    print(f"\n{'-' * 35}\n{text}\n{'-' * 35}")


def print_h3(text):
    print(f"\n{text}\n{'-' * 30}")


def print_h4(*text):
    combined_text = " ".join(str(t) for t in text)  # Join all elements of text into one string
    print(f"\n{combined_text}\n{'-' * 25}")

def print_h5(text):
    print(f"\n{text}\n{'-' * 20}")

def print_h6(text):
    print(f"\n{text}\n{'-' * 15}")

def print_span(*text):
    combined_text = " ".join(str(t) for t in text)  # Join all elements of text into one string
    # Using ANSI escape code for bold text
    print(f"\033[1m{combined_text}\033[0m")

def print_ordered_list(items):
    for i, item in enumerate(items, start=1):
        print(f"{i}. {item}")


def print_bullet_list(items, bullet_char='*'):
    for item in items:
        print(f"{bullet_char} {item}")


def print_blockquote(items, indent_char='> ', emoji='💬', new_line = False):
    if (new_line):
        print('\n')
    for item in items:
        print(f"{emoji} {indent_char}{item}")


def pretty_json(input):
    pprint.pprint(input, width=50, indent=2)


def print_tabular_list(data, col_widths = [15, 45]):
    """
    Print data in a tabular format with horizontal lines, differentiating the header.

    :param data: List of tuples, where each tuple represents a row.
    :param col_widths: List of integers representing the width of each column.
    """
    def print_divider():
        # Create and print a divider line with '+'
        line = "+" + "+".join("-" * width for width in col_widths) + "+"
        print(line)

    def print_header_divider():
        # Create and print a simple divider line without '+' for the header
        line = "-" * (sum(col_widths) + len(col_widths) - 1)
        print(line)

    # Print the header row
    print_divider()
    header = data[0]
    formatted_header = "|".join(
        f"{item:<{col_widths[i]}}" for i, item in enumerate(header))
    print("|" + formatted_header + "|")
    print_header_divider()

    # Print the rest of the rows
    for row in data[1:]:
        formatted_row = "|".join(
            f"{item:<{col_widths[i]}}" for i, item in enumerate(row))
        print("|" + formatted_row + "|")
        print_header_divider()

Documentation

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Print data in a tabular format with horizontal lines, differentiating the header.

:param data: List of tuples, where each tuple represents a row.
:param col_widths: List of integers representing the width of each column.
def print_divider():
    # Create and print a divider line with '+'
    line = "+" + "+".join("-" * width for width in col_widths) + "+"
    print(line)

def print_header_divider():
    # Create and print a simple divider line without '+' for the header
    line = "-" * (sum(col_widths) + len(col_widths) - 1)
    print(line)

# Print the header row
print_divider()
header = data[0]
formatted_header = "|".join(
    f"{item:<{col_widths[i]}}" for i, item in enumerate(header))
print("|" + formatted_header + "|")
print_header_divider()

# Print the rest of the rows
for row in data[1:]:
    formatted_row = "|".join(
        f"{item:<{col_widths[i]}}" for i, item in enumerate(row))
    print("|" + formatted_row + "|")
    print_header_divider()