- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloopmake.py
More file actions
Latest commit
177 lines (151 loc) · 4.83 KB
/
Copy pathloopmake.py
File metadata and controls
177 lines (151 loc) · 4.83 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
172
173
174
175
176
177
#!/usr/bin/env python
# $Id: loopmaker.py,v 1.7 2000/04/22 01:44:26 wesc Exp $
#
# loopmaker.py -- CASE script: creates Python loops given parameters
#
# created on 00/04/21 by wesc
#
fromkeywordimportiskeyword
# setup exec_str dictionary
exec_dict= {
# FOR loop
'f': '''
for %s in %s:
print %s
''',
# WHILE loop (sequences)
's': '''
%s = 0
%s = %s
while %s < len(%s):
print %s[%s]
%s = %s + 1
''',
# WHILE loop (numbers)
'n': '''
%s = %d
while %s < %d:
print %s
%s = %s + %d
'''
}
dashes='\n'+ ('-'*50)
defdashedprint(data, dashes=dashes):
printdata+dashes
defmain():
# prompt for loop type
while1:
ltype=raw_input('Loop type? ([F]or/While) ')
ifnotltype: ltype='f'
ifltype[0] in'fw':
break
# prompt for data type
while1:
dtype=raw_input('Data type? ([N]umber/Sequence [str,list,tuple]) ')
ifnotdtype: dtype='n'
ifdtype[0] in'ns':
break
# NUMBER type
ifdtype=='n':
while1:
start=raw_input('Starting value? [0] ')
ifnotstart:
start=0
else:
start=int(start)
while1:
stop=input('Ending value (non-inclusive)? ')
iftype(stop) !=type(0):
print'*** Ending value must be an integer\n'
else:
break
step=raw_input('Stepping value? [1] ')
ifnotstep:
step=1
else:
step=int(step)
seq=str(range(start, stop, step))
ifseq=='[]':
ans=raw_input('*** Invalid range; change your values? ([y]/n) ')
ifnotansorans[0] =='y':
continue
print
break
# SEQUENCE type
else:
while1:
seq=raw_input('Enter sequence (string, list, or tuple): ')
try:
test=eval(seq)
# invalid sequence... reprompt
exceptSyntaxError:
# keyword can be a string
ifiskeyword(seq):
test=seq=`seq`
else:
print"*** Invalid Python sequence data '%s'\n"%seq
continue
# user typed in string without the quotes... add the quotes using repr()
exceptNameError:
test=seq=`seq`
# must be string, list, or tuple
iftype(test) !=type('') andtype(test) !=type([]) andtype(test) !=type(()):
print"*** Invalid Python sequence data '%s'\n"%seq
continue
break
# iterator variable
while1:
var=raw_input('Enter iterative variable name? ')
ifnotvar:
print'*** Identifier name is required!\n'
elifiskeyword(var) orvarindir(__builtins__):
print"*** Identifier '%s' name is a keyword or built-in!\n"%var
else:
try:
execvar+'=0'
exceptSyntaxError:
print"*** Invalid identifier '%s'!\n"%var
continue
break
# FOR loop
ifltype=='f':
exec_str=exec_dict['f'] % (var, seq, var)
# WHILE loop
elifltype=='w':
# sequence length variable (only for sequences)
ifdtype=='s':
while1:
svar=raw_input('Enter sequence name? ')
ifnotsvar:
print'*** Identifier name is required for while loops!\n'
elifiskeyword(svar) orsvarindir(__builtins__):
print"*** Identifier '%s' name is a keyword or built-in!\n"%svar
else:
break
else:
svar=None
# must assign to sequence var if using sequences
ifdtype=='s':
exec_str=exec_dict['s'] % (var, svar, seq, var, svar, svar, var, var, var)
# use range() values for loop if using numbers
elifdtype=='n':
exec_str=exec_dict['n'] % (var, start, var, stop, var, var, var, step)
# EXECUTE code
dashedprint('')
dashedprint('The custom-generated code for you is:')
dashedprint(exec_str)
dashedprint('Test execution of the code:')
execexec_str
dashedprint('')
# top-level executes main()
if__name__=='__main__':
print'Welcome to Loop Maker v1.0\n'
while1:
main()
try:
again=raw_input('Try again? ([Y]es/No) ')
except:
print
break
ifagainandagain[0] !='y':
break