Monthly Archives: August 2010

Some Medium-Term Programming Goals

After poking around Hetland’s amazing Learning Python book for a few chapters and getting a better idea of what I can do in Python, I thought I would set some projects for myself above and beyond what is in Hetland — you know, self-inflicted fun 😕 .

A text adventure game

Despite the dated (to put it lightly) nature of text adventure games, I would like to make one myself to learn how they work. Also, I would love to make at least one game in my life — why not now?

A console (or possibly GUI) text editor

Despite the huge number of readily available text editors, I want to program one myself so I can learn the ins and outs of text processing and display. If I can make it a GUI editor, so much the better (in my opinion). I’d want to do something comparably light and functional like Metapad, my favorite text editor aside from Notepad++.

A console RSS aggregator/reader

I like to use RSS very much (especially since that’s how podcasts work 😉 ), and programming an RSS reader would help me not only to learn some more about the guts of the Internet, but would also help me to learn more about file formats (for the RSS archives).

These are a few things I would like to make, even if I’m the only one who ever uses them — which I probably will be 😛 . Once I get at least one reasonably decent program up and running, I hope to make my own custom repository on Launchpad, as I’ve been lead to believe that it’s not too difficult to do — I just don’t know if it will work for Linux Mint KDE/Kubuntu, though I assume yes.

A Sigh and A New Install

After posting about first failing, then succeeding in getting KDE SC 4.5 running on Limited Edition, KDE 4.5 has finally bit the dust functionality-wise for me. Dolphin especially suffered from frequent crashes, then Amarok, and eventually even Kontact. Now Plasma randomly suffers totally fatal crashes that print garbage on my screen and leaves even good old Alt-F1 inoperative 😡 .

To my mind, the only big improvements for me in 4.5 over 4.4 were faster KDE startup time and the more logical placement of the print and disk mounter tray icons — going back to 4.4 won’t be much of a loss. I’ve contemplated changing distros, but I quite like Linux Mint KDE, and will probably use it for a while longer.

(However, I stumbled upon Chakra, an Arch Linux derivative that uses KDE, and it rather intrigues me — though even an Arch derivative is rather intimidating to a newb like myself.)

So, I’m reinstalling my system again. Again. I think I’ll leave the 4.5 beast alone for now 😦 .

UPDATE: Yay! Limited Edition is alive again! My personal files are safe, and settings, though lost, are now hand-crafted into submission. Separate /home partitions rock!

Busy, Busy, Busy

This past week has been quite the time. I have many projects going on at work (new website, trifold mailer, several books to typeset/reprint, possibly a magnet mailer, etc.), I finally came up with a schedule for my life after several (post-work) hours, my basement flooded (again) because the furnace/central air conditioner is leaking like you wouldn’t believe, I was a judge at the National Bible Bee with my wife on Saturday, she was sick on Sunday — well, it was busy.

This means that I honestly haven’t had any time to work on learning Python and stay sane. In other words, I didn’t learn anything new in Python this week, for which I apologize profusely to my reading audience.

However, if you read closely, you know that I now have a schedule for my life. I now am dedicated to learning Python for ~8 hours in any given week (the freedom to do this is something for which I simply cannot thank my lovely wife enough), which I hope will greatly enrich my learning experience as well as your reading.

On a totally unrelated note, I finally got my CF-to-IDE adapters in the mail (seriously, could China-US shipping get any slower?), so I can pursue work on Frankenstein, and hopefully have a working… something by the end of the week. I’m not really sure what I can or should do with him. I’ve read K.Mandla’s excellent list of things to do with an old computer (though I’m sure he would laugh at my calling a vintage 2003 2.4GHz P4 with 512MB of RAM system “old”), but I’m still undecided on what I should do. Maybe I could turn it into a home network media PC if I get a new video card, but we’ll see.

A Change of Copyright, Plus Free GPL Banner!

I want this blog to be usable to as many people as possible, so I am changing several aspects of its copyright. Here is an overview of the new copyright setup:

I felt that Creative Commons was the best choice for the “literary” content of BG because the GNU Free Documentation License can make FDL-licensed materials unusable in non-FDL contexts. I also removed the NonCommercial restriction to the license because if the content is helpful, I want people to be able make use of BG’s content if they want to, even if it is in a commercial context.

I felt that the GPL was the best choice for any source code I write because it would make it generally compatible with most currently available Free/Open Source Software. I realized that by omission I my source code was put under the CC license as well, and I want to explicitly state that all source code (unless otherwise stated) on BG is licensed under the GPL v3 License. I found that there aren’t any good 80×15 banners for the GPL, so I made one:

GPL v3 License

GPL v3 License

 

I am making the banner copyright under the Creative Commons Attribution-ShareAlike 3.0 Unported License. To post it on your own website you can use the following code:

<a rel="license" href="http://www.gnu.org/licenses/gpl.html"><img title="GPL v3 License" src="https://becomingaglider.files.wordpress.com/2011/02/gpl-v3-80x15-1.png" width="80" height="15" /></a>

You can attribute it to me by linking to my website somewhere on your site — I wouldn’t even require attribution except that CC doesn’t have a pure ShareAlike license (I don’t mind if you forget to attribute — I’m happy if anyone can make use of my work elsewhere).

UPDATE: I just had a thought — if you use my GPL banner, go ahead and put a link to the website you used it on in the comments. I would really love to see what projects take a liking to it, and you just may get a free plug from me 😉 .

Poking Around VirtualBox

I have recently discovered VirtualBox, and to say the least, it is really amazing and very powerful tool. For example, I wanted to try out Debian running KDE, and behold:

Debian 5.0.5 in VirtualBox on Limited Edition

Debian 5.0.5 in VirtualBox on Limited Edition

Yeah, that’s Debian 5.0.5 Lenny running virtually on Limited Edition after only a few minutes of VirtualBox configuration — the install took longer than anything else! This is really awesome, because now I can try out more distros more easily that I could otherwise — while I don’t have the resources to burn unlimited numbers of CDs and/or DVDs, I do have hard drive space (and RAM, for that matter) I can burn in the name of science! I can even install Haiku, Plan9, MINIX, or ReactOS, just to have a different perspective on all things OS!

And if I want to, I can ignore my home OS and work exclusively on a virtual machine, transferring files when I need to via folder sharing through the VirtualBox virtual server — how cool is that?

I think VirtualBox would be the perfect tool to test my programs for cross-OS compatibility — just pop in the OS I’m working on porting it to, and voila, instantly available testing environment! If I want to test OSs that run on PowerPC architecture (e.g., OS X, AROS, MorphOS, Yellow Dog Linux), I’d have to use QEMU or bochs, but I’ll cross that bridge when I come to it.

As a side note, I’m still going to keep my native Windows XP Pro install around, especially seeing as it’s already on a separate hard drive — I’d rather have a native copy for the occasional Windows-only game. I’ll still poke around with virtual Windows installs, since I now have the option to fool around with some older versions of Windows that I have lying around (95, 98SE) without worrying about that they’ll destroy my MBR — and yeah, that’s happened to me before 😥 .

What I Learned In Python This Week #2: Lists, and How To Manipulate Them

Tools I Used:

  • Geany
    • My favorite mini-IDE!
  • Python shell / bash shell
    • I use the Python shell to learn functions interactively, and the bash shell to execute the scripts I write.

Accomplishments:

  • Date Translator (date.v1-1-1.py)
    • Kind of a bug release, I guess — I fixed a problem where single-digit days were showing up with an unnecessary initial zero.
  • Domain Name Translator (domainname.v1-0-0.py)
    • A simple script that strips the domain name from a URL. Basically just a proof-of-concept to help me understand lists in Python a bit better.
  • Quotation Box (quotationbox.v1-0-0.py)
    • Takes a line of user input and centers it in a ASCII text box in their terminal.

Functions/Concepts I Learned:

  • Lists
    • Lists are essentially sequences of data that you can manipulate in any number of ways — and manipulating data is what programming is all about!
    • [], [:], and [::] let you index, slice, and choose step size of the slice respectively. These are tools that by themselves only return the value of a list, but can be used in conjunction with other functions can powerfully manipulate lists or parts of lists.
    • None is Python’s concept of, not zero, but nothing at all; it is a null or an unset value. This is useful for filling a list up that you will put data in later.
    • len(), min(), and max() let you check the length of a list, the smallest member in a list, and the largest member in a list respectively.
    • del lets you delete members from a list.
  • Tuples
    • I think tuples are a bit above me at this point, but I understand them to be similar to lists, only non-modifiable once created. Useful for dictionaries, or perhaps an internal checksum.
  • Methods
    • Methods are really awesome! They are like functions, only you tell methods to work only on specific parts of a sequence, rather than the whole. Several of these can be implemented using slices, but using methods is less ugly looking and much more human-readable.
    • .append adds a single data point to the end of a list, while .extend lets you add multiple data points to the end of a list (including adding other lists).
    • .count counts the occurrences of elements in a list, but does not examine sublists (i.e., for numbers = [1,2,1,1,2], numbers.count(2) will return 2, but for numbers = [1,2,1,[1,2]], it would return only 1).
    • .index returns the index value of the first occurrence of your search term in a given list, and .remove will search for and remove the first occurrence of the term. However, these only deal with the first occurrence — if the value appears multiple times in a list, you will need multiple .index/.remove operations to deal with them, or else some other Python function I don’t know about yet.
    • .pop is interesting because it “pops” (i.e., deletes) the last entry in a list out and returns the value. According to Hetland, this is a way to implement a stack, a word which I have heard a lot concerning programming, but have never really heard explained.
    • .reverse reverses the elements in a list (not just returning them reversed like the reversed function).
    • .sort sorts the elements in a list in ascending order (not just returning them sorted like the sorted function). You can use the cmp, key, and reverse arguments to sort by various other means than the default ascending order.
  • list() converts a sequence to a list, and tuple() similarly converts a sequence to a tuple.
  • cmp lets you compare two values, returning 0 for a match, 1 when the first value is larger, and -1 when the first value is smaller.

This week I finally got lists, and it was very rewarding; I feel much more confident to take on the rest of Python now. While I didn’t learn any cool new functions, I learned a lot about how to put lists to work for me, and I think it was worth it. Next week, I will try to get through Hetland’s chapter on strings, and I think it will be similarly devoid of new functions, but I’ll sure learn a lot about how to play around with strings!

Some More Python Exercises

I’ve gotten a few more exercises done today. I also finally understand why lists were giving me so much trouble! I couldn’t figure out why slicing lists took one more entry than I thought it should, but upon a more careful reading of Hetland, I found this explanation that totally cleared things up for me:

[T]he first limit (the leftmost) is … inclusive, while the second (the rightmost) is exclusive. When using a negative step size, you have to have a first limit (start index) that is higher than the second one.

Date Translator v1.1.1

Date Translator v1.1.1

This really cleared up why date.py worked the way it did! I really was only selecting the appropriate numbers in MM/DD/YYYY format, I just didn’t understand why it appeared that I was selecting one more entry in list sequence than was necessary. Anyway, date.py got another revision that uses a str(int(DD)) function to strip the leading zero from a date that had a single-digit day (10/01/2010 was coming out “October 01st, 2010”). Here is date.v1-1-1.py:

#!/usr/bin/python

# date.py v1.1.1
# 21 Aug 2010
# My second program
# Date conversion tool

# Changelog:
# v1.1.1 - Correctly handles single-digit DD
# v1.1 - Changed to be a MM/DD/YYYY converter
# v1.0 - Convert numbers to American-style dates

# user instructions

print 'Convert MM/DD/YYYY dates to \"January 1st, 2010\" style.'

# month list

month_list = [
	'January',
	'February',
	'March',
	'April',
	'May',
	'June',
	'July',
	'August',
	'September',
	'October',
	'November',
	'December'
]

# sets suffixes for 31 days as 1st, 2nd, 3rd, 4th ... 20th, 21st, etc.

day_suffix = ['st','nd','rd'] + 17*['th'] \
	+ ['st','nd','rd'] \
	+ 7*['th'] + ['st']

# get user input

mmddyyyy = raw_input('Enter date in MM/DD/YYYY format: ')

# divides user input into strings

month_str = mmddyyyy[0:2]
day_str = str(int(mmddyyyy[3:5]))
year_str = mmddyyyy[6:10]

# format user input

month_name = month_list[int(month_str)-1]
day_ordinal = day_str + day_suffix[int(day_str)-1]

# print results onscreen

print 'Your date is: ' + month_name + ' ' + day_ordinal + ', ' + year_str

raw_input('Press <enter> to exit...')

You can download date.v1-1-1.py from my Dropbox.

Domain Name Translator v1.0.0

Domain Name Translator v1.0.0

I also wrote a very short, very simple script that strips the domain name from a URL. I’m not certain that it has any real-world use — in fact, even for testing it’s very limited in it’s ability. I just wanted to test my understanding of lists, otherwise I would have skipped the exercise entirely. Regardless, you may now behold domainname.v1-0-0.py!

#!/usr/bin/env python

# domainname.py v1.0.0
# 21 Aug 2010
# Extracts domain name from a URL

# Changelog:
# v.1.0.0 - Extract domain name from a URL

url = raw_input ('Please enter a URL (e.g., "http://www.yahoo.com": ')
domain = url[11:-4]

print 'Domain name of the URL you entered: ' + domain

raw_input ('Press <enter> to exit.')

You can download domainname.v1-0-0.py from my Dropbox, too. I may come back to it and spice it up a bit, but more likely I’ll go back to date.py if I want to put more complex concepts to use. Maybe.

Quotation Box v1.0.0

Quotation Box v1.0.0

Also, as a grand finale, I tried really hard to do this one entirely by hand, but I had to look at the example because Hetland never said anything about the len() function until he used it in the example 👿 . However, once I saw that, I got the rest working in short order. Yeah, it looks a lot like the book’s answer (as do pretty much all my code examples thus far), but I did everything by hand except the len() thing. I even have a working way to have the user choose the width of his terminal, but I commented it out and just chose 80 as the default width because I hated typing ’80’ every time I tested the script 😛 . Here is quotationbox.v1-0-0.py:

#!/usr/bin/env python

# quotationbox.py v1.0.0
# 21 Aug 2010
# Take user-supplied quotation and make a centered ASCII box around it

# Changelog:
# v1.0.0 - Take user input and make a centered ASCII box around it

# Get user's terminal width and quotation

term_width = 80 # int(raw_input ('What is your terminal width? (Usually 80): '))
quotation = raw_input ('Enter your quotation here: ')

# Determine width of quotation and box accordingly

text_width = len(quotation)
qbox_width = text_width + 2
margin_left = (term_width - qbox_width) // 2

print
print ' ' * margin_left + '+'  + '-' * qbox_width +  '+'
print ' ' * margin_left + '| ' + ' ' * text_width + ' |'
print ' ' * margin_left + '| ' +       quotation  + ' |'
print ' ' * margin_left + '| ' + ' ' * text_width + ' |'
print ' ' * margin_left + '+'  + '-' * qbox_width +  '+'
print

raw_input ('Press <enter> to exit.')

And yeah, you can download quotationbox.v1-0-0.py from my Dropbox. At some point I want to soup up quotationbox and make v2.0 kinda like cowsay (which is my favorite part of the Linux Mint default terminal settings) and have a better-looking box that can handle multiple lines.

As you can see, I’ve decided to go with a “version x.y.z” naming method for all my scripts and to keep a changelog for each file as well. Yeah, it makes the files bigger, and complicates running them from the terminal a bit, but I’d rather know at a glance what changes I made and when without having to browse the code itself (or go back and read my own blog entries!) than have slightly smaller file size.

So yeah, I’ve been a busy fella today. Maybe not very fast, but pretty busy. I’m slowly learning how to think my way through problems. Python is really fun to learn, and I’m excited to learn more functions so I can make cooler, better, and more powerful programs!

UPDATE: Added screenshots.