3

I'm currently learning Python and began a project to create a baseball sim for the 2000-2005 MLB showdown card game. The programs contains events of the baseball game as print statements in the middle of separate pieces of code ("Jeff hits a single", "Bobby hits a fly ball for an out", etc). I often will take out the print statements if I want to run a lot of games at once. My goal, for feasibility reasons, is to tell Python to not print certain statements (say, in a specific span of lines) even though the code says print " ". Is this possible?

example:

while numberofgames < 1000:
  [do not print any statements here]
  ---baseball games---
end of while loop

THEN: print simulation results

pjs
  • 18,696
  • 4
  • 27
  • 56
Andrew Marderstein
  • 237
  • 1
  • 4
  • 11
  • Can't you just comment out the print lines? – Pikamander2 Aug 08 '15 at 19:32
  • There's a lot of print lines for all the different events that can occur. I'm wondering if there's a simpler way to just tell it to stop printing lines then individually going through and commenting them out. – Andrew Marderstein Aug 08 '15 at 19:36
  • 4
    Use the `logging` module instead of `print` statements to control when and where output is produced. – chepner Aug 08 '15 at 19:38
  • I think the logging system would be great for messages intended for the developer e.g., but from the message examples it seems like these are supposed to go to the user on the console and I would say some form of print should be more appropriate for that. – Cobusve Aug 08 '15 at 19:42

6 Answers6

4

Can you create a global variable which you can check to decide how much you want to print ? By doing that you can control the amount of logging as you require.

  if printLevel > 3:
      print("Bobby hits a fly ball for an out")
Cobusve
  • 1,572
  • 10
  • 23
  • Thank you! I'm going to use this method as it offers the greatest flexibility with which print statements to display. Everyone's comments and answers were helpful, so thank you everyone! – Andrew Marderstein Aug 08 '15 at 19:58
2

Yes, you could put all the print statements into an if structure eg..

if printStuff:
    print 'I dont like baseball'
    print 'I love it!'

Then it is just a matter of setting printStuff to True if you want to print or False if you dont.

chepner
  • 497,756
  • 71
  • 530
  • 681
DrBwts
  • 3,470
  • 6
  • 38
  • 62
2

Python's print outputs to sys.stdout. You can put your own buffer for STDOUT.

# assuming python3
import sys
import io

my_buffer = io.StringIO()
sys.stdout = my_buffer

# print some stuff
stuff_to_print = ['foo', 'word', 'test']
for word in stuff_to_print:
    print(word)

# all the other stuff your script does

# change stdout back to original so print is pointing back to original buffer
sys.stdout = sys.__stdout__

# now print everything out at once
print(my_buffer.get_value())
Philip Ramirez
  • 2,972
  • 1
  • 16
  • 16
  • Very useful answer when the print statement are in a third-party library where we can't touch the code – Astariul May 21 '20 at 23:56
1

You could use Replace All to replace print( with #print(.

When you're ready to print again, you could do the opposite: Replace #print( with print(.

Pikamander2
  • 7,332
  • 3
  • 48
  • 69
1

A hack, sure, but why not override the print function for a while?

#the line below needs to be the first in the file
#and is required on Python 2.7
from __future__ import print_function

def to_null(*args, **kwds):
    pass

def test1(x):
    print ("test1(%s)" % (x))


#override the print 
old_print = __builtins__.print
__builtins__.print = to_null

test1("this wont print")

#restore it
__builtins__.print = old_print

test1("this will print")

output:

test1(this will print)

see also Is it possible to mock Python's built in print function?

Finally, the suggestion to use the logging module is spot-on. Though that module can be tricky to use well.

Community
  • 1
  • 1
JL Peyret
  • 10,917
  • 2
  • 54
  • 73
1

You can use the logging module for this:

https://docs.python.org/3/library/logging.html

https://docs.python.org/3/howto/logging.html#logging-basic-tutorial

The logging module has several different levels.

Level --------- Numeric value

CRITICAL ---50

ERROR ----- 40

WARNING --30

INFO ---------20

DEBUG -----10

NOTSET -----0

You assign messages a level. For instance, logging.info("Debug") is an INFO level message which prints "Debug". If the level of the logger is less than or equal to the level of the message, the message will be printed.

So if you want to turn a bunch of print statements off, you would just make the statements all the same level, and then turn the logger to a higher level.

>>>import logging

>>>T=logging.getLogger()                 #create a new logger object
#Set the logger level to INFO - note basicConfig only works once!!!
#Then you must use setLevel method to change the level
>>>logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
>>>logging.info("INFO")                 #this prints because the logger is set to INFO
INFO:Info
>>>logging.warning("Warning")
WARNING:Warning

>>>T.setLevel(logging.WARNING)      #set the logger level to WARNING
>>>logging.info("Debug")            #Does not print since logger level WARNING is higher than message level INFO
>>>logging.warning("Warning")
WARNING:Warning
Evan Rosica
  • 1,182
  • 1
  • 12
  • 22