"""
Some silly exercises for learning Python.
"""

def spamify(list):
  """ 
  Returns a new list in which 'spam' alternates with items from the original
  list.

  Example:
  >>> from exercises import spamify
  >>> spamify(['eggs', 'bacon', 'coffee', 'spam'])
  ['spam', 'eggs', 'spam', 'bacon', 'spam', 'coffee', 'spam', 'spam']
  """
  result = []
  for item in list:
    result.append('spam')
    result.append(item)
  return result

class Swallow(object):
  """ A generic swallow.
  A swallow has a maximum speed and a burden.
  The maximum speed is measured in miles per hour.
  The burden is a fraction of the bird's body weight.
  The swallow's actual speed depends on both its speed and its burden.
  """
  def __init__(self):
    "Initializes maxSpeed and burden to 0."
    self.maxSpeed = 0
    self.burden = 0

  def setBurden(self, burden):
    "Sets the burden to the given value."
    self.burden = burden

  def getBurden(self):
    "Gets the burden."
    return self.burden

  def setMaxSpeed(self, speed):
    "Sets the maximum speed to the given value." 
    self.maxSpeed = speed

  def getMaxSpeed(self):
    "Gets the maximum speed."
    return self.maxSpeed

  def getCurrentSpeed(self):
    """
    Actual speed is maxSpeed * (1 - burden).
    The actual speed should never be less than 0.
    """
    speed = self.maxSpeed * (1 - self.burden)
    if speed > 0:
      return speed
    else:
      return 0

class EuropeanSwallow(Swallow):
  """ According to http://www.style.org/unladenswallow/, European 
  swallows have a maximum speed of 24 mph. 
  """
  # In the constructor, initialize maxSpeed to the absolute maximum
  # speed.
  def __init__(self):
    Swallow.__init__(self) # Call superclass constructor
    self.maxSpeed = 24
    # self.burden = 0 # Set in Swallow constructor

  def getSpeciesMaxSpeed():
    return 24

  # Override setMaxSpeed(self, speed) so that if speed is greater than 
  # the absolute maximum, a ValueError exception is raised.
  def setMaxSpeed(self, speed):
    if (speed > 24):
      raise ValueError, "That speed is too big!"
    self.maxSpeed = speed
    
# If this file is loaded using "python <filename.py>", run some tests.
if __name__ == "__main__": 
  breakfast = ['eggs', 'bacon', 'coffee', 'spam']
  print "Breakfast: %s" % breakfast
  print "Spamified: %s" % spamify(breakfast)

  # Test Swallow.getSpeed()
  bill = Swallow()
  bill.setMaxSpeed(100)
  bill.setBurden(0.25)
  print "Bill's max speed is %d" % bill.getMaxSpeed()
  print "Bill's burden is %.2f" % bill.getBurden()
  print "Bill's current speed is %d (should be 75)" % bill.getCurrentSpeed()
  
  # Test EuropeanSwallow initialization
  coo = EuropeanSwallow()
  print "Coo's current speed is %d (should be 24)" % coo.getCurrentSpeed()

  # Test that a ValueError exception is raised when setting too large a 
  # maximum speed for a EuropeanSwallow
  try:
    coo.setMaxSpeed(100)
    print "If you see this, then a EuropeanSwallow has maxSpeed of 100!"
  except ValueError, msg:
    print msg

  print EuropeanSwallow.getSpeciesMaxSpeed()
  print coo.getSpeciesMaxSpeed()

