ground- water, geo- statistics, environmental- engineering, earth- science

Archive for July, 2013

Flipping Coins in IPython

without comments

Catherine Devlin demonstrates a fun way to show a flipped coin programmatically in an IPython workbook adapting the html representation of an object to show the correct picture (“heads” or “tails”) of a coin:

class Coin(object):

  def __init__(self, state=None):

      if state in ('heads', 'tails'):

          self.state = state



  def flip(self):

      self.state = random.choice(('heads', 'tails'))

  def __repr__(self):

      return "Coin('%s')" % self.state

  coin_faces = {'heads': 'CENT_OBV.jpg', 'tails': 'CENT_REV.jpg'}

  def _repr_html_(self):

      return '<img src="files/images/%s" />' % self.coin_faces[self.state]

Which results in this random output in IPython:


Written by Claus

July 29th, 2013 at 6:04 pm

Posted in