Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for YAML. More Types by cmuchar
# Taken from mission YAML. Simple Dict
def clean(a):
o = ''
status = ''
for i in a:
if status == 'backslash':
o += i
status = ''
else:
if i == '\\':
status = 'backslash'
elif i == '"':
status = ''
else:
status = ''
o += i
return o
def yaml(a):
d = {}
for i in a.split('\n'):
j = [x.strip() for x in i.split(':')]
if j[0]:
if j[1] == '':
d[j[0]] = None
elif j[1] == '"null"':
d[j[0]] = 'null'
elif j[1].isdigit():
d[j[0]] = int(j[1])
else:
d[j[0]] = clean(j[1])
if d[j[0]].lower() == 'false':
d[j[0]] = False
elif d[j[0]].lower() == 'true':
d[j[0]] = True
elif d[j[0]].lower() == 'null':
d[j[0]] = None
l = list(d.keys())
l.sort()
o = {}
for i in l:
o[i] = d[i]
return o
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 7, 2021