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 pokryshkin
# Taken from mission YAML. Simple Dict
def yaml(a):
#print(a)
a = a.replace('\\','') # удаляем \\
a = a.split('\n') # разбиваем по разделителю строк
#print(a)
a = [x.split(': ') for x in a if x] # разбиваем по : и удаляем пустые строки
#print(a)
a = [ [x[0][:-1],'None'] if len(x)==1 else [x[0],x[1]] for x in a] # None
#print(a)
a = [ [x[0],x[1][1:-1]] if x[1][0]=='"' and x[1][-1]=='"' else [x[0],x[1]] for x in a] # удаляем "" вторых подэлементов
#print(a)
a = [ [x[0],int(x[1])] if x[1].isdigit() else [x[0],x[1]] for x in a ] # заменяем стринговые цифры на int
#print(a)
a = [ [x[0],True] if x[1]=='true' else [x[0],x[1]] for x in a] # заменяем True
a = [ [x[0],False] if x[1]=='false' else [x[0],x[1]] for x in a] # заменяем False
a = [ [x[0],None] if x[1]=='None' or x[1]=='null' else [x[0],x[1]] for x in a] # заменяем None
a = [ [x[0],'null'] if x[1]=='"null" ' else [x[0],x[1]] for x in a] # заменяем 'null'
#print(a)
a = { x[0]:x[1] for x in a }
print(a)
return a
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):
#print(a)
a = a.replace('\\','') # удаляем \\
a = a.split('\n') # разбиваем по разделителю строк
#print(a)
a = [x.split(': ') for x in a if x] # разбиваем по : и удаляем пустые строки
#print(a)
a = [ [x[0][:-1],'None'] if len(x)==1 else [x[0],x[1]] for x in a] # None
#print(a)
a = [ [x[0],x[1][1:-1]] if x[1][0]=='"' and x[1][-1]=='"' else [x[0],x[1]] for x in a] # удаляем "" вторых подэлементов
#print(a)
a = [ [x[0],int(x[1])] if x[1].isdigit() else [x[0],x[1]] for x in a ] # заменяем стринговые цифры на int
#print(a)
a = [ [x[0],True] if x[1]=='true' else [x[0],x[1]] for x in a] # заменяем True
a = [ [x[0],False] if x[1]=='false' else [x[0],x[1]] for x in a] # заменяем False
a = [ [x[0],None] if x[1]=='None' or x[1]=='null' else [x[0],x[1]] for x in a] # заменяем None
a = [ [x[0],'null'] if x[1]=='"null" ' else [x[0],x[1]] for x in a] # заменяем 'null'
#print(a)
a = { x[0]:x[1] for x in a }
#print(a)
return a
if __name__ == '__main__':
print("Example:")
print(yaml('name: Alex\nage: 12'))
# 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