title: Python - Not repeating code with if statements, is there another way?
body: <p>Looking to improve my python and coding skills. I have a function that adds a particular timeframe to a time.
  I pass in:</p>
  
  <p><code>1M, 7D, 6M, 2H, M</code> etc .. and return the value. I feel like I am repeating myself.  Is there a more pythonic approach to this?</p>
  
  <pre class="lang-py prettyprint-override"><code>
  def add_timeframe(time, timeframe):                                                                                                                                     
  
      if 'H' in timeframe:                                                                                                                                                
      ┆   try:                                                                                                                                                            
      ┆   ┆   period = int(re.sub('\D', '', timeframe))                                                                                                                   
      ┆   ┆   return convert_datetime(time + datetime.timedelta(hours=period))                                                                                            
  
      ┆   except ValueError:                                                                                                                                              
      ┆   ┆   return convert_datetime(time + datetime.timedelta(hours=1))                                                                                                 
  
  
      if 'D' in timeframe:                                                                                                                                                
      ┆   try:                                                                                                                                                            
      ┆   ┆   period = int(re.sub('\D', '', timeframe))                                                                                                                   
      ┆   ┆   return convert_datetime(time + datetime.timedelta(days=period))                                                                                             
  
      ┆   except ValueError:                                                                                                                                              
      ┆   ┆   return convert_datetime(time + datetime.timedelta(days=1))                                                                                                  
  
  
      if 'W' in timeframe:                                                                                                                                                
      ┆   try:                                                                                                                                                            
      ┆   ┆   period = int(re.sub('\D', '', timeframe))                                                                                                                   
      ┆   ┆   return convert_datetime(time + datetime.timedelta(weeks=period))                                                                                            
  
      ┆   except ValueError:                                                                                                                                              
      ┆   ┆   return convert_datetime(time + datetime.timedelta(weeks=period))                                                                                            
  
  
      if 'M' in timeframe:                                                                                                                                                
      ┆   try:                                                                                                                                                            
      ┆   ┆   period = int(re.sub('\D', '', timeframe))                                                                                                                   
      ┆   ┆   return convert_datetime(time + datetime.timedelta(days=365/12*period))                                                                                      
  
      ┆   except ValueError:                                                                                                                                              
      ┆   ┆   return convert_datetime(time + datetime.timedelta(days=365/12))
  </code></pre>