Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 하이라이트
- CSS
- pycharm
- 의사 클래스
- 퀀트
- idxmax
- 코드블럭
- line number
- 금융결제원
- Tistory
- OpenAPI+
- multiprocessing
- highlight.js
- Windows
- 파이썬
- 필기
- freeze_support
- HTML
- 웹크롤링
- QueueHandler
- 티스토리
- Python
- 진행 상황
- idxmin
- 금결원
- 우리FIS
- DataFrame
- pywinauto
- 구현
- 멀티 로그인
Archives
- Today
- Total
맨땅에 헤딩하는 사람
[Python] tqdm을 사용하여 for 반복, 함수 진행률 출력하기 본문
실행 시간이 긴 프로그램의 경우 간혹 어느 정도 진행되었는지 확인할 필요가 있다. 물론 print()
와 carriage return \r
을 사용해서 직접 구현할 수 있지만 계속 출력되는 line이 지저분해 보이기도 하고 매번 직접 구현한 것을 옮겨 사용하기도 애매하다. python에서는 tqdm
이라는 모듈을 사용하여 프로그램의 진행 상황을 깔끔하게 출력할 수 있다. ProgressBar
라는 또 다른 모듈이 있긴 하지만 개발이 몇 년 전부터 이루어지지 않고 있으며 속도는 800ns/iter로 tqdm
의 60ns/iter보다 느린 속도를 가진다.
tqdm
모듈에서 tqdm
클래스를 사용하며 parameter는 다음과 같다.
class tqdm():
""" Decorate an iterable object, returning an iterator which acts exactly like the original iterable, but prints a dynamically updating progressbar every time a value is requested. """
def __init__(self, iterable=None, desc=None, total=None, leave=True, file=None, ncols=None, mininterval=0.1, maxinterval=10.0, miniters=None, ascii=None, disable=False, unit='it', unit_scale=False, dynamic_ncols=False, smoothing=0.3, bar_format=None, initial=0, position=None, postfix=None, unit_divisor=1000):
iterable
: 반복가능한 객체. range, list, tuple, dict 등을 넣을 수 있음.desc
: 진행바에 출력할 문구.total
: 진행바의 최대 수치. 값이 없으면 iterable의 length가 된다.file
: 어디에 출력할 지를 나타내는 값. 기본값은 std.err 이다.unit
: 반복의 단위를 나타내는 값.unit_scale
: 큰 단위에 대해 자동적으로 scale 적용할지 여부(Kilo, Mega, Giga 등)
주로 사용하는 것은 이 정도인 듯 하고 나머지는 tqdm document에서 확인하길 바란다. 사용 예제는 다음과 같다.
사용 예
from tqdm import tqdm
, import time
은 생략한다.
1
2
|
for _ in tqdm(range(10), desc='tqdm test'):
time.sleep(0.1)
|
cs |
출력 :
tqdm test: 100%|██████████| 10/10 [00:01<00:00, 9.53it/s]
|
cs |
Manual로 사용
1
2
3
4
|
with tqdm(total=100, desc='tqdm manual', unit='unit', ascii=True) as pbar:
for _ in range(10):
time.sleep(0.1)
pbar.update(10)
|
cs |
출력 :
tqdm manual: 100%|##########| 100/100 [00:01<00:00, 95.65unit/s]
|
cs |
Manual은 선언해서 사용하는 방법도 있다.
1
2
3
4
5
|
pbar = tqdm(total=1000**3, unit='unit', unit_scale=True)
for _ in range(20):
time.sleep(0.1)
pbar.update(1000**3 / 20)
pbar.close()
|
cs |
출력 :
100%|██████████| 1.00G/1.00G [00:02<00:00, 478Munit/s]
|
cs |
참고
tqdm gitgub
'파이썬 > 이론' 카테고리의 다른 글
Python multiprocessing에서 logging 사용하기 (QueueHandler) (0) | 2020.08.30 |
---|---|
[Python] pandas DataFrame 최적화 (삽입, 생성, 반복, 문자열) (0) | 2020.08.25 |
[Python] Windows GUI 자동화 pywinauto 사용법 (4) | 2020.08.22 |
Windows 환경 python multiprocessing 시 freeze_support() (0) | 2020.08.09 |
Windows 환경 ipython console에서 multiprocessing이 안되는 이유 (1) | 2020.08.09 |
Comments