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 ocertat
# Taken from mission YAML. Simple Dict
def yaml(a):
output = {}
lines = a.splitlines()
for line in lines:
kv = line.split(": ")
try:
output_line = {kv[0]: int(kv[1])}
except ValueError:
output_line = {kv[0]: kv[1]}
except IndexError:
output_line = {}
output = {**output, **output_line}
return output
if __name__ == '__main__':
# These "asserts" are used for self-checking and not for an auto-testing
assert yaml("""name: Alex
age: 12""") == {'age': 12, 'name': 'Alex'}
assert yaml("""name: Alex Fox
age: 12
class: 12b""") == {'age': 12,
'class': '12b',
'name': 'Alex Fox'}
print("Coding complete? Click 'Check' to earn cool rewards!")
def yaml(a):
output = {}
lines = a.splitlines()
print(lines)
for line in lines:
kv = line.split(": ")
kv = [item.replace('"', '') for item in kv]
kv = [item.replace('\\', '"') for item in kv]
try:
output_line = {kv[0]: int(kv[1])}
except ValueError:
output_line = {kv[0]: kv[1]}
except IndexError:
output_line = {}
try:
if kv[1] == ("false" or "False"):
output_line = {kv[0]: False}
elif kv[1] == ("true" or "True"):
output_line = {kv[0]: True}
except IndexError:
output_line = {}
try:
if 'null' in kv[1]:
if ' ' in kv[1]:
output_line = {kv[0]: 'null'}
else:
output_line = {kv[0]: None}
except IndexError:
try:
print('except!')
if kv[0]:
output_line = {kv[0][:-1]: None}
except IndexError:
output_line = {}
output = {**output, **output_line}
print(output)
return output
if __name__ == '__main__':
# 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 23, 2020