+++ title = "Utilidades" weight = 9 +++ ### dict_to_property Convertir diccionarios en PropertyValue ```python datos = { 'Hidden': True, 'Password': 'letmein', } propiedades = app.dict_to_property(datos) app.msgbox(propiedades) ``` ### data_to_dict Convertir `PropertyValue` en diccionarios ```python datos = app.data_to_dict(propiedades) app.msgbox(datos) ``` Convertir `tuplas` a diccionario. ```python tupla_de_tuplas = ( ('Hidden', True), ('Password', 'letmein'), ) datos = app.data_to_dict(tupla_de_tuplas) app.msgbox(datos) ``` Convertir `listas` a diccionario. ```python lista_de_listas = [ ['Hidden', True], ['Password', 'letmein'], ] datos = app.data_to_dict(lista_de_listas) app.msgbox(datos) ``` ### sleep Hacer una pausa de X segundos. ```python app.sleep(5) ``` ### render Reemplazar variables en cadenas de texto. ```python plantilla = """Hola $nombre Te envío este archivo: $archivo Saludos cordiales """ datos = { 'nombre': 'Ingrid Bergman', 'archivo': 'carta_de_amor.odt' } resultado = app.render(plantilla, datos) app.msgbox(resultado) ``` ### run Ejecutar un programa. ```python nombre_aplicacion = 'gnome-calculator' app.shell.run(nombre_aplicacion) ``` Ejecutar comandos shell y capturar la salida. ```python comandos = 'ls -lh ~' resultado = app.shell.run(comandos, True) app.debug(resultado) ``` ``` drwxr-xr-x 4 mau mau 4.0K Aug 15 23:36 Desktop drwxr-xr-x 6 mau mau 4.0K Jun 9 23:32 Documents drwxr-xr-x 5 mau mau 4.0K Aug 16 13:09 Downloads drwxr-xr-x 3 mau mau 4.0K Aug 14 15:19 Pictures drwxr-xr-x 10 mau mau 4.0K Jun 19 19:36 Projects drwxr-xr-x 2 mau mau 4.0K May 11 22:36 Templates drwxr-xr-x 2 mau mau 4.0K Jul 19 13:37 Videos ``` Ejectuar comandos y capturar la salida línea a línea. ```python comandos = 'ls -lh /home/mau' for line in app.shell.popen(comandos): app.debug(line) ``` ### digest Obtener hash. Por default se regresa en hexadecimal. ```python datos = 'LibreOffice con Python' digest = app.hash.digest('md5', datos) app.debug('MD5 = ', digest) digest = app.hash.digest('sha1', datos) app.debug('SHA1 = ', digest) digest = app.hash.digest('sha256', datos) app.debug('SHA256 = ', digest) digest = app.hash.digest('sha512', datos) app.debug('SHA512 = ', digest) ``` ``` 16/08/2022 18:48:07 - DEBUG - MD5 = 3801759ead20abc3ce0d0095289bdcfd 16/08/2022 18:48:07 - DEBUG - SHA1 = 1df74aaae9658c21074aa5a2d4c2055dcf79f0db 16/08/2022 18:48:07 - DEBUG - SHA256 = 228e90b15b6259307e580677939b1f2f45e9317461e98f603af8fcac0f9a598f 16/08/2022 18:48:07 - DEBUG - SHA512 = 3ef45f79f3bfd2b251d250489c91b631306456405510397fb1a7ee37005d196376b7d6ca86a9895f4eb97eb74813965c24d6564a383f4bdb1360665c8fbb192a ``` Para obtener bytes. ``` digest = app.hash.digest('md5', datos, False) app.debug('MD5 = ', digest) ``` ``` 16/08/2022 18:48:07 - DEBUG - MD5 = b'8\x01u\x9e\xad \xab\xc3\xce\r\x00\x95(\x9b\xdc\xfd' ``` ### config Puede guardar datos de configuración de su macro o extensión dentro del perfil de usuario. ```python nombre = 'mi_extension' datos = { 'ruta': '/home/mau/pruebas', 'guardar': True, } if app.config.set(nombre, datos): app.debug('Configuración guardada...') ``` Y recuperarlos en cualquier momento. ``` datos = app.config.get(nombre) app.debug(datos) ``` ### color Puede ver los colores que puede usar en Wikipedia [Colores Web][1] ```python color_nombre = 'darkblue' color = app.color(color_nombre) app.debug(color) color_rgb = (125, 200, 10) color = app.color(color_rgb) app.debug(color) color_html = '#008080' color = app.color(color_html) app.debug(color) ``` [1]: https://es.wikipedia.org/wiki/Colores_web