0

I have several functions and I only want output from the outermost function call. For example:

def f1():
    print 'foo'

def f2(boo=True):
    f1()
    if boo:
        print 'bar'     
    else:
        print 'black sheep'

def f3():
    f2()
    print 'shh!!!'  

f2(True)
print
f2(False)
print 
f3()

The above script outputs:

foo
bar

foo
black sheep

foo
bar
shh!!!

The desired output:

bar

black sheep

shh!!!
2
  • Is it possible to change the code inside the functions f2, f3? Commented Jan 27, 2014 at 11:33
  • 1
    I think logging could help here. Commented Jan 27, 2014 at 11:36

2 Answers 2

2

If it is allowed to modify the code inside f2, f3 then one way would be use a custom context manager which assigns sys.stdout to something else, and at exit of that context manager reassign sys.stdout to the original STDOUT.

import sys, StringIO

class Supress_print(object):

    def __init__(self):
        pass

    def __enter__(self):
        self.stdout = sys.stdout
        sys.stdout = StringIO.StringIO()

    def __exit__(self, *args):
        sys.stdout = self.stdout

def f1():
    print 'foo'

def f2(boo=True):
    with Supress_print():
        f1()
    if boo:
        print 'bar'
    else:
        print 'black sheep'

def f3():
    with Supress_print():
        f2()
    print 'shh!!!'  

f2(True)
print
f2(False)
print 
f3()

Output:

bar

black sheep

shh!!!

Update:

import sys, inspect, functools, StringIO

def supress_print(func):

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        #print inspect.getouterframes(inspect.currentframe())[3][3], func.__name__
        if inspect.getouterframes(inspect.currentframe())[3][3] != 'main':
            stdout = sys.stdout
            sys.stdout = StringIO.StringIO()
            val = func(*args, **kwargs)
            sys.stdout = stdout
            return val
        else:
            return func(*args, **kwargs)

    return wrapper

@supress_print
def f1():
    print 'foo'


@supress_print
def f2(boo=True):
    f1()
    if boo:
        print 'bar'
    else:
        print 'black sheep'

def f3():
    f2()
    print 'shh!!!'
Sign up to request clarification or add additional context in comments.

2 Comments

what if i am only allowed to change f3() but not f2()?
@alvas I've added another solution using decorator and inspect module, haven't tested it thoroughly though. :)
1

Use inspect

import inspect
def f1():
    if (len(inspect.stack())) <= 2:
        print 'foo'

def f2(boo=True):
    f1()
    if boo:
        if (len(inspect.stack())) <= 2:
            print 'bar'     
    else:
        if (len(inspect.stack())) <= 2:
            print 'black sheep'

def f3():
    f2()
    if (len(inspect.stack())) <= 2:
        print 'shh!!!'  

f2(True)
print
f2(False)
print 
f3()

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.