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 following new features and oddities:

Release 1.6.0

o Code-completion added to the debug console
o Entries in the debug console are evaluated on a line-by-line basis (previously an empty line was needed)
o Threads started with thread.start_new_thread are now properly traced in the debugger
o Added method -- pydevd.set_pm_excepthook() -- which clients may use to debug uncaught exceptions
o Printing exception when unable to connect in the debugger
o Interactive console may be created using the eclipse vm (which may be used for experimenting with Eclipse)
o Apply patch working (Fixed NPE when opening compare editor in a dialog)
o Added compatibility to Aptana Studio 3 (Beta) -- release from July 12th

http://pydev.org/

Among others, Pydev also integrates gracefully with Jython and IronPython (that’s right, it’s a competitor of visual studio), contains templates and tools for AppEngine and Django projects, inline unit testing, an interactive console with autocomplete, ability to refactor portions of your code, autoimport and dependency resolution (even for unimported modules), a nice set of embeded debugging tools, and of course, syntax coloring.

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, if we need to check whether a list is unique, we would have to construct another list to hold reference to all objects that have already been iterated over. This is where one of Python’s uglier “features” comes into play.

Python allows a list to reference itself during list comprehension by keeping track of the object, and all subsequent levels of iteration, in local scope:

#Single step removal of duplicate values.

>>> (lambda xs: [x for x in xs if x not in locals()['_[1]']])([1,2,2,3,4,4,5,5,5,6])
[1, 2, 3, 4, 5, 6]

But be warned: If you do this regularly, the python gods will hunt you down and torture you.

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

strcpy() implementation in C/C++

One of our ubiquitous C functions can be rather easily recreated via C:

char* strcpy(char* other, char* self){
    while (*self) *other++=*self++;
    *other = '\0';
}

Note that C strings are null terminating, hence we create a simple while loop in order to copy everything before we reach the null \0 character. In order to follow convention and meet C’s expectation, we manually add a null character at the end of the string.

Posted in Blog, Programming, Snippets | Tagged , , | 3 Comments

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]
# [1, 3, 5, 7, 9, 11]

Now let’s say that we want another list b with the same elements as a, this is relatively simple to accomplish, right?

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

However, when you need to update an element of object b, something unexpected may happen.

a[0] = 0

# a = [0, 3, 5, 7, 9, 11]
# b = [0, 3, 5, 7, 9, 11]

#By the same analogy:
b[0] = 1

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

Continue reading

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<<3 is equivalent to multiplying n by 2**3 or 8n. To reach 6n, we simply subtract 2n.

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

Tips and Tricks for Lua Beginners: Writing an at.exit registry

It’s relatively simple to write a function that takes in a function and executes it once Lua terminates.

at = {}
function at.exit(fn)
     getmetatable(newproxy(true)).__gc = fn
end

Continue reading

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

Lua – Get a table of function arguments

The lua standard libraries do not provide anything that helps you get the table of arguments for a given function. This can be accomplished by directly introspecting the bytecode of the function. The following snippet generates a function get_args that behaves as follows:

get_args(function(a,b,c) end) --> {a,b,c}

This also works with functions that have the varArgs flag:

get_args(function(a,b,c,...) end) --> {a,b,c,...}

and gracefully responds to C functions that cannot be dumped as bytecode:

get_args(table.insert) --> {?}

Continue reading

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

Lua – Get number of parameters in a function

The following snippet defines a function num_args(func) that returns the number of parameters within a function as a string:

num_args(function(a,b,c) end) --> 3

It also works with functions that have variable arguments:

num_args(function(a,...) end) --> 1+

However, since C functions do not embed parameter information within Lua bytecodes, these will return “?”:

num_args(string.gmatch) --> ?

Continue reading

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