Skip to content

Commit 7631fb7

Browse files
committed
\!value implementation in yaml_parse - see lisa-lab#1453
1 parent f21cea5 commit 7631fb7

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

pylearn2/config/yaml_parse.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ def initialize():
339339
yaml.add_multi_constructor('!obj:', multi_constructor_obj)
340340
yaml.add_multi_constructor('!pkl:', multi_constructor_pkl)
341341
yaml.add_multi_constructor('!import:', multi_constructor_import)
342+
yaml.add_multi_constructor('!value:', multi_constructor_value)
342343

343344
yaml.add_constructor('!import', constructor_import)
344345
yaml.add_constructor("!float", constructor_float)
@@ -406,6 +407,41 @@ def multi_constructor_import(loader, tag_suffix, node):
406407
raise yaml.YAMLError("!import: tag suffix contains no '.'")
407408
return try_to_import(tag_suffix)
408409

410+
def multi_constructor_value(loader, tag_suffix, node):
411+
"""
412+
Callback used by PyYAML when a "!value:" tag is encountered.
413+
"""
414+
if tag_suffix != "" and tag_suffix != u"":
415+
raise AssertionError('Expected tag_suffix to be "" but it is "'
416+
+ tag_suffix +
417+
'": Put space between !value: and the filename.')
418+
419+
assert node.tag == '!value:'
420+
421+
mapping = loader.construct_mapping(node)
422+
for key in mapping.keys():
423+
if not isinstance(key, six.string_types):
424+
message = "Received non string object (%s) as " \
425+
"key in mapping." % str(key)
426+
raise TypeError(message)
427+
428+
obj_to_call = _instantiate(mapping['obj_to_call'])
429+
func = getattr(obj_to_call, mapping['method_to_call'])
430+
if 'arguments' in mapping:
431+
arguments = mapping['arguments']
432+
else:
433+
arguments = None
434+
435+
if hasattr(func, '__call__'):
436+
if arguments == None:
437+
result = func()
438+
else:
439+
result = func(arguments)
440+
else:
441+
result = func
442+
443+
return result
444+
409445

410446
def constructor_import(loader, node):
411447
"""

0 commit comments

Comments
 (0)