Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Try / except, if /elif solution in Clear category for YAML. More Types by ail531
def yaml(a):
result_dict = {}
for line in a.splitlines():
if line:
try:
result_dict[line.split(':')[0].strip()] = int(line.split(':')[1].strip())
except ValueError:
value = line.split(':')[1].strip()
if value == 'null':
result_dict[line.split(':')[0].strip()] = None
else:
value = line.split(':')[1].strip(' "').replace('\\', '')
if '"' in value:
value += '"' ## For making Alex "Fox"
elif value.lower() == "true":
value = True
elif value.lower() == "false":
value = False
elif not value:
value = None
result_dict[line.split(':')[0].strip()] = value
return result_dict
if __name__ == '__main__':
print("Example:")
print(yaml("coding: null"))
print(yaml("coding: \"null\" "))
# 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 28, 2020