-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathband_replace.py
More file actions
74 lines (49 loc) · 1.93 KB
/
Copy pathband_replace.py
File metadata and controls
74 lines (49 loc) · 1.93 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
__author__ = 'yingminzhou'
bands = [{'name': 'sunset rubdown', 'country': 'UK', 'active': False},
{'name': 'women', 'country': 'Germany', 'active': False},
{'name': 'a silver mt. zion', 'country': 'Spain', 'active': True}]
def format_bands(bands):
for band in bands:
band['country'] = 'Canada'
band['name'] = band['name'].replace('.', ' ')
band['name'] = band['name'].title()
# format_bands(bands)
# print(bands)
def assoc(_d, key, value):
from copy import deepcopy
d = deepcopy(_d)
d[key] = value
return d
'''
def set_canada_as_country(band):
return assoc(band,'country','Canada')
def strip_punctuation_from_name(band):
return assoc(band,'name',band['name'].replace(',',' '))
def capitalize_names(band):
return assoc(band,'name',band['name'].title)
'''
def call(fn, key):
def apply_fn(record):
return assoc(record, key, fn(record.get(key)))
return apply_fn
set_canada_as_country = call(lambda x: 'Canada', 'country')
strip_punctuation_from_name = call(lambda x: x.replace(',', ' '), 'name')
capitalize_names = call(lambda x: x.title(), 'name')
def pipeline_each(data, fns):
from functools import reduce
return list(reduce(lambda a, x: map(x, a), fns, data))
# print(pipeline_each(bands,[set_canada_as_country,strip_punctuation_from_name,capitalize_names]))
'''
def extract_name_and_country(band):
plucked_band = {}
plucked_band['name'] = band['name']
plucked_band['country'] = band['country']
return plucked_band
'''
# print(pipeline_each(bands,[set_canada_as_country,strip_punctuation_from_name,capitalize_names,extract_name_and_country]))
def pluck(keys):
def pluck_func(record):
from functools import reduce
return reduce(lambda a, x: assoc(a, x, record[x]), keys, {})
return pluck_func
print(pipeline_each(bands,[set_canada_as_country,strip_punctuation_from_name,capitalize_names,pluck(['name','country'])]))