Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for YAML. More Types by DinRigtigeFar
def yaml(a):
nice = {}
no_new = [i.split(': ') for i in a.split('\n') if i]
if len(no_new) == 0:
raise('No input')
for i in no_new:
if len(i) == 1:
nice[i[0][:-1]] = None
else:
try:
nice[i[0]] = int(i[1])
except ValueError:
if i[1] in ('false','true'):
nice[i[0]] = i[1] == 'true'
elif i[1] == 'null':
nice[i[0]] = None
else:
nice[i[0]] = i[1].strip().replace('"','')
nice[i[0]] = nice[i[0]].replace('\\','\"')
return nice
if __name__ == '__main__':
""" print("Example:")
print(yaml('name: Alex\nage: 12'))
# These "asserts" are used for self-checking and not for an auto-testing
assert yaml('name: Alex\nage: 12') == {'age': 12, 'name': 'Alex'}
assert yaml('name: Alex Fox\n'
'age: 12\n'
'\n'
'class: 12b') == {'age': 12,
'class': '12b',
'name': 'Alex Fox'}
assert yaml('name: "Alex Fox"\n'
'age: 12\n'
'\n'
'class: 12b') == {'age': 12,
'class': '12b',
'name': 'Alex Fox'}
assert yaml('name: "Alex \\"Fox\\""\n'
'age: 12\n'
'\n'
'class: 12b') == {'age': 12,
'class': '12b',
'name': 'Alex "Fox"'}
assert yaml('name: "Bob Dylan"\n'
'children: 6\n'
'alive: false') == {'alive': False,
'children': 6,
'name': 'Bob Dylan'} """
assert yaml('name: "Bob Dylan"\n'
'children: 6\n'
'coding:') == {'children': 6,
'coding': None,
'name': 'Bob Dylan'}
assert yaml('name: "Bob Dylan"\n'
'children: 6\n'
'coding: null') == {'children': 6,
'coding': None,
'name': 'Bob Dylan'}
assert yaml('name: "Bob Dylan"\n'
'children: 6\n'
'coding: "null" ') == {'children': 6,
'coding': 'null',
'name': 'Bob Dylan'}
print("Coding complete? Click 'Check' to earn cool rewards!")
May 26, 2021