Category Archives: Python

Pydev: The Best Python IDE

While I don’t usually advertise, I love eclipse for all of its feature rich implementations of language specific IDE’s. Among those is Pydev, a complete first class IDE for Python.
Recently, Pydev just hit version 1.6.0 (Congratulations!) with the…

Posted in Programming, Python | Tagged , , , , , | Leave a comment

Python: Self references in List comprehension

One of the great abilities in Python that is copied over from Haskell is the ability to construct lists from other lists or iterators inline. This is of course limiting in certain cases where self references are necessary. For example,…

Posted in Blog, Programming, Python, Snippets | Tagged , , , , , , | Leave a comment

Python Tip: Copying an object

One of the several “features” of python that often land beginners head over feet with their faces in the mud is the distinction between references and copies. Say we have the following list a:

a = [1,3,5,7,9,11]

Posted in Blog, Programming, Python, Snippets | Tagged , , , , , , , , , , | Leave a comment

Brain Teaser: Multiply by 6

How do I go about multiplying a real number n by 6 in python without using the * or the + operators?

Solution:

x6 = lambda n: (n<<3) - n - n

Rationale:

n

Posted in Blog, Programming, Python, Snippets | Tagged , , , | Leave a comment

Linked Lists via ctypes in Python

from ctypes import *

class linknode(Structure):
    pass
linknode._fields_ = [
                ("nextNode", POINTER(linknode)),
                ("intData", c_int),
                ]
class linked_list():
    head_node = None
    def add(self, int_data):
        node_to_add = linknode(intData = c_int(int_data))
        if (self.head_node == None):
            self.head_node = node_to_add
        else:
            traverse_node

Posted in Programming, Python, Snippets | Tagged , , , , | Leave a comment

Best Hello World App Ever :D

Written in Python :D

See http://failboat.me/2009/cute-functions-creating-pseudo-operators-in-python/ for more details.

#Hello World

class Operator(object):
    def __init__(self, func, count=2):
        self._func_ = func
        self._args_ = []
        self._count_ = count
    def __ror__(self, first_arg):
        self._args_.append(first_arg)

Posted in Blog, Programming, Python, Snippets | Tagged , , | Leave a comment

Gettings the most out of your bits

The following will produce the bit representation of an ord(byte) in python

def bit(chr, i=0, str=''):
    if i > 7: return str
    coef = (2**(8-i-1))
    if chr >= coef: return bit(chr-coef, i+1, str+'1')
    return bit(chr, i+1, str+'0')

Posted in Blog, Programming, Python, Snippets | Leave a comment

Primitive Collision Detection – Circular objects

Consider the following –

If you are designing a game, and have the players shaped as circles, how would you detect bullet collisions with the players?

Well, the problem with bullet collision detection is that you can’t just use…

Posted in Blog, Programming, Python, Snippets | Leave a comment