Skip to content

Instantly share code, notes, and snippets.

@asmoore82
Created May 15, 2019 00:26
Show Gist options
  • Select an option

  • Save asmoore82/1cb9204d8e492808e48fb6bc240a639e to your computer and use it in GitHub Desktop.

Select an option

Save asmoore82/1cb9204d8e492808e48fb6bc240a639e to your computer and use it in GitHub Desktop.
Python generic attribute processor that calls filter and composer functions
#Copyright (c) 2019 Adam Moore, MIT License
#The generic attribute processor that calls filter and composer functions
import string
def filter_digits_only(s):
return ''.join(c for c in s if c in string.digits)
def get_org_id(data):
return '12345'
def get_email_lower(data):
return data['email'].lower()
attr_map = (('ORGID', 'ORGID'),
('id', 'USERID'),
('first_name', 'FIRSTNAME'),
('last_name', 'LASTNAME'),
('cell_phone', 'MOBLENUM'),
('e_mail', 'EMAIL'),
('USERNAME', 'USERNAME'))
attr_filter = {
'cell_phone': filter_digits_only,
}
attr_comp = {
'ORGID': get_org_id,
'USERNAME': get_email_lower,
}
def attr_pump(dataset,
         attr_map,
         attr_filter={},
         attr_comp={}):
ins, outs = zip(*attr_map)
#output headers
yield outs
for data in dataset:
yield tuple(attr_comp[attr](data)
if attr in attr_comp else
attr_filter[attr](data[attr])
if attr in attr_filter else
data[attr])
#needs a suitable dataset to test - an iterable of dictionaries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment