Skip to content

Instantly share code, notes, and snippets.

@miku
Last active August 29, 2015 14:01
Show Gist options
  • Save miku/e72628ee54fce9f06a34 to your computer and use it in GitHub Desktop.
Save miku/e72628ee54fce9f06a34 to your computer and use it in GitHub Desktop.
Example for BaseTask and ClosestDateParameter example
#!/usr/bin/env python
# coding: utf-8
"""
1) SimpleTask will "know" its output `path`.
2) ClosestDateParameter helps to map multiple dates to a single date,
e.g. I want to be able to call the task every day, but the output
from the most recent Monday will be enough.
"""
from gluish.parameter import ClosestDateParameter
from gluish.task import BaseTask
import datetime, luigi
class SimpleTask(BaseTask):
date = ClosestDateParameter(default=datetime.date.today())
def closest(self):
# for simplicity, only map this task to the *closest* Monday
return self.date - datetime.timedelta(days=self.date.weekday())
def run(self):
with self.output().open('w') as output:
output.write("It's just another manic Monday!")
def output(self):
return luigi.LocalTarget(path=self.path(ext='txt'))
if __name__ == '__main__':
# January 2014
# Su Mo Tu We Th Fr Sa
# 1 2 3 4
# 5 6 7 8 9 10 11
# 12 13 14 15 16 17 18
# 19 20 21 22 23 24 25
# 26 27 28 29 30 31
task = SimpleTask(date=datetime.date(2014, 1, 1))
print(task.output().path) # /tmp/default/SimpleTask/date-2013-12-30.txt
task = SimpleTask(date=datetime.date(2014, 1, 5))
print(task.output().path) # /tmp/default/SimpleTask/date-2013-12-30.txt
task = SimpleTask(date=datetime.date(2014, 1, 6))
print(task.output().path) # /tmp/default/SimpleTask/date-2014-01-06.txt
task = SimpleTask(date=datetime.date(2014, 1, 7))
print(task.output().path) # /tmp/default/SimpleTask/date-2014-01-06.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment