Skip to content

Commit

Permalink
Merge pull request #633 from spotify/erikbern/remove-util-deprecation
Browse files Browse the repository at this point in the history
Removed some deprecated stuff in luigi.util
  • Loading branch information
Erik Bernhardsson committed Jan 23, 2015
2 parents 1a84220 + 0343e93 commit 294c6eb
Show file tree
Hide file tree
Showing 5 changed files with 4 additions and 240 deletions.
111 changes: 0 additions & 111 deletions luigi/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,117 +169,6 @@ def run(self):
return Wrapped


def Derived(parent_cls):
''' This is a class factory function. It returns a new class with same parameters as
the parent class, sets the internal value self.parent_obj to an instance of it, and
lets you override the rest of it. Useful if you have a class that's an immediate result
of a previous class and you don't want to reimplement everything. Also useful if you
want to wrap a class (see wrap_test.py for an example).
Note 1: The derived class does not inherit from the parent class
Note 2: You can add more parameters in the derived class
Usage::
class AnotherTask(luigi.Task):
n = luigi.IntParameter()
# ...
class MyTask(luigi.util.Derived(AnotherTask)):
def requires(self):
return self.parent_obj
def run(self):
print self.n # this will be defined
# ...
'''
class DerivedCls(task.Task):
def __init__(self, *args, **kwargs):
param_values = {}
for k, v in self.get_param_values(self.get_params(), args, kwargs):
param_values[k] = v

# Figure out which params the parent need (it's always a subset)
parent_param_values = {}
for k, v in parent_cls.get_params():
parent_param_values[k] = param_values[k]

self.parent_obj = parent_cls(**parent_param_values)
super(DerivedCls, self).__init__(*args, **kwargs)

warnings.warn(
'Derived is deprecated, please use the @inherits decorator instead',
DeprecationWarning,
stacklevel=2
)

# Copy parent's params to child
for param_name, param_obj in parent_cls.get_params():
setattr(DerivedCls, param_name, param_obj)
return DerivedCls


def Copy(parent_cls):
''' Creates a new Task that copies the old task.
Usage::
class CopyOfMyTask(Copy(MyTask)):
def output(self):
return LocalTarget(self.date.strftime('/var/xyz/report-%Y-%m-%d'))
'''

class CopyCls(Derived(parent_cls)):
def requires(self):
return self.parent_obj

output = NotImplemented

def run(self):
i, o = self.input(), self.output()
f = o.open('w') # TODO: assert that i, o are Target objects and not complex datastructures
for line in i.open('r'):
f.write(line)
f.close()

warnings.warn(
'Copy is deprecated, please use the @copies decorator instead',
DeprecationWarning,
stacklevel=2
)
return CopyCls


class CompositionTask(task.Task):
# Experimental support for composition task. This is useful if you have two tasks where
# X has a dependency on Y and X wants to invoke methods on Y. The problem with a normal
# requires() style dependency is that if X and Y are run in different processes then
# X can not access Y. To solve this, you can let X own a reference to an Y and have it
# run it as a part of its own run method.

def __init__(self, *args, **kwargs):
warnings.warn('CompositionTask is deprecated, please use the @delegates decorator instead', DeprecationWarning)
super(CompositionTask, self).__init__(*args, **kwargs)

def subtasks(self):
# This method can (optionally) define a couple of delegate tasks that
# will be accessible as interfaces, meaning that the task can access
# those tasks and run methods defined on them, etc
return [] # default impl

def deps(self):
# Overrides method in base class
return task.flatten(self.requires()) + task.flatten([t.deps() for t in task.flatten(self.subtasks())])

def run_subtasks(self):
for t in task.flatten(self.subtasks()):
t.run()

# Note that your run method must also initialize subtasks
# def run(self):
# self.run_subtasks()
# ...


def deprecate_kwarg(old_name, new_name, kw_value):
""" Rename keyword arguments, but keep backwards compatibility.
Expand Down
47 changes: 0 additions & 47 deletions test/copy_test.py

This file was deleted.

24 changes: 0 additions & 24 deletions test/subtask_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,6 @@
import luigi
import unittest
import random, tempfile, os
from luigi.util import CompositionTask


class F(luigi.Task):
k = luigi.IntParameter()

def f(self, x):
return x ** self.k


class SubtaskTask(CompositionTask):
def subtasks(self):
return [F(1), F(2)]

def run(self):
self.run_subtasks()

for t in self.subtasks():
t.f(42)


class SubtaskTest(unittest.TestCase):
def test_multiple_workers(self):
luigi.build([SubtaskTask()], local_scheduler=True)


class AbstractTask(luigi.Task):
Expand Down
55 changes: 0 additions & 55 deletions test/util_test.py

This file was deleted.

7 changes: 4 additions & 3 deletions test/wrap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import luigi
from luigi.mock import MockFile
import unittest
from luigi.util import Derived
from luigi.util import inherits
import datetime
import luigi.notifications
luigi.notifications.DEBUG = True
Expand Down Expand Up @@ -45,9 +45,10 @@ def run(self):


def XMLWrapper(cls):
class XMLWrapperCls(Derived(cls)):
@inherits(cls)
class XMLWrapperCls(luigi.Task):
def requires(self):
return self.parent_obj
return self.clone_parent()

def run(self):
f = self.input().open('r')
Expand Down

0 comments on commit 294c6eb

Please sign in to comment.