Skip to content

Commit 9d25e7d

Browse files
committed
first
0 parents  commit 9d25e7d

File tree

8 files changed

+238
-0
lines changed

8 files changed

+238
-0
lines changed

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Python-generated files
2+
__pycache__/
3+
*.py[oc]
4+
build/
5+
dist/
6+
wheels/
7+
*.egg-info
8+
9+
# Virtual environments
10+
.venv

.python-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3.12

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# 프로젝트 001 - 데이터 내보내기 시스템
2+
3+
## 프로젝트 개요
4+
이 프로젝트는 다양한 형식으로 데이터를 내보내는 시스템을 구현한 Python 프로젝트입니다. 세 가지 다른 접근 방식을 통해 동일한 기능을 구현하는 방법을 보여줍니다.
5+
6+
## 파일 구조
7+
8+
### 1. main001.py - 기본적인 조건문 방식
9+
- **기능**: if-elif-else 문을 사용한 기본적인 데이터 내보내기
10+
- **특징**:
11+
- 각 형식별로 개별 함수 정의 (export_pdf, export_csv, export_excel, export_json)
12+
- export_data 함수에서 조건문으로 형식 선택
13+
- 간단하고 직관적인 구조
14+
15+
### 2. main002.py - 딕셔너리 기반 방식
16+
- **기능**: 딕셔너리를 사용한 함수 매핑 방식
17+
- **특징**:
18+
- export_functions 딕셔너리에 형식별 함수 저장
19+
- get() 메서드를 사용한 안전한 함수 호출
20+
- 조건문보다 깔끔한 코드 구조
21+
22+
### 3. main003.py - 데코레이터 기반 레지스트리 방식
23+
- **기능**: 데코레이터를 사용한 동적 함수 등록
24+
- **특징**:
25+
- @register_exporter 데코레이터로 함수 자동 등록
26+
- 확장성이 뛰어난 구조
27+
- 새로운 내보내기 형식 추가가 용이
28+
29+
### 4. registry.py - 범용 레지스트리 시스템
30+
- **기능**: 그룹과 이름을 가진 명령어 레지스트리
31+
- **특징**:
32+
- @register_command 데코레이터 사용
33+
- 그룹별 명령어 관리 가능
34+
- 범용적인 레지스트리 패턴 구현
35+
36+
### 5. pyproject.toml - 프로젝트 설정
37+
- **Python 버전**: >=3.12
38+
- **프로젝트명**: 001
39+
- **버전**: 0.1.0
40+
41+
## 공통 기능
42+
모든 main 파일들은 다음 형식으로 데이터를 내보낼 수 있습니다:
43+
- PDF
44+
- CSV
45+
- Excel
46+
- JSON
47+
48+
## 실행 방법
49+
```bash
50+
python main001.py # 기본 방식
51+
python main002.py # 딕셔너리 방식
52+
python main003.py # 데코레이터 방식
53+
```
54+
55+
## 설계 패턴
56+
이 프로젝트는 다음과 같은 설계 패턴을 보여줍니다:
57+
1. **Strategy Pattern** - 다양한 내보내기 전략
58+
2. **Registry Pattern** - 함수 등록 및 관리
59+
3. **Decorator Pattern** - 함수 등록을 위한 데코레이터 사용
60+
61+
## 타입 힌트
62+
모든 파일에서 Python의 타입 힌트를 적극 활용하여 코드의 가독성과 안정성을 높였습니다.

main001.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import Any
2+
3+
type Data = dict[str, Any]
4+
5+
def export_pdf(data: Data) -> None:
6+
print(f"Exporting data to PDF: {data}")
7+
8+
def export_csv(data: Data) -> None:
9+
print(f"Exporting data to CSV: {data}")
10+
11+
def export_excel(data: Data) -> None:
12+
print(f"Exporting data to Excel: {data}")
13+
14+
def export_json(data: Data) -> None:
15+
print(f"Exporting data to JSON: {data}")
16+
17+
18+
19+
20+
def export_data(data: Data, format: str) -> None:
21+
if format == "pdf":
22+
export_pdf(data)
23+
elif format == "csv":
24+
export_csv(data)
25+
elif format == "excel":
26+
export_excel(data)
27+
elif format == "json":
28+
export_json(data)
29+
else:
30+
raise ValueError(f"Invalid format: {format}")
31+
32+
def main() -> None:
33+
sample_data: Data = {"name": "John", "age": 30}
34+
35+
export_data(sample_data, "pdf")
36+
export_data(sample_data, "csv")
37+
export_data(sample_data, "excel")
38+
export_data(sample_data, "json")
39+
40+
41+
if __name__ == "__main__":
42+
main()

main002.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from typing import Any, Callable
2+
3+
type Data = dict[str, Any]
4+
type ExportFn = Callable[[Data], None]
5+
6+
7+
def export_pdf(data: Data) -> None:
8+
print(f"Exporting data to PDF: {data}")
9+
10+
def export_csv(data: Data) -> None:
11+
print(f"Exporting data to CSV: {data}")
12+
13+
def export_excel(data: Data) -> None:
14+
print(f"Exporting data to Excel: {data}")
15+
16+
def export_json(data: Data) -> None:
17+
print(f"Exporting data to JSON: {data}")
18+
19+
20+
export_functions: dict[str, ExportFn] = {
21+
"pdf": export_pdf,
22+
"csv": export_csv,
23+
"excel": export_excel,
24+
"json": export_json,
25+
}
26+
27+
28+
def export_data(data: Data, format: str) -> None:
29+
export = export_functions.get(format)
30+
if export is None:
31+
raise ValueError(f"Invalid format: {format}")
32+
export(data)
33+
34+
def main() -> None:
35+
sample_data: Data = {"name": "John", "age": 30}
36+
37+
export_data(sample_data, "pdf")
38+
export_data(sample_data, "csv")
39+
export_data(sample_data, "excel")
40+
export_data(sample_data, "json")
41+
42+
43+
if __name__ == "__main__":
44+
main()

main003.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from typing import Any, Callable
2+
from functools import wraps
3+
4+
type Data = dict[str, Any]
5+
type ExportFn = Callable[[Data], None]
6+
7+
export_functions: dict[str, ExportFn] = {}
8+
9+
def register_exporter(format: str) -> Callable[[ExportFn], ExportFn]:
10+
def decorator(fn: ExportFn) -> ExportFn:
11+
@wraps(fn)
12+
def wrapper(data: Data) -> None:
13+
return fn(data)
14+
15+
export_functions[format] = wrapper
16+
return wrapper
17+
return decorator
18+
19+
20+
@register_exporter("pdf")
21+
def export_pdf(data: Data) -> None:
22+
print(f"Exporting data to PDF: {data}")
23+
24+
@register_exporter("csv")
25+
def export_csv(data: Data) -> None:
26+
print(f"Exporting data to CSV: {data}")
27+
28+
@register_exporter("excel")
29+
def export_excel(data: Data) -> None:
30+
print(f"Exporting data to Excel: {data}")
31+
32+
@register_exporter("json")
33+
def export_json(data: Data) -> None:
34+
print(f"Exporting data to JSON: {data}")
35+
36+
37+
38+
def export_data(data: Data, format: str) -> None:
39+
export = export_functions.get(format)
40+
if export is None:
41+
raise ValueError(f"Invalid format: {format}")
42+
export(data)
43+
44+
def main() -> None:
45+
sample_data: Data = {"name": "John", "age": 30}
46+
47+
export_data(sample_data, "pdf")
48+
export_data(sample_data, "csv")
49+
export_data(sample_data, "excel")
50+
export_data(sample_data, "json")
51+
52+
53+
if __name__ == "__main__":
54+
main()

pyproject.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[project]
2+
name = "001"
3+
version = "0.1.0"
4+
description = "Add your description here"
5+
readme = "README.md"
6+
requires-python = ">=3.12"
7+
dependencies = []

registry.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import Any, Callable
2+
from functools import wraps
3+
4+
_registry: list[tuple[str, str, Callable[..., None]]] = []
5+
6+
def register_command(group: str, name: str):
7+
def decorator(func: Callable[..., None]):
8+
@wraps(func)
9+
def wrapper(*args: Any, **kwargs: Any):
10+
return func(*args, **kwargs)
11+
12+
_registry.append((group, name, wrapper))
13+
return wrapper
14+
15+
return decorator
16+
17+
def get_registry() -> list[tuple[str, str, Callable[..., None]]]:
18+
return _registry.copy()

0 commit comments

Comments
 (0)