Tuesday, May 14, 2013

Perilous Python Project 1

If you remember back to the first day of SoC, we wrote a small "hello world" code in Python, and it happened to be tied with lua for the simplest code. That being said, if Python is your first true coding experience, you might expect some challenges, but it certainly should not be as difficult as C/C++ or Java to learn. In our case, there are quite a few web frameworks that work with python (notably Django and web.py), which makes the language somewhat more important. That being said, lets start by creating a simple terminal program that allows you, the user, to read the contents of a "to do" list. This will be a combination of Python and Bash scripting, but it shouldn't be anything too difficult and should be good practice in approaching any coding project.

Step 1: Decide how you want the program to run

Personally, I would like a simple terminal command that prints out everything I have saved in my "to do" list. For that, I'll need the command itself, a file for that command to call, and the "to do" list. That makes three files.

Step 2: Create the "to do" list

Honestly, you don't have to write anything in it, you just have to make the file. To do this, just open up the file in any text editor you want and save it, possibly with an item you want to do later. For the sake of this example, let's call the file "list"


$ nedit list


I used nedit here because it seems the most straightforward to use. You could also use Vi/Vim, NaNo, Joe, or whatever you like. Enter something to do in the first line, if you like, like "learn to code" so your file reads simply this:


Learn to code


And there you have it. This is your "to do" list.

Step 3: Create a Python file

Now we want to call this file in python, so how would we do that? Well, let's create a python file called todo.py that reads:


import shutil
import sys
with open("/home/user/list", "r") as f:
    shutil.copyfileobj(f, sys.stdout)

This file is importing the "shutil" and "sys" modules from the standard python library. Let's go over each of these individually:

shutil- this module offers a number of high level operations for file manipulation outside of the python code. In this case, we need to open the "list" file we just made, and copy it to the terminal (stdout). As far as file manipulation is concerned, this one is rather easy.

sys- This gives the user access to some variables maintained by the interpreter. This means that it allows access to statistical information and terminal rights. In this case, we use it to envoke the terminal to print out information--namely our "to do" list.

The next line opens our file "/home/user/list" and reads it (which is what the "r" is for). After that, it assigns the list a value of "f". The colon, ":", indicates the next line is dependent on the first. That line calls the shutil module "copyfileobj" and copies "f," our list, to "sys.stdout," which is the terminal. 


Step 4: Create your command

At this point in the process, you should be able to call todo.py like so:


$ python todo.py


Output:


Learn to code


Which means everything we have done so far is working. That being said, It's a bit cumbersome to type in that command every time. Instead, let's create another file, "todo," with the following contents:


python /home/user/todo.py


Once saved, we need to give this command executable rights:


# chmod +x todo


This means that our new command to see the list is"


./todo


But that's still not enough! I don't want to see that annoying "./" term every time I want to check out my "to do" list! So I'm going to copy the file into my /usr/bin directory like so:


# cp todo /usr/bin


you might have been wondering why we have been using the full directory tree with "/home/user/" fully typed out when calling the files. This is so that no matter where we are in our directory tree, we can call our todo list by simply typing:


$ todo


Which is kinda cool.

Step 5: Echo in contents

Now, let's say you are on the run and you just remembered about your dentist appointment later that afternoon. You might not have time to go into your file and add in a new line for "Go to the Dentist." What should you do?

Well, there's a command just for that. It's called the "echo" command. If you want to add a new line to your file, just execute:


$ echo "Go to the Dentist" >> /home/user/list


and next time you use the "todo" command, thre should be a new line reading "Go to the Dentist". Unfortunately, the easiest way to remove content from your todo list is to go into the file and remove it yourself.

Regardless, your "to do" list is done! Woo!

Final Notes About Python:

Python is a modular language, meaning it is dependent on standard libraries of code. That being said, you are able to import your own python files whenever you like with the "import" command at the head of every file (much like we did with "shutil" and "sys" in today's example).

Outside of that, we will definitely return to do more python tasks later, but we should have enough information to cover a simple web.py tutorial, which will be saved for tomorrow.

Until then, Thanks for reading.
-Leios

No comments:

Post a Comment