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 olevoniuk
from re import match
# Taken from mission YAML. Simple Dict
def yaml(a):
lines = a.split('\n')
yml = {}
for l in lines:
m = match(r'^\s*(\w+):\s*(.+)\s*$', l)
if m:
yml[m.group(1)] = int(m.group(2)) if m.group(2).isdigit() else m.group(2)
return yml
if __name__ == '__main__':
print("Example:")
print(yaml("""name: Alex
age: 12"""))
# 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):
lines = a.split('\n')
yml = {}
for l in lines:
m = match(r'^\s*(\w+):\s*"(.+)"\s*$', l)
if m:
yml[m.group(1)] = m.group(2).replace('\\"', '"')
continue
m = match(r'^\s*(\w+):\s*("?)(.*)("?)\s*$', l)
if m:
if m.group(2) and m.group(4):
val = val = m.group(3)
if m.group(3).isdigit():
val = int(m.group(3))
elif m.group(3) == 'true':
val = True
elif m.group(3) == 'false':
val = False
elif not m.group(3) or m.group(3) == 'null':
val = None
else:
val = m.group(3)
yml[m.group(1)] = val
return yml
if __name__ == '__main__':
print("Example:")
print(yaml('name: Alex\nage: 12'))
print(yaml('name: "Bob Dylan"\n'
'children: 6\n'
'coding:'))
# 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!")
Dec. 22, 2020