Jython: Python para la plataforma Java (EL2009)

3,984 views
3,874 views

Published on

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
3,984
On SlideShare
0
From Embeds
0
Number of Embeds
948
Actions
Shares
0
Downloads
66
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Jython: Python para la plataforma Java (EL2009)

    1. 1. Python para la Plataforma Java Leo Soto M. Encuentro Linux 2009
    2. 2. http://www.flickr.com/photos/ferruiz/4036373874/
    3. 3. Python para la plataforma Java
    4. 4. http://www.flickr.com/photos/lab2112/457187539/
    5. 5. ¿Plataforma Java sin Java?
    6. 6. Plataforma Java con Java más Python, Ruby, Scala, Groovy...
    7. 7. ¿Por qué Python?
    8. 8. “Python is an experiment in how much freedom programmers need...”
    9. 9. “...Too much freedom and nobody can read another's code; too little and expressiveness is endangered” - Guido van Rossum, 1996
    10. 10. Python Dinámico, flexible, extremadamente legible
    11. 11. Jython Dinámico, flexible, extremadamente legible En una plataforma ubicua, sólida
    12. 12. http://www.flickr.com/photos/mushon/282287572/
    13. 13. Status de Jython http://www.flickr.com/photos/digital-noise/3650559857/
    14. 14. 2.5.1
    15. 15. Full compatibilidad con CPython
    16. 16. web2py etree nose setuptools virtualenv
    17. 17. OK
    18. 18. ¿Pero qué puedo hacer con Jython hoy?
    19. 19. GUIs
    20. 20. Demo con Swing
    21. 21. De Java a Jython
    22. 22. final JFrame frame = new JFrame("HelloSwing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(300,300);
    23. 23. frame = JFrame("HelloSwing") frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE) frame.setSize(300,300)
    24. 24. frame = JFrame("HelloSwing", defaultCloseOperation=JFrame.EXIT_ON_CLOSE, size=(300, 300))
    25. 25. Container content = frame.getContentPane(); content.setLayout(new FlowLayout());
    26. 26. content = frame.getContentPane(); content.setLayout(new FlowLayout());
    27. 27. content = frame.contentPane content.setLayout(new FlowLayout());
    28. 28. content = frame.contentPane content.setLayout(FlowLayout());
    29. 29. content = frame.contentPane content.layout = FlowLayout()
    30. 30. JButton botonSaludar = new JButton("Saludar"); JButton botonDespedir = new JButton("Despedirse"); botonSaludar.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(frame, "Hola!"); } }); botonDespedir.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(frame, "Chao!"); frame.dispose(); } }); content.add(botonSaludar); content.add(botonDespedir);
    31. 31. def saludar(event): JOptionPane.showMessageDialog(frame, "Hola!") def despedir(event): JOptionPane.showMessageDialog(frame, "Chao!") frame.dispose() content.add(JButton("Saludar", actionPerformed=saludar)) content.add(JButton("Despedirse", actionPerformed=despedir))
    32. 32. Otra demo con Swing (Si es que tenemos acceso a internet)
    33. 33. Créditos: Frank Wierzbicki
    34. 34. Web
    35. 35. Demo con Django
    36. 36. Y sólo por diversión...
    37. 37. ### View #!/usr/bin/env python from django import http """ from django.template import Template, Context WSW - the World's Shittiest Wiki. """ def wsw(request, title): import re title = urllib.unquote_plus(title) if title else "Home" import urllib p, created = Page.objects.get_or_create(title=title) from django.conf import settings form = PageForm(instance=p) if request.method == "POST": ### Config form = PageForm(request.POST, instance=p) settings.configure( form.save() DEBUG = True, return http.HttpResponseRedirect(request.get_full_path()) DATABASE_ENGINE = "doj.backends.zxjdbc.postgresql", else: DATABASE_NAME = "wsw", form = PageForm(instance=p) DATABASE_USER = "wsw", t = Template("""<h1>{{ p.title }}</h1><ul>{% for p in DATABASE_PASSWORD = "wsw", allpages %}<li><a href="{{ p }}">{{ p }}</a></li>{% endfor %} ROOT_URLCONF = __name__) </ul><form action="{{ request.get_full_path }}" method="POST"> <p>{{ form.content }}</p><input type="submit"></form>""") ### Model wikiwords = re.findall( from django.db import models '[A-Z][a-z]+[A-Z][a-z]+(?:[A-Z][a-z]+)*', p.content) allpages = [ class Page(models.Model): page.title for page in Page.objects.order_by('title')] title = models.CharField(max_length=300, allpages.extend( primary_key=True) word for word in wikiwords if word not in allpages) content = models.TextField() return http.HttpResponse(t.render(Context(locals()))) class Meta: ### Main app_label = "wsw" if __name__ == '__main__': from django.core import management ### URLs from django.db import connection from django.conf.urls.defaults import * from django.core.management.color import no_style cursor = connection.cursor() urlpatterns = patterns(__name__, ('(.*)', 'wsw')) statements, _ = connection.creation.sql_create_model( Page, no_style()) ### Form try: from django.forms import ModelForm for sql in statements: cursor.execute(sql) class PageForm(ModelForm): connection.connection.commit() class Meta: except: model = Page pass fields = ['content'] management.call_command('runserver')
    38. 38. Créditos: Jacob Kaplan-Moss
    39. 39. Testing http://www.flickr.com/photos/emotionaltoothpaste/26034597/
    40. 40. DocTests
    41. 41. def factorial(n): It must also not be ridiculously large: """Return the factorial of n, an >>> factorial(1e100) exact integer >= 0. Traceback (most recent call last): ... If the result is small enough to OverflowError: n too large fit in an int, return an int. """ Else return a long. import math >>> [factorial(n) if not n >= 0: ... for n in range(6)] raise ValueError("n must be >= 0") [1, 1, 2, 6, 24, 120] if math.floor(n) != n: >>> [factorial(long(n)) raise ValueError( ... for n in range(6)] "n must be exact integer") [1, 1, 2, 6, 24, 120] if n+1 == n: # catch a value like 1e300 >>> factorial(30) raise OverflowError("n too large") 265252859812191058636308480000000L result = 1 >>> factorial(30L) factor = 2 265252859812191058636308480000000L while factor <= n: >>> factorial(-1) result *= factor Traceback (most recent call last): factor += 1 ... return result ValueError: n must be >= 0 Factorials of floats are OK, but the float must be an exact integer: >>> factorial(30.1) Traceback (most recent call last): ... ValueError: n must be exact integer >>> factorial(30.0) 265252859812191058636308480000000L
    42. 42. Demo: DocTests + HtmlUnit
    43. 43. Demo: DocTests + HtmlUnit ¿Ah?
    44. 44. ¡Tests de Integración para web apps!
    45. 45. Oh, para eso tenemos Selenium, ¿no?
    46. 46. Ejecutar en el browser tarda muuuuucho http://www.flickr.com/photos/tambako/498552755/
    47. 47. Run headless...
    48. 48. Run headless... ...con HtmlUnit!
    49. 49. Demo: DocTests + HtmlUnit
    50. 50. Más Testing http://www.flickr.com/photos/emotionaltoothpaste/26034597/
    51. 51. ¿Por cierto, quien más usa Jython?
    52. 52. EADS http://www.flickr.com/photos/nguyendai/694158734/
    53. 53. Lockheed Martin http://www.flickr.com/photos/rcsj/2504022678/
    54. 54. ¿Y dónde puedo aprender más?
    55. 55. jythonbook.com
    56. 56. jythonbook.com
    57. 57. ¡Gracias! Contacto: @leosoto http://blog.leosoto.com
    58. 58. ¿Preguntas? Contacto: @leosoto http://blog.leosoto.com

    ×