Cleaning My Code…

I make it a principle to to post the complete text of my code in each post so you don’t have to download see how it works (though I include download links as well). However, due to the sheer size of this update (to make explicit that my code is licensed under GPLv3, and to otherwise tidy things up), the code will be collapsed rather than displayed in its fullness. Just click to show the code, and it’ll show for you.

So, first up: I updated my very first piece of Python programming, my take on “Hello, World!”. Say hello to hello.v1-1-1.py, looking prettier than ever:

Hello, World! v1.1.1

Hello, World! v1.1.1

#!/usr/bin/env python

# hello.py v1.1.1
# Hello World!
#
# Changelog:
# v1.1.1 (09/11/2010) - Added GPLv3 license text & changelog to comments
#                       Added user-visible program & (c) info
#                       Re-commented program, because we were all newbs once
# v1.1 (08/08/2010) - Added "Press <enter>" prompt for terminal execution
#                     Deleted unnecessary comments
# v1.0 (07/20/2010) - Initial release (My first program!)
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# user interface

print "Hello World! v1.1.1"
print "Copyright 2010 by Benjamin Braithwaite"
print
name = raw_input("What is your name? ")

# print user's name in a friendly greeting

print
print "Hello, " + name + "!"
print

# successful exit dialog

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

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

Next, I updated Date Translator and cleaned up the code some as well. Here is the new date.v1-1-2.py:

Date Translator v1.1.2

Date Translator v1.1.2

#!/usr/bin/python

# date.py v1.1.2
# Date Converter
#
# Changelog:
# v1.1.2 (09/11/2010) - Added GPLv3 license text & changelog to comments
#                       Added user-visible program & (c) info
#                       Cleaned up comments & code for consistency
# v1.1.1 (08/21/2010) - Now handles single-digit DD correctly
# v1.1 (08/08/2010) - Changed to be a MM/DD/YYYY converter
# v1.0 (08/08/2010) - Initial release (My second program!)
#                     Convert numbers to American-style dates
#
# v1.1.1 - Correctly handles single-digit DD
# v1.1 - Convert numbers to American-style dates
# v1.0 - Convert numbers to American-style dates
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# user interface

print "Date Converter v1.1.2"
print "Copyright 2010 by Benjamin Braithwaite"
print
print "Convert MM/DD/YYYY dates to \"January 1st, 2010\" style."
print
mmddyyyy = raw_input("Enter date in MM/DD/YYYY format: ")

# MM-to-month list

mm_list = (
	"January",
	"February",
	"March",
	"April",
	"May",
	"June",
	"July",
	"August",
	"September",
	"October",
	"November",
	"December"
)

# set DD-to-day suffixes (1st, 2nd, 3rd, 4th ... 20th, 21st, etc.)

dd_suffix = ["st","nd","rd"] + 17*["th"] + ["st","nd","rd"] + 7*["th"] + ["st"]

# divides user input into separate MM, DD, and YYYY strings

mm_str = mmddyyyy[0:2]
dd_str = str(int(mmddyyyy[3:5]))
yyyy_str = mmddyyyy[6:10]

# format MM and DD strings (done separately for readability)

mm_name = mm_list[int(mm_str)-1]
dd_ordinal = dd_str + dd_suffix[int(dd_str)-1]

# print results onscreen

print
print "Your date is: " + mm_name + " " + dd_ordinal + ", " + yyyy_str
print

# successful exit dialog

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

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

Then I moved on to the Domain Name Translator. Yeah, it’s basically worthless, but it may have potential for more some day, so it gets some love, too. Here is domainname.v1-0-1.py:

Domain Name Translator v1.0.1

Domain Name Translator v1.0.1

#!/usr/bin/env python

# domainname.py v1.0.1
# Domain Name Extractor
#
# Changelog:
# v1.0.1 (09/11/2010) - Added GPLv3 license text & changelog to comments
#                       Added user-visible program & (c) info
#                       Added comments for readability
#                       Changed URL stripping to require final "/"
# v1.0.0 (08/21/2010) - Initial version (Extract domain name from a URL)
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# user interface

print "Domain Name Extractor v1.0.1"
print "Copyright 2010 by Benjamin Braithwaite"
print
print "Strips the domain name from a full URL"
print
url = raw_input ("Please enter a full URL (e.g., \"http://www.yahoo.com/\"): ")

# strip domain name from url

domain = url[11:-5]

# print domain name

print
print "The domain name of the URL you entered is: " + domain
print

# successful exit dialog

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

You can even download the questionably useful domainname.v1-0-1.py from my Dropbox, because I’m just a nice guy like that.

Of course Quotation Box got some attention. I even updated the code to be smaller and more human-readable (and double-checked that it really does center — and yes, it does, wise guys!), so this is a (more) substantial update than the others. Behold! the new quotationbox.v1-1-0.py:

Quotation Box v1.1.0

Quotation Box v1.1.0

#!/usr/bin/env python

# quotationbox.py v1.1.0
# Quotation Box
#
# Changelog:
# v1.1.0 (09/11/2010) - Added GPLv3 license text & changelog to comments
#                       Added user-visible program & (c) info
#                       Cleaned up comments and code
# v1.0.0 (08/21/2010) - Initial release (Take user input and make a centered
#                         ASCII box around it)
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# user interface

print "Quotation Box v1.1.0"
print "Copyright 2010 by Benjamin Braithwaite"
print
quotation = raw_input ('Enter your quotation here: ')

# DEFAULT, assume terminal width is 80

term_width = 80

# OR, ask user for his terminal width

# term_width = int(raw_input ("What is your terminal width? (Usually 80): "))

# determine width of quotation and box accordingly

q_width = len(quotation)
margin_left = (term_width - (q_width + 4)) // 2

# print quotation in centered ASCII box

print
print " " * margin_left + "+-" + "-"*q_width + "-+"
print " " * margin_left + "| " + " "*q_width + " |"
print " " * margin_left + "| " +  quotation  + " |"
print " " * margin_left + "| " + " "*q_width + " |"
print " " * margin_left + "+-" + "-"*q_width + "-+"
print

# successful exit dialog

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

You can download quotationbox.v1-1-0.py from my Dropbox.

Madlibs came next; again, only minor changes were made. Here is the updated madlibs.v1-0-1.py:

Madlibs v1.0.1

Madlibs v1.0.1

#!/usr/bin/env python

# madlibs.py v1.0.1
# Madlibs
#
# Changelog:
# v1.0.1 (09/11/2010) - Cleaned up code and comments
# v1.0.0 (09/03/2010) - Initial release
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# imports template strings capability

from string import Template

# user interface

print "Madlibs v1.0.1"
print "Copyright 2010 by Benjamin Braithwaite"
print
print "Type a word or words as directed, and enjoy the silly results!"
print

# madlib template

madlib100Template = Template("I was $vbact01 down the $ncommon01 one \
$unittime01, and I $vbpast01 a $ncommon02. Not knowing what a \
$ncommon02 was, I immediately $vbpast02 and the $ncommon02 $vbpast03! \
A $noccupation01 looked my way, and I $vbpast04. Luckily, the \
$noccupation01 didn\'t notice, and it was all $adj01. $excl01!")

# create and fill dictionary of user's choices

madlib100Choices = {}

madlib100Choices["vbact01"] = raw_input("Active verb: ")
madlib100Choices["ncommon01"] = raw_input("Common noun: ")
madlib100Choices["unittime01"] = raw_input("Unit of time: ")
madlib100Choices["vbpast01"] = raw_input("Past tense verb: ")
madlib100Choices["ncommon02"] = raw_input("Common noun: ")
madlib100Choices["vbpast02"] = raw_input("Past tense verb: ")
madlib100Choices["vbpast03"] = raw_input("Past tense verb: ")
madlib100Choices["noccupation01"] = raw_input("Occupation: ")
madlib100Choices["vbpast04"] = raw_input("Past tense verb: ")
madlib100Choices["adj01"] = raw_input("Adjective: ")
madlib100Choices["excl01"] = raw_input("Exclamation: ")

# insert user's choices into madlib template & print

print
print madlib100Template.substitute(madlib100Choices)
print

# successful exit dialog

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

You can download the mostly unchanged madlibs.v1-0-1.py from my Dropbox.

E-mail Quotation Reformatter got cleaned up, too, though it is a bit more useful than the Domain Name Translator. Because I’m sure someone will want it, here is the updated emailquotationreformatter.v1-0-1.py:

E-mail Quotation Reformatter v1.0.1

E-mail Quotation Reformatter v1.0.1

#!/usr/bin/env python

# emailquotationreformatter.py v1.0.1
# E-mail Quotation Reformatter
#
# Changelog:
# v1.0.1 (09/11/2010) - Cleaned up code and comments
# v1.0.0 (09/06/2010) - Initial release (Strips initial spaces and >'s from
#                         e-mail quotations)
#
# Copyright 2010 Benjamin Braithwaite
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# user interface

print "E-mail Quotation Reformatter v1.0.1"
print "Copyright 2010 by Benjamin Braithwaite"
print
print "Strips initial spaces and \>\'s from e-mail quotations"
print
qtostrip = raw_input("Enter the e-mail quotation you wish to reformat: ")

# strip and print results

print
print qtostrip.lstrip(" >")
print

# successful exit dialog

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

You can download emailquotationreformatter.v1-0-1.py from my Dropbox… but I doubt that you really want to 😛 .

So there’s all the updates for you! Glad I collapsed them? I thought so. :mrgreen:

I didn’t get into dictionaries this week very much, so I don’t have any new code today, but I hope to have some by Monday. Cheers, and hoping you’re enjoying your weekend!

UPDATE: Fixed some random typos and punctuation errors — that’s what happens when you hurry to finish your post before the library closes! Additionally, WordPress does gruesome things with smilies in source code, so I removed them from the post… 😦 The original # user interface :P smilies are still in the files you can download from Dropbox. Also, I have added the wraplines="false" argument for the sourcecode posted on BG, as the GPL text and other long lines can triple the actual line count when the lines are wrapped.

UPDATE #2: Added screenshots.

Advertisements

One thought on “Cleaning My Code…

  1. Infant Strollers

    I loved this article and also wanted to point out that i truly such as the style and feel of your website. I’m using wordpress as well on my weblog but have been looking for a template like this that is much much better than what I’ve. I see the theme is listed on the footer so I’ll carry a better look at it.

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s