code: def maybe_profiled(profile_path):
    
    if not profile_path:
      yield
      return
  
    import cProfile
    profiler = cProfile.Profile()
    try:
      profiler.enable()
      yield
    finally:
      profiler.disable()
      profiler.dump_stats(profile_path)
      view_cmd = green('gprof2dot -f pstats {path} | dot -Tpng -o {path}.png && open {path}.png'
                       .format(path=profile_path))
      logging.getLogger().info(
        'Dumped profile data to: {}\nUse e.g. {} to render and view.'.format(profile_path, view_cmd)
      )
docs: A profiling context manager.
  
    :param string profile_path: The path to write profile information to. If `None`, this will no-op.