MacLochlainns Weblog

Michael McLaughlin's Technical Blog

Site Admin

Archive for the ‘Python 3.x’ Category

Basic Python Object

with one comment

One of my students wanted a quick example of a Python object with getters and setters. So, I wrote a little example that I’ll share.

You define this file in a physical directory that is in your $PYTHONPATH, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Define Coordinate class.
class Coordinate:
  # The method that initializes the Coordinate class.
  def __init__ (self, x, y):
    self.x = x
    self.y = y
 
  # Gets the x value from the instance.
  def getX (self):
    return self.x
 
  # Gets the y value from the instance.
  def getY (self):
    return self.y
 
  # Sets the x value in the instance.
  def setX (self, x):
    self.x = x
 
  # Sets the y value in the instance.    
  def setY (self, y):
    self.y = y
 
  # Prints the coordinate pair.
  def printCoordinate(self):
    print (self.x, self.y)

Assuming the file name is Coordinate.py, you can put it into the Python Idle environment with the following command:

from Coordinate import Coordinate

You initialize the class, like this:

g = Coordinate(49,49)

Then, you can access the variables of the class or it’s methods as shown below:

# Print the retrieved value of x from the g instance of the Coordinate class.
print g.getX()
 
# Print the formatted and retrieved value of x from the g instance of the Coordinate class.
print "[" + str(g.getX()) + "]"
 
# Set the value of x inside the g instance of the Coordinate class.
print g.setX(39)
 
# Print the Coordinates as a set.
g.printCoordinate()

You would see the following results:

49
[49]
(39,49)

As always, I hope that helps those looking for a solution.

Written by maclochlainn

October 30th, 2016 at 6:23 pm

Create a Python Module

without comments

Sometime formal programming documentation is less than clear. At least, it’s less than clear until you’ve written your first solution. The Modules section of the Python language is one of those that takes a few moments to digest.

Chapters 22 and 23 in Learning Python gives some additional details but not a clear step-by-step approach to implementing Python modules. This post is designed to present the steps to write, import, and call a Python module. I figured that it would be helpful to write one for my students, and posting it in the blog seemed like the best idea.

I wrote the module to parse an Oracle version string into what we’d commonly expect to see, like the release number, an “R”, a release version, and then the full version number. The module name is more or less equivalent to a package name, and the file name is effectively the module name. The file name is strVersionOracle.py, which makes the strVersionOracle the module name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# Parse and format Oracle version.
def formatVersion(s):
 
  # Split string into collection.
  list = s.split(".")
 
  # Iterate through the result set.
  for i, l in enumerate(list):
    if i == 0 and list[i] == "11":
      label = str(l) + "g"
    elif i == 0 and list[i] == "12":
      label = label + str(l) + "c"
    elif i == 1:
      label = label + "R" + list[i] + " (" + s + ")"
 
  # Return the formatted string.
  return label

You can put this in any directory as long as you add it to the Python path. There are two Python paths to maintain. One is in the file system and the other is in Python’s interactive IDLE environment. You can check the contents of the IDLE path with the following interactive commands:

import sys
print sys.path

It prints the following:

['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']

You can append to the IDLE path using the following command:

sys.path.append("/home/student/Code/python")

After putting the module in the runtime path, you can test the code in the IDLE environment:

1
2
3
import cx_Oracle
db = cx_Oracle.connect("student/student@xe")
print strVersionOracle.formatVersion(db.version)

Line 3 prints the result by calling the formatVersion function inside the strVersionOracle module. It prints the following:

11gR2 (11.2.0.2.0)

You can test the program outside of the runtime environment with the following oracleConnection.py file. It runs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Import the Oracle library.
import cx_Oracle
import strVersionOracle
 
try:
  # Create a connection.
  db = cx_Oracle.connect("student/student@xe")
 
  # Print a message.
  print "Connected to the Oracle " + strVersionOracle.formatVersion(db.version) + " database."
 
except cx_Oracle.DatabaseError, e:
  error, = e.args
  print >> sys.stderr, "Oracle-Error-Code:", error.code
  print >> sys.stderr, "Oracle-Error-Message:", error.message
 
finally:
  # Close connection. 
  db.close()

You can call the formatVersion() function rather than a combination of module and function names when you write a more qualified import statement on line 3, like:

3
from strVersionOracle import formatVersion

Then, you can call the formatVersion() function like this on line 10:

10
  print "Connected to the Oracle " + formatVersion(db.version) + " database."

It works because you told it to import a function from a Python module. The first example imports a module that may contain one to many functions, and that style requires you to qualify the location of functions inside imported modules.

The oracleConnection.py program works when you call it from the Bash shell provided you do so from the same directory where the oracleConnection.py and strVersionOracle.py files (or Python modules) are located. If you call the oracleConnection.py file from a different directory, the reference to the library raises the following error:

Traceback (most recent call last):
  File "oracleConnection.py", line 3, in <module>
    import strVersionOracle
ImportError: No module named strVersionOracle

You can fix this error by adding the directory where the strVersionOracle.py file exists, like

export set PYTHONPATH=/home/student/Code/python

Then, you can call successfully the oracleConnection.py file from any directory:

python oracleConnection.py

The program will connect to the Oracle database as the student user, and print the following message to the console:

Connected to the Oracle 11gR2 (11.2.0.2.0) database.

I hope this helps those trying to create and use Python modules.

Written by maclochlainn

October 19th, 2016 at 11:50 pm

Python for loops

without comments

It’s always interesting to explain a new programming language to students. Python does presents some challenges to that learning process. I think for-loops can be a bit of a challenge until you understand them. Many students are most familiar with the traditional for loop like Java:

for (i = 0; i < 5; i++) { ... }

Python supports three types of for-loops – a range for loop, a for-each expression, and a for-loop with enumeration. Below are examples of each of these loops.

  1. A range for-loop goes from a low numerical value to a high numerical value, like:
  2. for i in range(0,3):
      print i

    It prints the following range values:

    0
    1
    2
  1. A for-each loop goes from the first to the last item while ignoring indexes, like:
  2. list = ['a','b','c']
    for i in list:
      print i

    It prints the following elements of the list:

    a
    b
    c
  1. A for-loop with enumeration goes from the first to the last item while ignoring indexes, like:
  2. list = ['a','b','c']
      for i, e in enumerate(list):
        print "[" + str(i) + "][" + list[i] + "]"

    The i represents the index values and the e represents the elements of a list. The str() function casts the numeric value to a string.

    It prints the following:

    [0][a]
    [1][b]
    [2][c]

This should help my students and I hope it helps you if you’re trying to sort out how to use for loops in Python.

Written by maclochlainn

October 19th, 2016 at 9:02 pm