-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdell-server-code.py
More file actions
171 lines (154 loc) · 5.85 KB
/
dell-server-code.py
File metadata and controls
171 lines (154 loc) · 5.85 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/usr/bin/env python3
# Import libraries that will be used in this script
import re
import os
def main():
# This is a dictionary:
char1 = {
"C": "Compute optimized hyper-scale server",
"F": "Rack-based sleds for FX2/FX2s enclosure",
"M": "Modular Blade server",
"MX": "Modular Blade server",
"R": "Rack-mountable",
"XE": "Extreme performance and storage",
"XR": "Extreme ruggedness"
}
# This is a list:
char2 = [
"NA",
"1 Socket",
"1 Socket",
"1 Socket",
"2 Sockets",
"2 Sockets",
"2 Sockets",
"2 Sockets",
"2 or 4 sockets",
"4 Sockets"
]
# This is a list
char3 = [
"Gen 10",
"Gen 11",
"Gen 12",
"Gen 13",
"Gen 14",
"Gen 15",
"Gen 16",
"Gen 17",
"Gen 18",
"Gen 19"
]
# This is a dictionary
char4 = {
"0": "Intel",
"5": "AMD"
}
os.system('clear')
code = input("Enter a server code: ")
# PARSING THE USER INPUT START #################################################
# This REGEX code snippet is *very* common for parsing input. Use it often!
# This pattern is looking for letters only, followed by three digits
# c1 will capture letters, and; c2, c3, and c4 will each collect a single digit
# code = "R450" would be an example of a MATCH
# code = "wxq3" would NOT match
# Could we write a better pattern, YOU BET! Go for it.
pattern = re.compile(r"""
(?P<c1>^[a-zA-Z][a-zA-Z]?)
(?P<c2>\d)
(?P<c3>\d)
(?P<c4>\d)
""", re.VERBOSE)
match = pattern.match(code)
if match:
# If we get this far, the input matched the REGEX
code1 = match.group("c1").upper()
code2 = int(match.group("c2"))
code3 = int(match.group("c3"))
code4 = match.group("c4")
# PARSING THE USER INPUT END #####################################################
else:
# If this runs, the user entered an invalid code, so output help info.
# FIRST ERROR HANDLING OUTPUT STARTS HERE
print(f"The first character is the chassis type")
print(f"---------------------------------------------")
# Use the ".keys" method to lookup the key of each dictionary item
for key in char1.keys():
print (f"{key} = {char1[key]}")
print(f"\nThe second character is the socket count")
print(f"---------------------------------------------")
# Use the idex value (0-9) to reference each item in a list
i=0
for item in char2:
print (f"{i} = {item}")
i +=1
print(f"\nThe third character is the generation")
print(f"---------------------------------------------")
# Use the idex value (0-9) to reference each item in a list
i=0
for item in char3:
print (f"{i} = {item}")
i +=1
print(f"\nThe fourth character is the CPU type")
print(f"---------------------------------------------")
# Use the ".keys" method to lookup the key of each dictionary item
for key in char4.keys():
print (f"{key} = {char4[key]}")
print(f"---------------------------------------------\n")
print(f"You entered \"{code}\" which is NOT a dell server code\n")
print("You must enter a valid code like r450 or mx345\n")
print("Reference the above and PLEASE TRY AGAIN\n")
# NOTE THE NEXT LINE = EXIT!
exit() # No use continuing. Exit the script.
# FIRST ERROR HANDLING ENDS HERE
# If we got this far, all four codes passed the regex test,
# but additional validity testing is necessary
print(f"Looking up Dell {code1}{code2}{code3}{code4}")
if code1 in char1:
# If code1 can be found in the code1 dictionary, it is valid, so print it.
print(f" {code1} = {char1[code1]}")
else:
# Else the code was not found so start error handling
#c ode1 ERROR HANDLING STARTS
print(f"{code1} is an invalid, valid codes are:")
for key in char1.keys():
print (f" {key} = {char1[key]}")
#code1 ERROR HANDLING ENDS
if 1 <= code2 <= 9:
# If code2 is a single digit 1-9, it is valid, print it
print(f" {code2} = {char2[code2]}")
else:
# Else it is not valid, so handle the error
#code2 ERROR HANDLING STARTS
print(f" {code2} is invalid. Valid CPU socket codes are:")
i=0
for item in char2:
print (f" {i} = {item}")
i +=1
#code2 ERROR HANDLING ENDS
if 0 <= code3 <= 9:
# If code3 is a single digit 0-9, it is valid, print it
print(f" {code3} = {char3[code3]}")
else:
# Hmmm, this should NEVER happen, but let's write a defensive
# error handler in spite of that.
#code3 ERROR HANDLING STARTS
print(f"error: {code3} Must be 0 to 9")
i=0
for item in char3:
print (f"{i} = {item}")
i +=1
#code3 ERROR HANDLING ENDS
if code4 in char4:
# If code4 is found, print it
# Wow, error handling with dictionaries seems easier!
print(f" {code4} = {char4[code4]}")
else:
# else the code was not found, so handle the error
#code4 ERROR HANDLING STARTS
print(f" {code4} is invalid, here are valid options:")
for key in char4.keys():
print (f" {key} = {char4[key]}")
#code4 ERROR HANDLING ENDS
if __name__ == "__main__":
main()