Skip to content

Instantly share code, notes, and snippets.

@boxmein
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save boxmein/7088ea879f250f91eef2 to your computer and use it in GitHub Desktop.

Select an option

Save boxmein/7088ea879f250f91eef2 to your computer and use it in GitHub Desktop.
Tiny task/etc system for Python.
#
# Tiny task system
# ~boxmein 2014
# free to use
#
# {'name': callback, ...}
tasks = {}
# Add a task
def add_task(name, callback):
tasks[name] = {'callback': callback, 'called': False}
# (decorator!) This task depends on other tasks: (check for dupes)
def depends(f, *deps):
def func():
for dep in deps:
do_task(dep)
f()
return func
# (decorator!) This task depends on other tasks: (don't check for dupes)
def depends1(f, *deps):
def func():
for dep in deps:
do_task(dep, duplicate_check=False)
f()
return func
# (decorator!) After this task, run these tasks: (check for dupes)
def after_depends(f, *deps):
def func():
f()
for dep in deps:
do_task(dep)
return func
# (decorator!) After this task, run these tasks: (don't check for dupes)
def after_depends1(f, *deps):
def func():
f()
for dep in deps:
do_task(dep, duplicate_check=False)
return func
# (decorator!) After all calls to this task, run these tasks: (don't check for dupes)
# (decorator!) Add a task:
def task(f):
add_task(f.__name__, f)
print ('discovered task: ' + f.__name__ + (': ' + f.__doc__ if f.__doc__ else ''))
return f
# Run a task
# (check for if it's run twice with duplicate_check=True)
def do_task(name, duplicate_check=True):
name = str(name)
if name in tasks:
if duplicate_check and not tasks[name].called:
print ('running task: ' + name)
tasks[name].called = True
tasks[name].callback()
else if not duplicate_check:
print ('running task: ' + name)
tasks[name].called = True
tasks[name].callback()
else:
print ('skipping task: ' + name)
# Run all tasks listed in sys.argv
def do_all_tasks():
import sys
for arg in sys.argv[1:]:
do_task(arg)
# ---
# define tasks here
# ---
do_all_tasks()
# Example usage
# @task
# @depends('other_task')
# def this_task():
# """ doc string """
# print ('hello world')
# @task
# def other_task():
# """ doc string """
# print ('goodbye world')
#
# running python file.py this_task will output:
# goodbye world
# hello world
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment