简体   繁体   English

如何防止当前功能的先前功能打印?

[英]How to prevent printing from previous function in current function?

I'm currently writing a test function for class to test provided cases on provided solution code. 我目前正在为类编写一个测试函数,以在提供的解决方案代码上测试提供的案例。 However I'm running into an issue where a print statement is executing when I don't want it to. 但是,我遇到了一个我不希望执行打印语句的问题。

This is the provided solution that I'm testing: 这是我正在测试的提供的解决方案:

def alphapinDecode(tone):
     phone_num = ''

if checkTone(tone):         #or checkTone2
    while len(tone) > 0:

        # retrieve the first tone
        next_tone = tone[0:2]
        tone = tone[2:]

        # find its position
        cons = next_tone[0]
        vow = next_tone[1]

        num1 = consonants.find(cons)
        num2 = vowels.find(vow)

        # reconstruct this part of the number -
        # multiply (was divided) and add back
        # the remainder from the encryption division.
        phone = (num1 * 5) + num2

        # recreate the number
        # by treating it as a string
        phone = str(phone)

        # if single digit, not leading digit, add 0
        if len(phone) == 1 and phone_num != '':
            phone = '0' + phone

        phone_num = phone_num + phone

    # but return in original format
    phone_num = int(phone_num)

else:
    print('Tone is not in correct format.')
    phone_num = -1
return phone_num

Here's the (partially done) code for the test function I have written: 这是我编写的测试函数的(部分完成)代码:

def test_decode(f):
    testCases = (
            ('lo', 43),
            ('hi', 27),
            ('bomelela', 3464140),
            ('bomeluco', 3464408),
            ('', -1),
            ('abcd', -1),
            ('diju', 1234),
            )

    for i in range(len(testCases)):
        if f(testCases[i][0]) == testCases[i][1] and testCases[i][1] == -1:
            print('Checking '+ f.__name__ + '(' + testCases[i][0] + ')...Tone is not in correct format.')
            print('Its value -1 is correct!')
    return None

When executing test_decode(alphapinDecode), I get this: 当执行test_decode(alphapinDecode)时,我得到了:

Tone is not in correct format.
Checking alphapinDecode()...Tone is not in correct format.
Its value -1 is correct!
Tone is not in correct format.
Checking alphapinDecode(abcd)...Tone is not in correct format.
Its value -1 is correct!

As you can see, because of the print statement in alphapinDecode(I think), it is printing an extra "Tone is not in correct format." 如您所见,由于alphapinDecode(我认为)中的print语句,它正在打印额外的“ Tone格式不正确”。 above the print statement I have written. 在我写的打印声明上方。

How would I prevent this print statement from executing, and why is it printing if the print statement I wrote in my test function doesn't ask for the result of alphapinDecode? 如果我在测试函数中编写的打印语句不要求提供alphapinDecode的结果,我将如何阻止该打印语句执行以及为什么打印?

We are not allowed to alter the code of the given solution. 我们不允许更改给定解决方案的代码。

I'm fairly new to stackOverflow, so sorry for any formatting issues. 我对stackOverflow还是相当陌生,所以对任何格式化问题都感到抱歉。 Thank you! 谢谢!

Edit: Fixed the idents of the test_decode function 编辑:修复了test_decode函数的标识

One easy solution would be to pass an extra parameter say, a boolean variable debug to the function. 一种简单的解决方案是将一个额外的参数(即布尔变量debug)传递给该函数。 That would go something like this. 那会像这样。

def func1(var1, debug):
    if debug:
        print("Printing from func1")
    # Do additional stuff

Now when you call it. 现在,当您调用它时。 You now have the option of setting the debug variable. 现在,您可以选择设置调试变量。

func1("hello", debug=True) # will print the statement
func1("hello", debug=False) # will not print statement.

If you cannot modify the called function. 如果无法修改所调用的函数。 Then you can follow this method. 然后,您可以按照此方法。 explained by @FakeRainBrigand here . @FakeRainBrigand 在这里解释。

import sys, os

# Disable
def blockPrint():
    sys.stdout = open(os.devnull, 'w')

# Restore
def enablePrint():
    sys.stdout = sys.__stdout__


print 'This will print'

blockPrint()
print "This won't"

enablePrint()
print "This will too"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何防止 function 在鼠标单击时运行,直到上一个完成 - How to prevent a function from running on mouse click until previous completes 防止函数在 Python 中的批处理控制台中打印 - To prevent a function from printing in the batch console in Python 防止OpenCV函数CreateVideoWriter在Python中打印到控制台 - Prevent OpenCV function CreateVideoWriter from printing to console in Python 打印浮点数时如何防止打印功能中的自动舍入? - How to prevent automatic rounding in print function when printing a float number? 如何创建一个在每次调用时从前一个值减去当前值的函数 - How to create a function that subtracts the current value from previous value every time it's called 如何使用 scrapy 从以前的 function 中抓取链接 - how to scrape link from a previous function with scrapy 如何使用从当前行和上一行访问数据的 function 向数据框添加新列? - How can I add a new column to a data frame using a function that accesses data from both current and previous row? 如何在另一个 function 中使用前一个 function 返回的变量? (Python) - How to use a returned variable from a previous function in another function? (python) 如何防止函数被强制转换为bool - How to prevent a function from being cast to bool 如何防止Python function返回None - How to prevent Python function from returning None
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM