RSS Feed

RSS

Comments RSS

Python – it all starts coming together

I have previously blogged concerning my excursion into learning the Python programming language. In this post, I show how it has all started coming together with the code for a program I wrote at work. The purpose of the program was to generate an email with information about an Engineering Change Notice. It features examples of using the cx_Oracle module for accessing an Oracle database, the win32com module from Pywin32 for creating an email in MS Outlook, and Python’s native tkinter module for creating a simple GUI interface. There are lots of goodies here in a relatively short program. Enjoy!

My Engineering Change Notice Program: http://wa5pb.sdf.org/ECNdist_Tkv4-x.py

On a side note, my buddy K5WLF is also learning Python and writing a program in the process. He has described the program as a calculator for Amateur Radio Operators to handle some of the computations we commonly perform. http://www.rebelwolf.com/blog/?p=274

Python – Accessing Oracle Databases

Still learning the Python programming language here. However, along the way, I have found a need to branch out with some add-on modules for specific tasks. One of these is a module for accessing Oracle Databases. The module is cx_Oracle. Performing a query to an Oracle database become this simple process:

import the cx_Oracle module

import cx_Oracle

make your connection to the Oracle database

connection = cx_Oracle.connect(“username”,”password”,”oracle_instance”)

create an instance of the cursor

cursor = connection.cursor()

Perform a query. This demonstrates a simple select query, how to pass some user input as an argument to it, and how to do something something with the result.

user_input = str.upper(str(input(“Please provide value to query>>”)))

cursor.execute(“select some_field from some_table where some_field like :arg_1”, arg_1 = user_input)

for some_field in cursor:

print(some_field)

Python – a learning exercise

In the process of learning Python, I have given myself a programming exercise to do.  This is a standard thing I do whenever learning a new programming language and I have used this same exercise several times before.  It is a simple text based game that simply lets a user explore a maze of rooms.   If fully fleshed out, it would be a Zork or D&D type game.  It demonstrates some basic programming tasks such as reading data into an array from a file, getting and responding to user input, looping, control structures, system calls and function definitions.  I share it here as example code for others also learning Python and I welcome comments.   The code is for Python 3.x, but the comments explain how to easily convert it for use in Python 2.x.  It is far from perfect and is for learning purposes only – something to tinker with.  It is based on concepts from Creating Adventure Games on Your Computer by Tim Hartnell, which used the BASIC programming language.   You are welcome to take this code and use and modify for any purpose you like.  The current version of this code will always be at this location: mazegame-current .  In this directory you find the program and the data files it will ask for when the program is run.

My previous blog entry Learning Python provides resources for those wanting to learn Python, as does the Dev page from my main website.

Learning Python

I have picked up learning to program in Python again.   Looking to add it to my trophy case along with BASIC, x86 Assembler, IBM BAL, Pascal, Modula-2, Forth and C.   In the process I have found some great resources, aside from the official Python website to share. The first two sites are excellent on-line books about programming in Python. The last two sites teach programming skills generally using Python as the example programming language.

A Byte of Python
Dive Into Python
Learning to Program
How to Think Like a Computer Scientist

More resources can be found on my Web Development and Programming page.