-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtakeinput.py
150 lines (125 loc) · 4.49 KB
/
takeinput.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
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
from termcolor import colored
def get_input(param_name):
''' Takes input from the user '''
interact = colored('Please enter the value of the parameter', 'green') + ', ' + colored(param_name, 'red') + ':'
param = input(interact)
return param
def set_input(args):
''' Returns a dict input_values containing all input values '''
input_values = argshandler(args)
keys = input_values.keys()
if 'span' not in keys:
span = float(get_input('span'))
input_values.update({'span':span})
if 'supports' not in keys:
no_of_supports = int(get_input('no of supports'))
supports = []
for i in range(no_of_supports):
print('FOR SUPPORT '+str(i+1))
support = [get_input('support type'),
0,
float(get_input('support position'))]
supports.append(support)
input_values.update({'supports':supports})
if 'UDLs' not in keys:
no_of_UDLs = int(get_input('no of UDLs'))
UDLs = []
for i in range(no_of_UDLs):
print('FOR UDL ' + str(i+1))
UDL = [float(get_input('UDL value')),
float(get_input('UDL start')), float(get_input('UDL end'))]
UDLs.append(UDL)
input_values.update({'UDLs':UDLs})
if 'point_loads' not in keys:
no_of_point_loads = int(get_input('no of point loads'))
point_loads = []
for i in range(no_of_point_loads):
print('FOR POINT LOAD '+str(i+1))
point_load = [float(get_input('point load value')),
float(get_input('point load position'))]
point_loads.append(point_load)
input_values.update({'point_loads':point_loads})
return input_values
def get_support_value(supports):
''' Returns the total support value '''
support_value = 0
for support in supports:
if(support[0]=='pin'):
support_value +=1
elif(support[0]=='fixed'):
support_value +=3
elif(support[0]=='roller'):
support_value +=7
return support_value
def validate_input(input_values):
''' Validates input based on support input '''
supports = input_values.get('supports')
span = input_values.get('span')
if(len(supports)>2):
return False
for support in supports:
if(support[0]=='pin'):
pass
elif(support[0]=='fixed'):
pass
elif(support[0]=='roller'):
pass
else:
return False
if(float(support[2])>float(span)):
return False
support_value = get_support_value(supports)
if(support_value in (2,3,8,14)):
pass
else:
return False
return True
def argshandler(args):
input_values = {}
for arg in args:
if '-l=' in arg:
input_values.update({'span':int(arg[3:])})
elif '-s=' in arg:
input_values.update({'supports':listconvert(arg[3:])})
elif '-p=' in arg:
input_values.update({'point_loads':listconvert(arg[3:])})
elif '-u=' in arg:
input_values.update({'UDLs':listconvert(arg[3:])})
elif '-f=' in arg:
input_values.update(fileconvert(arg[3:]))
return input_values
def listconvert(string):
bracket_count = 0
input_list = []
list_element = []
element = ''
for piece in string:
if piece == '[':
bracket_count+=1
elif piece == ']':
bracket_count-=1
elif bracket_count == 1 and not piece==',':
element += piece
elif bracket_count ==1 and piece ==',':
list_element.append(element)
element =''
if bracket_count == 0 and not piece==',':
list_element.append(element)
input_list.append(list_element)
element = ''
list_element = []
return input_list
def fileconvert(textfile):
input_values = {}
f = open(textfile, 'r')
lines = f.readlines()
for line in lines:
if 'span=' in line:
input_values.update({'span':line.strip('span=')[:-1]})
elif 'point_loads=' in line:
input_values.update({'point_loads':listconvert(line.strip('point_loads=')[:-1])})
elif 'UDLs=' in line:
input_values.update({'UDLs':listconvert(line.strip('UDLs=')[:-1])})
elif 'supports=' in line:
input_values.update({'supports':listconvert(line.strip('supports=')[:-1])})
return input_values