You are hereCoding
Coding
Coding tips and tricks, pitfalls, snippets, tutorials and more.
Parser Generator in Python - Quick PLY Introduction
I have been using regular expressions within my Python projects for a while now, but I've never felt the need to use a full-featured parser generator. This changed yesterday and I had a look at PLY, a pythonic implementation for Lex/Yacc. Being used to parser generation in Java using ANTLR or Cup I came across some remarkable differences which I want to point out for you after the stop. You will also see a small calculator code snippet.
Using D-Bus to communicate with Gnome Network Manager
Today, I tried to figure out how to execute scripts in Ubuntu on network connection changes. I came across the D-Bus system, which is used for inter-application communication in Linux. It is indeed very easy to access D-Bus within a Python program. If you read on, you will see the source code of a Python script, that - when launched - receives events from the Gnome Network Manager via the D-Bus interface.
Creativity - New LAN Game on Project Page!

In the last couple of days, my brother and me spent some time to code a little game in Python/PyQt. Though the code is quite a hack, you can still use the program to spend some funny nights with your friends. The program runs on all major platforms and is simple to use. The only thing you will need is a game file with questions and answers. Read more about everything on the application project page here. Feel free to leave your comments.
Python Coolness 2: Tips and Tricks
Welcome back Pythoneers! Last time we learned the underlying concepts of the Python programming language and runtime environment. In this second part, I would like to show you some of the Python tricks, that make this programming language so special. I will also briefly highlight some of the areas, which prove to be more difficult or dangerous than in other common languages, such as Java and C++. After this tutorial you will be able to write lines of code that will definitely impress your fellow geeks.
Python Coolness Series, Part 1
Hello fellow coders! Today, I present you the Python Coolness Series, which will (hopefully) show how you can use Python to make cool apps and tools, that would otherwise take days or weeks to create. We will make use of many cool high-level Python APIs, that wrap complex code, and allow you to focus on the aspects that interest you most. So, you won't be seeing any Hello-Worlds in this series, but actually some really cool tools that you can show off to your friends. Take note, that some of these tools will be Mac OS X only. I also recommend all Mac users, who would like to follow this series, to grab a copy of Fink, so that they can install Python modules painlessly.
Script: Highlight Output of Program
I was running a rather large configure script this morning, and keeping my eyes peeled for any lines on OpenGL (Leopard users will know why). Now, a Linux ubern00b would probably roll his eyes and tell me that I should simply pipe configure through grep, but I would also like to see the rest of the configure output, just in case there are any errors.
Enter my little highlight script. Simply pass it a grep regular expression, and any lines that match will be highlighted in yellow. Non-matching lines will be left as they are.
#!/bin/bash # Usage: higrep <regexp> if [[ "$#" != "1" ]]; then echo "Usage: higrep <regexp>" exit -1 fi while read LINE; do FOUND=$(echo "$LINE" | grep -c "$1") if [[ $FOUND -gt 0 ]]; then echo -e '\E[30;43m'${LINE }'\033[0m' else echo -e "$LINE" fi done
So here we first check the usage, and then pass each line to grep. If a match is found, we output the highlighted line. Otherwise we output the line as it was. Of course, this script could be extended to allow more colors, or it could be simplified to use only bash expression matching to avoid the call of another process. Anyway, save this to a script called higrep (or anything else geeky sounding), and do something like:
ps -A | higrep 'Applications'
Happy hi-grepping!