Module:Tableau rang commune de France

Une page de Wikipédia, l'encyclopédie libre.

 Documentation[créer] [purger]
local p = {}

function p.donneesRang( frame )
    local annee = frame.args[1]
    local codeCommune = frame.args[2]

    local premierChiffre = codeCommune:sub( 1, 1 )
    local subdataTitle = 'Module:Données Rang' .. annee .. '/préfixe ' .. premierChiffre

    if mw.title.new( subdataTitle ).exists then
        local restoreLoaded = package.loaded[subdataTitle] -- should be nil, unless it had been "require"d elsewhere
        local subdata = require( subdataTitle )
        local rangCommune
        if tonumber( codeCommune ) then
            rangCommune = subdata[tonumber( codeCommune )]
        else
            rangCommune = subdata[codeCommune]
        end
        package.loaded[subdataTitle] = restoreLoaded -- release data from memory
        return rangCommune or ''
    else
        local alldata = require( 'Module:Données Rang' .. annee )
        return alldata.parse{ args = { codeCommune } }
    end
end

function p.verify( frame )
    local annee = frame.args[1]

    local alldata = require( 'Module:Données Rang' .. annee ).parse{ args = { 'all' } }

    local subdatas = {}
    for i = 0, 9 do
        local subdataTitle = 'Module:Données Rang' .. annee .. '/préfixe ' .. i
        if mw.title.new( subdataTitle ).exists then
            subdatas[i] = mw.loadData( subdataTitle )
        end
    end

    local ok = 0
    local fail = 0
    local failReport = {}
    local miss = 0

    for k, v in pairs( alldata ) do
        local code = tostring( k )
        if code:len() == 4 then
            code = '0' .. code
        end
        local premierChiffre = tonumber( code:sub( 1, 1 ) )

        if subdatas[premierChiffre] then
            if v == subdatas[premierChiffre][k] then
                ok = ok + 1
            else
                fail = fail + 1
                failReport[#failReport+1] = tostring( k ) .. '-' .. tostring( v ) .. '-' .. tostring( subdatas[premierChiffre][k] )
            end
        else
            miss = miss + 1
        end
    end

    return 'ok: ' .. tostring( ok ) .. ' // fail: ' .. tostring( fail ) .. ( #failReport > 0 and ' (' .. table.concat( failReport, ' / ' ) .. ')' or '' ) .. ' // miss: ' .. tostring( miss )
end

return p