public
Created

run once decorator

  • Download Gist
run_once.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# from http://stackoverflow.com/questions/4103773/efficient-way-of-having-a-function-only-execute-once-in-a-loop
from functools import wraps
def run_once(f):
"""Runs a function (successfully) only once.
 
The running can be reset by setting the `has_run` attribute to False
"""
@wraps(f)
def wrapper(*args, **kwargs):
if not wrapper.has_run:
result = f(*args, **kwargs)
wrapper.has_run = True
return result
wrapper.has_run = False
return wrapper
 
@run_once
def load_ipython_extension(ip):
"""Load the extension in IPython."""
# do work
pass

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.