0

In the code below, gen_window has a print statement in it but when I run the code the print statement doesn't get executed. Why is that and then how should I go about debugging such lambda functions? (Even the debugger ignores the breakpoint in these functions.)

getpairs = rdd.flatMap(lambda xi: gen_window(xi, n))


def gen_window(xi, n):
    x, i = xi
    l = []
    for offset in range(n):
        print("-->", (i - offset, (i, x)))
        l.append((i - offset, (i, x)))
    return l
2
  • 1
    where do you set n? Commented Feb 12, 2019 at 20:37
  • @vs97 This isa part of larger code. I just hard code n as 3 for testing. Would you like me to post more code? I unfortunately can't post all of the code. Commented Feb 12, 2019 at 20:44

2 Answers 2

1

Works:

def gen_window(xi, n):
    x, i = xi
    l = []
    for offset in range(n):
        print("-->", (i - offset, (i, x)))
        l.append((i - offset, (i, x)))
    return l

xi = [3,5]
n = 3

gen_window(xi, n)

Lambdas only get executed wenn you actually use them - If you get no output you are probably never using it.

Output:

--> (5, (5, 3))
--> (4, (5, 3))
--> (3, (5, 3))
Sign up to request clarification or add additional context in comments.

Comments

0

Lambda functions always return an iterator hence you would have to wrap that lambda call into a list

Define a function that returns the length of a string

def string_length(s): length = len(s) print(f"The length of '{s}' is {length}") return length

Define a list of strings

strings = ["hello", "world", "python"]

Apply the function to each element in the list using map and a lambda function,

then convert the result to a list

lengths = list(map(lambda x: string_length(x), strings))

Print the resulting list of lengths

print(lengths) This would execute the print statements

Define a function that returns the length of a string

def string_length(s): length = len(s) print(f"The length of '{s}' is {length}") return length

Define a list of strings

strings = ["hello", "world", "python"]

Apply the function to each element in the list using map and a lambda function,

then convert the result to a list

lengths = map(lambda x: string_length(x), strings)

Print the resulting list of lengths

print(lengths)

But this would not

1 Comment

please format it.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.