@contextmanager
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(u'gprof2dot -f pstats {path} | dot -Tpng -o {path}.png && open {path}.png'.format(path=profile_path))
  logging.getLogger().info(u'Dumped profile data to: {}\nUse e.g. {} to render and view.'.format(profile_path, view_cmd))

@ contextmanager 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 ( u'gprof2dot -f pstats {path} | dot -Tpng -o {path}.png && open {path}.png' . format ( path = profile_path ) ) logging . getLogger ( ) . info ( u'Dumped profile data to: {}\nUse e.g. {} to render and view.' . format ( profile_path , view_cmd ) )
a profiling context manager .