Skip to content
Advertisement

return boolean value for a custom function

I am comparing key elements in a list with dictionary keys

event_test = {
    "EventType": "ShipmentDueDate", 
    "Endpoint": "https://example.net"
    }

events_list = ['EventType','Endpoint']
body = "https"

I wrote a custom function as follows

def validate_input(event_keys,body): 
  count = 0 
  for list_item in events_list: 
    if list_item in event_keys:
        count+= 1
  if count != len(events_list):
      print("One/All of: 'EventType','Endpoint' parameters are missing.")
  if not "https" in body:
      print("Only https endpoints are accepted")
  return bool

I want to execute the other part of my code, only if the function executes without any errors. I dont understand how to specify a return value for the function and execute my code based on the return value

I am trying this : calling my function first

validate_response = validate_input(list(event_test.keys()),body)

if validate_response == False:
    print("error in your input")
try:
    print("execute my rest of the code")

Is this the right way to do?

Advertisement

Answer

First things first, I see a couple of issues

1. There is an issue with your eval condition (you are missing an else)

if validate_response == False:
    print("error in your input")
else:
    print("execute my rest of the code")

2. Your function is not returning True or False AND the not in operator isn’t used correctly when checking for ‘https’

def validate_input(event_keys,body):
    count = 0
    for list_item in events_list:
        if list_item in event_keys:
            count+= 1
    if count != len(events_list):
        print("One/All of: 'EventType','Endpoint' parameters are missing.")
        return False
    if "https" not in body:
        print("Only https endpoints are accepted")
        return False
    return True

Moving on…

I am a bit unclear as to what you’re referring to as the “right way”

I’ll take a stab at it and assume you are referring to reducing the complexity and/or lines of code in your sample…

You may try to use the following “optimizations”

Changing your function to use list comprehensions

NOTE: I did change the name of the “body” function param to “body_str” as it shadowed the body variable from the outer scope. Please avoid this as a rule of thumb

def validate_input(event_keys, body_str):
    count = len([x for x in events_list if x in event_keys])
    # print count to debug
    print(f"Count is {count}")
    if count != len(events_list):
        print("One/All of: 'EventType','Endpoint' parameters are missing.")
        return False
    if "https" not in body_str:
        print("Only https endpoints are accepted")
        return False
    return True

The following line essentially returns a new list of elements that match your if condition, it then uses the len operation to count the number of elements that matched the said condition

count = len([x for x in events_list if x in event_keys])

Changing your evaluation

One possibility (I would personally use this)

if not validate_response:
    print("error in your input")
else:
    print("execute my rest of the code")

Another possibility is to get rid of temporary variable assignment altogether – reduces readability though

# validate_response = validate_input(list(event_test.keys()),body)

if not validate_input(list(event_test.keys()), body_str):
    print("error in your input")
else:
    print("execute my rest of the code")
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement