From Java to Python


Published on

Ivor talk about Java and Python on the first PyGrunn monthly (Jan 2012)

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • I would like to talk about two things : the idea of organizing python meetings : Do the kick-off presentation myself
  • Would like to call it (after groningen.rb) The purpose is to talk and learn about python usage
  • Let me know if you have ideas on any of the above.
  • Quick scan: how do you use python We don’t need definitive answers
  • I prefer to hear from experience, over tutorial style talks
  • My name is Ivor Bosloper A few months ago I started a company called Crop-R. We’re building a product for farmers and developing it in GeoDjango. We all use a load of languages, but if you’d ask me: what’s your preferred general purpose language I’d come up with this answer. Only very recently I switched from Java to Python.
  • I would still consider Java for many projects. (reference, gc, oo, API, general purpose). This all holds for Python as well…
  • I wanted to make this talk experience driven, so not a theoretic comparison. Let me know at the end if I succeeded. Still love Java but prefer python
  • Developers spend 80% of their time reading code.
  • Hello world starts showing it. But I don’t like theoretic examples, so let’s take some example code from a project
  • Repetitiveness. Verbosity.
  • Powerful language: you don’t need configuration languages
  • Inefficient. Unreadable.
  • And worse: you have to think of everything in advance! It’s not a programming language (executes some logic). E.g. servlet-mapping has strict rules. You can not do random pattern matching!
  • The language has some nice constructs to make your code really concise. Declarative programming. Callbacks.
  • I want to be able to plugin a new XML Implementation.
  • What I miss (or don’t know): please Concurrency ( One big global lock )
  • I didn’t understand why no of the datastructutes (like dict) had concurrent behaviour documented. In Java, most datastructures have this part documented. Only after reading a bit I discovered there is no concurrency… ?! In CPython at least.
  • Java only crashes (in practice) on native code. That is: non-managed code. Java libraries
  • From Java to Python

    1. 1. / 16-01-2012
    2. 2. Schedule• Input for Python meetups• From Java to Python
    3. 3. Meetups• (thanks Mark)• Talk and learn about Python development• Community• Regular meetings
    4. 4. What do we need?• A time, space & some drinks• Subjects / Format• You!• Please invite and suggest
    5. 5. How do you use python? • For how long? • Web development? • As main language? • Framework?
    6. 6. Contents• Any subject you’d like to hear about?• Any subject you’d like to talk about? python 3.0 web services django 1.4 data migration performance tuning
    7. 7. Meeting schedule• How often?• What day of week / time?
    8. 8. Let’s startFrom Java to Python
    9. 9. Ivor Bosloper using GeodjangoPreferred general purpose language: Basic/ Assembler C Java Python 1985 1995 2001 2010
    10. 10. Java2pyDisclaimer: I love Java/JVM • Typesafe/checked references • Garbage collection • Object Oriented • Huge API • General purpose • Security, i18n • Great debugging (e.g. with stacktraces)
    11. 11. Java2pyWhy Python? Productivity! • Readability • Pragmatic and language possibilities • Development cycle
    12. 12. Readability matters
    13. 13. Readabilitypublic class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); }}
    14. 14. Readability @Entity public class Farm { @Id @GeneratedValue private Long id; private String name; @ManyToOne(optional=true) private Address address; public Long getId() { return; } public void setId(Long id) { = id; } public String getName() { return; }DRY public void setName(String name) { = name; } public Address getAddress() { return this.address; } public void setAddress(Address address) { this.address = address; } public String toString() { return; } }
    15. 15. Readability
    16. 16. ReadabilityNo .properties, .xml, sql (/hql)
    17. 17. ReadabilityXML Theory:human readableXML Practice:Readability of a binarycombined with theefficiency of flat text.
    18. 18. Flexibility• XML or .properties configuration allows only programmed logic• allows custom logic
    19. 19. Readability
    20. 20. Pragmatic APIs• APIs created for use, not for possibilities DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); Document doc = docBuilder.parse(new File("file.xml")); XpathFactory xpf = XPathFactory.newInstance(); XPath xpath = xpf.newXPath(); NodeSet books = (NodeSet) xpath.evaluate("/Books/Book",doc,XpathConstants.NODESET);
    21. 21. Pragmatic APIs• APIs created for use, not for possibilities InputStream imageInputStream = FileInputStream("c.jpg"); SeekableStream seekableImageStream = SeekableStream.wrapInputStream(imageInputStream, true); RenderedOp originalImage = JAI.create(JAI_STREAM_ACTION, seekableImageStream); ((OpImage) originalImage.getRendering()).setTileCache(null); int origImageWidth = originalImage.getWidth(); double scale = 220.0 / originalImage.getWidth(); ParameterBlock paramBlock = new ParameterBlock(); paramBlock.addSource(originalImage); // The source image paramBlock.add(scale); // The xScale paramBlock.add(scale); // The yScale paramBlock.add(0.0); // The x translation paramBlock.add(0.0); // The y translation RenderingHints qualityHints = new RenderingHints(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); RenderedOp resizedImage = JAI.create(JAI_SUBSAMPLE_AVERAGE_ACTION, paramBlock, qualityHints); // lastly, write the newly-resized image to an output stream, in a specific encoding FileOutputStream encoderOutputStream = new FileOutputStream("c-thumb.jpg"); JAI.create(JAI_ENCODE_ACTION, resizedImage, encoderOutputStream, JAI_ENCODE_FORMAT_JPEG, null);
    22. 22. Development cycle• Getting started (Django)
    23. 23. Development cycleEdit Save Edit Save Reload page Reload page Compile (2 seconds) Restart Package (war) Deploy (20 seconds)
    24. 24. Missing from py?• Concurrency (!)• Learn again: Performance tuning• Safety of jvm• (Adoption of big companies)
    25. 25. Concurrency? • The free lunch is over (2005) • Azul 768-core machine (2007) • Python GIL in 2011?
    26. 26. Performance tuning• Profiling• GC options• Memory
    27. 27. Safety of native code
    28. 28. Organizational adoption• Commercial vendors• Developer/deployer roles