title: How can I refactor my two @listens_for event callbacks into one?
body: <p>I have two callbacks that are triggered on an <code>before_insert</code> and an <code>before_update</code>. The code that is executed are the exact same code and what I would like to do is to refactor that so that both <code>@listens_for</code> execute the same method.</p>
  
  <p>This is what I have:</p>
  
  <pre><code>@listens_for(Employee, 'before_insert')
  def change_employee_path(mapper, connection, target):
      if target.path:
          os.rename(os.path.join(app.config['UPLOAD_FOLDER'], target.path), 'static/images/' + remove_blackduck_address(target.email))
          target.path = remove_blackduck_address(target.email)
  
  @listens_for(Employee, 'before_update')
  def change_employee_path(mapper, connection, target):
      if target.path:
          os.rename(os.path.join(app.config['UPLOAD_FOLDER'], target.path), 'static/images/' + remove_blackduck_address(target.email))
          target.path = remove_blackduck_address(target.email)
  </code></pre>
  
  <p>This is what I would like (doesn't work btw):</p>
  
  <pre><code>def change_employee_path(mapper,connection, target):
      if target.path:
          os.rename(os.path.join(app.config['UPLOAD_FOLDER'], target.path), 'static/images/' + remove_blackduck_address(target.email))
          target.path = remove_blackduck_address(target.email)
  
  @listens_for(Employee, 'before_insert')
  change_employee_path(mapper, connection, target)
  
  @listens_for(Employee, 'before_update')
  change_employee_path(mapper, connection, target)
  </code></pre>
  
  <p>Is there a way of achieving this?</p>