refactoring

This commit is contained in:
frankknoll
2022-11-22 14:25:36 +01:00
parent e0a86efa96
commit 8d1f38dfb6
4 changed files with 51 additions and 77 deletions

View File

@@ -0,0 +1,22 @@
from bs4 import BeautifulSoup
from HtmlTransformerUtil import HtmlTransformerUtil
from CountryOptionsSetter import CountryOptionsSetter
from DateProvider import DateProvider
def saveCountryOptions(countryOptions):
HtmlTransformerUtil().applySoupTransformerToFile(
file = "../docs/batchCodeTable.html",
soupTransformer =
lambda soup:
BeautifulSoup(
CountryOptionsSetter().setCountryOptions(html = str(soup), options = countryOptions),
'lxml'))
def saveLastUpdatedBatchCodeTable(lastUpdated):
def setLastUpdated(soup):
soup.find(id = "last_updated").string.replace_with(lastUpdated.strftime(DateProvider.DATE_FORMAT))
return soup
HtmlTransformerUtil().applySoupTransformerToFile(
file = "../docs/batchCodeTable.html",
soupTransformer = setLastUpdated)

View File

@@ -0,0 +1,15 @@
from IOUtils import IOUtils
import numpy as np
def createAndSaveBatchCodeTableForCountry(createBatchCodeTableForCountry, country, minADRsForLethality = None):
batchCodeTable = createBatchCodeTableForCountry(country)
batchCodeTable.index.set_names("Batch", inplace = True)
if minADRsForLethality is not None:
batchCodeTable.loc[batchCodeTable['Adverse Reaction Reports'] < minADRsForLethality, ['Severe reports', 'Lethality']] = [np.nan, np.nan]
IOUtils.saveDataFrame(batchCodeTable, '../docs/data/batchCodeTables/' + country)
# display(country + ":", batchCodeTable)
display(country)
def createAndSaveBatchCodeTablesForCountries(createBatchCodeTableForCountry, countries, minADRsForLethality = None):
for country in countries:
createAndSaveBatchCodeTableForCountry(createBatchCodeTableForCountry, country, minADRsForLethality)

View File

@@ -7,7 +7,6 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"import numpy as np\n",
"import pandas as pd\n", "import pandas as pd\n",
"\n", "\n",
"pd.set_option('display.max_rows', 100)\n", "pd.set_option('display.max_rows', 100)\n",
@@ -124,26 +123,6 @@
"from BatchCodeTableFactory import BatchCodeTableFactory" "from BatchCodeTableFactory import BatchCodeTableFactory"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"id": "4db36933",
"metadata": {},
"outputs": [],
"source": [
"from HtmlTransformerUtil import HtmlTransformerUtil"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "32d4eecf",
"metadata": {},
"outputs": [],
"source": [
"from CountryOptionsSetter import CountryOptionsSetter"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@@ -151,44 +130,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from bs4 import BeautifulSoup\n", "from BatchCodeTableHtmlUpdater import saveCountryOptions, saveLastUpdatedBatchCodeTable"
"\n",
"\n",
"def saveCountryOptions(countryOptions):\n",
" HtmlTransformerUtil().applySoupTransformerToFile(\n",
" file = \"../docs/batchCodeTable.html\",\n",
" soupTransformer =\n",
" lambda soup:\n",
" BeautifulSoup(\n",
" CountryOptionsSetter().setCountryOptions(html = str(soup), options = countryOptions),\n",
" 'lxml'))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f02dddfe",
"metadata": {},
"outputs": [],
"source": [
"def saveLastUpdatedBatchCodeTable(lastUpdated):\n",
" def setLastUpdated(soup):\n",
" soup.find(id = \"last_updated\").string.replace_with(lastUpdated.strftime(DateProvider.DATE_FORMAT))\n",
" return soup\n",
"\n",
" HtmlTransformerUtil().applySoupTransformerToFile(\n",
" file = \"../docs/batchCodeTable.html\",\n",
" soupTransformer = setLastUpdated)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6aa28541",
"metadata": {},
"outputs": [],
"source": [
"from IOUtils import IOUtils"
] ]
}, },
{ {
@@ -220,18 +162,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def createAndSaveBatchCodeTableForCountry(createBatchCodeTableForCountry, country, minADRsForLethality = None):\n", "from BatchCodeTablePersister import createAndSaveBatchCodeTableForCountry, createAndSaveBatchCodeTablesForCountries"
" batchCodeTable = createBatchCodeTableForCountry(country)\n",
" batchCodeTable.index.set_names(\"Batch\", inplace = True)\n",
" if minADRsForLethality is not None:\n",
" batchCodeTable.loc[batchCodeTable['Adverse Reaction Reports'] < minADRsForLethality, ['Severe reports', 'Lethality']] = [np.nan, np.nan]\n",
" IOUtils.saveDataFrame(batchCodeTable, '../docs/data/batchCodeTables/' + country)\n",
" # display(country + \":\", batchCodeTable)\n",
" display(country)\n",
"\n",
"def createAndSaveBatchCodeTablesForCountries(createBatchCodeTableForCountry, countries, minADRsForLethality = None):\n",
" for country in countries:\n",
" createAndSaveBatchCodeTableForCountry(createBatchCodeTableForCountry, country, minADRsForLethality)"
] ]
}, },
{ {
@@ -241,11 +172,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"def getCountryOptions(countries):\n", "from HtmlUtils import getCountryOptions"
" return [getCountryOption(country) for country in countries]\n",
"\n",
"def getCountryOption(country):\n",
" return '<option value=\"{country}\">{country}</option>'.format(country = country)"
] ]
}, },
{ {
@@ -256,7 +183,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"countries = sorted(internationalVaersCovid19['COUNTRY'].unique())\n", "countries = sorted(internationalVaersCovid19['COUNTRY'].unique())\n",
"countryOptions = ['<option value=\"Global\" selected>Global</option>'] + getCountryOptions(countries)" "countryOptions = getCountryOptions(countries)\n"
] ]
}, },
{ {

10
src/HtmlUtils.py Normal file
View File

@@ -0,0 +1,10 @@
def getCountryOptions(countries):
return ['<option value="Global" selected>Global</option>'] + _getCountryOptions(countries)
def _getCountryOptions(countries):
return [_getCountryOption(country) for country in countries]
def _getCountryOption(country):
return '<option value="{country}">{country}</option>'.format(country=country)