A comprehensive guide through Python packaging (a.k.a. setup scripts)

May 13th, 2012 by exhuma.twn

One of the really useful things in python are the setup scripts. When doing “serious” business, you really should look into them. Setup scripts are amazingly powerful. But they don’t necessarily need to be complex. But because of this flexibility, the documentation around them seems like a lot to read. Additionally, the state of packaging has changed quite a bit over the years. So you now have a couple of packages (distutils, setuptools, distribute) which all seem to try to solve the same problem. See the current state of packaging for more details on this.

This post attempts to summarize the important bits using a “Hello World” project and steeping through the process of creating the setup.py file:

  • Creating a package distribution
  • Automatic generation of executables
  • Version numbers
  • Dependency management
  • Publishing your package (thus also making it available for automatic dependency resolution)
  • Some more food for thought.
NOTE: The setup.py script we will construct in this post, will use two bits of code which may not work in all cases:

  • importing the package itself (to centralize the version number)
  • Reading the long_description content from a text-file

Both methodologies have their issues. Importing the package only works if you can import it cleanly (i.e. without dependencies) from standard python. Reading the text file only works if the setup.py is called from the proper location.

This is mostly true. There are corner-cases however where this is not possible. If that is the case, you will need to live without these helpful shortcuts. See the comments on this article for more on this.

Read the rest of this entry »

Posted in Python | 30 Comments »

Custom bash completion for fabric tasks

March 20th, 2012 by exhuma.twn

Here’s a small bash function to provide TAB-completion for fabric tasks.

Simply add the following to your ~/.bashrc

You may already have a block like if [ -f /etc/bash_completion ]... in that case, simply add the extra line into that block.

Have fun

Posted in Coding Voodoo, Linux | 2 Comments »

First steps with the closure library

March 1st, 2012 by exhuma.twn

I recently switched work, and now after a lot of JavaEE development for the past 5 years, I am finally back home: Web-development. During my Java time, I did some small web bits here and there. Mainly to keep up with evolution, and followed the massive move towards more JavaScript heavy development. During my free time I took baby steps with a couple of JavaScript libraries, starting with Prototype, then script.aculo.us, MochiKit and MooTools to get some visual effects done. Without doubt, I preferred MochiKit because of it’s similarity to Python. I also dipped into Dojo because of it’s nice HTML Form widgets, fooled around with backbone.js. But eventually I ended up with jQuery, simply because it currently has the backing of The Community. All I’ve done in all of these libraries can only be summarized as “wetting my feet”. At some point I consumed all the Crockford material I could find, and boy was that eye-opening. I realized I was not aware of the most important aspects of JavaScript (especially: “Everything is global! Use Namespaces!”)!

The next thing that caught my eye, was CoffeeScript. It allowed me to write pythonesque code, and compile right down to proper JavaScript.

So what has this to do with the closure library? Well, recently (as stated above) I started web-development again. And some parts of the project made it obvious, that a good dash of JavaScript would help the project a lot. So I needed to take a decision on what library I would base my work on. My first reflex was jQuery. But out of sheer coincidence I stumbled across a blog post from someone discussing jQuery and closure. Unfortunately, I don’t have the link anymore 🙁 The TL;DR of that blog post was however (paraphrasing):

If I had known about closure before, I would have used it instead of jQuery, because it makes it easier to structure the source code.

This made me more that curious. So the next thing I did, was read the closure tutorials, and also read the API top to bottom. I wanted to know what was in there before deeming it useful. And boy did it look interesting! The most interesting feature, as the author of said blog post pointed out, it the ability to easily structure your code. Using goog.provides and goog.require you can very easily split your JS files into multiple, well structured files, and the closure compiler will then re-combine them into one file, all properly ordered. To me, this feels a lot like writing Java or Python, which both allow you to structure your code very well. This also allows you to easily write modules which can in turn be re-used with ease in other projects (or, you could publish them).

For now, I am not missing anything from jQuery except the easy DOM querying from it. But I can happily live without it.

With my current experience, I can see the following benefits:

  • Makes it easy to structure code
  • The optional linter forces you to write proper/clean code. Especially with the --strict flag. Using it right from the start forces you to write in a uniform coding style, which makes code more readable!
  • The compiler solves dependency chains with the help of goog.provide and goog.require, and can optionally minify your code.
  • The library feels very professional. It’s even got a Logger loosely inspired by java.util.logger, and let’s you do exactly what you expect it to! This is awesome for debugging!
  • The library source code is very readable

And some disadvantages:

  • The community is much smaller than the one around jQuery. But so far I could figure out all I needed by reading the code.
  • It does not seem to have versioned downloads. You have to checkout from SVN trunk. I would prefer to be able to say: “I am basing my work on version 1.0” instead of “trunk”
  • Instead of linking one CSS file (as in jQuery), you have to link multiple styles and figure out which ones by yourself. But given the filenames are self-explanatory, that’s not too difficult.
  • There are no predefined CSS themes. Something jQuery’s theme builder would be nice. But, I assume that the closure-style-sheet project should help writing CSS files easily. I did not yet look into that one!

As I progress with the library I’ll post my findings…

Here are two related links I found while looking for the mentioned blog post:

Posted in JavaScript | No Comments »

JScript to query scheduled tasks

June 9th, 2011 by exhuma.twn

Soon, we will need to send out notifications as soon something bad happens with a scheduled task on Windows. The following JScript file runs natively on Windows and is capable of just that. It uses the command line tool “schtasks” to query the information and wraps the result into a list of usable object instances.

It’s possible to use this list to react to important events in the job executions. For example, you could loop through the list and send emails to the appropriate people if the variable “lastResult” is non-zero.

Posted in Coding Voodoo | No Comments »

Windows script to remove old files

June 8th, 2011 by exhuma.twn

Simple script… still, I thought I’d share…

Posted in Coding Voodoo | No Comments »

Change JPA EntityManager connection properties at Runtime

December 30th, 2010 by exhuma.twn

There are many times you want to be able to use different connection options for a JPA EntityManager. The most obvious is different user-credentials (think of a user login-screen and re-using these credentials to connect to the DB), or to make the distinction between development/testing/production environment.

However, if you let Netbeans create the persistence configuration, it will hardcode all connection parameters into the persistence.xml file. When retrieving an EntityManager instance, it will use this information to connect.

If, instead you would like to do this at runtime, you can do the following:

  1. Remove the “properties” tag from the persistence.xml. This may not be necessary, but this will make it clear, that the properties are set inside the code.
  2. Create a “Map<String, String>” which will contain the properties. A list of standard properties can be found in the specs in section 8.2.1.9.
  3. Use this map to create an EntityManagerFactory and use this to create your EntityManager

An example persistence.xml without properties

<?xml version="1.0" encoding="UTF-8"?>                                          
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLo
 <persistence-unit name="myTestPU" transaction-type="RESOURCE_LOCAL">    
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>            
    <class>my.package.Entity1</class>                    
    <class>my.package.Entity2</class>                    
  </persistence-unit>                                                              
</persistence>

In case Netbeans created a method “getEntityManager”, you can safely replace this. Here is an example I currently have in use. The “appConf” instance is a singleton I use to store configuration data in the user’s home folder, and yes, the password is stored in plain-text, but for this test-case I did not need to go any further:

    private EntityManager getEntityManager() {                                  
                                                                               
        Map<String, String> dbProps = new HashMap<String, String>();            
                                                                               
        dbProps.put("eclipselink.logging.level",                                
                appConf.get("eclipselink.logging.level", "INFO").toString());  
                                                                               
        // On linux, the GSSAPI is not available. Use a default user/password  
        // pair to connect                                                      
        if ("Linux".equals(System.getProperty("os.name"))) {                    
            dbProps.put("javax.persistence.jdbc.url",                          
                    String.format("jdbc:jtds:sqlserver://%s/%s",                
                    appConf.get("db.host", "my-default-host"),                          
                    appConf.get("db.database", "my-default-db")));                
            dbProps.put("javax.persistence.jdbc.driver",                        
                    "net.sourceforge.jtds.jdbc.Driver");                        
            dbProps.put("javax.persistence.jdbc.password",                      
                    appConf.get("db.password").toString());                    
            dbProps.put("javax.persistence.jdbc.user",                          
                    appConf.get("db.user", "my-default-username").toString());          
        } else {                                                                
            dbProps.put("javax.persistence.jdbc.url",                          
                    String.format("jdbc:sqlserver://%s;databaseName=%s;integratedSecurity=true",
                    appConf.get("db.host", "my-default-host"),                          
                    appConf.get("db.database", "my-default-db")));                
            dbProps.put("javax.persistence.jdbc.driver",                        
                    "com.microsoft.sqlserver.jdbc.SQLServerDriver");            
        }                                                                      
        appConf.flush();                                                        
                                                                               
        EntityManagerFactory fact = Persistence.createEntityManagerFactory("myTestPU", dbProps);
        return fact.createEntityManager();                                      
   }

This example uses eclipselink. All available properties can be found it the EclipseLink Wiki

Posted in Coding Voodoo | 14 Comments »

SPSS, MS-SQL2008 & bigint

March 31st, 2010 by exhuma.twn

There seems to be an issue with SPSS while reading data from an MS-SQL-Server instance. Notably with the SQL datatype “bigint”. Assume the following SPSS syntax:

GET DATA
   /TYPE=ODBC
   /CONNECT='DSN=my_dsn;SERVER=server_name;Trusted_Connection=yes;DATABASE=db_name'
   /SQL = 'SELECT year FROM  my_table'
.
EXECUTE.

If the field in question (in this case: “year”) is of SQL-type “bigint” then SPSS will show these values in the majority of the cases as “MISSING”. Sporadically some values appear, but they are completely wrong.

Once the cause is known (problem with the “biging” type), the solution is straight-forward: Cast the type to another appropriate type which is understood by SPSS. Which type you choose obviously depends on the values stored in the affected fields. Casting blindly to “int” may (I haven’t tested this!) resolve in strange results if the values lie outside of the “int” range (-2^31 to 2^31-1). In this case you may need to cast it to something alphanumeric like “varchar” and re-cast it in SPSS into “Numeric”. As said, I haven’t tested this but I thought it might be worth mentioning!

So, here’s the above query with the appropriate cast:

GET DATA
   /TYPE=ODBC
   /CONNECT='DSN=my_dsn;SERVER=server_name;Trusted_Connection=yes;DATABASE=db_name'
   /SQL = 'SELECT CONVERT(int, year) AS year FROM  my_table'
.
EXECUTE.

Note also that in this case you need to add an alias for the column ( “… AS year” ). Otherwise SPSS will return it as “VXXX” (where XXX is a sequential number).

I have tested this solution on all combinations of SPSS 11.5, SPSS 18, SQL-Server 2008 64bit, SQL-Server 2008 Express 32bit. And casting the value worked every time.

Depending on your use, it may be helpful to create views which do the casting. I have not yet tried this, but I don’t see a reason why it shouldn’t work. Additionally, it might be noteworthy that I have only encountered this problem with “bigint” so far. There may be problems with other types as well. I expect, casting them to something else should work there too.

Posted in Coding Voodoo | No Comments »

Dialog buttons not responding in Eclipse under KDE/GNOME

February 1st, 2010 by wickeddoc

In case you’re running into the same trouble as me, that dialog buttons are not “clickable” anymore under Eclipse, just add the following line to one of your linux startup scripts to fix the problem:

export GDK_NATIVE_WINDOWS=1

Posted in Coding Voodoo | No Comments »

Mayflower Zend Framework Cheatsheet

December 31st, 2009 by wickeddoc

Several weeks ago I was scouting the Internet for a Zend Framework cheatsheet and I found a blog entry somewhere about a Zend Framework Cheatsheet Poster, created by a German company called Mayflower.

On their blog they say, if you’re an eager Zend Framework developer and want to get your copy of their Cheatsheet poster, you’ll just have to send them an email and you’ll get this great Poster delivered to your office or home or whatever, free of charge. So that’s what I did and guess what, a week later I received this very useful poster in the mail.
So if you are a Zend Framework developer yourself and want to own this cool poster, don’t be shy, just send an email to Björn Schotte over at Mayflower.

Here’s a photo of the poster in our office @ Visual Online, Luxembourg

Mayflower Zend Framework Cheatsheet Poster

Posted in Zend Framework | No Comments »

Unable to easy_install psycopg2 on debian

October 29th, 2009 by exhuma.twn

Problem:

$ easy_install psycopg2
Searching for psycopg2                                                                                                                                                                                                                                                      
Reading http://pypi.python.org/simple/psycopg2/                                                                                                                                                                                                                              
Reading http://initd.org/projects/psycopg2                                                                                                                                                                                                                                  
Reading http://initd.org/pub/software/psycopg/                                                                                                                                                                                                                              
Best match: psycopg2 2.0.13                                                                                                                                                                                                                                                  
Downloading http://initd.org/pub/software/psycopg/psycopg2-2.0.13.tar.gz                                                                                                                                                                                                    
Processing psycopg2-2.0.13.tar.gz                                                                                                                                                                                                                                            
Running psycopg2-2.0.13/setup.py -q bdist_egg --dist-dir /tmp/easy_install-cHE0C_/psycopg2-2.0.13/egg-dist-tmp-x-CxRS                                                                                                                                                        
error: Setup script exited with error: No such file or directory

Solution:

This most likely indicates that you are missing the “libpq” headers:

sudo aptitude install libpq-dev

should solve the problem

Posted in Python | No Comments »

« Previous Entries Next Entries »

Pages

Recent Posts

Categories

Links


Archives

Meta