Exercise 5.1

Objectives:

  • Defining a simple class to represent data.

  • Further use of library modules and the import statement.

Files Created: stock.py.

(a) Objects as Data Structures

In section 2 and 3, we worked with data represented as tuples and dictionaries. For example, a holding of stock could be represented as a tuple like this:

s = ('GOOG',100,490.10)

or as a dictionary like this:

s = { 'name'   : 'GOOG',
      'shares' : 100,
      'price'  : 490.10
}

Another approach for representing data would be to define a class. Create a file called stock.py and define a class Stock that represents a single holding of stock. Have the instances of Stock have name, shares, and price attributes. For example:

>>> ================================ RESTART ================================
>>> import stock
>>> s = stock.Stock('GOOG',100,490.10)
>>> s.name
'GOOG'
>>> s.shares
100
>>> s.price
490.1
>>>

Create a few more Stock objects and manipulate them. For example:

>>> a = stock.Stock('AAPL',50,122.34)
>>> b = stock.Stock('IBM',75,91.75)
>>> a.shares*a.price
6117.0
>>> b.shares*b.price
6881.25
>>> stocks = [a,b,s]
>>> stocks
[<stock.Stock object at 0x37d0b0>, <stock.Stock object at 0x37d110>, <stock.Stock object at 0x37d050>]
>>> for t in stocks:
     print '%10s %10d %10.2f' % (t.name, t.shares, t.price)

... look at the output ...
>>>

One thing to emphasize here is that the class Stock acts like a factory for creating instances of objects. Basically, you just call it as a function and it creates a new object for you. Also, it needs to be emphasized that each object is distinct---they each have their own data that is separate from other objects that have been created.

An object defined by a class is somewhat similar to a dictionary—just with somewhat different syntax. For example, instead of writing s['name'] or s['price'], you now write s.name and s.price.

(b) Reading Data into a List of Objects

In your stock.py program, write a function read_portfolio(filename) that reads portfolio data from a file into a list of Stock objects. This function is going to mimic the behavior of earlier code you have written.

Here’s how your function will behave:

>>> ================================ RESTART ================================
>>> import stock
>>> portfolio = stock.read_portfolio('Data/portfolio.csv')
>>> portfolio
[<stock.Stock object at 0x81d70>, <stock.Stock object at 0x81cf0>, <stock.Stock object at 0x81db0>,
 <stock.Stock object at 0x81df0>, <stock.Stock object at 0x81e30>, <stock.Stock object at 0x81e70>,
 <stock.Stock object at 0x81eb0>]
>>>

It is important to emphasize that read_portfolio() is a top-level function, not a method of the Stock class. This function is merely creating a list of Stock objects; it’s not an operation on an individual Stock instance.

Try performing some calculations with the above data. First, try printing a formatted table:

>>> for s in portfolio:
         print '%10s %10d %10.2f' % (s.name, s.shares, s.price)

... look at the output ...
>>>

Try a list comprehension:

>>> more100 = [s for s in portfolio if s.shares > 100]
>>> for s in more100:
         print '%10s %10d %10.2f' % (s.name, s.shares, s.price)

... look at the output ...
>>>

Again, notice the similarity between Stock objects and dictionaries---they’re basically the same idea, but the syntax for accessing values differs.

(c) Adding some Methods

With classes, you can attach functions to your objects. These are known as methods and are simply functions that operate on the data stored inside an object. Add a cost() and sell() method to your Stock object. They should work like this:

>>> ================================ RESTART ================================
>>> import stock
>>> s = stock.Stock('GOOG',100,490.10)
>>> s.cost()
49010.0
>>> s.shares
100
>>> s.sell(25)
75
>>> s.shares
75
>>> s.cost()
36757.5
>>>
Links