Shawn Driscoll's Tech Blog

Stuff That Happened in the Past

Wednesday, May 16, 2012

Printing Formatted Floats in Python

A long time ago, I had written a program to do a die roll chart for GURPS. I used QBASIC to get the results.

Since learning Python, I thought I'd give it a shot at calculating the results for me. But getting the decimal point to line up nice in the percent column was not automatic for me. I could not find a print "formatter" for Python 2.5.4 that would let me state how many digits I wanted displayed on each side of the decimal point. I looked on the Interweb and saw lots of people asking the same question about Python and not getting much help. I figured Python 3's new print() function may very well have such a feature, but I got my system all perfect with verison 2.5.4 for now.

So I thought back to 1979 when I had a class assignment to print a table of decimal values which needed its columns all lined up. This was on an HP-2000F running BASIC. The trick was, we couldn't use "PRINT USING" to print the floating-point numbers.

This line from Python is not as robust as that. For I only needed one digit after the decimal point here. And I'm still using a print "formatter". Here is the line:

print '%2d.%d' % ((dr[i] * 100 / n), ((dr[i] * 100. / n) - (dr[i] * 100 / n)) * 10)

The first value is the "10"s or "1"s digit with nothing after the decimal. The second value is a float of the first value minus the first value, leaving just the digits after the decimal point which gets multiplied by ten and rounded down. NOTE: There is a "." printed between both values.

Anyway... I'm posting this so that aliens exploring our burnt out world may find this bit of info useful.

Monday, May 14, 2012

A Not-So-Needed Distraction

I got side-tracked a bit last weekend. I did finish the Core Python Programming book and did a review of it on Amazon. Afterwords, I installed and played Bioshock for the very first time ever. Then I gave System Shock 2 a try (which Bioshock borrowed heavily from). I won't go into the reason here though because this is my Tech Blog.

I'm now reading through the O'Reilly Python in a Nutshell book (2nd Edition), by Alex Martelli. I bought this book years ago for when I'd have time to actually do some Python programming (which is now, so that's good). My plan is to spend this week doing some said Python programming.

Monday, May 7, 2012

24 Hours Later, MinGW GCC 4.3.3 is Saving my Privates

Ok. Things got a lot worse after my last post. My poor Microsoft Visual C++ 6.0 is no longer supported by my copy of Pyton 2.5.4. The planets are no longer aligned for these two. Lots of fussing and fighting with Python's MSVC++ 7.1's code and my Python's re-compiled MSVC++ 6.0 code... And still no way to fool all of the software all of the time into letting me add C code extensions to Python (ie. getting Python to import any of my compiled C code).

I have to say that I'm not much of a Google person. I prefer Bing. It showed me all the problems people had back in the day relating to the problems I saw waiting for me around the corner. Not only was Python being developed in MSVC++ 7.1 (aka MS Visual C++ 2003), GNU C++ was having a meltdown. GNU C++ had too many cooks in the kitchen, altering each other's source code and posting them on the web as "official" releases. Particularly, the MinGW variety of the famous GCC console command.

I already new I was not going to buy another Microsoft C compiler just so I could toy around with Python (before two weeks ago, I didn't even know I would be programming in Python). MSVC++ 6.0 saved my skin a few times in college twelve years ago, but it was not up to this task.

So the trick was to find the eye in the hurricane. Sure enough, someone was calm enough back then to package an installer for GNU C++ that would allow for setup options for users wanting to create C extensions for Python.

Windows has a Common Files folder. But this installer was like having a Common Sense folder.

So long story short, I'm able to compile C/C++ code as extensions for Python 2.5.4 (ie. Python imports my C code now). The awesome MinGW GCC 4.3.3 can be found at

Sunday, May 6, 2012

Debug Shmebug

What? I'm missing a Python.h file? Ok, I'll point MS Visual C++ 6.0 to Python25's INCLUDE folder.

What? I'm missing a python25_d.lib file? Ok, I'll just do a search for it and... What the hey?! Oh, that's right. I did the .msi install of Python 2.5.4 which doesn't include the python25_d.lib file.

So I'll just download 2.5.4 again and... What the...? Ok, cool. I found a nearly dead link to it still. That was a close one. What kind of compression did they use on this .tar whatever? I extracted something... just a huge garbage file. Hmmm... What if I extract the garbage? It's extracting. I don't know what, but it's extracting.

Ok. What do I got here? Files with no extensions (a pet peeve of mine, by the way). I see a README... Lame. I see another README... Something about only MSVC++ 7 can compile this. Panic! What's in this folder?... It says MSVC++ 8 is required. I'm going the wrong direction.

Finally! Something mentioning MSVC++ 6.0 and NT/2000? Hmmm.... I'm doing the math in my head... 1999 was about the time that Microsoft Visual Studio 6 had come out. So XP was not out yet.

Let's see if this Python C source code loads into... It did. But what the hell? Source safe code something or other? Need username and password? After 20 minutes I figured out that I can just hit cancel cancel cancel cancel cancel cancel don't cancel cancel cancel ok.

Ok, what do I compile? Hmmm... Try the Pythoncore one... Boring. Try just the Python one... Hey, I got a DOS version of Python popping up. No IDLE? Close it.

Now let's see what .lib files Visual Studio C++ 6.0 made for me.... I see it! python25_d.lib! Get over here! Ok, now... Copy to C:\Python25\libs...


Ok, now what was I trying to do nearly three hours ago? I remember. Step one of three involving putting a wrapper around a C program so it can be used as a Python extension. Sounds easy enough... Easy as cake. Piece of pie.

Fire up MSVC++ 6 again and look for where my C program was... Ok. Loading, linking, no way! It worked! Up popped a DOS window with the C code running in it.

And that was supposed to be the easy part of wrapping. Only two more steps to go.

Using a GUI to Write a GUI (or Two GUIs Make a...?)

I just found out about wxGlade, so I had to give that a try. It's compatible with Python 2.5 still, which is great. I was able to zap out some Python code to use with wxPython and Python crunched through it without errors.

That was cool.

So what all I got here?



Python 2.5.4 win32
wxPython ANSI win32
wxGlade 0.6.5 win32


Saturday, May 5, 2012

Python megawidgets (or Pmw)

Just downloaded Pmw 1.3.2 and have been trying out the demos that come with it. It seems to run fine so far in Python 2.5.4, which I'm using because my Python books are all geared for that version. This Pmw was written for Python 1.5 and later it says.

So far, I rather like this Pmw (Python megawidgets) for doing GUI stuff in.

I was not able to get Python to recognize the pygtk GUI. If programmers can't write proper installation instructions for their own software, that tells me a lot about the quality of their programming as well. So I'm skipping that one.

WxPython imported on the first try. It was a huge import, too. I'm not a big fan of Wx in general. A lot of Windows (and Mac) software companies that relied heavily on Wx for their GUI widgets in the past are no longer with us. Maybe Wx has fewer bugs these days? I'll find out soon.

Friday, May 4, 2012

Converting Python Code to C Code

I know this will sound crazy to most people.

I need a program that will convert Python code to C code. Yes, I know that Python can make calls to other programs (like C) when machine code speed is needed. Yes, I know that I'm going against the grain, since nearly all converters translate from C to Python.

All I can say is, "It's a science experiment."

Anyway, I already have the Python-2-C translator code thought out in my head (in Python, of course, because I love working with Python) in case I end up doing the programming myself. It's going to be a two-pass converter so far.

The first pass converts each Python line of code into a C line of code (notice how I'm not saying C++). The second pass will add in the variables and declare them.

The resulting C code should work for a specific C compiler I have for a specific CPU I've been wanting to play more with. So why not write the programs in C in the first place, instead of writing a converter to do it? Because I prefer to program in Python instead of in C. And I can write programs much faster in Python. And if the converter spits out C code just as fast I can write Phython code for it, then my C programs get done faster than if I hand-type them and go through all the debugging, etc.

I know I'll need to find a balance somewhere between writing Python code that easily translates into C code while not taking the fun out of programming in Python because I'm structuring my programs as though I'm writing in C. I might as well just write in C if Python becomes too much like it. I'm talking crazy talk now. I'll find a good middle ground to stay in. And it should be a fun science project.

Wednesday, May 2, 2012

A+ Students Will Not Need This Info

Page 529 of the Core Python Programming (2nd Edition) book is missing some code in order for the __del__() "Destructor" Method example in Section 13.5.4 to run. Here is the full code.

This is for the April 2009, fifth printing of the book.