Category Archives: Tutorials

Intro to Twisted

I presented a short Intro to Twisted at the SoCal Piggies meetup in Santa Monica last night. It went pretty well and I think I did a good job explaining the basics. I know it took me a long time to even become even remotely familiar with the way Twisted operates, so I felt like this was a good choice for a five-minute presentation. Short, sweet, and to the point!

Here is is for your viewing pleasure:

I borrowed the image on slide five from the Twisted Intro, which is a very excellent introductory tutorial written by Dave Peticolas. If you are curious about Twisted, what it can do, or are even experienced and want a great refresher, it’s definitely worth your time to give it a look!

On Using eval() in Python

Originally posted on StackOverflow.

I have used eval() in the past (and still do from time-to-time) for massaging data during quick and dirty operations. It is part of the toolkit that can be used for getting a job done, but should NEVER be used for anything you plan to use in production such as any command-line tools or scripts, because of all the reasons mentioned in the other answers.

You cannot trust your users–ever–to do the right thing. In most cases they will, but you have to expect them to do all of the things you never thought of and find all of the bugs you never expected. This is precisely where eval() goes from being a tool to a liability.

A perfect example of this would be using Django, when constructing a QuerySet. The parameters passed to a query accepts keyword arguments, that look something like this:

If you’re programmatically assigning arguments, you might think to do something like this:

But there is always a better way that doesn’t use eval(), which is passing a dictionary by reference:

By doing it this way, it’s not only faster performance-wise, but also safer and more Pythonic.

Moral of the story?

Use of eval() is ok for small tasks, tests, and truly temporary things, but bad for permanent usage because there is almost certainly always a better way to do it!

Using Caps Lock on Your iPhone

I’ve had an iPhone for over a year and still hadn’t learned how to use the mysterious caps lock feature.  I enabled it in the settings, but had no idea how the hell to make use of it!  Of course it turns out to be just as intuitive as everything else on the iPhone. I just figured this out today so I thought I would share it.

Step 1:  Enable Caps Lock

Navigate to Settings > General > Keyboard and switch “Enable Caps Lock” to “ON”.

Enable Caps Lock

Enable Caps Lock

Step 2: Use Caps Lock

To use caps lock, all you have to do is double tap the shift key on the lower left of your keyboard.The key will turn blue indicating that you have successfully engaged CAPS LOCK.



To turn it off, just tap the shift key once more.

Your life has now been enriched, and I can move on with mine.

Python List Comprehension for Dummies

So I code a lot. I code in Python a lot. You might say I love Python. I might say you’re right.

One of the most powerful things about Python is its ability to iterate over ANYTHING as if it were a list. Lists, tuples, dictionaries, and even strings can all be iterated quickly and elegantly. Python also introduces a concept known as list comprehension which allows you to do rather complex filtering of list contents within a single statement.

To illustrate how awesome and powerful list comprehension is, let’s start with a basic example that is NOT using it:

So, let’s assume that we want to identify all even numbers inside of my_list, and put them into a new list called evens the old-fashoned way:

Why the old-fashioned way sucks First things first, the empty list called evens had to be declared ahead of time. This is because when we looped thru the list called my_list using the for statement, when the if test is performed on each item we have to reference evens by name to append() the even numbers to it.

Why list comprehension rocks With list comprehension, the logic that isolates the even numbers and the declaration of the list that will capture this output are compressed into a single statement:

The logic is encapsulated in [square brackets] indicating that the output will be a list. The list comprehension itself is the logic between the brackets that determines what will be in the list that it spits out.

So list comprehensions at their most basic level allow for compression of code and streamlining of logical statements. Advanced usage of list comprehension can get pretty silly, but then so can nested loop statements.  It supports nesting as many statements as you can throw at it so longs as they are syntactically correct.

If you find yourself coding shit like this:

Then maybe list comprehension is for you:

No offense to anyone named Joe, Jim, or Jon.