Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Carpediem solution in Clear category for YAML. More Types by DODOABBA
# Taken from mission YAML. Simple Dict
def yaml(a):
def trimming(ts) :
return ts.strip(" '\n").replace('"', "$")
sla = sorted(a.splitlines())
try :
sla.remove("")
except :
print(" NoBlank")
ans = {}
for item in sla :
index0, value0 = item.split(":")
index = trimming(index0)
value = trimming(value0)
if value == "" :
value = None
elif value == "false" :
value = False
elif value == "null" :
value = None
elif value == "$null$" :
value = value.strip("$")
elif value.isdigit() :
value = int(value)
else :
value = value.replace("\$", '"').strip("$")
ans[index] = value
return ans
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!")
June 10, 2020