-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.py
More file actions
405 lines (306 loc) · 11.5 KB
/
Copy pathlib.py
File metadata and controls
405 lines (306 loc) · 11.5 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import sys
import re
from StringIO import StringIO
from arbol import Arbol, Nodo
## Constantes
TAB = ' '
## Funciones
def nivelar(nivel, texto):
texto = texto.replace('\n', '\n' + TAB * nivel)
return "%s%s\n" % (TAB * nivel, texto)
## Clases
class Controlador(object):
def __init__(self):
super(Controlador, self).__init__()
self.contenedor = ErdosBloquePrincipal()
self.arbol = Arbol(self.contenedor)
def agregar_sentencia(self, target, texto):
destino = target.get_contenedor()
self.agregar_figura(destino, ErdosSentencia(destino, texto))
def agregar_entrada(self, target, variable, texto):
destino = target.get_contenedor()
self.agregar_figura(destino, ErdosEntrada(destino, variable, texto))
def agregar_salida(self, target, variable, texto):
destino = target.get_contenedor()
self.agregar_figura(destino, ErdosSalida(destino, variable, texto))
def agregar_if(self, target, texto):
destino = target.get_contenedor()
self.agregar_figura(destino, ErdosIf(destino, texto))
def agregar_for(self, target, n_var, r_max, r_min):
destino = target.get_contenedor()
self.agregar_figura(destino, ErdosFor(destino, n_var, r_max, r_min))
def agregar_while(self, target, texto):
destino = target.get_contenedor()
self.agregar_figura(destino, ErdosWhile(destino, texto))
def agregar_until(self, target, texto):
destino = target.get_contenedor()
self.agregar_figura(destino, ErdosUntil(destino, texto))
def agregar_case(self, target, texto):
destino = target.get_contenedor()
self.agregar_figura(destino, ErdosCase(destino, texto))
def agregar_figura(self, destino, figura):
destino.insertar(figura)
class ErdosBase(Nodo):
def __init__(self, padre=None):
super(ErdosBase, self).__init__()
self.padre = padre
def get_uid(self):
return self.uid
def get_contenedor(self):
return self.padre.get_contenedor()
def borrar(self):
if self.padre:
self.padre.hijos.remove(self)
def gen_cod(self, nivel=0):
return nivelar(nivel, ("id_ultimo = %s;" % self.uid) + self.texto)
def ejecutar(self):
self.codigo = """
from __future__ import division
import sys
from PyQt4 import QtGui
id_ultimo = 0
"""
self.codigo += self.gen_cod()
salida = StringIO()
sys.stdout = salida
fallo = 0
self.locales = {}
self.globales = {}
try:
exec(self.codigo, self.globales, self.locales)
except SystemExit:
print "salida forzada..."
except Exception as inst:
tipo = type(inst)
if tipo is SyntaxError:
print "Error de sintaxis."
matcheo = re.match(
"(.+)id_ultimo = (\d+);(.+)", str(inst.args[1])
)
if matcheo:
fallo = int(matcheo.groups()[1])
elif tipo is TypeError:
print "Error de tipos."
elif tipo is NameError:
print "Error de nombres."
elif tipo is IndexError:
print "Error de indices."
elif tipo is ZeroDivisionError:
print "Error de division por cero."
print inst.args
if not fallo:
fallo = self.locales.get('id_ultimo') or \
self.globales.get('id_ultimo')
# restauramos el stdout original
sys.stdout = sys.__stdout__
# los hacemos None por la serializacion
print self.locales
print self.globales
self.locales = None
self.globales = None
return salida, fallo
class ErdosRect(ErdosBase):
def __init__(self, padre):
super(ErdosRect, self).__init__(padre)
def set_texto(self, texto):
self.texto = texto or \
self.__class__.__name__ + ":" + str(self.get_uid())
class ErdosSentencia(ErdosRect):
def __init__(self, padre, texto):
super(ErdosSentencia, self).__init__(padre)
self.editar(texto)
def editar(self, texto):
self.set_texto(texto)
class ErdosEntrada(ErdosRect):
def __init__(self, padre, variable, tipo_dato, texto_dialogo=None):
super(ErdosEntrada, self).__init__(padre)
self.editar(variable, tipo_dato, texto_dialogo)
def editar(self, variable, tipo_dato, texto_dialogo=None):
self.variable = variable
self.tipo_dato = tipo_dato
self.texto_dialogo = texto_dialogo or "Ingrese el valor de " + variable
self.texto_dibujo = '%s <-- "%s"' % (variable, self.texto_dialogo)
self.set_texto(self.texto_dibujo)
def gen_cod(self, nivel=0):
formato = """var, ok = QtGui.QInputDialog.getText(None, 'Erdos', '%s')
tipo_dato = '%s'
if tipo_dato == "Numerico":
var = float(var)
elif tipo_dato == "Booleano":
var = bool(var)
else:
var = str(var)
%s = var;
if not ok:
raise SystemExit"""
return nivelar(nivel,
formato % (
self.texto_dialogo, self.tipo_dato, self.variable
)
)
class ErdosSalida(ErdosRect):
FORMATO_PROMPT = "\nQtGui.QMessageBox.information(None, 'Erdos',"
def __init__(self, padre, variable, texto_dialogo=None, prompt=False):
super(ErdosSalida, self).__init__(padre)
self.editar(variable, texto_dialogo, prompt)
def editar(self, variable, texto_dialogo=None, prompt=False):
self.variable = variable or None
self.texto_dialogo = texto_dialogo or "Valor de %s:" % variable
if self.variable:
formato = '"%s" --> %s'
valores = (self.texto_dialogo, self.variable)
else:
formato = '"%s" -->'
valores = self.texto_dialogo
self.texto_dibujo = formato % valores
self.prompt = prompt
self.set_texto(self.texto_dibujo)
def gen_cod(self, nivel=0):
if self.variable:
formato = """id_ultimo = %s; print "%s", %s"""
valores = self.uid, self.texto_dialogo, self.variable
if self.prompt:
valores.extend((self.texto_dialogo, self.variable))
formato += self.FORMATO_PROMPT + """"%s " + str(%s))"""
else:
valores = self.uid, self.texto_dialogo
formato = """id_ultimo = %s; print "%s" """
if self.prompt:
valores.append(self.texto_dialogo)
formato += self.FORMATO_PROMPT + "'%s')"
return nivelar(nivel, formato % tuple(valores))
class ErdosBloque(ErdosRect):
def __init__(self, padre=None):
super(ErdosBloque, self).__init__(padre)
def get_contenedor(self):
return self
def gen_cod(self, nivel=0):
codigo = ""
if not self.hijos:
codigo = nivelar(nivel + 1, "pass")
for i in self.hijos:
codigo += i.gen_cod(nivel + 1)
return codigo
class ErdosBloquePrincipal(ErdosBloque):
def __init__(self):
super(ErdosBloquePrincipal, self).__init__(None)
self.uid = 1
def parentItem(self): # no sirve mas?
return self.item.parentItem()
def get_uid(self):
return 1
def gen_cod(self):
codigo = ""
for i in self.hijos:
codigo += i.gen_cod(0)
return codigo
class ErdosIf(ErdosRect):
def __init__(self, padre, condiciones):
super(ErdosIf, self).__init__(padre)
self.editar(condiciones)
self.inicializar()
def editar(self, condiciones):
self.condiciones = condiciones
self.set_texto(self.gen_texto())
def gen_texto(self):
return "Si (%s)" % self.condiciones.get_texto()
def inicializar(self):
self.lado_true = ErdosBloque(self)
self.lado_false = ErdosBloque(self)
self.agregar_hijo(self.lado_true)
self.agregar_hijo(self.lado_false)
def gen_cod(self, nivel=0):
codigo = nivelar(nivel,
"id_ultimo = %s\nif %s:" % (self.uid, self.condiciones.get_texto())
)
codigo += self.lado_true.gen_cod(nivel)
codigo += nivelar(nivel, "else:")
codigo += self.lado_false.gen_cod(nivel)
return codigo
class ErdosBucle(ErdosRect):
def __init__(self, padre):
super(ErdosBucle, self).__init__(padre)
self.bucle = ErdosBloque(self)
self.agregar_hijo(self.bucle)
class ErdosFor(ErdosBucle):
FORMATO_COD = "for %s in range(int(%s), int(%s)):"
def __init__(self, padre, nom_var, r_max, r_min=1):
super(ErdosFor, self).__init__(padre)
self.editar(nom_var, r_max, r_min)
def editar(self, nom_var, r_max, r_min):
self.nom_var = nom_var
self.rango_minimo = r_min
self.rango_maximo = r_max
self.set_texto(self.gen_text())
def gen_text(self):
return "Para %s <- de %s a %s hacer" % (
self.nom_var, self.rango_minimo, self.rango_maximo
)
def gen_cod(self, nivel=0):
codigo = nivelar(nivel, self.FORMATO_COD %
(self.nom_var, self.rango_minimo, self.rango_maximo + " + 1")
)
codigo += self.bucle.gen_cod(nivel)
return codigo
class ErdosWhile(ErdosBucle):
def __init__(self, padre, condiciones):
super(ErdosWhile, self).__init__(padre)
self.editar(condiciones)
def editar(self, condiciones):
self.condiciones = condiciones
self.set_texto(self.gen_texto())
def gen_texto(self):
return "Mientras (%s) hacer" % self.condiciones.get_texto()
def gen_cod(self, nivel=0):
condicion = self.condiciones.get_texto()
codigo = nivelar(nivel,
"id_ultimo = %s\nwhile %s:" % (self.uid, condicion)
)
codigo += self.bucle.gen_cod(nivel)
return codigo
class ErdosUntil(ErdosWhile):
def __init__(self, padre, condiciones):
super(ErdosUntil, self).__init__(padre, condiciones)
def gen_texto(self):
return "Hasta que (%s) repetir" % self.condiciones.get_texto()
def gen_cod(self, nivel=0):
condicion = self.condiciones.get_texto()
codigo = nivelar(nivel, "id_ultimo = %s\nwhile True:" % self.uid)
codigo += self.bucle.gen_cod(nivel)
codigo += nivelar(nivel + 1, "if %s: break" % condicion)
return codigo
class ErdosCase(ErdosRect):
def __init__(self, padre, nom_var, r_min, r_max):
super(ErdosCase, self).__init__(padre)
self.casos = {}
self.editar(nom_var, r_min, r_max)
def editar(self, nom_var, r_min, r_max):
self.nom_var = nom_var
self.rango_minimo = int(r_min)
self.rango_maximo = int(r_max)
self.set_texto(self.gen_texto())
self.inicializar()
self.default = ErdosBloque(self)
def inicializar(self):
for i in range(self.rango_minimo, self.rango_maximo + 1):
cont = ErdosBloque(self)
self.casos[i] = cont
self.agregar_hijo(cont)
def gen_texto(self):
return "Si (%s) vale" % self.nom_var
def gen_cod(self, nivel=0):
codigo = ""
primero = True
for i in self.casos.keys():
if primero:
code = 'if'
primero = False
else:
code = 'elif'
codigo += nivelar(nivel, "%s %s == %s:" %
(code, self.nom_var, i)
)
codigo += self.casos[i].gen_cod(nivel)
codigo += nivelar(nivel, "else:")
codigo += self.default.gen_cod(nivel)
return codigo