Test classes and objects in python
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(" ", "_")
-
def __init__(self, title):
- # 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(" ", "+")
-
def name(self):
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.
- Python Classes
- “Taken together, these properties make it possible to design reliable and extensible classes with multiple inheritance.”
More Python
- Quick-and-dirty old-school island script
- Here’s a Python-based island generator using the tables from the Judges Guild Island Book 1.
- Astounding Scripts on Monterey
- Monterey removes Python 2, which means that you’ll need to replace it if you’re still using any Python 2 scripts; there’s also a minor change with Layer Windows and GraphicConverter.
- Goodreads: What books did I read last week and last month?
- I occasionally want to look in Goodreads for what I read last month or last week, and that currently means sorting by date read and counting down to the beginning and end of the period in question. This Python script will do that search on an exported Goodreads csv file.
- Timeout class with retry in Python
- In Paramiko’s ssh client, timeouts don’t seem to work; a signal can handle this—and then can also perform a retry.
- Percentage-based random tables
- Our current random item generator assumes that each item shows up as often as any other item. That’s very OD&D-ish. But AD&D uses percentage dice to weight toward some monsters and items more than others.
- 30 more pages with the topic Python, and other related pages