Skip to content

Commit c7831fc

Browse files
committed
lint: run through formatter
1 parent 2300626 commit c7831fc

File tree

6 files changed

+126
-41
lines changed

6 files changed

+126
-41
lines changed

epidatpy/_constants.py

-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,5 @@
22

33

44
__version__: Final = "0.5.0"
5-
6-
75
HTTP_HEADERS: Final = {"User-Agent": f"epidatpy/{__version__}"}
8-
96
BASE_URL: Final = "https://api.delphi.cmu.edu/epidata/"

epidatpy/_covidcast.py

+17-7
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,11 @@ def define_covidcast_fields() -> List[EpidataFieldInfo]:
6969
categories=list(get_args(GeoType)),
7070
),
7171
EpidataFieldInfo("geo_value", EpidataFieldType.text),
72-
EpidataFieldInfo("time_type", EpidataFieldType.categorical, categories=list(get_args(TimeType))),
72+
EpidataFieldInfo(
73+
"time_type",
74+
EpidataFieldType.categorical,
75+
categories=list(get_args(TimeType)),
76+
),
7377
EpidataFieldInfo("time_value", EpidataFieldType.date_or_epiweek),
7478
EpidataFieldInfo("issue", EpidataFieldType.date),
7579
EpidataFieldInfo("lag", EpidataFieldType.int),
@@ -215,7 +219,8 @@ class DataSource(Generic[CALL_TYPE]):
215219
signals: Sequence[DataSignal] = field(default_factory=list)
216220

217221
def __post_init__(
218-
self, _create_call: Callable[[Mapping[str, Union[None, EpiRangeLike, Iterable[EpiRangeLike]]]], CALL_TYPE]
222+
self,
223+
_create_call: Callable[[Mapping[str, Union[None, EpiRangeLike, Iterable[EpiRangeLike]]]], CALL_TYPE],
219224
) -> None:
220225
self.link = [WebLink(alt=l["alt"], href=l["href"]) if isinstance(l, dict) else l for l in self.link]
221226
signal_fields = fields(DataSignal)
@@ -228,7 +233,14 @@ def __post_init__(
228233
def to_df(sources: Iterable["DataSource"]) -> DataFrame:
229234
df = DataFrame(
230235
sources,
231-
columns=["source", "name", "description", "reference_signal", "license", "dua"],
236+
columns=[
237+
"source",
238+
"name",
239+
"description",
240+
"reference_signal",
241+
"license",
242+
"dua",
243+
],
232244
)
233245
df["signals"] = [",".join(ss.signal for ss in s.signals) for s in sources]
234246
return df.set_index("source")
@@ -389,12 +401,10 @@ def __iter__(self) -> Iterable[DataSource[CALL_TYPE]]:
389401
return iter(self.sources)
390402

391403
@overload
392-
def __getitem__(self, source: str) -> DataSource[CALL_TYPE]:
393-
...
404+
def __getitem__(self, source: str) -> DataSource[CALL_TYPE]: ...
394405

395406
@overload
396-
def __getitem__(self, source_signal: Tuple[str, str]) -> DataSignal[CALL_TYPE]:
397-
...
407+
def __getitem__(self, source_signal: Tuple[str, str]) -> DataSignal[CALL_TYPE]: ...
398408

399409
def __getitem__(
400410
self, source_signal: Union[str, Tuple[str, str]]

epidatpy/_model.py

+40-13
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,12 @@
2121
from epiweeks import Week
2222
from pandas import DataFrame, CategoricalDtype
2323

24-
from ._parse import parse_api_date, parse_api_week, parse_api_date_or_week, fields_to_predicate
24+
from ._parse import (
25+
parse_api_date,
26+
parse_api_week,
27+
parse_api_date_or_week,
28+
fields_to_predicate,
29+
)
2530

2631
EpiDateLike = Union[int, str, date, Week]
2732
EpiRangeDict = TypedDict("EpiRangeDict", {"from": EpiDateLike, "to": EpiDateLike})
@@ -33,6 +38,7 @@ def format_date(d: EpiDateLike) -> str:
3338
# YYYYMMDD
3439
return d.strftime("%Y%m%d")
3540
if isinstance(d, Week):
41+
# YYYYww
3642
return cast(str, d.cdcformat())
3743
return str(d)
3844

@@ -81,7 +87,9 @@ def __str__(self) -> str:
8187
return f"{format_date(self.start)}-{format_date(self.end)}"
8288

8389

84-
EpiDataResponse = TypedDict("EpiDataResponse", {"result": int, "message": str, "epidata": List})
90+
EpiDataResponse = TypedDict(
91+
"EpiDataResponse", {"result": int, "message": str, "epidata": List}
92+
)
8593

8694

8795
EpiRangeParam = Union[EpiRangeLike, Iterable[EpiRangeLike]]
@@ -180,8 +188,10 @@ def _verify_parameters(self) -> None:
180188
# hook for verifying parameters before sending
181189
pass
182190

183-
def _formatted_paramters(
184-
self, format_type: Optional[EpiDataFormatType] = None, fields: Optional[Iterable[str]] = None
191+
def _formatted_parameters(
192+
self,
193+
format_type: Optional[EpiDataFormatType] = None,
194+
fields: Optional[Iterable[str]] = None,
185195
) -> Mapping[str, str]:
186196
"""
187197
format this call into a [URL, Params] tuple
@@ -194,12 +204,14 @@ def _formatted_paramters(
194204
return {k: format_list(v) for k, v in all_params.items() if v is not None}
195205

196206
def request_arguments(
197-
self, format_type: Optional[EpiDataFormatType] = None, fields: Optional[Iterable[str]] = None
207+
self,
208+
format_type: Optional[EpiDataFormatType] = None,
209+
fields: Optional[Iterable[str]] = None,
198210
) -> Tuple[str, Mapping[str, str]]:
199211
"""
200212
format this call into a [URL, Params] tuple
201213
"""
202-
formatted_params = self._formatted_paramters(format_type, fields)
214+
formatted_params = self._formatted_parameters(format_type, fields)
203215
full_url = self._full_url()
204216
return full_url, formatted_params
205217

@@ -225,13 +237,16 @@ def request_url(
225237
return u
226238

227239
def __repr__(self) -> str:
228-
return f"EpiDataCall(endpoint={self._endpoint}, params={self._formatted_paramters()})"
240+
return f"EpiDataCall(endpoint={self._endpoint}, params={self._formatted_parameters()})"
229241

230242
def __str__(self) -> str:
231243
return self.request_url()
232244

233245
def _parse_value(
234-
self, key: str, value: Union[str, float, int, None], disable_date_parsing: Optional[bool] = False
246+
self,
247+
key: str,
248+
value: Union[str, float, int, None],
249+
disable_date_parsing: Optional[bool] = False,
235250
) -> Union[str, float, int, date, None]:
236251
meta = self.meta_by_name.get(key)
237252
if not meta or value is None:
@@ -247,11 +262,15 @@ def _parse_value(
247262
return value
248263

249264
def _parse_row(
250-
self, row: Mapping[str, Union[str, float, int, None]], disable_date_parsing: Optional[bool] = False
265+
self,
266+
row: Mapping[str, Union[str, float, int, None]],
267+
disable_date_parsing: Optional[bool] = False,
251268
) -> Mapping[str, Union[str, float, int, date, None]]:
252269
if not self.meta:
253270
return row
254-
return {k: self._parse_value(k, v, disable_date_parsing) for k, v in row.items()}
271+
return {
272+
k: self._parse_value(k, v, disable_date_parsing) for k, v in row.items()
273+
}
255274

256275
def _as_df(
257276
self,
@@ -270,11 +289,19 @@ def _as_df(
270289
if info.type == EpidataFieldType.bool:
271290
data_types[info.name] = bool
272291
elif info.type == EpidataFieldType.categorical:
273-
data_types[info.name] = CategoricalDtype(categories=info.categories or None, ordered=True)
292+
data_types[info.name] = CategoricalDtype(
293+
categories=info.categories or None, ordered=True
294+
)
274295
elif info.type == EpidataFieldType.int:
275296
data_types[info.name] = int
276-
elif info.type in (EpidataFieldType.date, EpidataFieldType.epiweek, EpidataFieldType.date_or_epiweek):
277-
data_types[info.name] = int if disable_date_parsing else "datetime64[ns]"
297+
elif info.type in (
298+
EpidataFieldType.date,
299+
EpidataFieldType.epiweek,
300+
EpidataFieldType.date_or_epiweek,
301+
):
302+
data_types[info.name] = (
303+
int if disable_date_parsing else "datetime64[ns]"
304+
)
278305
elif info.type == EpidataFieldType.float:
279306
data_types[info.name] = float
280307
else:

epidatpy/_parse.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ def parse_api_date_or_week(value: Union[str, int, float, None]) -> Optional[date
2929
return d
3030

3131

32-
def fields_to_predicate(fields: Optional[Iterable[str]] = None) -> Callable[[str], bool]:
32+
def fields_to_predicate(
33+
fields: Optional[Iterable[str]] = None,
34+
) -> Callable[[str], bool]:
3335
if not fields:
3436
return lambda _: True
3537
to_include: Set[str] = set()

epidatpy/async_request.py

+28-8
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ async def _call(
8484
return await _async_request(url, params, self._session)
8585

8686
async def classic(
87-
self, fields: Optional[Iterable[str]] = None, disable_date_parsing: Optional[bool] = False
87+
self,
88+
fields: Optional[Iterable[str]] = None,
89+
disable_date_parsing: Optional[bool] = False,
8890
) -> EpiDataResponse:
8991
"""Request and parse epidata in CLASSIC message format."""
9092
self._verify_parameters()
@@ -99,13 +101,17 @@ async def classic(
99101
return {"result": 0, "message": f"error: {e}", "epidata": []}
100102

101103
async def __call__(
102-
self, fields: Optional[Iterable[str]] = None, disable_date_parsing: Optional[bool] = False
104+
self,
105+
fields: Optional[Iterable[str]] = None,
106+
disable_date_parsing: Optional[bool] = False,
103107
) -> EpiDataResponse:
104108
"""Request and parse epidata in CLASSIC message format."""
105109
return await self.classic(fields, disable_date_parsing=disable_date_parsing)
106110

107111
async def json(
108-
self, fields: Optional[Iterable[str]] = None, disable_date_parsing: Optional[bool] = False
112+
self,
113+
fields: Optional[Iterable[str]] = None,
114+
disable_date_parsing: Optional[bool] = False,
109115
) -> List[Mapping[str, Union[str, int, float, date, None]]]:
110116
"""Request and parse epidata in JSON format"""
111117
self._verify_parameters()
@@ -119,7 +125,9 @@ async def json(
119125
]
120126

121127
async def df(
122-
self, fields: Optional[Iterable[str]] = None, disable_date_parsing: Optional[bool] = False
128+
self,
129+
fields: Optional[Iterable[str]] = None,
130+
disable_date_parsing: Optional[bool] = False,
123131
) -> DataFrame:
124132
"""Request and parse epidata as a pandas data frame"""
125133
self._verify_parameters()
@@ -138,7 +146,9 @@ async def csv(self, fields: Optional[Iterable[str]] = None) -> str:
138146
return await response.text()
139147

140148
async def iter(
141-
self, fields: Optional[Iterable[str]] = None, disable_date_parsing: Optional[bool] = False
149+
self,
150+
fields: Optional[Iterable[str]] = None,
151+
disable_date_parsing: Optional[bool] = False,
142152
) -> AsyncGenerator[Mapping[str, Union[str, int, float, date, None]], None]:
143153
"""Request and streams epidata rows"""
144154
self._verify_parameters()
@@ -149,7 +159,9 @@ async def iter(
149159
async for line in response.content:
150160
yield self._parse_row(loads(line), disable_date_parsing=disable_date_parsing)
151161

152-
async def __(self) -> AsyncGenerator[Mapping[str, Union[str, int, float, date, None]], None]:
162+
async def __(
163+
self,
164+
) -> AsyncGenerator[Mapping[str, Union[str, int, float, date, None]], None]:
153165
return self.iter()
154166

155167

@@ -258,10 +270,18 @@ async def CovidcastEpidata(
258270
meta_data_res.raise_for_status()
259271
meta_data = await meta_data_res.json()
260272

261-
def create_call(params: Mapping[str, Union[None, EpiRangeLike, Iterable[EpiRangeLike]]]) -> EpiDataAsyncCall:
273+
def create_call(
274+
params: Mapping[str, Union[None, EpiRangeLike, Iterable[EpiRangeLike]]],
275+
) -> EpiDataAsyncCall:
262276
return EpiDataAsyncCall(base_url, session, "covidcast", params, define_covidcast_fields())
263277

264278
return CovidcastDataSources.create(meta_data, create_call)
265279

266280

267-
__all__ = ["Epidata", "EpiDataAsyncCall", "EpiDataAsyncContext", "EpiRange", "CovidcastEpidata"]
281+
__all__ = [
282+
"Epidata",
283+
"EpiDataAsyncCall",
284+
"EpiDataAsyncContext",
285+
"EpiRange",
286+
"CovidcastEpidata",
287+
]

epidatpy/request.py

+38-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
from datetime import date
2-
from typing import Final, Generator, Sequence, cast, Iterable, Mapping, Optional, Union, List
2+
from typing import (
3+
Final,
4+
Generator,
5+
Sequence,
6+
cast,
7+
Iterable,
8+
Mapping,
9+
Optional,
10+
Union,
11+
List,
12+
)
313
from json import loads
414

515
from requests import Response, Session
@@ -25,7 +35,10 @@
2535

2636
@retry(reraise=True, stop=stop_after_attempt(2))
2737
def _request_with_retry(
28-
url: str, params: Mapping[str, str], session: Optional[Session] = None, stream: bool = False
38+
url: str,
39+
params: Mapping[str, str],
40+
session: Optional[Session] = None,
41+
stream: bool = False,
2942
) -> Response:
3043
"""Make request with a retry if an exception is thrown."""
3144
basic_auth = HTTPBasicAuth("epidata", get_api_key())
@@ -78,7 +91,9 @@ def _call(
7891
return _request_with_retry(url, params, self._session, stream)
7992

8093
def classic(
81-
self, fields: Optional[Iterable[str]] = None, disable_date_parsing: Optional[bool] = False
94+
self,
95+
fields: Optional[Iterable[str]] = None,
96+
disable_date_parsing: Optional[bool] = False,
8297
) -> EpiDataResponse:
8398
"""Request and parse epidata in CLASSIC message format."""
8499
self._verify_parameters()
@@ -93,13 +108,17 @@ def classic(
93108
return {"result": 0, "message": f"error: {e}", "epidata": []}
94109

95110
def __call__(
96-
self, fields: Optional[Iterable[str]] = None, disable_date_parsing: Optional[bool] = False
111+
self,
112+
fields: Optional[Iterable[str]] = None,
113+
disable_date_parsing: Optional[bool] = False,
97114
) -> EpiDataResponse:
98115
"""Request and parse epidata in CLASSIC message format."""
99116
return self.classic(fields, disable_date_parsing=disable_date_parsing)
100117

101118
def json(
102-
self, fields: Optional[Iterable[str]] = None, disable_date_parsing: Optional[bool] = False
119+
self,
120+
fields: Optional[Iterable[str]] = None,
121+
disable_date_parsing: Optional[bool] = False,
103122
) -> List[Mapping[str, Union[str, int, float, date, None]]]:
104123
"""Request and parse epidata in JSON format"""
105124
if self.only_supports_classic:
@@ -112,7 +131,11 @@ def json(
112131
for row in cast(List[Mapping[str, Union[str, int, float, None]]], response.json())
113132
]
114133

115-
def df(self, fields: Optional[Iterable[str]] = None, disable_date_parsing: Optional[bool] = False) -> DataFrame:
134+
def df(
135+
self,
136+
fields: Optional[Iterable[str]] = None,
137+
disable_date_parsing: Optional[bool] = False,
138+
) -> DataFrame:
116139
"""Request and parse epidata as a pandas data frame"""
117140
if self.only_supports_classic:
118141
raise OnlySupportsClassicFormatException()
@@ -130,7 +153,9 @@ def csv(self, fields: Optional[Iterable[str]] = None) -> str:
130153
return response.text
131154

132155
def iter(
133-
self, fields: Optional[Iterable[str]] = None, disable_date_parsing: Optional[bool] = False
156+
self,
157+
fields: Optional[Iterable[str]] = None,
158+
disable_date_parsing: Optional[bool] = False,
134159
) -> Generator[Mapping[str, Union[str, int, float, date, None]], None, Response]:
135160
"""Request and streams epidata rows"""
136161
if self.only_supports_classic:
@@ -142,7 +167,9 @@ def iter(
142167
yield self._parse_row(loads(line), disable_date_parsing=disable_date_parsing)
143168
return response
144169

145-
def __iter__(self) -> Generator[Mapping[str, Union[str, int, float, date, None]], None, Response]:
170+
def __iter__(
171+
self,
172+
) -> Generator[Mapping[str, Union[str, int, float, date, None]], None, Response]:
146173
return self.iter()
147174

148175

@@ -184,7 +211,9 @@ def CovidcastEpidata(base_url: str = BASE_URL, session: Optional[Session] = None
184211
meta_data_res.raise_for_status()
185212
meta_data = meta_data_res.json()
186213

187-
def create_call(params: Mapping[str, Union[None, EpiRangeLike, Iterable[EpiRangeLike]]]) -> EpiDataCall:
214+
def create_call(
215+
params: Mapping[str, Union[None, EpiRangeLike, Iterable[EpiRangeLike]]],
216+
) -> EpiDataCall:
188217
return EpiDataCall(base_url, session, "covidcast", params, define_covidcast_fields())
189218

190219
return CovidcastDataSources.create(meta_data, create_call)

0 commit comments

Comments
 (0)