88"""
99
1010import asyncio
11- from typing import Literal
11+ from typing import Literal , cast
1212
1313import pandas as pd
1414from pysus .api import types
1515from pysus .api .client import PySUS
16- from tqdm import tqdm
16+ from tqdm . asyncio import tqdm
1717
1818__all__ = [
1919 "sinan" ,
@@ -57,10 +57,9 @@ def _fetch_data(
5757 month : int | list[int], optional
5858 Month or list of months to fetch.
5959 show_progress : bool, optional
60- Whether to display a tqdm progress bar during download. Default is True.
60+ Whether to display a tqdm progress bar during download.
6161 as_dataframe : bool, optional
6262 Whether to concatenate and return the data as a pandas DataFrame.
63- Default is False.
6463 **kwargs
6564 Additional arguments forwarded to :meth:`PySUS.read_parquet`.
6665
@@ -71,48 +70,41 @@ def _fetch_data(
7170 as_dataframe is True, returns a concatenated DataFrame.
7271 """
7372
74- async def _fetch ():
75-
73+ async def _fetch () -> list [str ] | pd .DataFrame :
7674 async with PySUS () as pysus :
77- years = [year ] if isinstance (year , int ) else (year or [None ])
78- months = [month ] if isinstance (month , int ) else (month or [None ])
75+ files = await pysus .query (
76+ dataset = dataset ,
77+ group = group ,
78+ state = state ,
79+ year = year ,
80+ month = month ,
81+ )
7982
80- files = []
81- for y in years :
82- for m in months :
83- files .extend (
84- await pysus .query (
85- dataset = dataset ,
86- group = group ,
87- state = state ,
88- year = y ,
89- month = m ,
90- )
91- )
83+ if not files :
84+ return pd .DataFrame () if as_dataframe else cast (list [str ], [])
85+
86+ sem = asyncio .Semaphore (3 )
87+
88+ async def _throttled_download (f ):
89+ async with sem :
90+ return await pysus .download (f )
91+
92+ tasks = [_throttled_download (f ) for f in files ]
9293
93- paths = []
9494 if show_progress :
95- for file in tqdm (
96- files ,
95+ downloaded_files = await tqdm . gather (
96+ * tasks ,
9797 desc = f"Downloading { dataset } " ,
9898 unit = "file" ,
99- ):
100- f = await pysus .download (file )
101- paths .append (str (f .path ))
99+ )
102100 else :
103- for file in files :
104- f = await pysus . download ( file )
105- paths . append ( str (f .path ))
101+ downloaded_files = await asyncio . gather ( * tasks )
102+
103+ paths : list [ str ] = [ str (f .path ) for f in downloaded_files ]
106104
107105 if as_dataframe :
108- return (
109- pysus .read_parquet (
110- paths ,
111- ** kwargs ,
112- ).df ()
113- if paths
114- else pd .DataFrame ()
115- )
106+ res = pysus .read_parquet (paths , ** kwargs ).df ()
107+ return cast (pd .DataFrame , res )
116108
117109 return paths
118110
@@ -132,9 +124,11 @@ async def _fetch():
132124 "Install it with: pip install nest_asyncio"
133125 )
134126 raise RuntimeError (msg ) from None
135- return loop .run_until_complete (_fetch ())
136- else :
137- return asyncio .run (_fetch ())
127+ result = loop .run_until_complete (_fetch ())
128+ return cast (list [str ] | pd .DataFrame , result )
129+
130+ result = asyncio .run (_fetch ())
131+ return cast (list [str ] | pd .DataFrame , result )
138132
139133
140134def sinan (
0 commit comments