0

I have written multiple functions that when I call them directly, they result as expected. This is the function:

def number_generator():
    for number in range(1, 815+1):
        file = open('numbers.txt', 'a')
        file.write(f'{number}\n')
        file.close()

It writes numbers from 1 to 815 without problem.

But when I call this function inside another function, it returns unexpected value. This is the other function:

def test():
    file1 = open('file1.txt', 'a')
    file1.write(f'{query_generator()}{number_generator()}\'\n')
    file1.close()

The current result is NoneNone'.

query_generator generates 815 times one SQL query in 815 lines, and number_generator generates number for ID of each my website products.

But when I use them inside test function I do not get the expected value.

What I did so far is I removed the lines related to file variable related to number_generator and query_generator, and added a return statement inside both of them but that did not work.

My expected value is like something below:

UPDATE `tblpricing` SET `msetupfee` = '1'
UPDATE `tblpricing` SET `msetupfee` = '2'
UPDATE `tblpricing` SET `msetupfee` = '3'

How may I reach it?

1 Answer 1

1

You're passing a function that doesn't return anything into an f string. You noted that you added a return statement, but I am just going to assume that it was just a return followed by nothing else, seeing that that still didn't work. See this simple demo:

def add(a, b):
    result = a+b
    return
a = 1
b = 4
print(f'{a} + {b} is {add(a, b)}')

Output:

1 + 4 is None

Why? Because there is no return value in add(). If we changeadd() to:

def add(a, b):
    result = a+b
    return result

The output is:

1 + 4 is 5

You have not shown what your query_generator() function looks like so without seeing that function, we cannot say if there is another issue than just a missing return value. This line is your culprit:

file1.write(f'{query_generator()}{number_generator()}\'\n')

number_generator() doesn't return anything, it just opens a file and writes the numbers 1 to 815. To solve your issue, create two functions: one for writing the file and one for reading it. I suggest implementing with open() instead of just open(). Also, your current number_generator() opens a file, writes one line, and closes that file 815 times which you probably don't want. Do this instead:

def number_generator():
    with open('numbers.txt', 'w') as file:
        for number in range(1, 816):
            file.write(f'{number}\n')

def get_numbers():
    with open('numbers.txt', 'r') as file:
        lines = [line.strip() for line in file]
        return lines

def query_generator():
    # logic for query generator here

def get_queries():
    # get queries
    # IMPORTANT: Return queries as a list

Note that in get_queries(), the return type should be a list. This is so you can easily iterate and compare both the queries and the numbers simultaneously. Your function could then look like this:

def test():
    # write queries and numbers
    query_generator()
    number_generator()

    # read queries and numbers
    queries = get_queries()
    numbers = get_numbers()

    # zip queries and numbers
    lines = zip(queries, numbers)

    # open file and write each line
    with open('file1.txt', 'w') as file:
        for line in lines:
            comp_line = f'{line[0]} = {line[1]}\n'
            file.write(comp_line)

This gets you your desired output:

UPDATE `tblpricing` SET `msetupfee` = '1'
UPDATE `tblpricing` SET `msetupfee` = '2'
UPDATE `tblpricing` SET `msetupfee` = '3'
...
Sign up to request clarification or add additional context in comments.

Comments

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.