hossam 0.4.5__py3-none-any.whl → 0.4.6__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
hossam/data_loader.py DELETED
@@ -1,203 +0,0 @@
1
- # -*- coding: utf-8 -*-
2
- # -------------------------------------------------------------
3
-
4
- import requests
5
- import json
6
- from os.path import join, exists
7
- from io import BytesIO
8
- from pandas import DataFrame, read_csv, read_excel
9
- from typing import Optional, Tuple, Any
10
- from .hs_util import pretty_table
11
-
12
- BASE_URL = "https://data.hossam.kr"
13
-
14
- # -------------------------------------------------------------
15
- def __get_df(path: str, index_col=None) -> DataFrame:
16
- p = path.rfind(".")
17
- exec = path[p+1:].lower()
18
-
19
- if exec == 'xlsx':
20
- # If path is a remote URL, fetch the file once and reuse the bytes
21
- if path.lower().startswith(('http://', 'https://')):
22
- path = path.replace("\\", "/")
23
- with requests.Session() as session:
24
- r = session.get(path)
25
-
26
- if r.status_code != 200:
27
- raise Exception(f"HTTP {r.status_code} Error - {r.reason} > {path}")
28
-
29
- data_bytes = r.content
30
-
31
- # Use separate BytesIO objects for each read to avoid pointer/stream issues
32
- df = read_excel(BytesIO(data_bytes), index_col=index_col)
33
-
34
- try:
35
- info = read_excel(BytesIO(data_bytes), sheet_name='metadata', index_col=0)
36
- #print("\033[94m[metadata]\033[0m")
37
- print()
38
- pretty_table(info)
39
- print()
40
- except Exception:
41
- #print(f"\033[91m[!] Cannot read metadata\033[0m")
42
- pass
43
- else:
44
- df = read_excel(path, index_col=index_col)
45
-
46
- try:
47
- info = read_excel(path, sheet_name='metadata', index_col=0)
48
- #print("\033[94m[metadata]\033[0m")
49
- print()
50
- pretty_table(info)
51
- print()
52
- except:
53
- #print(f"\033[91m[!] Cannot read metadata\033[0m")
54
- pass
55
- else:
56
- df = read_csv(path, index_col=index_col)
57
-
58
- return df
59
-
60
- # -------------------------------------------------------------
61
- def __get_data_url(key: str, local: str | None = None) -> Tuple[str, Any, Any]:
62
- global BASE_URL
63
-
64
- path = None
65
-
66
- if not local:
67
- data_path = join(BASE_URL, "metadata.json").replace("\\", "/")
68
-
69
- with requests.Session() as session:
70
- r = session.get(data_path)
71
-
72
- if r.status_code != 200:
73
- raise Exception("[%d Error] %s" % (r.status_code, r.reason))
74
-
75
- my_dict = r.json()
76
- info = my_dict.get(key.lower())
77
-
78
- if not info:
79
- raise FileNotFoundError("%s는 존재하지 않는 데이터에 대한 요청입니다." % key)
80
-
81
- path = join(BASE_URL, info['url'])
82
- else:
83
- data_path = join(local, "metadata.json")
84
-
85
- if not exists(data_path):
86
- raise FileNotFoundError("존재하지 않는 데이터에 대한 요청입니다.")
87
-
88
- with open(data_path, "r", encoding="utf-8") as f:
89
- my_dict = json.loads(f.read())
90
-
91
- info = my_dict.get(key.lower())
92
- path = join(local, info['url'])
93
-
94
- return path, info.get('desc'), info.get('index')
95
-
96
- # -------------------------------------------------------------
97
- def load_info(search: str | None = None, local: str | None = None) -> DataFrame:
98
- """메타데이터에서 사용 가능한 데이터셋 정보를 로드한다.
99
-
100
- Args:
101
- search (str, optional): 이름 필터 문자열. 포함하는 항목만 반환.
102
- local (str, optional): 로컬 메타데이터 경로. None이면 원격(BASE_URL) 사용.
103
-
104
- Returns:
105
- DataFrame: name, chapter, desc, url 컬럼을 갖는 테이블
106
-
107
- Examples:
108
- ```python
109
- from hossam import *
110
- info = load_info()
111
- list(info.columns) #['name', 'chapter', 'desc', 'url']
112
- ```
113
- """
114
- global BASE_URL
115
-
116
- path = None
117
-
118
- if not local:
119
- data_path = join(BASE_URL, "metadata.json").replace("\\", "/")
120
-
121
- with requests.Session() as session:
122
- r = session.get(data_path)
123
-
124
- if r.status_code != 200:
125
- raise Exception("[%d Error] %s ::: %s" % (r.status_code, r.reason, data_path))
126
-
127
- my_dict = r.json()
128
- else:
129
- data_path = join(local, "metadata.json")
130
-
131
- if not exists(data_path):
132
- raise FileNotFoundError("존재하지 않는 데이터에 대한 요청입니다.")
133
-
134
- with open(data_path, "r", encoding="utf-8") as f:
135
- my_dict = json.loads(f.read())
136
-
137
- my_data = []
138
- for key in my_dict:
139
- if 'index' in my_dict[key]:
140
- del my_dict[key]['index']
141
-
142
- my_dict[key]['url'] = "%s/%s" % (BASE_URL, my_dict[key]['url'])
143
- my_dict[key]['name'] = key
144
-
145
- if 'chapter' in my_dict[key]:
146
- my_dict[key]['chapter'] = ", ".join(my_dict[key]['chapter'])
147
- else:
148
- my_dict[key]['chapter'] = '공통'
149
-
150
- my_data.append(my_dict[key])
151
-
152
- my_df = DataFrame(my_data)
153
- my_df2 = my_df.reindex(columns=['name', 'chapter', 'desc', 'url'])
154
-
155
- if search:
156
- my_df2 = my_df2[my_df2['name'].str.contains(search.lower())]
157
-
158
- return my_df2
159
-
160
- # -------------------------------------------------------------
161
- def load_data(key: str, local: str | None = None) -> Optional[DataFrame]:
162
- """키로 지정된 데이터셋을 로드한다.
163
-
164
- Args:
165
- key (str): 메타데이터에 정의된 데이터 식별자(파일명 또는 별칭)
166
- local (str, optional): 로컬 메타데이터 경로. None이면 원격(BASE_URL) 사용.
167
-
168
- Returns:
169
- DataFrame | None: 성공 시 데이터프레임, 실패 시 None
170
-
171
- Examples:
172
- ```python
173
- from hossam import *
174
- df = load_data('AD_SALES') # 메타데이터에 해당 키가 있어야 함
175
- ```
176
- """
177
- index = None
178
- try:
179
- url, desc, index = __get_data_url(key, local=local)
180
- except Exception as e:
181
- try:
182
- print(f"\033[91m{str(e)}\033[0m")
183
- except Exception:
184
- print(e)
185
- return
186
-
187
- #print("\033[94m[data]\033[0m", url.replace("\\", "/"))
188
- #print("\033[94m[desc]\033[0m", desc)
189
- print(f"\033[94m{desc}\033[0m")
190
-
191
- df = None
192
-
193
- try:
194
- df = __get_df(url, index_col=index)
195
- except Exception as e:
196
- try:
197
- print(f"\033[91m{str(e)}\033[0m")
198
- except Exception:
199
- print(e)
200
- return
201
-
202
-
203
- return df
@@ -1,16 +0,0 @@
1
- hossam/NotoSansKR-Regular.ttf,sha256=0SCufUQwcVWrWTu75j4Lt_V2bgBJIBXl1p8iAJJYkVY,6185516
2
- hossam/__init__.py,sha256=OkMeP15jt6aCy7QNXMtkO0YRVvgOQYumkb7GuVKrbcs,2712
3
- hossam/data_loader.py,sha256=K0-MJaVeedF5x8mSp22X2rD_CZ-T185EhoUFEqzP8Ss,6352
4
- hossam/hs_classroom.py,sha256=rgayol3U5PSo4rLfdbClfiAtG21bFrASaSW56PUsjus,27144
5
- hossam/hs_gis.py,sha256=DVmndBK-_7GMK3J1_on3ieEQk1S0MfUZ8_wlX-cDdZQ,11581
6
- hossam/hs_plot.py,sha256=3j9B69pl-zQM_09lTXxLKAMaDM0vwOTsUWbzcU8hCK8,86228
7
- hossam/hs_prep.py,sha256=kCmFxnMyFZ5tLUfoE8msbwTracajHAmruJbFj6A6eIU,38020
8
- hossam/hs_stats.py,sha256=uGYkEk8Rb8qMoZ5FiZ7Yg6jssLIGl_EBbmwvvSYljhQ,115780
9
- hossam/hs_timeserise.py,sha256=gSj3cPgOGLOZEXhfW1anXbwpoJja847ZY9F8l9piJPE,42601
10
- hossam/hs_util.py,sha256=xuNXC6FJSAmyAbcRAUMsigCKHXM25t3H90nFMgq7IBs,8482
11
- hossam/leekh.png,sha256=1PB5NQ24SDoHA5KMiBBsWpSa3iniFcwFTuGwuOsTHfI,6395
12
- hossam-0.4.5.dist-info/licenses/LICENSE,sha256=nIqzhlcFY_2D6QtFsYjwU7BWkafo-rUJOQpDZ-DsauI,941
13
- hossam-0.4.5.dist-info/METADATA,sha256=HM5qrrvaFZWAyUlhgV_BLPHAcxEZdZ4gp2p3V4X4pzo,3676
14
- hossam-0.4.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
15
- hossam-0.4.5.dist-info/top_level.txt,sha256=_-7bwjhthHplWhywEaHIJX2yL11CQCaLjCNSBlk6wiQ,7
16
- hossam-0.4.5.dist-info/RECORD,,
File without changes