skip to Main Content

Need a recommendation on how to handle this. I have a python lambda function that accepts in json. There could be 10 different types of payload.

For example:

{
  "animalType": "dog",
  "name": "josie",
  ...
}

another app can send in:

{
  "animalType": "cheeta",
  "name": "cheetos",
  ...
}

another app can send in:

{
  "furniture": "chair",
  "storeName": "Freedom furniture",
  ...
}

I want to apply different logic based on the payload that is sent in.

Do you think it’s wise to:

if "animalType" in json.dumps(event):

    ...do something

elseif "furniture" in json.dumps(event):

    ...do something

else

    ...do something

any thoughts? the …do something area could be pretty long and I want to consider these separate modules, so should I call another function?

2

Answers


  1. so just store the output in a data struct. Core Dev solution here:

    # using a data structure: Array (in python a list)
    data_list =[]
    
    # you will need to learn to use the requests lib
    import requests
    
    data = requests.post(
                url="https://endpoint/api1/").json()
    
    ''' 
       print() is a basic way to test data 
       to see if returns what is expected or not
    '''
    # print(data) 
    
    '''
       Looping through iterative data structures is a key skill 
       and will be used a lot in all levels of python programing
    '''
    
    for d in data:
        data_list.append(d)
    

    Now that you have the data in a data_list you can loop through it and use constants to get back the logic you need:

    for data in data_list:
        ''' 
            An algorithm is a list of steps, 
            and conditional statements help control 
            the flow of data through each step. 
            A if statement is a basic example to control 
            the logic of your algorithm
        '''
    
        if data['animalType'] == "foo":
               ...do something
        if data['animalType'] == "bar":
               ...do something
    

    you can repeat this logic as needed. One of the most powerful ways to get started with data structure, design algorithms and OOP.

    I say OOP as this logic can be optimized by not just adding functions to deal with new dynamic use case and future requirements, but the above logic can also be refactored to use classes using OOP concepts.

    you will evolve as you learn and explore:

    1. Procedural programing ( logic like presented in this example)
    2. Functional Programing ( adding functions to deal with dynamic inputs and outputs)
    3. OOP ( adding classes to create a reusable Dynamic systems for your programs requirements) 
    4. Data structures and design algorithms (the proving ground to get a job as a pro developer)
    

    happy coding:)

    Login or Signup to reply.
  2. '''
    {
      "animalType": "dog",
      "name": "josie",
      ...
    }
    '''
    
    '''
    {
      "animalType": "cheeta",
      "name": "cheetos",
      ...
    }
    '''
    
    '''
    {
      "furniture": "chair",
      "storeName": "Freedom furniture",
      ...
    }
    '''
    
    You can also do like this one:-
    ```
    #for animalType do this
    def animalType():
        pass
    #for furniture do this
    def furniture():
        pass
    
    
    caller={"animalType":animalType(),
            "furniture":furniture()}
    
    '''
    if you are using >=python3.6 version  , for key ordering
    if order of inserting these keys (animalType,furniture) in dict is
      always same then you can use index to access the value as below
    if you are getting your animalType on 0th index 
    '''
    key=list(events.keys())[0]
    caller[key]
    
    
    
    ```
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search