Skip to content

Commit

Permalink
Improve Function Factory examples.
Browse files Browse the repository at this point in the history
  • Loading branch information
shawnbrown committed Dec 13, 2020
1 parent 77f2fb4 commit da133f2
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions docs/discussion/tips-and-tricks.rst
Expand Up @@ -329,21 +329,22 @@ function factory is a function that makes other functions.
from datatest import validate
def ends_with(suffix): # <- Helper function factory.
suffix = suffix.lower()
def helper(x):
return x.lower().endswith(suffix)
helper.__doc__ = f'should end with {suffix!r}'
return helper
data1 = [...]
validate(data1, ends_with('.csv'))
data2 = [...]
validate(data2, ends_with('.txt'))
data3 = [...]
validate(data3, ends_with('.ini'))
.. group-tab:: No Factory

Instead of a factory, this example uses separate helper functions:
Expand All @@ -353,22 +354,27 @@ function factory is a function that makes other functions.
from datatest import validate
def ends_with_csv(x): # <- Helper function.
"""should end with '.csv'"""
return x.lower().endswith('.csv')
data1 = [...]
validate(data1, ends_with_csv)
def ends_with_txt(x): # <- Helper function.
"""should end with '.txt'"""
return x.lower().endswith('.txt')
def ends_with_ini(x): # <- Helper function.
"""should end with '.ini'"""
return x.lower().endswith('.ini')
data1 = [...]
validate(data1, ends_with_csv)
data2 = [...]
validate(data2, ends_with_txt)
data3 = [...]
validate(data3, ends_with_ini)
Lambda Expressions
==================
Expand Down

0 comments on commit da133f2

Please sign in to comment.