[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: the equivalent of python's "try import <something> except: import fallback" ?
- From: Rici Lake <lua@...>
- Date: Fri, 16 Feb 2007 23:13:01 -0500
On 16-Feb-07, at 9:29 PM, gary ng wrote:
Hi,
I am writing a generic module which I want to use in
slightly different config and want to do the following
python equivalent :
try
import "lcrypto"
md5 = lcrypto.md5
except:
import "my_md5"
md5 = my_md5.md5
What about:
local md5 = (pcall(require, "lcrypto") and lcrypto
or require"my_md5" and my_md5)
.md5
Or, if the modules actually returned themselves (as imho modules
should):
function prefer(modname)
local ok, mod = pcall(require, modname)
return ok and mod
end
local md5 = (prefer"lcrypto" or require"my_md5").md5