1

I have the following function check_overload(...) where I check my links if they get overload or not using the parameters shown below.

def check_overload(SR_path, Router_1, Router_2, Router_3, Router_4, step, capacity):
    iterations_array = number_iteration_tunnels(SR_path)
    load_tunnels = bandwidth_tunnel(SR_path, Router_1, Router_2, Router_3, Router_4)
    global overload_point
    overload_point = np.zeros(len(iterations_array))
    for i in range(len(iterations_array)):
        if load_tunnels[i] >= capacity - (iterations_array[i][1] * step):
            print "Link is overloaded"
            overload_point = load_tunnels[i] - step
            return overload_point
        else:
            print "Link is not overloaded"
            return 0

First of all, I calculate number of iteration for each element of SR_path in the function number_iteration_tunnels(..). Then, using the array created by this function iterations_array, I do a loop to navigate that array.

I aim to return in each iteration the overload_point if the condition is met, otherwise, I return 0. The problem that I am facing is that using this implementation, the function returns only ones which is obvious. I could not find a way where to do it as my aim is.

Tim Luka
  • 461
  • 1
  • 5
  • 11
  • 2
    If i understand correctly `yield` is keyword you want to use. Replace `return` with `yield`. Or alternative solution is to use list and return list. – Poojan Jul 30 '19 at 18:04

1 Answers1

2

Solution 1

  • You can achieve mensioned behavior using yield.
  • The following example try to reproduce your solution.
def check_overload(SR_path, Router_1, Router_2, Router_3, Router_4, step, capacity):
    iterations_array = number_iteration_tunnels(SR_path)
    load_tunnels = bandwidth_tunnel(SR_path, Router_1, Router_2, Router_3, Router_4)
    global overload_point
    overload_point = np.zeros(len(iterations_array))
    for i in range(len(iterations_array)):
        if load_tunnels[i] >= capacity - (iterations_array[i][1] * step):
            print "Link is overloaded"
            overload_point = load_tunnels[i] - step
            yield overload_point
        else:
            print "Link is not overloaded"
            yield 0

for result in check_overload(*args):
    print(result) # or do whatever you want.

Solution 2

  • using list to store results.
def check_overload(SR_path, Router_1, Router_2, Router_3, Router_4, step, capacity):
    res = []
    iterations_array = number_iteration_tunnels(SR_path)
    load_tunnels = bandwidth_tunnel(SR_path, Router_1, Router_2, Router_3, Router_4)
    global overload_point
    overload_point = np.zeros(len(iterations_array))
    for i in range(len(iterations_array)):
        if load_tunnels[i] >= capacity - (iterations_array[i][1] * step):
            print "Link is overloaded"
            overload_point = load_tunnels[i] - step
            res.append(overload_point)
        else:
            print "Link is not overloaded"
            res.append(0)
    return res

for result in check_overload(*args):
    print(result) # or do whatever you want.
Poojan
  • 3,366
  • 2
  • 17
  • 33