Tuesday, April 22, 2014

How to build a full-stack python

PyCon US 2014, Kate Heddleston

This is a talk about building full-stack python web applications where you manage every part of the application yourself. I will walk through how to setup a production server with your web application code, a local development environment using vagrant, and how to deploy from your local environment to production. I will also walk through python and Django libraries that will make your life easier.

Postgres Performance for Humans

PyCon US 2014, Craig Kerstiens

Postgres Performance for Humans

To many developers the database is a black box. You expect to be able to put data into your database, have it to stay there, and get it out when you query it... hopefully in a performant manner. When its not performant enough the two options are usually add some indexes or throw some hardware at it. We'll walk through a bit of a clearer guide of how you can understand and manage DB performance.

Saturday, April 19, 2014

Getting Started Testing

PyCon US 2014, Ned Batchelder

If you've never written tests before, you probably know you should, but view the whole process as a bureaucratic paperwork nightmare to check off on your ready-to-ship checklist. This is the wrong way to approach testing. Tests are a solution to a problem that is important to you: does my code work? I'll show how Python tests are written, and why.

-- I've heard it's said DRY doesn't apply to testing, when I see my coworker just cut and paste one test to the next and I go back and something changes with the test, it feels like it should be dry?
-- That's silly, yeah! I have no idea why people say DRY should not apply to testing. Testing is one of those mysterious worlds where you'll find experts and they will say things to you (not me, I can be trusted, but the other ones who will say these mysterious things :) and you believe them because testing is mysterious, but maybe they are wrong.
-- Well, I mean in one case where it makes sense to me is that once you start making your tests DRY your tests become more complex and then you have to test your tests.
-- Yeah, you do. I know why they say they should not be DRY, because some people like tests sort almost documentation of how things should work and they like a test case be completely self-contained. But if that means that the same 10 lines setting up the same fixture you are going to use in sixty tests that just seems silly to me.

A Scenic Drive through the Django Request-Response Cycle

PyCon US 2014, Dan Langer:

A simple "Hello World!" page, served via Django, passes though a surprising number of layers & components. For a newcomer to the language or the platform, this can be overwhelming at the start. Here I'll take you on a drive through Django's request-response cycle, focusing on using its layered model to understand what's going on and get things done.

Thursday, April 17, 2014

Insertion sort in Python

Implementation of Insertion sort in Python:
def sort(array):

  for i, item in enumerate(array):
    j = i
    while j > 0 and array[j - 1] > item:
      array[j - 1], array[j] = item, array[j - 1]
      j -= 1

  return array

import random

for i in xrange(10000):
  n = random.randint(10, 1000)
  array = [random.randint(0, n) for j in xrange(n)]
  print "%s (%s)," % (i, n),
  assert sorted(array) == sort(array)

Wednesday, April 9, 2014

Get current exception details in ipdb

So, I am debugging a program and want to see what is  currently active exception.

ipdb does not have a buitl-in command for this, so let's try using sys.exc_info():

> /home/vic/projects/test.py(4)<module>()
      2     1/0
      3 except:
----> 4     import ipdb; ipdb.set_trace()

ipdb> import sys; sys.exc_info()
(<type 'exceptions.AttributeError'>, AttributeError("Pdb instance has no attribute 'do_import'",), <traceback object at 0x2341bd8>)


Ok, let's ask on Stackoverflow. And here is the solution: put

     alias exc_info !import sys; sys.exc_info() %1

into ~/.pdbrc

> /home/vic/projects/test.py(4)<module>()
      2     1/0
      3 except:
----> 4     import ipdb; ipdb.set_trace()

ipdb> exc_info
(<type 'exceptions.ZeroDivisionError'>, ZeroDivisionError('integer division or modulo by zero',), <traceback object at 0x7fe8dc7909e0>)

Tuesday, April 8, 2014

Setup IntelliJ IDEA with GoLang on Ubuntu

  1. Install golang package
  2. Put this into ~/.profile:
    export GOROOT=$(go env GOROOT)
    export GOPATH="$HOME/projects/go"
  3. Download and unpack IntelliJ IDEA Community Edition from here. Launch idea-IC-135.480/bin/idea.sh
  4. Install GoLang plugin for IDEA. Open the Plugins installation page: File -> Settings -> Plugins -> Browse Repositories... Search for "golang". Right click on the proper plugin and install.
More info here.