Monday, September 24, 2012

DjangoCon US 2012


I was lucky enough to attend DjangoCon earlier this month. It was a great experience, I'm still following up on the new tips and tricks I picked up. 
This was my first purely software focused conference, they are normally scientific. There are some stark differences. Notably I'm not the only person tweeting! Thanks to the wonders of lanyrd.com and slideshare.net I was able to leave DC with all the presentations I attended. Although I took copious notes during the talks it is handy to double check details. Plus it was dual track so I missed half the talks, some of which I would of liked to of seen. 

So scientists, why don't you tweet and share your research more? You have gone to the effort of writing (thus getting approval to disclose your work) and attending a conference - that is the hard part. Uploading to slideshare.net and sharing on twitter and/or LinkedIn is the easy step! By the same token I will follow my own advice too.

Tuesday, July 17, 2012

Using Gunicorn with PyCharm

The Green Unicorn
When you develop projects with Django it comes with a handy runserver which is a lightweight webserver bundled with Django, see more here. It goes without saying you would never want to deploy any production code with this as it doesn't scale nor is it particularly secure. It does not get much attention from the Django developers, lets face it there are better things to work on. So a few alternatives have popped up. The one which caught my eye is gunicorn. Now gunicorn can be used on preprod or production sites as well, but I stick to development here (what I use for production will feature in another post).

Gunicorn is actually a fork of Unicorn which is designed for use with Ruby. It is a WSGI webserver, WSGI is the way to run webservers. It also features integration with Django so can be a drop in replacement for runserver. Installation is a breeze, pip install gunicorn and add gunicorn to your INSTALLED_APPS in your settings.py - Easy.


From the command-line you might do ./manage.py runserver 192.168.1.1:9000 with the Django runserver. To use Gunicorn simply run ./manage.py run_gunicorn 192.168.1.1:9000. Again straight forward. Within PyCharm you need edit your django run configuration and tick the "run custom command" box. Add run_gunicorn and run that. run_gunicorn is a helper function that gunicorn adds to manage.py. Unfortunately, I found not all gunicorn options can be specified via this, which can be annoying.  To access all gunicorn options you need to run gunicorn direct and feed it a wsgi application, handily django is wsgi ready and from 1.4 includes a wsgi file when you create a new project (look for wsgi.py in your project directory). When using gunicorn direct you do not need to list gunicorn in your INSTALLED_APPS. From your virtualenv run gunicorn -b 192.168.1.1:9000 myproject.wsgi:application, where myproject is your Django project name. You can drop in any gunicorn options you like as well. As for running in PyCharm you need to create a Python run configuration, not Django, and provide the following information:

  1. Script - /Users/craig/.virtualenvs/blogenv/bin/gunicorn (your virtualenv of choice)
  2. Script parameters: -b 192.168.1.1:9000 myproject.wsgi:application
  3. Working directory: /Users/craig/PycharmProjects/MyDjangoApp
There is another reason to use gunicorn directly - you can use it with supervisor.


You may come across the gunicorn_django application as well. That is fine to use if you are not on Django 1.4, otherwise use the gunicorn application direct, as shown above.

There is also devserver from David Cramer. I've not used it personally but the other projects that David works on are top rate, so I imagine this is too.

Thursday, July 12, 2012

PyCharm for Django development

I have been dabbling with Django for a few years now. Previously, I always used Eclipse for this. Eclipse by itself doesn't support Python or Django but add PyDev or Aptana Studio and it merrily understands what is going on. This was great when I worked on multiple projects in multiple languages. I now find myself working solely in a Python/Django environment. PyCharm was getting rave reviews from my former work colleagues, but I did so little Django it didn't seem worth making the transition. However, with a new role I thought now was the time and I haven't looked back. If you have ever used IntelliJ IDEA for Java you'll be familiar with PyCharm as it is built upon the same framework. Being written in Java also means you are able to code on your Mac, Linux and Windows devices.

For once PyCharm is fully aware of Django, not just syntax highlighting (which was also hit and miss especially in templates mixing HTML/CSS/Django template syntax in Eclipse).  Truth be told I have barely touched the surface of features that PyCharm packs in. See http://www.jetbrains.com/pycharm/features/index.html for a full run down. Each release, of which updates are frequent, either refine or add more features. In the 2.5 release virtualenv support was added, it worked with manual intervention before, but now PyCharm automatically detects them. In addition the support at JetBrains is superb. Admittedly this is a commercial product, although they license favorably for open source project - free.

Just like IntelliJ various plugins are available. A whole host ship by default, Git, CoffeeScript and JavaScript to name a few. More are available online at the plugin repository. The nginx support and NodeJS plugins caught my eye.


One of my favorite features is suport for running mange.py tasks from within the IDE, no need to setup the environment and run from there, as shown (image courtesy of JetBrains).

In fairness I did look at other Python IDEs with Django support, but feel they fall into the same camp as Eclipse. The Django support is just tacked on and is limited. With PyCharm you can see Django support was core to the design and features implemented. I see the PyCharm team will be at DjangoCon US. This is great for two reasons a) I can congratulate them on a great IDE and b) they will be bring a new update (http://blog.jetbrains.com/pycharm/2012/06/pycharm-at-djangocons-around-the-world/). 

Tuesday, May 1, 2012

Homebrew

Courtesy of https://twitter.com/MacHomebrew/
Homebrew, the OS X package manager, not the beverage, is the third package manager I've used on OS X. I started in 2005 with Fink and MacPorts. MacPorts seemed to be more active, especially with the flurry of Leopard releases. Indeed it also has some advanced features including dependencies to let you install MAMP stacks. However, I find I prefer to have control of my MAMP stack especially in my volatile development environments, upgrades can be particularly painful with MacPorts.

Homebrew has quite a different approach. There is no need for sudo, all binaries get symlinks in /usr/local, but point to /usr/local/Cellar which controls the formula (aka package) version. The formula scripts are ruby based, so adding your own is very easy.

If you don't want to search forumlae via the command-line then head to http://braumeister.org/ (another GitHub project).

Installation is pretty simple. If you are a developer, e.g. you have Xcode installed it is probably a one-liner. Installation alongside MacPorts is not recommended, but to check you can get everything you need from Homebrew it will probably not cause too many problems.

Now you have brew in your path lets explore some use cases:

  • brew list - list installed formulae
  • brew search wget - search for formula called wget
  • brew info wget - get information about the formula wget
  • brew install wget - install wget
  • brew uninstall wget -uninstall wget
  • brew update - update homebrew (effectively a git pull)
  • brew outdated - list formulae which are out of date
  • brew upgrade - upgrade all outdated formulae

For more features see the FAQ and follow @MacHomebrew.

Now I wonder if I can convince puppet to use Homebrew over MacPorts...

Sunday, March 4, 2012

Python Virtual Environments


Anyone who uses Python regularly will soon encounter the need to install an extra package be it MySQLdb, numpy or Django. Thanks to tools like pip and easy_install this is very trivial to do (as long as you have internet access from your command-line. Those of you behind corporate firewalls might want to try cntlm to help). Typically they will install the package into your home directory for only you to use. If you are root they will install into the system installation for all users to use. In both scenarios it is hard to mix and match multiple versions of packages. As a developer this becomes very important, I might want to try the lastest version of a package, but for my production code I want to remain on the stable release. The easiest way to manage this is using virtual environments. Virtualenv is a python package that allows you to manage multiple python instances with different combinations of packages. It is easy to mix python and package versions for development and production code. Virtualenv is great and has been extended with virtualenvwrappers, which adds even more functionality.

To get started run the following:

easy_install virtualenv
easy_install virtualenvwrappers

Next you need to alter your environment slightly in ~/.bashrc or equivalent add:


export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/projects
export VIRTUALENVWRAPPER_VIRTUALENV=/usr/local/bin/virtualenv
source /usr/local/bin/virtualenvwrapper.sh

Fire up a new terminal or run source ~/.bashrc and the new commands are now available to you. Lets create our new virtual environment with mkvirtualenv test. This creates and loads the new virtual environment called test. Your command prompt will indicate you are using test as well. Now run easy_install to install your packages. You can see what packages are available with lssitepackages. To leave this environment run deactivate. Create another environment and list your packages, there will be none. Switch back to test with workon test.

You can tie this virtual environment to Eclipse by pointing at the python for the test environment, which can be found at ~/.virtualenvs/test/bin/python.

See all the commands available for virtualenvwrappers at http://www.doughellmann.com/projects/virtualenvwrapper/. Enjoy your new virtual python environments!

Image courtesy of http://www.flickr.com/photos/ptshello/2312382347/











Monday, February 20, 2012

Notes and more everywhere


I download lots of iOS apps, mainly out of curiosity. Some I grow to use others just disappear. One which I use more and more and has got better and better is Evernote. With the tagline "Remember Everything" you use Evernote via the web, iOS, Android, BlackBerry etc. You can do simple notes, todo lists, upload files or images. I started to use it for making notes at conferences. The Evernote ecosystem has grown and a great feature is the web clipper. This add-in for your favorite browser clips all or part of a web site and saves it as an evernote note. Once in evernote it is easy to search through it via tags; my google searches even checks my evernote account now (with permission). Skitch is a recent acquisition but makes annotating images dead easy on your desktop, iPad or Android device (and then saves the results to Evernote).

It works on a freemium model. You get everything with some restrictions (60MB upload a month, no offline data on mobile devices). They get you hooked then you can pay for unlimited access, which I'm giving serious consideration to. Why not give it a try at your next conference? I hear CUP XIII should be good fun.

'via Blog this'

Sunday, February 19, 2012

Operating systems which are current for now


After switching employer I'm no longer tied to Windows Vista & Red Hat. Truth be told I'm quite happy on Red Hat, Vista less so. Anyway, my new setup is Mac OS X Lion & Ubuntu. I use Lion on my personal computer so that is no problem. Although from a developer perspective OS X isn't Linux like Red Hat, but once you learn the tweaks it does play ball.



As for my new Linux workstation I was encouraged to use Ubuntu. I tried Ubuntu briefly at University and didn't really see what the fuss was about. If anything I found it really annoying as everything I wanted (developer/sysadmin tools) were not installed. However, a new job means an open mind and I've landed with Ubuntu 11.10. I had heard about Unity and indeed it is an impressive desktop, which has been a pleasure to use. Ubuntu 12.04 is out in a matter of weeks and will feature HUD, which again is very nice, but is nothing new over one of my favorite Mac apps: Alfred (Like QuickSilver).

Mountain Lion has just been announced as well. So there will be plenty of activity for my operating systems this year, just after I've got them both setup the way I want them!

All change

After three years at a AstraZeneca I decided it was time for a change. So instead of Manchester I opted for Santa Fe, New Mexico. I've been here for nearly two weeks now and in terms of culture, food and weather it couldn't be more different to Manchester. Thankfully, I'm loving every minute of it. In no small part that is thanks to my new employer - OpenEye. I've had a week at work so far and am immersing myself in the startup like culture that OpenEye has.

I'm speaking at CUP XIII next month on some of my work from AstraZeneca entitled, "Keepalive with OEChem 24x7x365". Given I work for a software company now I expect (though I won't promise) to actually post more to this blog.