from java to python
DESCRIPTION
Ivor talk about Java and Python on the first PyGrunn monthly (Jan 2012)TRANSCRIPT
groningen.py / 16-01-2012
ScheduleSchedule
• Input for Python meetups
• From Java to Python
MeetupsMeetups
• groningen.py (thanks Mark)
• Talk and learn about Python development
• Community
• Regular meetings
What do we need?What do we need?
• A time, space & some drinks
• Subjects / Format
• You!
• Please invite and suggest
How do you use How do you use python?python?
• For how long?
• Web development?
• As main language?
• Framework?
ContentsContents• Any subject you’d like to hear
about?
• Any subject you’d like to talk about?
django 1.4
python 3.0
data migration
performance tuning
web services
Meeting scheduleMeeting schedule
• How often?
• What day of week / time?
Let’s startLet’s start
From Java to Python
Ivor BosloperIvor Bosloper
2010200119951985
using Geodjango
Basic/Assembler C Java Python
Preferred general purpose language:
Java2pyJava2pyDisclaimer: I love Java/JVM
• Typesafe/checked references
• Garbage collection
• Object Oriented
• Huge API
• General purpose
• Security, i18n
• Great debugging (e.g. with stacktraces)
Java2pyJava2pyWhy Python? Productivity!
• Readability
• Pragmatic and language possibilities
• Development cycle
Readability mattersReadability matters
ReadabilityReadability
public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World"); }}
ReadabilityReadability@Entitypublic class Farm { @Id @GeneratedValue private Long id;
private String name;
@ManyToOne(optional=true) private Address address;
public Long getId() { return this.id; }
public void setId(Long id) { this.id = id; }
public String getName() { return this.name; }
public void setName(String name) { this.name = name; }
public Address getAddress() { return this.address; }
public void setAddress(Address address) { this.address = address; } public String toString() { return this.name; }}
DRY
ReadabilityReadability
ReadabilityReadability
No .properties, .xml, sql (/hql)
ReadabilityReadability
XML Theory:human readable
XML Practice:Readability of a binary combined with the efficiency of flat text.
FlexibilityFlexibility
• XML or .properties configuration allows only programmed logic
• settings.py allows custom logic
ReadabilityReadability
Pragmatic APIsPragmatic 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);
Pragmatic APIsPragmatic 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 imageparamBlock.add(scale); // The xScaleparamBlock.add(scale); // The yScaleparamBlock.add(0.0); // The x translationparamBlock.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 encodingFileOutputStream encoderOutputStream = new FileOutputStream("c-thumb.jpg");JAI.create(JAI_ENCODE_ACTION, resizedImage, encoderOutputStream, JAI_ENCODE_FORMAT_JPEG, null);
Development cycleDevelopment cycle• Getting started (Django)
Development cycleDevelopment cycleEdit Save
Reload page
(2 seconds)
Edit Save
Reload page
(20 seconds)
Compile
Package
(war)Deploy
Restart
Missing from py?Missing from py?
• Concurrency (!)
• Learn again: Performance tuning
• Safety of jvm
• (Adoption of big companies)
Concurrency?Concurrency?• The free
lunch is over (2005)
• Azul 768-core machine (2007)
• Python GIL in 2011?
Performance tuningPerformance tuning
• Profiling
• GC options
• Memory
Safety of native codeSafety of native code
Organizational Organizational adoptionadoption
• Commercial vendors
• Developer/deployer roles