TCL code producing python code

Manuel Gutierrez Algaba algaba at my-deja.com
Sat Sep 18 04:58:00 EDT 1999


Usually there're flame wars between TCL and python.

Today I've found a nice way of working together to get the
easiest.

I use my consdiag to generate lots of classes. The problem is
that I have to write a lot of python code. But using TCL is much
easier, for example this code:
#!/usr/bin/tcl

proc escribe {t} {
    puts $t
}

proc escribenonl {t} {
    puts -nonewline $t
}

proc escribecoma {t} {
    puts "$t,"
}

proc gensimple {t modulo clase atributos} {
    escribe "def genera_$t\(con_atributos =None, con_funciones=None):"
    escribe "\tc = a_simple_class()"
    escribe "\tc.do_name_clase($modulo.$clase.__name__)"
    escribenonl "\tc.do_attributes(\["

    foreach i $atributos  {
        escribecoma "\'$i\'"
    }
    escribe "\])"
    escribe "\tif not con_funciones is None:"
    escribe "\t\tc.do_functions\($modulo.$clase\)"
    escribe "\tc.do_lines\(\)"
    escribe "\treturn c"
}

escribe "from consdiag import *
import dibcla
import nucleo
import serv_sok"

gensimple nucleo nucleo nucleo [list puerto_clips puerto_visualizador \
        temporizador mapa_X mapaY]


proc genera_acceso { t n} {
    escribe "\tv=genera_$t\(\)"
    escribe "\tv.generate_latex_code\(\'result$n.tex\'\)"
}

escribe "def genera_fic():"

genera_acceso nucleo 1


escribe "if __name__=='__main__':
\tgenera_fic()
"

Produces a bit of :

from consdiag import *
import dibcla
import nucleo
import serv_sok

def genera_nucleo(con_atributos = None, con_funciones = None):
    c = a_simple_class()
    c.do_name_clase(nucleo.nucleo.__name__)
    c.do_attributes(['puerto_clips', 'puerto_visualizador',
                     'temporizador', 'mapa_X',
                     'mapa_Y', 'mapa_Z',
                     'periodo_actualizacion_mapa', 'dimensiones',
                     'turnador', 'interprete',
                     'maximo_ciclos', 'dicc_jugadores'])
    if not con_funciones is None:
        c.do_functions(nucleo.nucleo)
    c.do_lines()
    return c

def genera_servidor_de_socket(con_atributos = None, con_funciones =
None):
    c = a_simple_class()
    c.do_name_clase(serv_sok.servidor_de_socket.__name__)
    c.do_attributes(['funcion_a_ejecutar', 'tiempo',
                     'conn'])
    if not con_funciones is None:
        c.do_functions(serv_sok.servidor_de_socket)
    c.do_lines()
    return c

def genera_asociacion_nucleo_servidor_de_sockets():
    n = genera_nucleo()
    g = genera_servidor_de_socket()
    e = union_association()
    e.add_union(n, g, "1:1", "1:1")

    return e


def genera_fic():
    t = genera_nucleo()
    t.generate_latex_code('result1.tex')
    t = genera_asociacion_nucleo_servidor_de_sockets()
    t.generate_latex_code('result2.tex')

if __name__=='__main__':
    genera_fic()


I think that this way of producing python code by TCL will
save me some hundreds lines.

I hope this may be useful for anyone else.

--
-------------
Manolo


Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.




More information about the Python-list mailing list