흔히들 print() 해서 우리의 프로그램 로직이 바르게 가고 있는 지를 살핀다. 하지만 보다 일목요연하게 프로그램 로직을 살필 수 있는 기능으로 logging 기능이 단연 최고다. 특히나 방대한 프로그램을 개발할 때엔 이 logging 기능을 활용하지 않고선 너무나 비능률적이란 걸 경험해본 이들은 다들 안다. DEBUG, INFO, WARNING, ERROR, CRITICAL 다섯 단계가 있으며, 갈수록 심각한 문제를 알린다.
LC training - What you can do with logging is you can set certain levels so for example Let's go ahead an import logging and then let's set up and configure it So we can do logging dot basic config camel case there level equals logging dot and we'll say info for now So any log information that we say is debug level under this configuration won't actually log I mean the codes there but it's not gonna log because we're setting the logging level to info So everything info and above will log But the debug lay level stuff just isn't gonna log either It's not going to come to our console and or go to our file so But we can change that So like let's say for some reason something went really awry and we don't know what's up and maybe we even had it set toe just warning and then we checked a log file and sure enough Oh we've got a log of warnings in here but we're not seeing exactly what's wrong We might later decide ok let's set it to debug Run everything again and see what's happening and so It's just a really quick way to change one thing and get a whole lot more information from your program as apposed to the typical you know
import logging
'''
DEBUG Detailed information, typically of interest only when diagnosing problems.
INFO Confirmation that things are working as expected.
WARNING An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected.
ERROR Due to a more serious problem, the software has not been able to perform some function.
CRITICAL A serious error, indicating that the program itself may be unable to continue running.
'''
logging.basicConfig(filename='logfile.log',level=logging.INFO)
STARTING_BLUE_BLOBS = 15
STARTING_RED_BLOBS = 7
STARTING_GREEN_BLOBS = 10
class BlueBlob(Blob):
def __init__(self, x_boundary, y_boundary):
Blob.__init__(self, (0, 0, 255), x_boundary, y_boundary)
def __add__(self, other_blob):
logging.info('Blob add op {} + {}'.format(str(self.color), str(other_blob.color)))
if other_blob.color == (255, 0, 0):
self.size -= other_blob.size
other_blob.size -= self.size
elif other_blob.color == (0, 255, 0):
self.size += other_blob.size
other_blob.size = 0
elif other_blob.color == (0, 0, 255):
pass
else:
raise Exception('Tried to combine one or multiple blobs of unsupported colors.')
def handle_collisions(blob_list):
blues, reds, greens = blob_list #seperate each blob's dictionary
for blue_id, blue_blob, in blues.copy().items(): #copy the item except modifing purpose only
for other_blobs in blues, reds, greens:
for other_blob_id, other_blob in other_blobs.copy().items():
logging.debug('Checking if blobs are touching {} + {}'.format(str(blue_blob.color), str(other_blob.color)))
if blue_blob == other_blob:
pass
else:
if is_touching(blue_blob, other_blob):
blue_blob + other_blob #calls + dunder add function
if other_blob.size 꺽쇠= 0:
del other_blobs[other_blob_id]
if blue_blob.size 꺽쇠= 0:
del blues[blue_id]
return blues, reds, greens #what specifically is returned?
def main():
while True:
try:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
except Exception as e:
logging.critical(str(e))
pygame.quit()
quit()
break
blue_blobs,red_blobs,green_blobs = draw_environment([blue_blobs,red_blobs,green_blobs])
clock.tick(60)
if __name__ == '__main__':
main()
원본 비디오: https://www.youtube.com/watch?v=-RcDmGNSuvU&list=PLQVvvaa0QuDfju7ADVp5W1GF9jVhjbX-_&index=23&t=2s