0
  • 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'

2 Answers 2

1

The following code:

import logging
logging.basicConfig(format='%(message)s', filename='output.log',level=logging.INFO)

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"}]}]

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)

        if business:
            logging.info(f"extraction Business' {d['id']} is completed'")

        parents = []
        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)
                if parent_found not in parents:
                    logging.info(f"extraction {parent_found}' {d['id']} is completed'")
                    parents.append(parent_found)

        if policy:
            logging.info(f"extraction policy' {d['id']} is completed'")

        logging.info(f"extraction id' {d['id']} is completed'")

        resp.append(d)

    return resp


#def lambda_handler(event,context):
if __name__ == '__main__':
    import pprint
    pp = pprint.PrettyPrinter(4)
    print("Behold Magic in Progress...")
    the_thing_result = do_the_thing(test)
    print("\nThe parsed dictionary:")
    pp.pprint(the_thing_result)

Has both outputs:

Output:

Behold Magic in Progress...

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

Log in 'output.log':

extraction Business' 100 is completed'
extraction level1' 100 is completed'
extraction policy' 100 is completed'
extraction id' 100 is completed'
extraction Business' 101 is completed'
extraction level1' 101 is completed'
extraction policy' 101 is completed'
extraction id' 101 is completed'
Sign up to request clarification or add additional context in comments.

Comments

1

I'm not exactly sure with what you mean with "** Expected out_two" But here's how to implement logging

import logging

logger = logging.getLogger
logger.setLevel('INFO')

logging.info("This is information")
logging.warning("This is a warning!")

Roughly implemented in your problem:

import logging
import pprint

test = ...

def do_the_thing(lst):
    ...
    for el in lst:
        ...
        for business in el.get('Business', []):
            ...


        for policy in el.get('policies', []):
            ...
        resp.append(d)

        logging.info(f"extraction id {d['id']} is completed")
    return resp


#def lambda_handler(event,context):
if __name__ == '__main__':
    logger = logging.getLogger()
    logger.setLevel('INFO')

    pp = pprint.PrettyPrinter(4)
    pp.pprint(do_the_thing(test))

Which results in logs

INFO:root:extraction id 100 is completed
INFO:root:extraction id 101 is completed

2 Comments

can you do ** Expected out_two
I still don't understand what you mean... @nons

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.