Parameters to and from YAML

Requires the python pyyaml library.

sample

A sample of using argparse with ecto.

#!/usr/bin/env python
from ecto import Constant
from ecto.ecto_test import Multiply
from ecto.opts import CellYamlFactory
import yaml
import os

m_factory = CellYamlFactory(Multiply, 'mult')

#write to file
tmp_name = os.tmpnam()
with open(tmp_name,'w') as f:
    m_factory.dump(f)

#to string
print '#some yaml\n',m_factory.dump() 

#read from file
parsed = {}
with open(tmp_name,'r') as f:
    parsed = yaml.load(f)
os.remove(tmp_name)

#create an instance from a parsed yaml file
m = m_factory.load(parsed,cell_name='my_mult_instance')
print m, m.name(), m.params.factor

Running gives us:

/home/vrabaud/workspace/recognition_kitchen/src/ecto/doc/kitchen/ecto/usage/advanced/sample_yaml.py:11: RuntimeWarning: tmpnam is a potential security risk to your program
  tmp_name = os.tmpnam()
No module named PySide.QtCore
#some yaml
mult:
  factor: 3.14
  factor__doc__: A factor to multiply by.

<ecto.ecto_test.Multiply object at 0x7fac60c22db8> my_mult_instance 3.14
class ecto.opts.CellYamlFactory(CellOrCellType, prefix)[source]

A factory to go between cells and YAML files

Parameters:
  • CellOrCellType (Cell Class, or Cell Instance) – The prototype to base the factory off of.
  • prefix (str) – The top level key for the parameters dict for this factory
dump(stream=None)[source]

Dump YAML for this factory.

Parameters:stream – A stream like object to print the YAML to.
Returns:The string YAML representation of the prototype used to initialize the factory.
load(parsed, cell_name='')[source]

Create a cell from a parsed bit of YAML.

Parameters:
  • parsed – A dictionary that has been parsed from a YAML file.
  • cell_name – will be used as the cell’s instance name.
Returns:

A brand new instance of a cell, modeled after whatever is in the YAML