Skip to content

Commit a849384

Browse files
committed
style(black): format kcdc acquisition
1 parent b8900a0 commit a849384

File tree

1 file changed

+49
-41
lines changed

1 file changed

+49
-41
lines changed

src/acquisition/kcdc/kcdc_update.py

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,14 @@
4242
from delphi.utils.epiweek import delta_epiweeks, range_epiweeks, add_epiweeks
4343
from delphi.utils.epidate import EpiDate
4444

45+
4546
def ensure_tables_exist():
46-
(u,p) = secrets.db.epi
47-
cnx = mysql.connector.connect(user=u,password=p,database='epidata')
47+
(u, p) = secrets.db.epi
48+
cnx = mysql.connector.connect(user=u, password=p, database="epidata")
4849
try:
4950
cursor = cnx.cursor()
50-
cursor.execute('''
51+
cursor.execute(
52+
"""
5153
CREATE TABLE IF NOT EXISTS `kcdc_ili` (
5254
`id` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
5355
`release_date` DATE NOT NULL,
@@ -58,69 +60,76 @@ def ensure_tables_exist():
5860
`ili` DOUBLE NOT NULL,
5961
UNIQUE KEY (`issue`, `epiweek`, `region`)
6062
);
61-
''');
63+
"""
64+
)
6265
cnx.commit()
6366
finally:
6467
cnx.close()
6568

69+
6670
def safe_float(f):
6771
try:
68-
return float(f.replace(',',''))
72+
return float(f.replace(",", ""))
6973
except:
7074
return 0
7175

76+
7277
def safe_int(i):
7378
try:
74-
return int(i.replace(',',''))
79+
return int(i.replace(",", ""))
7580
except:
7681
return 0
7782

78-
def get_rows(cnx, table='kcdc_ili'):
79-
# Count and return the number of rows in the `kcdc_ili` table.
80-
select = cnx.cursor()
81-
select.execute('SELECT count(1) num FROM %s' % table)
82-
for (num,) in select:
83-
pass
84-
select.close()
85-
return num
83+
84+
def get_rows(cnx, table="kcdc_ili"):
85+
# Count and return the number of rows in the `kcdc_ili` table.
86+
select = cnx.cursor()
87+
select.execute("SELECT count(1) num FROM %s" % table)
88+
for (num,) in select:
89+
pass
90+
select.close()
91+
return num
92+
8693

8794
def get_kcdc_data():
8895
issue = EpiDate.today().get_ew()
89-
last_season = issue//100 + (1 if issue % 100 > 35 else 0)
90-
url = 'http://www.cdc.go.kr/npt/biz/npp/iss/influenzaListAjax.do'
96+
last_season = issue // 100 + (1 if issue % 100 > 35 else 0)
97+
url = "https://www.cdc.go.kr/npt/biz/npp/iss/influenzaListAjax.do"
98+
# Started in 2004
9199
params = {
92-
'icdNm': 'influenza',
93-
'startYear': '2004', # Started in 2004
94-
'endYear': str(last_season)
100+
"icdNm": "influenza",
101+
"startYear": "2004",
102+
"endYear": str(last_season),
95103
}
96104
response = requests.post(url, params)
97105
datas = response.json()
98-
data = datas['data']
106+
data = datas["data"]
99107
ews = []
100108
ilis = []
101109
ew1 = 200436
102-
for year in range(2004,last_season):
103-
year_data = data[year-2004]
110+
for year in range(2004, last_season):
111+
year_data = data[year - 2004]
104112
if year > 2004:
105113
ew1 = ews[-1] + 1
106-
ili_yr = year_data["VALUE"].split('`')
107-
ili_yr = [float(f) for f in ili_yr if f != '']
108-
ew2 = add_epiweeks(ew1,len(ili_yr))
109-
new_ews = list(range_epiweeks(ew1,ew2))
114+
ili_yr = year_data["VALUE"].split("`")
115+
ili_yr = [float(f) for f in ili_yr if f != ""]
116+
ew2 = add_epiweeks(ew1, len(ili_yr))
117+
new_ews = list(range_epiweeks(ew1, ew2))
110118
for i in range(len(new_ews)):
111119
j = float(ili_yr[i])
112120
ilis.append(j)
113121
ews.append(new_ews[i])
114122
return ews, ilis
115123

124+
116125
def update_from_data(ews, ilis, date, issue, test_mode=False):
117126
u, p = secrets.db.epi
118-
cnx = mysql.connector.connect(user=u, password=p, database='epidata')
127+
cnx = mysql.connector.connect(user=u, password=p, database="epidata")
119128
rows1 = get_rows(cnx)
120-
print('rows before: %d' % (rows1))
129+
print("rows before: %d" % (rows1))
121130
insert = cnx.cursor()
122131

123-
sql = '''
132+
sql = """
124133
INSERT INTO
125134
`kcdc_ili` (`release_date`, `issue`, `epiweek`, `region`, `lag`,
126135
`ili`)
@@ -129,15 +138,15 @@ def update_from_data(ews, ilis, date, issue, test_mode=False):
129138
ON DUPLICATE KEY UPDATE
130139
`release_date` = least(`release_date`, '%s'),
131140
`ili` = %s
132-
'''
141+
"""
133142

134143
for i in range(len(ews)):
135144
ew = ews[i]
136145
ili = ilis[i]
137146
lag = delta_epiweeks(ews[i], issue)
138147

139-
insert_args = [date,issue,ew,'ROK',lag,ili]
140-
update_args = [date,ili]
148+
insert_args = [date, issue, ew, "ROK", lag, ili]
149+
update_args = [date, ili]
141150
try:
142151
insert.execute(sql % tuple(insert_args + update_args))
143152
except Exception:
@@ -146,34 +155,33 @@ def update_from_data(ews, ilis, date, issue, test_mode=False):
146155
# cleanup
147156
insert.close()
148157
if test_mode:
149-
print('test mode, not committing')
158+
print("test mode, not committing")
150159
rows2 = rows1
151160
else:
152161
cnx.commit()
153162
rows2 = get_rows(cnx)
154-
print('rows after: %d (added %d)' % (rows2,rows2-rows1))
163+
print("rows after: %d (added %d)" % (rows2, rows2 - rows1))
155164
cnx.close()
156165

166+
157167
def main():
158168
# args and usage
159169
parser = argparse.ArgumentParser()
160170
parser.add_argument(
161-
'--test',
162-
action='store_true',
163-
help='do dry run only, do not update the database'
171+
"--test", action="store_true", help="do dry run only, do not update the database"
164172
)
165173
args = parser.parse_args()
166174

167-
date = datetime.datetime.now().strftime('%Y-%m-%d')
168-
print('assuming release date is today, %s' % date)
175+
date = datetime.datetime.now().strftime("%Y-%m-%d")
176+
print("assuming release date is today, %s" % date)
169177
issue = EpiDate.today().get_ew()
170178

171179
ensure_tables_exist()
172180

173-
ews,ilis = get_kcdc_data()
181+
ews, ilis = get_kcdc_data()
174182

175183
update_from_data(ews, ilis, date, issue, test_mode=args.test)
176184

177185

178-
if __name__ == '__main__':
186+
if __name__ == "__main__":
179187
main()

0 commit comments

Comments
 (0)