Jump to content
Wikipedia The Free Encyclopedia

Module:Sandbox/Jts1882/Covid

From Wikipedia, the free encyclopedia
Module documentation[create] [purge]
You might want to create a documentation page for this Scribunto module.
Editors can experiment in this module's sandbox (create | mirror) and testcases (create) pages.
Add categories to the /doc subpage. Subpages of this module.
 require('strict')
 localp={}
 localDate=require('Module:Date')._Date
 localmm=require('Module:Math')

 locallist=require('Module:Sandbox/Jts1882/Covid/data')
 locallist2={
 {"Austria","Q86847911","Q40"},
 {"Belgium","Q84446340","Q31",},
 {"Bahrain","Q87070999","Q398"},
 {"Bangladesh","Q87540454","Q902"},
 {"Czech Republic","Q86919406","Q213"},
 --	{ "Poland", "Q87250695", "Q36" },
 {"Spain","Q84166704","Q29"},

 }

 p.main=function(frame)
 localoutput="{|\n"
 localgetData=false
 localoutput='{| class="wikitable sortable" \n'
 .."! Country !! Cases !! Deaths !! Population !! Cases/million pop !! Deaths/million pop "
 fork,vinpairs(list)do
 -- code for getting population data in blocks
 --if v[1] == "Afghanistan" then getData = true end -- start from country X
 --		if v[1] == "Germany" then break end -- end with country Y
 getData=true
 ifgetDatathen
 localqid=v[2]-- "Q84446340" -- Belgium

 --output = output .. v[1] .. ": " .. p.getWikidata(v) .. "<br/>"
 output=output..p.getWikidata(v)

 -- line to get population from wikidata in blocks
 --output = output .. '{ "' .. v[1] .. '", "' .. v[2] .. '", "' .. v[3] .. '", ' .. p.getWikidataPopulation(v[3]).. ' },<br/>'
 end
 end
 output=output.."\n|}"
 returnoutput
 end
 p.getWikidata=function(v)

 localcountryName=v[1]
 localcovidQid=v[2]
 localcountryQid=v[3]
 localpopulation=v[4]

 localvalue="EMPTY"
 localitem

 localwd={}
 localcovidCases
 localcovidDeaths
 localdateCases
 localdateDeaths

 if1==1then
 ifcovidQidandmw.wikibase.isValidEntityId(covidQid)then-- valid id

 item=mw.wikibase.getEntity(covidQid)
 ifnotitemthenreturn"Covid Qid not found"end


 locali=1
 -- get cases 
 whileitem:getBestStatements('P1603')[i]do
 localtimeStamp
 localstatements=item:getBestStatements('P1603')[i]
 covidCases=statements.mainsnak.datavalue.value.amount
 ifstatements.qualifiersandstatements.qualifiers['P585']andstatements.qualifiers['P585'][1]['datavalue']then
 timeStamp=statements.qualifiers['P585'][1]['datavalue']['value']['time']--point in time
 end
 --output = covidCases

 dateCases="missing"
 iftimeStampthen
 dateCases=Date(timeStamp):text("dmy")or"missing"
 end
 i=i+1
 end
 --output = (covidCases or "n/a") ..' (' .. (dateCases or "missing date") .. ')' 

 -- get deaths
 i=1
 whileitem:getBestStatements('P1120')[i]do
 localtimeStamp
 localstatements=item:getBestStatements('P1120')[i]
 covidDeaths=statements.mainsnak.datavalue.value.amount
 ifstatements.qualifiersandstatements.qualifiers['P585']andstatements.qualifiers['P585'][1]['datavalue']then
 timeStamp=statements.qualifiers['P585'][1]['datavalue']['value']['time']--point in time
 end


 iftimeStampthen
 dateDeaths=Date(timeStamp):text("dmy")or"missing"
 end
 i=i+1
 end
 --output = output .. " | " .. (covidDeaths or "n/a") .. ' (' .. (dateCases or "missing date") .. ')'
 end
 end
 if1==2then
 localstatements-- don't want a lot of large local objects
 ifcountryQidandmw.wikibase.isValidEntityId(countryQid)then-- valid id

 --item = mw.wikibase.getEntity(countryQid)
 --if not item then return "Country Qid not found" end 

 -- get population 
 locali=1
 statements=nil
 statements=mw.wikibase.getBestStatements(countryQid,'P1082')
 i=1--#statements 

 whilestatements[i]do
 --while item:getBestStatements('P1082')[i] do
 localtimeStamp
 localstatement=statements[i]--item:getBestStatements('P1082')[i]
 population=statement.mainsnak.datavalue.value.amount
 ifstatement.qualifiersandstatement.qualifiers['P585']andstatement.qualifiers['P585'][1]['datavalue']then
 timeStamp=statement.qualifiers['P585'][1]['datavalue']['value']['time']--point in time
 end

 datePopulation="missing"
 iftimeStampthen
 datePopulation=Date(timeStamp):text("dmy")or"missing"
 end
 i=i+1
 end
 end
 end

 --[[ 
  local output = "Cases: " .. (covidCases or "n/a") .. ' (' .. (dateCases or "missing date") .. ')' 
  .. ". Deaths: " .. (covidDeaths or "n/a") .. ' (' .. (dateDeaths or "missing date") .. ')'
  .. ". Population: " .. (population or "n/a") -- .. ' (' .. (datePopulation or "missing date") .. ')' 
  ]]


 localoutput='\n|-\n|'..countryName
 ..'||'..(covidCasesor"n/a")..' ('..(dateCasesor"missing date")..')'
 ..'||'..(covidDeathsor"n/a")..' ('..(dateDeathsor"missing date")..')'
 ..'||'..(populationor"n/a")

 ifcovidCases==nilthencovidCases=0.001end
 ifcovidDeaths==nilthencovidDeaths=0.001end

 localoutput=output
 ..'||'..mm._precision_format(covidCases/population*1000000,0)
 ..'||'..mm._precision_format(covidDeaths/population*1000000,0)





 returnoutputor"no value found"

 end
 p.getWikidataPopulation=function(countryQid)

 localpopulation
 localdatePopulation
 localstatements-- don't want a lot of large local objects
 ifcountryQidandmw.wikibase.isValidEntityId(countryQid)then-- valid id

 --item = mw.wikibase.getEntity(countryQid)
 --if not item then return "Country Qid not found" end 

 -- get population 
 locali=1
 statements=nil
 statements=mw.wikibase.getBestStatements(countryQid,'P1082')
 i=1--#statements 

 whilestatements[i]do
 --while item:getBestStatements('P1082')[i] do
 localtimeStamp
 localstatement=statements[i]--item:getBestStatements('P1082')[i]
 population=statement.mainsnak.datavalue.value.amount
 ifstatement.qualifiersandstatement.qualifiers['P585']andstatement.qualifiers['P585'][1]['datavalue']then
 timeStamp=statement.qualifiers['P585'][1]['datavalue']['value']['time']--point in time
 end

 datePopulation="missing"
 iftimeStampthen
 datePopulation=Date(timeStamp):text("dmy")or"missing"
 end
 i=i+1
 end
 end
 return(populationor"n/a")

 end

 returnp

AltStyle によって変換されたページ (->オリジナル) /