-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
141 lines (110 loc) · 2.79 KB
/
utils.py
File metadata and controls
141 lines (110 loc) · 2.79 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
'''
utils
'''
from os import system, name
from math import floor
def validate_yes_no(value):
'''
Checks to make sure user typed expected response.
Acceptable ['y','ye','yes','n','no'], return True
Else return False.
'''
if value.lower() in ['y', 'ye', 'yes', 'n', 'no']:
return True
print_error_message("Invalid input.")
return False
def validate_input(value, max_value):
'''
Inside the try, converts input string value into integer.
Raises ValueError if strings cannot be converted into int,
or if outside the expected range.
'''
try:
try:
int_value = int(value)
except TypeError:
print_error_message("Invalid input.")
return False
if int_value >= 0 and int_value <= int(max_value):
return True
raise ValueError()
except ValueError:
print_error_message("Invalid input.")
return False
return True
def print_current_balance(stats):
'''
Print cash balance.
'''
return f'£{"{:.2f}".format(floor(stats["cash"]*100)/100)}'
def print_error_message(data):
'''
Function to provide error message received (data)
'''
if data:
text = red(data)
else:
text = red('Error.')
print(f'{text}')
print_press_enter_to("Press Enter to retry...")
clear_terminal()
def clear_terminal():
'''
Clears terminal for better user experience
'''
system('cls' if name == 'nt' else 'clear')
def print_press_enter_to(text):
'''
Print "Press Enter....
'''
input(orange(f'\n{text}'))
def print_go_back():
'''
Print 0. Go Back in yellow
'''
print(f"\n{yellow('0. Go Back')}")
def pink(text):
'''
Changes to PINK if printed to terminal
'''
return coloured(255, 105, 180, text)
def gold(text):
'''
Changes to GOLD if printed to terminal
'''
return coloured(255, 215, 0, text)
def green(text):
'''
Changes to GREEN if printed to terminal
'''
return coloured(50, 205, 50, text)
def cyan(text):
'''
Changes to CYAN if printed to terminal
'''
return coloured(0, 255, 255, text)
def orange(text):
'''
Changes to ORANGE if printed to terminal
'''
return coloured(255, 165, 0, text)
def red(text):
'''
Changes to RED if printed to terminal
'''
return coloured(255, 0, 0, text)
def yellow(text):
'''
Changes to YELLOW if printed to terminal
'''
return coloured(255, 255, 0, text)
# Credit:
# https://www.codegrepper.com/code-examples
# /python/how+to+color+text+in+python+3
def coloured(c_red, c_green, c_blue, text):
'''
Allows to change text colour
'''
return "\033[38;2;{};{};{}m{}\033[38;2;255;255;255m".format(
c_red, c_green, c_blue, text
)