Skip to content

Instantly share code, notes, and snippets.

@ian-whitestone
Created November 1, 2018 02:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ian-whitestone/60fa7debc031dd273d083e657680e920 to your computer and use it in GitHub Desktop.
Save ian-whitestone/60fa7debc031dd273d083e657680e920 to your computer and use it in GitHub Desktop.
import sys
import dask.bag as db
def gt(x):
return x > 3
def even(x):
return x % 2 == 0
## WORKS
bag = db.from_sequence([1,2,3,4,5,6])
bag = bag.filter(lambda x: gt(x))
bag = bag.filter(lambda x: even(x))
res = bag.compute()
print (res)
## DOES NOT WORK
bag = db.from_sequence([1,2,3,4,5,6])
for tform in ['gt', 'even']:
transform_func = getattr(sys.modules[__name__], tform)
bag = bag.filter(transform_func)
res = bag.compute()
print (res)
## DOES NOT WORK
bag = db.from_sequence([1,2,3,4,5,6])
transform_func = getattr(sys.modules[__name__], 'gt')
lambda_func = lambda x: transform_func(x)
bag = bag.filter(lambda_func)
transform_func = getattr(sys.modules[__name__], 'even')
lambda_func = lambda x: transform_func(x)
bag = bag.filter(lambda_func)
res = bag.compute()
print (res)
## DOES NOT WORK
bag = db.from_sequence([1,2,3,4,5,6])
for tform in ['gt', 'even']:
lambda_func = lambda x: getattr(sys.modules[__name__], tform)(x)
bag = bag.filter(lambda_func)
res = bag.compute()
print (res)
## WORKS
bag = db.from_sequence([1,2,3,4,5,6])
lambda_func = lambda x: getattr(sys.modules[__name__], 'gt')(x)
bag = bag.filter(lambda_func)
lambda_func = lambda x: getattr(sys.modules[__name__], 'even')(x)
bag = bag.filter(lambda_func)
res = bag.compute()
print (res)
@ian-whitestone
Copy link
Author

Filters will get overridden in certain scenarios....

In general, want the ability to pass kwargs to a filter function like you can with dask.map

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment