pwtools.batch.FileTemplate

class pwtools.batch.FileTemplate(basename=None, txt=None, keys=None, templ_dir='calc.templ', filename=None, func=<function FileTemplate.<lambda>>)[source]

Bases: object

Class to represent a template file in parameter studies.

Each template file is supposed to contain a number of placeholder strings (e.g. XXXFOO or @foo@ or whatever other form). The dct passed to self.write() is a dict which contains key-value pairs for replacement (e.g. {‘foo’: 1.0, ‘bar’: ‘say-what’}, keys=dct.keys()). Each key is converted to a placeholder by func.

We use common.template_replace(…, mode=’txt’). dict-style placeholders like “%(foo)s %(bar)i” will not work.

Examples

This will take a template file calc.templ/pw.in, replace the placeholders “@prefix@” and “@ecutwfc@” with some values and write the file to calc/0/pw.in .

Fist, set up a dictionary which maps placeholder to values. Remember, that the placeholders in the files will be obtained by processing the dictionary keys with func. In the example, this will be:

'prefix' -> '@prefix@'
'ecutwfc' -> '@ecutwfc@'
>>> dct = {}
>>> dct['prefix'] = 'foo_run_1'
>>> dct['ecutwfc'] = 23.0
>>>
>>> # Not specifying the `keys` agrument to FileTemplate will instruct the
>>> # write() method to replace all placeholders in the template file which
>>> # match the placeholders defined by dct.keys(). This is the most simple
>>> # case.
>>> #
>>> templ = FileTemplate(filename='calc.templ/pw.in',
...                      func=lambda x: "@%s@" %x)
>>> templ.write(dct, 'calc/0')
>>>
>>> # Now with `keys` explicitely.
>>> templ = FileTemplate(filename='calc.templ/pw.in',
...                      keys=['prefix', 'ecutwfc'],
...                      func=lambda x: "@%s@" %x)
>>> templ.write(dct, 'calc/0')
>>>
>>> # or with SQL foo in a parameter study
>>> from sql import SQLEntry
>>> dct = {}
>>> dct['prefix']  = SQLEntry(sqlval='foo_run_1')
>>> sct['ecutwfc'] = SQLEntry(sqlval=23.0)
>>> templ.writesql(dct, 'calc/0')
__init__(basename=None, txt=None, keys=None, templ_dir='calc.templ', filename=None, func=<function FileTemplate.<lambda>>)[source]
Parameters:
  • basename (string) –

    The name of the template file and target file.
    example: basename = pw.in
    template = calc.templ/pw.in
    target = calc/0/pw.in

  • txt (string, optional) – Text of the template file. If None, then we assume a file templ_dir/basename and read that.

  • keys ({None, list of strings, []}) –

    keys=None: All keys dct.keys() in self.write() are used. This is
    useful if you have a dict holding many keys, whose placeholders
    are spread across files. Then this will just replace every
    match in each file. This is what most people want.
    keys=[<key1>, <key2>, …] : Each string is a key. Each key is
    connected to a placeholder in the template. See func. This is
    for binding keys to template files, i.e. replace only these
    keys.
    keys=[]: The template file is simply copied to calc_dir (see
    self.write()).

  • templ_dir (dir where the template lives (e.g. 'calc.templ'))

  • filename (str) – full file name of the template, then templ_dir=os.path.dirname(filename) and basename=os.path.basename(filename)

  • func (callable) –

    A function which takes a string (key) and returns a string, which
    is the placeholder corresponding to that key.
    example: (this is actually default)
    key = “lala”
    placeholder = “XXXLALA”
    func = lambda x: “XXX” + x.upper()

Methods

write(dct[, calc_dir, mode])

Write file self.filename (e.g. calc/0/pw.in) by replacing placeholders in the template (e.g. calc.templ/pw.in).

writesql(sql_record[, calc_dir])