import numpy as npimport pandas as pdimport matplotlib.pyplot as pltimport time#---#import torchimport transformersimport tarfile
/home/cgb3/anaconda3/envs/hf/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
from .autonotebook import tqdm as notebook_tqdm
3. with
A. 기본사용
- 아래와 같이 with를 사용한다. 여기에서 ???? 자리에 올 수 있는 오브젝트는 __enter__ 와 __exit__을 포함하는 어떠한 오브젝트이다. (그리고 이러한 오브젝트를 “컨텍스트 매니저”라고 부른다)
with ????: 블라블라~ 야디야디~
# 예제1 – 기본예제
- context manager 를 찍어내는 Dummy 클래스
class Dummy: def__enter__(self):print("enter")def__exit__(self,*args): # *args는 에러처리 관련된 __exit__의 입력변수들 print("exit")
d = Dummy()d.__enter__()print("context")d.__exit__()
enter
context
exit
d = Dummy()with d:print("context")
enter
context
exit
-with 뒤에 올 수 있는 오브젝트는 __enter__ 와 __exit__ 을 포함해야한다.
lst = [1,2,3]with lst:pass
TypeError: 'list' object does not support the context manager protocol
with33:pass
TypeError: 'int' object does not support the context manager protocol
Some weights of DistilBertForSequenceClassification were not initialized from the model checkpoint at distilbert/distilbert-base-uncased and are newly initialized: ['classifier.bias', 'classifier.weight', 'pre_classifier.bias', 'pre_classifier.weight']
You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.
- 따라서 __enter__ 가 self를 리턴하는 경우에는 with 샌드위치생성기(빵="허니오트") as 샌드위치 이 부분을 해석할때 “샌드위치생성기(빵="허니오트") 코드의 실행결과 만들어지는 오브젝트를 샌드위치로 저장”한다고 해석해도 무리가 없다.
#
# 예제6
-example.txt 를 만들고 “asdf” 라는 글자를 넣는 파이썬코드
withopen("example.txt","w") asfile: # 대충해석: open("example.txt","w") 을 실행하여 나오는 오브젝트를 file로 받음 # 더 엄밀한 해석: # open("example.txt","w") 을 실행하여 나오는 오브젝트를 ???라고 하자. # 그런데 ??? 에는 `__enter__`가 있을텐데, 그 `__enter__`를 실행하여 나오는 오브젝트를 # file로 받음.file.write("asdf")