提交 cdaaf137 authored 作者: lamblin's avatar lamblin

Merge pull request #1232 from nouiz/lazy_config_default_value

Lazy config default value
...@@ -126,7 +126,7 @@ def _config_print(thing, buf): ...@@ -126,7 +126,7 @@ def _config_print(thing, buf):
for cv in _config_var_list: for cv in _config_var_list:
print >> buf, cv print >> buf, cv
print >> buf, " Doc: ", cv.doc print >> buf, " Doc: ", cv.doc
print >> buf, " Value: ", cv.val print >> buf, " Value: ", cv.__get__()
print >> buf, "" print >> buf, ""
...@@ -141,7 +141,7 @@ def get_config_md5(): ...@@ -141,7 +141,7 @@ def get_config_md5():
all_opts = sorted([c for c in _config_var_list if c.in_c_key], all_opts = sorted([c for c in _config_var_list if c.in_c_key],
key=lambda cv: cv.fullname) key=lambda cv: cv.fullname)
return theano.gof.cc.hash_from_code('\n'.join( return theano.gof.cc.hash_from_code('\n'.join(
['%s = %s' % (cv.fullname, cv.val) for cv in all_opts])) ['%s = %s' % (cv.fullname, cv.__get__()) for cv in all_opts]))
class TheanoConfigParser(object): class TheanoConfigParser(object):
...@@ -226,8 +226,18 @@ def AddConfigVar(name, doc, configparam, root=config, in_c_key=True): ...@@ -226,8 +226,18 @@ def AddConfigVar(name, doc, configparam, root=config, in_c_key=True):
configparam.fullname) configparam.fullname)
configparam.doc = doc configparam.doc = doc
configparam.in_c_key = in_c_key configparam.in_c_key = in_c_key
# trigger a read of the value from config files and env vars # Trigger a read of the value from config files and env vars
configparam.__get__() # This allow to filter wrong value from the user.
if not callable(configparam.default):
configparam.__get__()
else:
# We do not want to evaluate now the default value when it is a callable.
try:
fetch_val_for_key(configparam.fullname)
# The user provided a value, filter it now.
configparam.__get__()
except KeyError:
pass
setattr(root.__class__, sections[0], configparam) setattr(root.__class__, sections[0], configparam)
_config_var_list.append(configparam) _config_var_list.append(configparam)
...@@ -253,12 +263,14 @@ class ConfigParam(object): ...@@ -253,12 +263,14 @@ class ConfigParam(object):
# invalid and causes a crash or has unwanted side effects. # invalid and causes a crash or has unwanted side effects.
def __get__(self, *args): def __get__(self, *args):
#print "GETTING PARAM", self.fullname, self, args
if not hasattr(self, 'val'): if not hasattr(self, 'val'):
try: try:
val_str = fetch_val_for_key(self.fullname) val_str = fetch_val_for_key(self.fullname)
except KeyError: except KeyError:
val_str = self.default if callable(self.default):
val_str = self.default()
else:
val_str = self.default
self.__set__(None, val_str) self.__set__(None, val_str)
#print "RVAL", self.val #print "RVAL", self.val
return self.val return self.val
......
...@@ -390,7 +390,7 @@ def default_blas_ldflags(): ...@@ -390,7 +390,7 @@ def default_blas_ldflags():
AddConfigVar('blas.ldflags', AddConfigVar('blas.ldflags',
"lib[s] to include for [Fortran] level-3 blas implementation", "lib[s] to include for [Fortran] level-3 blas implementation",
StrParam(default_blas_ldflags())) StrParam(default_blas_ldflags))
@utils.memoize @utils.memoize
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论