국어교사의 파이썬 일기

중학교 국어 교사의 파이썬 일기: 날짜 다루기- strftime의 활용과 HTTP 요청

국밥먹여주는남자NobackT 2024. 1. 14. 21:53
오늘의 학습 주제: strftime을 통한 자동 날짜 처리 및 HTTP 요청

오늘의 파이썬 여정에서는 strftime 함수의 다양한 사용 방법을 탐구하고, HTTP 요청을 다뤄봤습니다. 이를 통해 데이터 포맷팅과 웹 통신의 기초를 다질 수 있었죠.

strftime 사용법:

%a에서 %V까지 다양한 지시자를 사용하여 날짜와 시간을 다양한 형태로 표현할 수 있습니다.
예를 들어, %Y%m%d는 '20240110'과 같이 연, 월, 일을 연결하여 표시합니다.

HTTP 요청의 기초:

requests 모듈을 사용하여 HTTP 요청을 보내는 기본적인 방법을 배웠습니다.
get, post, put, delete 등 다양한 메소드를 통해 웹 서버와 데이터를 주고받을 수 있습니다.

오늘의 코드: 날짜 자동 채우기와 HTTP 요청

import requests
from datetime import datetime

## 오늘의 날짜를 자동으로 채우는 예제
import requests
from datetime import datetime

pixela_endpoint = "https://pixe.la/v1/users"
today = datetime.now()

user_params = {
    "token": TOKEN,
    "username": USERNAME,
    "agreeTermsOfService": "yes",
    "notMinor": "yes"
}

# response = requests.post(url=pixela_endpoint, json=user_params)

# 그래프 등록
graph_endpoint = f"{pixela_endpoint}/{USERNAME}/graphs"

graph_config = {
    "id": GRAPH_ID,
    "name": "달리기 기록🏃", #emoji 는 cntr + i
    "unit": "Km",
    "type": "float",
    "color": "momiji"
}

# 헤더를 사용한 인증 방법
headers = {
    "X-USER-TOKEN": TOKEN
}

# response = requests.post(url=graph_endpoint, json=graph_config, headers=headers)
# print(response.text)

# 내용 입력
graph_insert_point = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}"

insert_data = {
    "date": today.strftime("%Y%m%d"), #오늘 배운  strftime
    "quantity": input("How many kilometers did you run today? "), #input 은 문자열로 받아야 함. 숫자는 안됨.
}

response = requests.post(url=graph_insert_point, json=insert_data, headers=headers)

# # 내용 수정
# edit_day = "yyyyMMdd"
# graph_edit_point = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}/{edit_day}"

# update_data = {
#     "quantity": "6"
# }

# response = requests.put(url=graph_edit_point, json=update_data, headers=headers)

# # 내용 삭제
# delete_day = "yyyyMMdd"
# graph_delete_point = f"{pixela_endpoint}/{USERNAME}/graphs/{GRAPH_ID}/{delete_day}"
# response = requests.delete(url=graph_delete_point, headers=headers)

개인적 성찰:

파이썬을 배우면서 제가 가진 언어에 대한 이해가 새로운 형태의 '언어', 즉 프로그래밍 언어에 어떻게 적용될 수 있는지를 탐구하게 되었습니다. 문법과 표현 방식의 차이는 있지만, 결국 모든 언어는 의사소통의 도구라는 공통점을 갖고 있죠.

오늘 배운 strftime의 사용법과 HTTP 요청의 기본은 앞으로의 프로그래밍 여정에 큰 도움이 될 것입니다. 다음 시간에는 strftime을 활용한 보다 복잡한 날짜 처리 방법을 탐구해보려 합니다. 여러분의 경험도 공유해주세요!