Halite Home

Suggestions on debugging python code with halite locally?


#1

I am not experienced with python, however I find it interesting to start with the ML starter pack (Thank you brianvanleeuwen). Thus I am currently using python3 for the bots. However currently finding it difficult to debug my bots.

I have two questions:

  1. How do I output to a local file if I want to print out anything? I tried logging as mentioned in this post, however it doesn't seem work: the logging message is sent to halite.
  2. Is there anyway to trigger a pdb (or similar) environment, so that I can play with the variables more efficiently?

Thank you so much.


#2

Yo, here is a small logger class.

class Logger:
  
    def __init__(self, file_name):
        self.file_name = ".".join( ("_".join( (file_name, str(randint(0, 100000))) ), "log") )
        self.stream = None
        self.turn = -1
    
    def open(self):
        self.stream = open(self.file_name, "a")
        assert self.stream
        self.turn += 1
    
    def close(self):
        assert self.stream
        self.stream.close()
    
    def log(self, msg):
        assert self.stream
        self.stream.write("".join( (" ".join( ("Turn", str(self.turn), ":", str(msg) ) ), "\n") ))

logger = Logger("Your_file_name")

It will generate a log file into your halite directory. You should adapt your code like that:

# Beginning of initialization

logger.open()

# Some fancy initializations

logger.close()

while True:
    logger.open()
    # Your bot runs
    logger.close()

Then you can call logger.log(msg) everywhere you want :slight_smile: I think yeah it should be a pdb equivalent but I've no idea of how to emulate that into halite... I debug everything with the logger


#3

Hi Risitop,

Thank you so much for sharing it. I have just tested it and it worked perfectly.

Good luck,

Tong