Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
eat_value(deque) solution in Clear category for YAML. Complex Structure by flpo
from collections import deque
const = {'': None, 'null': None, 'true': True, 'false': False}
def eat_value(lines):
def convert(value):
if not value and lines and lines[0].startswith(' '):
nested = deque()
while lines and lines[0].startswith(' '):
nested.append(lines.popleft()[2:])
return eat_value(nested)
if value in const:
return const[value]
if value[0] in '"\'':
return value[1:-1].encode('utf-8').decode('unicode_escape')
value = value.split('#')[0].strip()
return int(value) if value.isdigit() else value
def it():
while lines: yield lines.popleft()
if lines[0].startswith('-'):
return [convert(line[2:]) for line in it()]
return {k: convert(v.strip()) for k, v in (l.split(':') for l in it())}
def yaml(s):
return eat_value(deque(l for l in s.splitlines() if l and not l.strip().startswith('#')))
Nov. 18, 2019