본문 바로가기

Python tips12

서버 로컬 지역에 상관없이 한국 시간 기준 datetime from datetime import datetimefrom dateutil import tzlocal_now = datetime.now(tz.tzlocal())kor = tz.gettz("Asia/Seoul")time_zone_now = local_now.astimezone(kor)print(time_zone_now) # 2024-06-19 11:50:35.899414+09:00# datetime 연산을 위해서는 변환 필요kor_now = datetime.strptime(str(time_zone_now)[:19], "%Y-%m-%d %H:%M:%S")print(kor_now) # 2024-06-19T11:50:35 2024. 6. 19.
urllib3 request 사용해서 requests.exceptions.SSLError 해결 requests 라이브러리를 통해 외부 API 개발을 하는 도중,로컬 및 개발 환경에서는 큰 문제가 없었지만 운영 환경에서 SSLError가 발생했다.verify=False 인자를 추가하거나 파이썬 3.9.x 버전을 사용하는 것은 좋은 방법이 아닌 것 같아 다른 해결 방법을 찾던 중 좋은 자료를 찾았다. (하단 참고) urllib3가 requests보다 더 low level의 코드라고 한다. 자세한 건 chatgpt에게 물어보는 것도 좋다.예제를 통해 두 라이브러리를 비교하면서 알아보자. [GET] requestimport hashlibimport requestsparams ={ "key_a": "val_a", "key_b": "val_b", "key_c": "val_c",}sig = ".. 2024. 5. 15.
임시 비밀번호, 랜덤 문자열 generator import randomdef temporary_password_generator() -> str: """임시 비밀번호 발급 함수 참고사항: 소문자 & 대문자 & 숫자 & 특수문자(!@#$%&*?) 중 임의의 8자리 Returns: temp password """ lower_case = 'abcdefghijklmnopqrstuvwxyz' upper_case = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' number = '0123456789' symbols = '!@#$%&*?' USE_FOR = lower_case + upper_case + number + symbols length_for_pass = 8 temp_password.. 2024. 5. 8.
@property와 __call__ 비교 class Calculate: def __init__(self): self.result = 3 @property def one_plus_two(self) -> int: """ 일급 객체와는 다르다. 일급 객체는 변수 할당, 함수 인자 전달, 함수의 리턴 값이 될 수 있는 특징이 있기 때문이다. """ return self.resultif __name__ == "__main__": cal = Calculate() print(cal) # print(type(cal)) # # print(cal()) # 'Calculate' object is not callable 예외 print(c.. 2024. 5. 8.