Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
nested cycles and quotation stripper solution in Uncategorized category for YAML. More Types by Vaarwel
def formatter(dct):
for key,value in dct.items():
liner = value.strip()
try:
dct[key] = int(liner)
except ValueError:
if liner == 'false':
dct[key] = False
elif liner == 'true':
dct[key] = True
elif liner == 'null' or liner == '':
dct[key] = None
else:
liner = liner.replace('\\"','<(protected)>')
liner = liner.strip('"')
liner = liner.replace('<(protected)>','"')
dct[key] = liner
return dct
def yaml(a):
dct = {}
i,j,k = 0,0,0
while i < len(a):
if a[i].isalnum():
j = i + 1
while j < len(a):
if a[j] == ":":
k = j + 1
while k <= len(a):
if k == len(a) or a[k] == '\n':
dct[a[i:j]] = a[j+1:k]
i = k
break
else:
k += 1
break
else:
j += 1
else:
i += 1
return formatter(dct)
if __name__ == '__main__':
print("Example:")
print(yaml('name: "Bob Dylan"\n'
'children: 6\n'
'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!")
April 11, 2021