Skip to content

Instantly share code, notes, and snippets.

@deltheil
Created November 12, 2015 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deltheil/e1be741aee7b26a3da02 to your computer and use it in GitHub Desktop.
Save deltheil/e1be741aee7b26a3da02 to your computer and use it in GitHub Desktop.
Playing with torch/argcheck
require 'torch'
local argcheck = require 'argcheck'
local env = require 'argcheck.env'
function env.istype(obj, typename)
if typename == 'torch.Tensor' then
-- could also check the storage type!
return torch.isTensor(obj)
else
return type(obj) == typename
end
end
local TYPES = {
byte = true,
float = true,
double = true,
}
local valid = function(x)
return TYPES[x]
end
-- dst, filename, depth, tensortype
local check = argcheck{
{name="dst", type="torch.Tensor", opt=true },
{name="filename", type="string" },
{name="depth", type="number", opt=true },
{name="tensortype", type="string", opt=true, check=valid}
}
do
local a, b, c, d = check("foo.jpg")
print(a, b, c, d)
end
print("\n\n")
do
local a, b, c, d = check("foo.jpg", 3)
print(a, b, c, d)
end
print("\n\n")
do
local a, b, c, d = check("foo.jpg", 3, "byte")
print(a, b, c, d)
end
print("\n\n")
do
local buf = torch.Tensor()
local a, b, c, d = check(buf, "foo.jpg")
print(a, b, c, d)
end
print("\n\n")
do
local buf = torch.Tensor()
local a, b, c, d = check(buf, "foo.jpg", 3)
print(a, b, c, d)
end
print("\n\n")
do
local buf = torch.Tensor()
local a, b, c, d = check(buf, "foo.jpg", 3, "byte")
print(a, b, c, d)
if a and d then
print('redundant params: should error')
end
end
print("\n\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment