Mimsy Were the Borogoves

Hacks: Articles about programming in Python, Perl, Swift, BASIC, and whatever else I happen to feel like hacking at.

Test classes and objects in python

Jerry Stratton, February 6, 2016

One of the critical advances in computer programming since I began programming in the eighties are Objects. An Object is a thing that can include both functions and variables. In Python, an object is an instance of a class. For example, Django relies heavily on classes for its models. In Adding links to PDF in Python the main class used is for a restaurant. Each object is an instance of the restaurant class.

But one of the great things about object-oriented programming is that the things that access your objects don’t care what class it is. They care only whether it has the appropriate functions and variables. When they are on an object, a function is called a method and a variable is called a property.

Any object can masquerade as another object by providing the same methods and properties. This means that you can easily make test classes that allows creating objects for use in the PDF example.

In Adding links to PDF in Python, I had a Django model for the Restaurants and a Django model for the Links that were each restaurant’s web page. But because Django models are nothing more than (very useful) classes, you can make a fake Restaurant and fake Link to impersonate what the code snippet expects.

[toggle code]

  • # in real life, the Link class would probably pull information from a database of links
  • # and live would be whether it is currently a valid link,
  • # and get_absolute_url would be the actual URL for that link
  • class Link():
    • def __init__(self, title):
      • self.title = title
    • def live(self):
      • return True
    • def get_absolute_url(self):
      • return "http://www.example.com/" + self.title.replace(" ", "_")
  • # in real life, the Restaurant class would probably be a table of restaurants
  • # and would store the name of each restaurant, an id for the restaurant's web site
  • # and the restaurant's address
  • class Restaurant():
    • def name(self):
      • return "The Green Goblin"
    • def url(self):
      • myURL = Link("The Green Goblin")
      • return myURL
    • def address(self):
      • return "1060 West Addison, Chicago, IL"
    • def mapref(self):
      • return "https://www.google.com/maps/place/" + self.address().replace(" ", "+")

Save that as restaurant.py.

Objects are created from classes using:

  • object = ClassName()

For example,

  • restaurant = Restaurant()

If the class has an init method which requires parameters, those parameters go in the parentheses after the class.

  • link = Link("The Green Goblin")

Classes in this way look sort of like functions, but they’re not. They return an object which is an instance of a class. That object can have all sorts of methods and properties which define each member of that class. In this case, which can return the things that make up a restaurant: the restaurant’s name, address, and so on.

Run python interactively and you can test it:

  • python
  • >>> from restaurant import Restaurant
  • >>> restaurant = Restaurant()
  • >>> restaurant.name()
  • 'The Green Goblin'
  • >>> restaurant.url()
  • >>> restaurant.address()
  • '1060 West Addison, Chicago, IL'
  • >>> restaurant.mapref()
  • 'https://www.google.com/maps/place/1060+West+Addison,+Chicago,+IL'
  • >>> restaurantLink = restaurant.url()
  • >>> restaurantLink.get_absolute_url()
  • 'http://www.example.com/The_Green_Goblin'

In this test example, all of your restaurants will be called The Green Goblin and be located at 1060 West Addison because that text is hardcoded into each method, but you should be able to modify the class, method by method, to pull real data from a database or xml/csv file if you desire. It isn’t necessary for testing, however.

In response to Adding links to PDF in Python: It is very easy to add links to PDF documents using reportlab or platypus in Python.