Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
dislike =( solution in Clear category for YAML. More Types by Alexey.K.
# Taken from mission YAML. Simple Dict
def dequote(str):
if str[0]=="\"" and str[-1]=="\"":
return str[1:-1]
if str[0]=="\'" and str[-1]=="\'":
return str[1:-1]
return str
def demask(str):
return str.replace('\\"',"\"").replace("\\'",'\'')
def to_bool(str):
if str.lower()=='false':
return False
elif str.lower()=='True':
return True
else:
return str
def yaml(a):
l = [r.split(':') for r in a.split('\n') if r]
o = {}
#print(l)
for k, v in l:
k = None if k=='' else k.strip()
v = None if v.strip()=='' or v.strip()=='null' \
else to_bool(demask(dequote(v.strip()))) if not v.strip().isdigit() else int(v.strip())
o.update({k:v})
print(o)
return o
if __name__ == '__main__':
print("Example:")
print(yaml(': Alex\nage: '))
# 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!")
March 12, 2021