Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
script:python:xtra2 [2011/08/08 18:54] alfred |
— (actual) | ||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| - | ====== Xtra (Python) 2 ====== | ||
| - | ===== Nuevos conceptos ===== | ||
| - | |||
| - | ==== Reflection ==== | ||
| - | <code Python> | ||
| - | # sin reflection: | ||
| - | Foo().hello() | ||
| - | |||
| - | # con reflection: | ||
| - | getattr(globals()['Foo'](), 'hello')() | ||
| - | </code> | ||
| - | ==== Logging ==== | ||
| - | |||
| - | ===== Pequeñas librerías ===== | ||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | |||
| - | ==== PyYAML ==== | ||
| - | * [[http://pyyaml.org/wiki/PyYAML]] | ||
| - | * {{script:python:pyyaml-3.10.zip|PyYAML}} | ||
| - | Para parsear documentos [[tags:yaml|YAML]]. | ||
| - | === Uso === | ||
| - | Agregaremos la librería. | ||
| - | <code python> | ||
| - | import yaml | ||
| - | </code> | ||
| - | Cargar un texto yaml: | ||
| - | <code python> | ||
| - | print yaml.load(""" | ||
| - | name: Vorlin Laruknuzum | ||
| - | sex: Male | ||
| - | class: Priest | ||
| - | title: Acolyte | ||
| - | hp: [32, 71] | ||
| - | sp: [1, 13] | ||
| - | gold: 423 | ||
| - | inventory: | ||
| - | - a Holy Book of Prayers (Words of Wisdom) | ||
| - | - an Azure Potion of Cure Light Wounds | ||
| - | - a Silver Wand of Wonder | ||
| - | """) | ||
| - | </code> | ||
| - | Crear un texto yaml: | ||
| - | <code python> | ||
| - | print yaml.dump({'name': "The Cloak 'Colluin'", 'depth': 5, 'rarity': 45, 'weight': 10, 'cost': 50000, 'flags': ['INT', 'WIS', 'SPEED', 'STEALTH']}) | ||
| - | </code> | ||
| - | === Notas === | ||
| - | * Para instalar, en el directorio: | ||
| - | <code> | ||
| - | $ sudo python setup.py install | ||
| - | </code> | ||
| - | ===== Scripting ===== | ||
| - | ==== In a nutshell ==== | ||
| - | === Básico === | ||
| - | * Recoger parámetros pasados en la ejecución: | ||
| - | <code python> | ||
| - | for arg in sys.argv: | ||
| - | print arg | ||
| - | </code> | ||
| - | * Recoger lo introducido por consola: | ||
| - | <code python> | ||
| - | value = raw_input("Sure?") | ||
| - | </code> | ||
| - | |||
| - | |||
| - | |||
| - | === Strings === | ||
| - | * Saber la longitud: | ||
| - | <code> | ||
| - | len(str) | ||
| - | </code> | ||
| - | * Concatenar: | ||
| - | <code> | ||
| - | path = path + os.sep | ||
| - | </code> | ||
| - | * Saber el último carácter: | ||
| - | <code> | ||
| - | path[len(path) - 1:] | ||
| - | </code> | ||
| - | * Formato: | ||
| - | <code python> | ||
| - | "La capital de %s es %s" % ("Araba", "Gasteiz") # 'La capital de Araba es Gasteiz' | ||
| - | "%s tiene %d provincias y %4d habitantes" % ("Araba", 1, 100) # 'Gasteiz tiene 1 provincias y 0100 habitantes' | ||
| - | "Cada uno con %.2f ojos" % (2.5457) # 'Cada uno con 2.54 ojos' | ||
| - | </code> | ||
| - | |||
| - | |||
| - | ==== Utilidades con ficheros y directorios ==== | ||
| - | * Eliminar un directorio: | ||
| - | <code python> | ||
| - | shutil.rmtree(path) | ||
| - | </code> | ||
| - | * Crear directorio: | ||
| - | <code python> | ||
| - | os.mkdir(path) | ||
| - | </code> | ||
| - | * Saber el separador de directorios: | ||
| - | <code python> | ||
| - | os.sep | ||
| - | </code> | ||
| - | * Copiar un directorio: | ||
| - | <code python> | ||
| - | shutil.copytree(src, dst) | ||
| - | </code> | ||
| - | |||
| - | ==== Utilidades con webs ==== | ||
| - | ==== Utilidades con imágenes ==== | ||
| - | |||
| - | |||
| - | ===== Notas ===== | ||
| - | |||
| - | ==== Utilidades ==== | ||
| - | === easy_install === | ||
| - | Es un gestor de paquetes para Python. Permite instalar librerías de una forma muy sencilla. Para instalarlo simplemente haremos: | ||
| - | <code> | ||
| - | wget http://peak.telecommunity.com/dist/ez_setup.py | ||
| - | sudo python ez_setup.py | ||
| - | </code> | ||
| - | A partir de entonces podremos instalar los paquetes con simplemente poner el nombre (esto hará que lo busque en el repositorio [[http://pypi.python.org/pypi|PyPi]], lo baje y lo instale]]: | ||
| - | <code> | ||
| - | easy_install SQLObject | ||
| - | </code> | ||
| - | O a partir de una ruta, de un .tgz, de un .egg... | ||
| - | <code> | ||
| - | easy_install http://example.com/path/to/MyPackage-1.2.3.tgz | ||
| - | easy_install . | ||
| - | </code> | ||
| - | === Usar una librería sin instalarla === | ||