코드 리팩토링

This commit is contained in:
icurfer 2024-10-03 08:32:03 +09:00
parent 2b0d3ebb6c
commit 4d1fd7a5be
8 changed files with 40 additions and 12 deletions

4
main.py Normal file
View File

@ -0,0 +1,4 @@
import package as pkg
url = pkg.get_url.fetch_data_from_mariadb()

30
package/GetConfig.py Normal file
View File

@ -0,0 +1,30 @@
import os
from dotenv import load_dotenv
load_dotenv(r'./.env.dev')
class GetConfig:
def __init__(self):
self.host = os.getenv('DB_HOST')
self.user = os.getenv('DB_USER')
self.password = os.getenv('DB_PASSWORD')
self.database = os.getenv('DB_NAME')
def show_config(self):
for key, value in self.__dict__.items():
print(f"{key.upper()}: {value}")
if __name__ == "__main__":
# 결과 확인
config = GetConfig()
config.show_config()
# 오랜만에 보다보니 헷갈려서 참고용으로 작성
# class GetConfig:
# def __init__(self, name=None):
# self.name = name if name is not None else "default_name"
# self.host = os.getenv('DB_HOST')
# self.user = os.getenv('DB_USER')
# self.password = os.getenv('DB_PASSWORD')
# self.database = os.getenv('DB_NAME')

1
package/__init__.py Normal file
View File

@ -0,0 +1 @@
from . import get_url

View File

@ -1,8 +1,8 @@
import os
from dotenv import load_dotenv
import markdown
import translate_article as ta
import open_ai as oa
import package.translate_article as ta
import ../openai/open_ai as oa
# .env 파일에서 API 키 로드
load_dotenv(r'./.env.dev')

View File

@ -1,14 +1,7 @@
import mysql.connector
from dotenv import load_dotenv
import os
# .env.demo 파일 로드
load_dotenv(r'./.env.dev')
# 환경 변수 가져오기
host = os.getenv('DB_HOST')
user = os.getenv('DB_USER')
password = os.getenv('DB_PASSWORD')
database = os.getenv('DB_NAME')
# MariaDB에 연결하는 함수
def fetch_data_from_mariadb():
try:

View File

@ -1,7 +1,7 @@
import os
from openai import OpenAI
from dotenv import load_dotenv
import translate_article as ta
import package.translate_article as ta
# .env 파일에서 API 키 로드
load_dotenv()

View File

@ -1,6 +1,6 @@
import requests
from bs4 import BeautifulSoup
import get_url
import db_ctr.get_url as get_url
url = get_url.fetch_data_from_mariadb()['url']