ctwrap Parser

The parser module defines a convenience object Parser, which supports dimensions via pint for easy access to data defined in YAML configuration files within ctwrap simulation modules.

Usage

The following illustration assumes configuration data stored using YAML syntax in a file config.yaml with content:

defaults:
  upstream:
    T: 300. kelvin # temperature
    P: 1. atmosphere # pressure
    phi: .55 # equivalence ratio
    fuel: H2
    oxidizer: O2:1,AR:5
  chemistry:
    mechanism: h2o2.yaml
  domain:
    width: 30 millimeter # domain width

A Parser object can be created via the class-method from.yaml(), which acts much like a dictionary that also includes access via attributes:

defaults = Parser.from_yaml('config.yaml')
keys = default.keys() # returns 'upstream', 'chemistry', 'domain'
upstream = defaults.upstream # Parser containing 'upstream'

If dimensions are defined, entries are accessible as pint.quantity objects:

defaults.upstream.P # returns "1.0 standard_atmosphere" (pint)
upstream.P # equivalent
upstream['P'] # equivalent
upstream.P.to('pascal') # returns "101325.0 pascal" (pint)
upstream.P.m_as('pascal') # returns 101325.0 (float)

If no dimensions are defined, entries are accessed as conventional dictionaries entries:

upstream.phi # returns 0.55 (float)
upstream.oxidizer # returns 'O2:1,AR:5' (str)

Underlying dictionaries and/or data are accessed via the raw attribute, i.e.

defaults.raw # returns dictionary corresponding to original YAML
defaults.raw['upstream']['P'] # returns '1. atmosphere' (str)
defaults.upstream.raw['P'] # equivalent
upstream.raw['P'] # equivalent
upstream.raw['oxidizer'] # returns 'O2:1,AR:5' (str)

Class Definition

class ctwrap.parser.Parser(raw)[source]

A lightweight class that handles units.

The handling mimics that of a python dictionary, while adding direct access to keyed values via attributes.

Parameters

raw (Dict[str, Any]) – Dictionary to be parsed

keys()[source]

Returns list of keys

Return type

KeysView[str]

values()[source]

Return parser values

items()[source]

Return parser items

classmethod from_yaml(yml, defaults=False, path=None, keys=None)[source]

Load parser from YAML

Parameters
  • yml (str) – File name or YAML string

  • defaults (Optional[bool]) – If True, load from ctwrap.defaults database

  • path (Optional[str]) – Relative/absolute path

  • keys (Optional[str]) – List of keys

Return type

Parser

to_yaml()[source]

Convert Parser content to YAML string

get(key, default=None)[source]

Get key

update(new)[source]

Update Parser object (recursive)

Parameters

new (Union[Dict, Parser]) – Object containing replacement values