#StackBounty: #python #logging How to add log info in the Code in python in the loop

Bounty: 50

  • Below is the dictionary
  • There are two ids, I need to generat
  • Once extract of completion of first id i need to get an info saying First id(100) is completed
  • Once extract of completion of second id i need to get an info saying second id(101) is completed

logger.info('extraction id' + str(id) + 'completed')

logger.info('extraction id' + + str(id) + 'completed')

Expected out

    test = [{"id":"100","name":"A",
    "Business":[{"id":"7","name":"Enterprise"},
    {"id":"8","name":"Customer"}],
    "policies":[{"id":"332","name":"Second division","parent":"Marketing"},
    {"id":"3323","name":"First division","parent":"Marketing"}]},
    {"id":"101","name":"B",
    "Business":[{"id":"7","name":"Enterprise"},
    {"id":"8","name":"Customer"}],
    "policies":[{"id":"332","name":"Second division","parent":"Marketing"},
    {"id":"3323","name":"First division","parent":"Marketing"}]}]

code


def do_the_thing(lst):
    resp = []

    parents_mapper = {
        'Marketing': 'level1',
        'Advertising': 'level2'
    }

    for el in lst:
        d = {
            'id': el['id'],
            'name': el['name'],
            'Business': [],
            'level1': [],
            'level2': []
        }
        for business in el.get('Business', []):
            business_name = business.get('name')
            if business_name:
                d['Business'].append(business_name)

        for policy in el.get('policies', []):
            policy_parent = policy.get('parent')
            parent_found = parents_mapper.get(policy_parent)
            policy_name = policy.get('name')
            if parent_found and policy_name:
                d[parent_found].append(policy_name)

        resp.append(d)
    return resp


#def lambda_handler(event,context):
if __name__ == '__main__':
    import pprint
    pp = pprint.PrettyPrinter(4)
    pp.pprint(do_the_thing(test))

output for 2 ids

[
  {
    "id": "100",
    "name": "A",
    "Business": ["Enterprise", "Customer"],
    "level1": ['Second division', 'First division'],
    "level2": [None ]
  },
  {
    "id": "101",
    "name": "B",
    "Business": ["Enterprise", "Customer"],
    "level1": ['Second division', 'First division'],
    "level2": [None ]
  }
]

First id completed then i will get extraction id' 100 is completed' second id completed then i will get extraction id' 10 is completed’`

** Expected out_one

extraction id' 100 is completed'
extraction id' 101 is completed'

** Expected out_two

extraction Business' 100 is completed'
extraction policy' 100 is completed'
extraction level1' 100 is completed'
extraction Business' 101 is completed'
extraction policy' 101 is completed'
extraction level1' 101 is completed'


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.