티스토리 뷰
https://stackoverflow.com/a/45669280
How to block calls to print?
Is there a way to stop a function from calling print? I am using the pygame.joystick module for a game I am working on. I created a pygame.joystick.Joystick object and in the actual loop of the game
stackoverflow.com
import os, sys
class HiddenPrints:
def __enter__(self):
self._original_stdout = sys.stdout
sys.stdout = open(os.devnull, 'w')
def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._original_stdout
with HiddenPrints():
print("This will not be printed")
print("This will be printed as before")
print가 호출되지 않았으면 하는 함수, 클래스 생성자를 호출할때
with HiddenPrints(): 로 감싸주면 된다.
왜 이게 필요했는가
COCO 데이터셋의 어노테이션 포맷을 따르는 데이터를 로드할 일이 있었다. 이를 위해 pycocotools 라이브러리를 사용했다.
https://github.com/cocodataset/cocoapi
GitHub - cocodataset/cocoapi: COCO API - Dataset @ http://cocodataset.org/
COCO API - Dataset @ http://cocodataset.org/ . Contribute to cocodataset/cocoapi development by creating an account on GitHub.
github.com
그런데 코드를 분석해보면 데이터를 파싱하는 과정에 요란하게 출력되는 메시지들이 몇 존재한다.
https://github.com/cocodataset/cocoapi/blob/master/PythonAPI/pycocotools/coco.py#L86
GitHub - cocodataset/cocoapi: COCO API - Dataset @ http://cocodataset.org/
COCO API - Dataset @ http://cocodataset.org/ . Contribute to cocodataset/cocoapi development by creating an account on GitHub.
github.com
읽어야할 데이터가 COCO 포맷으로 어노테이션이 되어 있었는데 이게 하나의 json 파일로 되어있던게 아니라 수천개의 json 파일로 구성돼있었다. 그렇다보니 COCO 클래스의 생성자가 json 파일개수만큼 호출되어서 로그가 너무 지저분해지는 문제가 있었다. 이를 해결하다보니 해당 방법을 사용하게 됐다.
'Python' 카테고리의 다른 글
| linux pycocotools 설치시 ERROR: Failed building wheel for pycocotools 해결 (0) | 2022.11.17 |
|---|---|
| Anaconda 지원되는 python 버전 확인 명령어 (0) | 2022.11.08 |
| [Python] Numpy Batched IoU 코드 (0) | 2022.01.19 |
| PyPi 배포 스크립트 (0) | 2022.01.09 |
| [Python] 파일 수정 시간 받아오기 (0) | 2022.01.07 |
- Total
- Today
- Yesterday
- 백준 11053
- 자료구조
- 위상 정렬 알고리즘
- MOT
- PyCharm
- 문제집
- 조합
- 가장 긴 증가하는 부분 수열
- ㅂ
- 단축키
- 이분탐색
- 인공지능을 위한 선형대수
- 순열
- 백준 1766
- LCA
- FairMOT
- 백트래킹
- cosine
- 파이참
- C++ Deploy
- Lowest Common Ancestor
- 백준 11437
- 백준
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
