본문 바로가기

Python tips10

datetime UTC +9 한국 시간 from datetime import datetime, timedelta, timezoneutc_now = datetime.now(timezone.utc)kst_now = utc_now.astimezone(tz=timezone(timedelta(hours=9)))ret = kst_now.strftime("%Y%m%d%H%M%S") + "something" [참고] [python] datetime 여러 활용법 알아보기 ( datetime, timezone, truncate)- 목차 들어가며.이번 글에서는 Python 의 Datetime 모듈의 여러가지 사용 사례들을 작성해보려고 합니다.Python 개발을 하다보면 Datetime 모듈을 활용해야하는 경우가 많습니다.저의 경우에는 매번 "Tiwestlife0.. 2024. 6. 19.
임시 비밀번호, 랜덤 문자열 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.
map(), filter(), pipe()를 활용하여 데이터 다루기 map(), filter() 함수는 리스트 안의 데이터를 내가 원하는 대로 추출하거나 변형시킬 수 있습니다.def square(num): return num**2number_list = [1, 2, 3, 4, 5]result = map(square, number_list)print(result) # # for loop -> [1, 4, 9, 16, 25]print(list(result)) # [1, 4, 9, 16, 25]# 람다result = list(map(lambda x: x**2, number_list))print(result) # [1, 4, 9, 16, 25]map()의 리턴 데이터 타입은 map objectfilter() 또한 filter object 리스트 to 리스트를 통해 DB에.. 2024. 4. 8.