Today’s post was motivated by a problem at Project Euler involving the Collatz conjecture. Since this is a little bit spoiler-y, I’ve included the main body of the post after the cut. Don’t read it unless you’ve either solved the problem at Project Euler or you’re actually looking for spoilers. You have been warned.
Archive for the ‘code’ Category
Stuff involving code.
A bit of Mathematica
Posted by thepythonista on 29 September 2008
Posted in code | Leave a Comment »
Pure Python Fibonacci Numbers
Posted by thepythonista on 3 July 2008
A while back, I came across this post, in which the author implements a couple of different algorithms to generate Fibonacci numbers in Python. What he finally ends up with is an algorithm that essentially does matrix exponentiation by repeated squaring, and it runs fairly fast.
Read the rest of this entry »
Posted in code, python | Leave a Comment »
Seek and ye shall find….
Posted by thepythonista on 13 June 2008
One of the things I like best about the Python community is that, chances are, if you’re after a module to do X, someone’s probably already written it (and likely at least 2 or 3 someones have). While the Python Package Index (PyPI) isn’t nearly as comprehensive as Perl’s CPAN, or the LaTeX equivalent (CTAN), there’s a lot of good stuff on there.
Specifically, I was looking for a database interface implemented as a Python iterator. Why, you might ask? Well, so I could do stuff like
results = [r for r in records if "foo" in r.name]
using list comprehensions. I know I could easily do this stuff with SQL cursors, but, for this particular app, I’d like to avoid that as much as possible, keeping the code “pure python,” if I can.
A quick search of PyPI yielded buzhug, which does exactly what I want it to. I can’t really evaluate it yet, since I’m not done playing with it, but I’ll probably post about it sometime later. If anyone else has any experiences with buzhug, I’d really appreciate if you dropped me a comment.
Posted in code, free software, python | Leave a Comment »
The quest to translate PyPy
Posted by thepythonista on 3 June 2008
I’ve been playing around a little with PyPy, the reimplementation of Python in Python, recently. Unfortunately, my laptop isn’t beefy enough to translate the Python-based interpreter to C. I guess it’ll have to wait until after I get another gig or two of RAM (it reportedly takes about a gig just to compile the interpreter to C code which gcc can then compile, to get a native Python interpreter).
You may have foiled me this time, PyPy, but I’ll be back!
Posted in code, free software, python | Leave a Comment »
Help a Pythonista out!
Posted by thepythonista on 2 June 2008
Has anybody read Programming Collective Intelligence? I’m considering purchasing it from amazon.com, but I don’t really trust the reviews there much. Post a comment if you’ve read this book, and tell me what you think of it!
Posted in code, python | Leave a Comment »
Macros (for Python)!
Posted by thepythonista on 2 June 2008
As I hinted at in my previous post, Andrew Dalke has written a very interesting piece of software: python4ply. Basically, he’s used ply to implement a parser and lexer for Python 2.5, in Python. The tutorial gives some neat examples of how you can modify this parser to enhance the grammar of Python. (My favorite is where he implemented syntax-level support for decimal.Decimal numbers.)
I’d definitely like to play with this a bit. He calls it a “half-formed macro system for Python,” and I see the similarity to Lisp macros. “Half-formed,” perhaps, because ply4python “macros” are basically second-class citizens: there’s no support for them in the language itself.
In spite of its limitations, this is definitely an interesting piece of software. Thanks, Andrew.
Posted in code, python | Leave a Comment »
Syntax highlighting for the command line
Posted by thepythonista on 26 May 2008
I found a fascinating post over at The Py Side of Life that tells how to get Python syntax highlighting with the Unix command less:
The de-facto standard UNIX pager less supports an environment variable called LESSOPEN that can be set to the name of an input preprocessor. It is normally used to transparently view compressed files etc., but of course it can also colorize your source files using Pygments!
Read the entire post here.
Posted in code, python | Leave a Comment »