I Write Poetry in Python

I enjoy songwriting. Here are the lyrics to a silly, fun song I wrote a couple of months ago.

I Write Poetry in Python by Catherine Ray

I have no life and I can prove it mathematically.
I judge your human worth using a spectrum of rationality
I know vector calculus but I can’t remember how to do long division.
When I compile, my waiting ritual resembles superstition.
I know how to integrate a chicken and take the derivative of water,
I had to pick just one, Asimov ‘s my favorite author.
And, I, I write poetry in Python.

You put the period outside the quotes! You’re not quoting the end of the sentence
What the heck do english majors know, anyway. Correction fail, I demand repentance!
I’ve gutted and rebuilt my laptop 5 times since I last changed the oil in my crappy car.
Yet, I’ll bet you I can’t name a single television star.
‘Cause I, I write poetry in Python.

I’ve got to bring a jacket with me, in the middle of summer,
’cause there’s a wind-chill factor in the lab, my priority is comfort
The three most dangerous things in the world are a programmer with a soldering iron,
a hardware engineer with a software patch,
and a user with an idea.
So, I, I just write poetry in Python.

Doctor Who Poems

I was writing a letter to my grandma. In the letter, I included a poem. My Uncle asked if I would send him more poems.

I asked him for a topic and promised to compose a poem for him. My Uncle requested Dr. Who and the following two “poems” resulted:

Alone by Catherine Ray

Alone in the night
A fiery man
burns,
through time
through space,
in step
with the scrape of an emergency brake.
The sound of adventure.
The sound of destruction.
The sound of the universe.
The sound of him.

TARDIS by Catherine Ray

Out from
the churning blue-black
it’s corners protrude.
From the box in the sky,
light falls forward.
A looking glass
into
the unaging intellect
whistling inside.

Listing Programming Languages on Your Resume

I love programming and know a few programming languages. Every programmer I’ve met has a favorite language, however, most of us can code in more than one language. As I was revising the ‘Language’ portion of my resume, I became confused.

Should you mention all the languages you can code in?

What if they expect you to be practiced in (insert language you haven’t used in the last 3 months) and you fail the interview?

Should you be safe and only list the languages you are fluent in?

How do you simply represent skill level in languages?

I decided to divide my knowledge of languages into “proficient” and “familiar.” Here is a guide on how to sort your languages into these two categories.

If you are “proficient” at a programming language, you can sit down and code (without consulting the internet or a book) and produce a functional program. You can write efficient, concise code in this language. If given source code, you can optimize it.
If you are “familiar” at a programming language, you’ve coded at least 5 functional programs in this. You can code a simple program with no bugs. If given internet access, you can produce a complex program.  If given source code in this language, you can understand what it does and check for errors. You will likely think “I know how to do this in (insert proficient language here)! How do I do this in (insert familiar language here)?” over 3 times while coding a simple program.

For example, my ‘Language’ section currently looks like this:

(I added my knowledge of Braille and Morse Code because it amuses me to present all types of languages in my language section.)

Pythonic Method Calling

I have an aversion to hard-coding. Hard coding is when you write out a long, elaborate code that could also be written with a dynamic loop. This usually limits the ability to easily adjust your own code in case you want to change something later (or re-use it). “Hard coding” refers to “rigidly” writing out things instead of keeping them dynamic.

This practice is frowned upon by most of the programmers I’ve met (especially the Python programmers). In Python, code is ‘pythonic’ if it is well-written and concise. This link is to an excellent article on ‘What is Pythonic?’, I highly recommend reading this for a more in-depth explanation of the term: http://blog.startifact.com/posts/older/what-is-pythonic.html

Today, while I was revising my Prime Number Algorithm program (an earlier post), I figured out how to pass functions as strings. The eval function allows me to run python code within itself. This definition of eval is vague and unhelpful.

Let me show you to make a calling method, ‘execute,’ pythonic in order to explain the purpose of the eval function.

Let’s assume that we want a program that executes methods p1(n), p2(n), …, p5(n) given some string n. I will refer to these methods collectively as ‘p’-methods.

We could hard-code this as such:
______________________

class Test():
    def execute(self, n):
        self.p1(n)
        self.p2(n)
        self.p3(n)
        self.p4(n)
        self.p5(n)

    def p1(self,n):
        print “Hello”+n
    def p2(self,n):
        print “Hello2″+n
    def p3(self, n):
        print “Hello3″+n
    def p4(self,n):
        print “Hello4″+n
    def p5(self,n):
        print “Hello5″+n

if __name__ == ‘__main__’:
    test = Test()
    test.execute(‘ World!’)

________________________

The above program will run all 5 ‘p’-methods and print the appropriate strings. However, what if there were 1000 different ‘p’-methods we needed to run? Must we type out each method in order to call these ‘p’-methods within the execute method?

At first, I created a forloop to iteratively call execute, providing a new method each time. If you run the code below, execute will treat the ‘method’ parameter as a String type (instead of a function) and throws an error.

_________________________(NON-FUNCTIONAL)
class Test():
    def execute(self, method_name):
        self.method_name(‘ World!’)   

    def send_method(self):
        for version in xrange(1,6):
            version = str(version) #run each ‘p’-method version = 1, 2, 3, 4
            method = ‘self.’+’p’+version #note that type(method == String
            self.execute(method)

    def p1(self,n):
        print “Hello”+n
    def p2(self,n):
        print “Hello2″+n
    def p3(self, n):
        print “Hello3″+n
    def p4(self,n):
        print “Hello4″+n
    def p5(self,n):
        print “Hello5″+n

if __name__ == ‘__main__’:
    test = Test()
    test.send_method()
_________________________

We are so very close to having a pythonic solution. In order to fix this error, we must make the String into a method call. With one extra line, we evaluate the String input (‘method_name’) as a function and run all ‘p’-methods! The final version of the execute method is:

_________________________

class Test():
    def execute(self, method_name):
        method = eval(method_name)
        methodrun = method(‘ World!’)   

    def send_method(self):
        for version in xrange(1,5):
            version = str(version) #test each version of the algorithm, version = 1, 2, 3, 4
            method = ‘self.’+’p’+version
            self.execute(method)

    def p1(self,n):
        print “Hello”+n
    def p2(self,n):
        print “Hello2″+n
    def p3(self, n):
        print “Hello3″+n
    def p4(self,n):
        print “Hello4″+n
    def p5(self,n):
        print “Hello5″+n

if __name__ == ‘__main__’:
    test = Test()
    test.send_method()

_________________________

I hope this gives you an idea of: why hard-coding is bad, what ‘pythonic’ means, how ‘eval’ comes in handy and what I do in my free time.