MrHacks.tumblr.com

Progressive Programming, Politics, Electronics, and Gardening.
Feb 19 '11

Python in color!

So, I’m currently getting my feet wet in Python again while I work on my bash script and tumblr theme.

Before I begin, I’d like to take the time to mention a couple of things with the source code of my current tumblr theme (which is modified from someone elses at the moment) and state that I hope that I can replace some of the Yahoo! User Interface stuff with something more popular: jQuery. While Tumblr does have some comprehensive documentation on how to write your own theme, everything around their syntax that you write should still be able to use advanced code. So as we move toward HTML5, it would seem the best programming practice is to use a programming language that can handle both high-level object-oriented programing (like C, C++, or Java) and script programming (bash, PERL, PHP). Python offers the best of both worlds. While PERL can do the same thing as Python and is a bit more C-like, Python is a versatile programming language that can be used for both writing program and writing scripts. It is also possible to migrate a Python program into becoming a C or C++ program so that it can run faster. (Note: I did not say “convert”. Migrate seems a more appropriate term because you still need to go into a python program and modify a few things to make it become a C/C++ program.

Call me old school, but any program that requires the less amount of “mousework” (using the mouse to make things) and greater coding and usability with just the keyboad is way better. Take my gardening data table that I made yesterday. If I wanted to, I could use a program like awk or cut to move the columns around in a single command. No menus to fumble with, no worries about making mistakes. In fact, the only time things could change is if I gave a command to redirect it to another file or to make the current command actually modify the file.

So if Python is idea for programming and scripting, then what is the most ideal Python-based web framework? And answer is as easy as pie: CherryPy to be exact! For a few months now, I’ve used SABnzbd+ to retrieve Usenet binary posts. (Yes, the Usenet is still there. But only a few groups are worth going to, but I’ll explain that some other time.) SABnzbd+ uses CherryPy to effciently use the localhost without the bloat or obsfucation that Django. Right now, I just need to get that set up.

But enough blathering, today I’d like to start off with something simple.

I find the “Hello World” program to be a waste of bytes. What good is it to write a program if you don’t apply several tasks in the same program.

The following program has been proven to work on Linux in a few other programming languages, including Bash and C/C++. Now this program is ported to Python. YMMV!

#!/usr/bin/python
# File: meat.py
# Info: A file with no mystery but plenty of protein.
#    Print text in COLOR! 
red = "\x1b[1;31m"      # set the string color to red
grn = "\x1b[1;32m"      # green
blu = "\x1b[1;34m"      # blue
rst = "\x1b[0m"         # reset the string back to normal (required!)
 
print "%sHello %sColorful %sWorld%s" % (red,grn,blu,rst)

What you should see should be something like this

$ chmod u+x meat.py
$ ./meat.py
Hello Colorful World

(Note: If you are looking at this on your Tumblr dashboard, you’re not going to see the colors, so look at this post at my Tumblr page!)

I added the chmod command so that the program can be run as ./meat.py instead of python meat.py. I only recommend doing this for the main module of a program that you are making.

As for the colors, they are simple hacks that have been around for decades, but thanks to the Windows centric world, the fun of playing with escape sequences has been moot over the past decade.

I’m currently trying to get a program called beep to work so that I can play around with another terminal feature that has been left by the wayside: Making Noise. If anyone can provide a solution to this problem, answer here.

It would be nice if I could get a little help answering some of my questions so that I can close some of the currently 86 tabs that are open in my browser.

View comments Tags: python linux bash jquery html5 c c++ CherryPy SABnzbd+ beep

Blog comments powered by Disqus