Icona de documentació de mòdul Documentació del mòdul [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]

Mòdul SimpleDebug (codi · ús · discussió · proves · tests · casos prova | subpàgines · enllaços)

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Conté funcions per ajudar a depurar els mòduls lua. Permet recollir i visualitzar els valors de diverses variables i/o punts del vostre programa lua, des d'un mòdul (que és l'habitual) o en diversos mòduls (que són requerits des del mòdul principal).

Està dissenyat perquè les seves funcions siguin cridades des de dins del mòdul que es vol depurar, crides que hauran de formar part del codi (del mòdul que heu dissenyat, o que vol millorar o adaptar) fins que decidiu eliminar-les (quan ja hàgiu determinat l'error). Així, no s'ha de cridar cap de les seves funcions amb un invoke.

Usos modifica

Un o diversos punts a veure
Abreviacions de les funcions: w: where (on). n: names (noms). v: variables. s: string (cadena).
Variables
Nom Per defecte
tab.oneline true
  • Si és false o és true i conté taules aniuades, mostrarà una línia per cada element de la taula i amb un sagnat per a cada taula aniuada.
  • Si és true i no conté taules aniuades, mostra la taula en una línia.
tab.allidx false

Si és true, també mostra els índexs numèrics d'una taula.

dec -1

Espais pels decimals:

  • -1: mostra tots els decimals necessaris.
  • 0: sense decimals.
  • n: 1: un decimal, 2: dos decimals, etc,
enabled true

Si és false, totes les crides a les funcions següents no fan res.

nohtml false

Substitueix < per ⪡ i > per ⪢ a les cadenes.

plaintext false

Elimina el format html.

Un punt a veure
Funcions
w (where)
  • where: etiqueta del punt.
v (...)
  • ...: un seguit de variables = var1, var2...
wv (where, ...)
  • where: etiqueta del punt.
  • ...: un seguit de variables = var1, var2...
nv (...)
  • ...: un seguit de parells de nom-variable = name1, var1, name2, var2....
wnv (where, ...)
  • where: etiqueta del punt.
  • ...: un seguit de parells de nom-variable = name1, var1, name2, var2....
Diversos punts a veure
Variables
Nom Per defecte
s

La variable de cadena que conté els valors retornats de les funcions següents.

maxlines.num 100

Nombre màxim de línies (en cridar a les funcions següents).

maxlines.doerror true

Si és veritat i s’arriba a maxlines.num, llavors es crida error(s).

counter false

Afegeix un número autoincremental al principi de cada utilització d'una funció.

Funcions
breakline ()

Afegeix un retorn de línia a s.

wtos (where)

Igual a where, però la cadena retornada es guarda a s.

vtos (...)

Igual a v, però la cadena retornada es guarda a s.

wvtos (where, ...)

Igual a wv, però la cadena retornada es guarda a s.

nvtos (...)

Igual a nv, però la cadena retornada es guarda a s.

wnvtos (where, ...)

Igual a wnv, però la cadena retornada es guarda a s.

Exemples modifica

Un punt a veure modifica

Seguint el flux modifica

local SD = require "Module:SimpleDebug"
return SD.w ("S'ha passat per aquí")

retorna:

S'ha passat per aquí


Nombre de decimals i valor d'una variable modifica

local SD = require "Module:SimpleDebug"
SD.dec = 2
return SD.v (1/3)

retorna:

0.33


Nohtml modifica

local SD = require "Module:SimpleDebug"
SD.nohtml = true
return SD.v ("<b>bold</b>")

retorna:

"⪡b⪢bold⪡/b⪢"


Plaintext modifica

local SD = require "Module:SimpleDebug"
SD.plaintext = true
return SD.v ("<b>bold</b>")

retorna:

"bold"


El valor de diverses variables modifica

local SD = require "Module:SimpleDebug"
local a = 12
local b = 'Hola'
return SD.v (a,b)

retorna:

12  •  "Hola"


Detecció de variable no assignada modifica

local SD = require "Module:SimpleDebug"
local a = true
return SD.v (a,b)

retorna:

true  •  nil


El valor d'una taula modifica

local SD = require "Module:SimpleDebug"
local a = {1, tab='a', 'b'}
return SD.v (a)

retorna: { 1, "b", [tab]="a", }


local SD = require "Module:SimpleDebug"
local a = {{1,2,3},{4,5,6},{7,8,9}}
return SD.v (a)

retorna:

{
  [1] = {1, 2, 3, },
  [2] = {4, 5, 6, },
  [3] = {7, 8, 9, },
} 


local SD = require "Module:SimpleDebug"
local a = {{Primer=1,2,3},{4,Segon=5,6},{7,8,9}}
return SD.v (a)

retorna:

{ 
 [1] = {2, 3, [Primer]=1, },
 [2] = {4, 6, [Segon]=5, },
 [3] = {7, 8, 9, },
}


local SD = require "Module:SimpleDebug"
SD.tab.allidx = true
local a = {{1,2,3},{4,nil,6},{7,8,9}}
return SD.v (a)

retorna:

{ 
  [1]={[1]=1, [2]=2, [3]=3, }, 
  [2]={[1]=4, [3]=6, }, 
  [3]={[1]=7, [2]=8, [3]=9, }, 
}


Normalment, implementareu aquestes funcions amb funció d'error:

local SD = require "Module:SimpleDebug"
local a = {{1,2,3},{4,5,6},{7,8,9}}
error (SD.v (a))

mostra:

Error de Lua: Mòdul:VostreMòdul:Línia: {

  [1] = {1, 2, 3, },
  [2] = {4, 5, 6, },
  [3] = {7, 8, 9, },
}

El valor d'una taula, tota en multilínia modifica

local SD = require "Module:SimpleDebug"
SD.tab.oneline = false
local a = {{Primer=1,2,3},'Enmig',{4,Segon=5,6}}
return SD.v (a)

retorna:

{
 [1] = {
     [1] = 2,
     [2] = 3,
     ["Primer"] = 1,
   },
 [2] = "Enmig",
 [3] = {
     [1] = 4,
     [2] = 6,
     ["Segon"] = 5,
   },
} 


El valor de diverses variables amb seu nom en un punt modifica

local SD = require "Module:SimpleDebug"
local a = 12
local b = 'Hola'
return SD.nv ('a',a,'b',b)

retorna:

a: 12  •  b: "Hola"


Diversos punts a veure modifica

Seguint el flux modifica

local SD = require "Module:SimpleDebug" 
local tab = {1,12,7}
function p.CheckValues ()
  local function LittleNum()
    SD.wtos ('número petit')
  end
  local function BigNum(num)
    SD.wtos ('gran='..num)
  end
  for i, num in ipairs(tab) do
    if num > 9 then
      BigNum(num)
    else
      LittleNum()
    end  
  end  
  error (SD.s)
end

retorna:

Error de Lua: Mòdul:VostreMòdul:Línia:

número petit

gran=12

número petit.


Amb comptador modifica

local SD = require "Module:SimpleDebug" 
function Increm()
  local n = 0
  for i = 1, 3 do
    n = n + 2
    SD.vtos (n)
  end
end
SD.counter = true
Increm()
return SD.s

retorna:

1  •  2

2  •  4

3  •  6


Seguiment de diverses variables modifica

local SD = require "Module:SimpleDebug"
a = 12
b = 'Hola'
SD.wvtos (1,a,b)
a = a + a
b = b..' món!'
SD.wvtos ('Finalment',a,b)
return SD.s

retorna:

1 => 12  •  "Hola"

Finalment => 24  •  "Hola món!"


local SD = require "Module:SimpleDebug"
SD.breakline ()
a = 12
b = 'Hola'
c = false
SD.wnvtos (1, 'a',a,'b',b,'c',c)
a = a + a
b = b..' món!'
SD.wnvtos ('Finalment', 'a',a,'b',b)
error (SD.s)

mostra:

Error de Lua: Mòdul:VostreMòdul:Línia:

1 => a: 12  •  b: "Hola"  •  c: false

Finalment => a: 24  •  b: "Hola món!"


Variables i la seva presentació amb condicions modifica

local SD = require "Module:SimpleDebug"
SD.breakline()
SD.enabled = false
SD.maxlines.num = 3
local a = 'AA'
for i = 1, 10 do
  a = a + 'AA'
  if i == 3 then
    SD.enabled = true
  end
  SD.wvtos (i, string.len(a), a)
end

mostra:

Error de Lua: Mòdul:VostreMòdul:Línia:

3 => 8  •  "AAAAAAAA"

4 => 10  •  "AAAAAAAAAA"

5 => 12  •  "AAAAAAAAAAAA".

--2020-06-16 fix error when vtos(nil), then it showed two nil
--2020-06-08 if a variable is a function now is displayed as function (before "function")
--2020-06-06 fix error which occasionally happens when a value == nil
local p = {}

p.s = ''
p.tab = {
	oneline = true,
	allidx = false, 
	}
p.dec = -1
p.maxlines = {
	num = 100,
	doerror = true,
	}
p.enabled = true
p.nowiki = false
p.nohtml = false
p._plaintext = false
p.counter = false

local LinCount = 0
local vep = '  •  '
local function MessRaised (n)
	return '\n\nIt has been reached to '..n..', you can change this limit with "maxlines.num".'
end	
local function arrow()
	return ' => '
end

function p.breakline ()
	LinCount = LinCount + 1
	p.s = p.s..'\n\n'
	if p.counter then
		p.s = p.s..LinCount..vep
	end	
	if (LinCount > p.maxlines.num) and p.maxlines.doerror then
		p.pa = p.s..MessRaised(p.maxlines.num)
		error (p.s,0)
	end	
end	--breakline

local function CheckWhereName (wn, what)
	if wn == nil then
		return '"'..what..'" == nil'
	elseif (type(wn) == "table") then
		return 'Table as "'..what..'"!'
	else
		return wn	
	end	
end --CheckWhereName

function p._plain (text) --Modified from "Module:Plain text"
	if not text then return end
	text = mw.text.killMarkers(text)
		:gsub('&nbsp;', ' ') --replace nbsp spaces with regular spaces
		:gsub('<br ?/?>', ', ') --replace br with commas
		:gsub('<span.->(.-)</span>', '%1') --remove spans while keeping text inside
		:gsub('<b>(.-)</b>', '%1') --remove bold while keeping text inside
		:gsub('<i>(.-)</i>', '%1') --remove italic while keeping text inside
		:gsub('<sub>(.-)</sub>', '%1') --remove bold while keeping text inside
		:gsub('<sup>(.-)</sup>', '%1') --remove bold while keeping text inside
		:gsub('<.->.-<.->', '') --strip out remaining tags and the text inside
		:gsub('<.->', '') --remove any other tag markup
		:gsub('%[%[%s*[Ff]ile%s*:.-%]%]', '') --strip out files
		:gsub('%[%[%s*[Ii]mage%s*:.-%]%]', '') --strip out use of image:
		:gsub('%[%[%s*[Cc]ategory%s*:.-%]%]', '') --strip out categories
		:gsub('%[%[[^%]]-|', '') --strip out piped link text
		:gsub('[%[%]]', '') --then strip out remaining [ and ]
		:gsub("'''''", "") --strip out bold italic markup
		:gsub("'''?", "") --not stripping out '''' gives correct output for bolded text in quotes
		:gsub('----', '') --remove ---- lines
		:gsub("^%s+", "") --strip leading
		:gsub("%s+$", "") --and trailing spaces
		:gsub("%s+", " ") --strip redundant spaces
	return text
end --plain

function p._plain_len (text)
	return mw.ustring.len (p._plain(text))
end	
	
function p.plain (frame)
	return p._plain (frame.args[1])
end

function p.plain_len (frame)
	return p._plain_len (frame.args[1])
end

local function totext (text)
	if p._plaintext then
		return p._plain (text)
	else
		return text
	end	
end --totext

local function NumToStr (N)		
	if (p.dec == -1) or (N == math.floor(N)) then
		return tostring(N)
	else
		return tostring (math.floor ((N*10^p.dec)+0.5) / (10^p.dec))
	end	
end	--NumToStr

local iniTab1Line = true
function p.containsTab (avar)
	result = false
	for k,v in pairs(avar) do
		if type(v) == 'table' then
			result = true
			break
		end	
	end
	return result
end --containsTab

local var
	
local function DumTab (tbl, indent)
	if not indent then indent = 1 end
	local toprint = " {\r\n"
	indent = indent + 2 
	for k, v in pairs(tbl) do
		toprint = toprint..string.rep(" ", indent)
		local id = k
		if (type(k) == "string") then
			k = '"'..k..'"'
		end
		toprint = toprint.."["..k.."] = "
		if (type(v) == "number") then
			toprint = toprint..NumToStr(v)..",\r\n"
		elseif (type(v) == "string") then
			toprint = toprint.."\""..totext(v).."\",\r\n"
		elseif (type(v) == "table") then
			if iniTab1Line and (not p.containsTab (v)) then
				local wds = '{'
				for kk,vv in pairs(v) do
					if (p.tab.allidx == true) or (type(kk) ~= 'number')  then 
						wds = wds..'['..kk..']='..var(vv)..', '
					else	
						wds = wds..var(vv)..', '
					end
				end
				toprint = toprint..wds.."},\r\n"
			else	
				toprint = toprint..DumTab(v, indent + 2)..",\r\n"
			end
		else
			toprint = toprint.."\""..tostring(v).."\",\r\n"
		end
	end
	toprint = toprint..string.rep(" ", indent-2).."}"
	return toprint
end --DumTab

function var (avar)
	local EndStr = ''
	if avar == nil then	
		EndStr = 'nil'
	elseif type(avar) == 'table' then
		if #avar > 0 then
			p.s = p.s..'\r\n'
		end	
		if p.tab.oneline then
			local wds = '{ '
			for k,v in pairs(avar) do
				if (p.tab.allidx == true) or (type(k) ~= 'number')  then 
					wds = wds..'['..k..']='..var(v)..', '
				else	
					wds = wds..var(v)..', '
				end
			end
			EndStr = wds .. '} '
		else
			EndStr = DumTab (avar)
		end	
	elseif type(avar) == 'number' then
		EndStr = NumToStr (avar)
	elseif type(avar) == 'boolean' then
		if avar == true then
			EndStr = 'true'
		else
			EndStr = 'false'
		end	
	elseif type(avar) == 'function' then	
		EndStr = 'function'
	else
		avar = totext (tostring(avar))
		if p.nohtml then
			avar = string.gsub (avar, "<", "⪡")
			avar = string.gsub (avar, ">", "⪢")
		end	
		EndStr = '"'..avar..'"'
	end
	return EndStr
end --var

function p.w (where)
	if p.enabled then
		return CheckWhereName (where, 'w')
	end	
end --w

local function varx (avar)
	iniTab1Line = p.tab.oneline
	if p.tab.oneline and (type(avar) == 'table') then
		p.tab.oneline = not p.containsTab(avar)
	end
	local ss = var(avar)
	p.tab.oneline = iniTab1Line
	return ss
end --varx

function p.v (...)
	if p.enabled then
		local str = ''
		if #arg == 0 then
			str = 'nil'
		else
			local c = 0
			for k, i in ipairs(arg) do
				c = k
			end	
			--error (c)
			for i = 1, #arg do
				if str ~= '' then
					str = str..vep
				end	
				str = str..varx(arg[i])
			end	
		end	
		return str
	end	
end	--v

function p.wv (where, ...)
	if p.enabled then
		return CheckWhereName(where,'w')..arrow()..p.v(unpack(arg))
	end	
end	--wv

function p.nv (...)
	if p.enabled then
		if math.mod(#arg,2) ~= 0 then
			EndStr = 'Any parameter has not a name or variable'
		else
			local s = ''
			local IsName = true
			function Concat(wds)
				if s ~= '' then
					if IsName then
						s = s..vep
					else	
						s = s..': '
					end	
				end
				s = s..wds
			end
			for i = 1, #arg do
				if IsName then
					Concat (CheckWhereName(arg[i],'n'))
					IsName = false
				else	
					Concat (varx(arg[i]))
					IsName = true
				end	
			end
			EndStr = s
		end
		return EndStr
	end	
end --nv

function p.wnv (where, ...)
	if p.enabled then
		return CheckWhereName(where,'w')..arrow()..p.nv (unpack(arg))
	end	
end

----------

local function EnabAndBl ()
	if p.enabled then
		if LinCount < p.maxlines.num then
			p.breakline ()
			return true
		else
			p.s = p.s..MessRaised(p.maxlines.num)
			error (p.s)
			return false
		end	
	else
		return false
	end
end --EnabAndBl

function p.wtos (where)
	if EnabAndBl () then
		p.s = p.s..p.w (where)
	end	
end --wtos

function p.vtos (...)
	if EnabAndBl () then
		local end_nil_count = arg["n"] - #arg
		p.s = p.s..p.v (unpack(arg))
		if #arg == 0 then
			end_nil_count = end_nil_count-1
		end	
		for i = 1, end_nil_count do
			p.s = p.s..vep..'nil'
		end	
	end	
end --vtos

function p.wvtos (where, ...)
	if EnabAndBl () then
		p.s = p.s..p.wv (where,unpack(arg))
	end	
end --wvtos

function p.nvtos (...)
	if EnabAndBl () then
		local end_nil_count = arg["n"] - #arg
		if end_nil_count > 0 then
			for i = 1, arg["n"] do
				if math.mod(i,2) ~= 0 then
					p.s = p.s..arg[i]..': '
				else
					p.s = p.s..p.v(arg[i])
					if i < arg["n"] then
						p.s = p.s..vep
					end	
				end	
			end	
		else
			p.s = p.s..p.nv (unpack(arg))
		end	
	end	
end --nvtos

function p.wnvtos (where, ...)
	if EnabAndBl () then
		local end_nil_count = arg["n"] - #arg
		if end_nil_count > 0 then
			p.s = p.s..where..arrow()
			for i = 1, arg["n"] do
				if math.mod(i,2) ~= 0 then
					p.s = p.s..arg[i]..': '
				else
					p.s = p.s..p.v(arg[i])
					if i < arg["n"] then
						p.s = p.s..vep
					end	
				end	
			end	
		else
			p.s = p.s..p.wnv (where, unpack(arg))
		end	
	end	
end --wnvtos

return p