본문 바로가기
반응형

Python111

[Python] exception 메시지 자세히 출력하기 (Traceback) try except 에서 Traceback log 찍기 바로 예제 보시죠 try: print('오류 전') 0/0 print('오류 후') except Exception as ex: print('except 로 들어옴') print(ex) 실행 결과는 오류 전 except 로 들어옴 division by zero 이렇게 나옵니다. print(str(ex)) 도 마찬가지죠. ex 에서 뭐를 뽑을 수 있을까요? 음.. 뭐 없네요 ㅎㅎ logging 이나 개발자에게 알람 메시지를 보내고 싶은데, 딸랑 'division by zero' 이라고 보내면.. 뭐 어디서 오류난건지 찾는게 더 어렵겠죠. 이럴때 traceback 을 쓰면 자세한 오류 내용을 문자열로 받아올 수 있습니다. 수정된 코드 import trac.. 2022. 10. 25.
[Python] host(ip,url) port 로 연결 가능한지 확인 port 로 Ping 날려보기 Python Socket 을 이용한 방법 hello-bryan.service.com:8080 으로 통신해야할 일이 있을 때, 우선 접속이 가능한지 체크해보는 방법입니다. 아래와 같은 메소드를 정의해 두고, result = is_connectable('hello-bryan.service.com', '8080') # result = True or False def is_connectable(ip, port): import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.settimeout(1) try: s.connect((ip, int(port))) s.shutdown(socket.SHUT_RDWR) return True except: retu.. 2022. 9. 28.
[Python] 현재 날짜 포멧 출력하기 from datetime import datetime format_data = "%Y-%m-%d %H:%M:%S.%f" now_str = datetime.now().strftime(format_data) print(now_str) # 2022-08-22 19:11:38.565332 포멧 종류는 아래와 같습니다. format code meaning example %a Abbreviated weekday name Sun, Mon %A Full weekday name Sunday, Monday %w Weekday as decimal number 0…6 %d Day of the month as a zero-padded decimal 01, 02 %-d day of the month as decimal numbe.. 2022. 8. 22.
[Python] 실행시간 체크하기. decorator 사용하기 기본적으로 파이썬에서 time 을 이용해서 실행시간 체크하는 방법입니다. import time # 작업 전 시간 start = time.time() print('work something') time.sleep(1) # 작업 후 시간 end = time.time() # 시간 계산 print(f'time = {(end-start)}s') # time = 1.005115270614624s 시간의 소수점이 너무 길다면 출력 format 을 변경합니다. print(f'time = {(end-start):.3f}s') # time = 1.005s 함수 실행 시간 체크 특정 함수가 있을 때, 그 함수의 실행 시간을 체크하는 방법입니다. start = time.time() some_function() end = ti.. 2022. 8. 13.
[Python] PIL.Image 를 byte array 로 변환 import io img = Image.open('file_path', mode='r') buffer = io.BytesIO() img.save(buffer, format='PNG') buffer.seek(0) 2022. 6. 20.
[Python] tensor 를 이미지로 저장하기 방법 1. torchvision.utils 사용 from torchvision.utils import save_image save_image(tensor_data, 'image_path/image_name.png') 방법 2. PIL 이미지로 변경 후 저장 transforms 를 이용하여 Image 로 변경 import torchvision.transforms as T from PIL import Image transform = T.ToPILImage() img = transform(tensor_data) img.save("image_path.png") 방법 3. tensor -> numpy -> Image import numpy as np from PIL import Image np_arr = np.a.. 2022. 6. 20.
TypeError: Descriptors cannot not be created directly (protoc >= 3.19.0) 해결하기 TypeError: Descriptors cannot not be created directly. If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0. in Python 이러한 오류는 보통 Tensorflow, Tensorboard, Tritonclient 등등의 버전과 protobuf 의 버전이 맞지 않아서 발생합니다. 위에 저 빨간글씨대로 해결을 할 수도 있지만, 1번은 protobuf 의 버전을 낮추는 방법이고, 2번은 딱봐도 안좋은 방법입니다 ㅋ 버전을 낮추기 보다는 Tensorflow 등의 버전을 올리는게 나을거 같은데요. 부득이하게.. 2022. 6. 20.
[PyQT5] UI Designer QLabelEdit - password PyQT5 UI Designer QLabelEdit Password 실행 PasswordEchoOnEdit 입력할 때는 보임. focus out 되면 password 처럼 보임. clearButtonEnabled 2022. 3. 25.
[PyQT5] UI Designer 에서 QLabel 색상 변경 PyQT5 UI Designer QLabel Color color: rgb(85, 170, 127); 2022. 3. 24.
[PyQT5] UI Designer 에서 Grid Layout 배치 해보기 PyQT5 Designer Grid Layout 이전에 하던 Window 에 Tab Widget 안에 Grid Layout 을 추가합니다. 이전 포스트 참고 [PyQT5] UI Designer 에서 리사이즈 시 같이 확장하기 [PyQT5] UI Designer 에서 Tab Widget 생성 하기 Tab Widget 의 Tab 중에 Grid Layout 을 추가해줍니다. 그리고 사이즈를 같이 늘어나게 하기 위해서 배치를 설정해 줍니다. Grid Layout 의 Parent 인 Tab 에서 우클릭을 합니다~ Grid Layout 에 Widget 추가하기 Label 을 추가 두번째 Label 추가 이렇게 Grid 처럼 추가가 됩니다. 이번에는 세번째 Label 을 아래에 추가해 봅니다. 이렇게 추가되었습니다... 2022. 3. 24.
728x90
반응형