TaskVine Functional Examples
These three examples show the use of higher order functions
map
, pair
and treereduce
which transparently apply
Python functions to data via remote tasks:
#!/usr/bin/env python
# Example program to show use of map() abstraction
# which generates PythonTasks automatically.
import ndcctools.taskvine as vine
import sys
def calc_age(birth_year):
import datetime
current_year = datetime.date.today().year
return current_year - birth_year
birth_years = [
2017,2019,2015,2018,2020,
2005,2008,2006,2004,2007,
1999,2002,2000,1997,1995,
1990,1989,1993,1987,1988,
1980,1975,1978,1983,1977,
]
if __name__ == "__main__":
m = vine.Manager()
print(f"listening on port {m.port}")
print("mapping ages...")
ages = m.map(calc_age, birth_years)
for (birth_year, age) in zip (birth_years, ages):
print(f"{birth_year}: {age}")
# vim: set sts=4 sw=4 ts=4 expandtab ft=python:
# This example program shows the use of the pair()
# abstraction to generate all pairs of several values.
import ndcctools.taskvine as vine
import sys
def make_name(namepair):
return namepair[0] + " " + namepair[1]
firsts = ["Joe", "Sarah", "Mark", "Lewis", "Jane", "James", "Abby", "Kate",
"Sean", "William", "Emma", "Miles", "Grace", "Cole", "Robert"]
lasts = ["Smith", "Johnson", "Thomas", "Long", "Jackson", "Knoddington",
"Riley", "Shirley", "Donaldson", "Madden", "Tyler", "Morales",
"McKinsey", "Perez", "Redford"]
if __name__ == "__main__":
m = vine.Manager()
print("listening on port", m.port)
print("pairing first and last names...")
result = m.pair(make_name, firsts, lasts, chunksize=3)
try:
print("\n".join(result))
except:
# some error in execution...
pass
# vim: set sts=4 sw=4 ts=4 expandtab ft=python:
#!/usr/bin/env python3
import ndcctools.taskvine as vine
import sys
def find_max(nums):
return max(nums)
numbers = [1,10,10000,100,1000,
57,90,68,72,45,
4268,643,985,6543,
7854,2365,98765,123,
12,34,56,78,90]
if __name__ == "__main__":
m = vine.Manager()
print("listening on port", m.port)
print("reducing array...")
max_number = m.tree_reduce(find_max, numbers, chunksize=3)
print(f"maximum number is {max_number}")
# vim: set sts=4 sw=4 ts=4 expandtab ft=python: