edits for report
This commit is contained in:
parent
09ee735e16
commit
d9f2d4c417
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
*.pdf
|
||||
venv
|
||||
.idea
|
||||
.idea
|
||||
*.docx
|
||||
|
||||
1
3.py
1
3.py
@ -95,6 +95,7 @@ if brackets is not None:
|
||||
result = result[:len(result) - 2] + '\n'
|
||||
result += '}'
|
||||
with open('output.json', 'w', encoding='UTF-8') as output:
|
||||
print(result)
|
||||
result = result.replace('\n', '')
|
||||
r = ''
|
||||
data = False
|
||||
|
||||
126
4.py
126
4.py
@ -19,7 +19,7 @@ def task0():
|
||||
global result, tab
|
||||
result += f'{" " * (tabArray + tab + 2)}{string}'
|
||||
|
||||
with open('input.yaml', encoding='UTF-8') as inpt:
|
||||
with open('input-csv.yaml', encoding='UTF-8') as inpt:
|
||||
inpt = inpt.readlines()
|
||||
for key, i in enumerate(inpt):
|
||||
i = i.split(':')
|
||||
@ -66,7 +66,7 @@ def task0():
|
||||
|
||||
|
||||
def task1():
|
||||
with open('input.yaml', encoding='UTF-8') as inpt:
|
||||
with open('input-csv.yaml', encoding='UTF-8') as inpt:
|
||||
obj = yaml.safe_load(inpt)
|
||||
json.dumps(obj, ensure_ascii=False).encode('utf8')
|
||||
|
||||
@ -87,7 +87,7 @@ def task2():
|
||||
global result, tab, tabArray
|
||||
result += f'{" " * (tabArray + tab + 2)}{string}'
|
||||
|
||||
with open('input.yaml', encoding='UTF-8') as inpt:
|
||||
with open('input-csv.yaml', encoding='UTF-8') as inpt:
|
||||
inpt = inpt.readlines()
|
||||
for key, i in enumerate(inpt):
|
||||
i = i.split(':')
|
||||
@ -137,6 +137,121 @@ def task2():
|
||||
result += '}'
|
||||
|
||||
|
||||
# --- TASK 3 --- #
|
||||
|
||||
TAB_SIZE = 2
|
||||
tab = 0
|
||||
tabArray = 0
|
||||
result = '{\n'
|
||||
brackets = []
|
||||
array = 0
|
||||
simpleArray = False
|
||||
|
||||
|
||||
def append_to_result(string):
|
||||
global result, tab
|
||||
result += f'{" " * (tabArray + tab + 2)}{string}'
|
||||
|
||||
|
||||
def compute_tab_size(yamlFile):
|
||||
for i in yamlFile[1:]:
|
||||
deltaTab = len(i) - len(i.lstrip())
|
||||
if deltaTab != 0:
|
||||
return deltaTab
|
||||
return 0
|
||||
|
||||
|
||||
def task3():
|
||||
with open('input.yaml', encoding='UTF-8') as inpt:
|
||||
inpt = inpt.readlines()
|
||||
TAB_SIZE = compute_tab_size(inpt)
|
||||
|
||||
for key, i in enumerate(inpt):
|
||||
if i.lstrip()[0] == '#':
|
||||
continue
|
||||
|
||||
if i.count(':') == 0 and i.strip()[0] == '-':
|
||||
append_to_result(f'{transform(i.strip()[2:])},\n')
|
||||
simpleArray = True
|
||||
continue
|
||||
if simpleArray:
|
||||
result = result[:len(result) - 2] + '\n'
|
||||
append_to_result(brackets.pop() + ',\n')
|
||||
simpleArray = False
|
||||
|
||||
i = [i.split('#')[0].split(':')[0], ':'.join(i.split('#')[0].split(':')[1:])]
|
||||
|
||||
# Обработка табов
|
||||
deltaTab = len(i[0]) - len(i[0].lstrip())
|
||||
if tab != deltaTab:
|
||||
if tab < deltaTab:
|
||||
tab = deltaTab
|
||||
else:
|
||||
result = result[:len(result) - 2] + '\n'
|
||||
append_to_result(brackets.pop() + ',\n')
|
||||
if abs(deltaTab - tab)//TAB_SIZE > 1:
|
||||
result = result[:len(result) - 2] + '\n'
|
||||
append_to_result(brackets.pop() + ',\n')
|
||||
tab = deltaTab
|
||||
|
||||
# Обработка массивов
|
||||
if i[0].strip()[0] == '-':
|
||||
i[0] = i[0].lstrip()[1:].strip()
|
||||
append_to_result('{\n')
|
||||
brackets.append('}')
|
||||
tab += TAB_SIZE
|
||||
|
||||
# Обработка вложенных объектов
|
||||
if i[1].strip() == '':
|
||||
br = ''
|
||||
if inpt[key+1].split(':')[0].strip()[0] == '-':
|
||||
br = '[]'
|
||||
else:
|
||||
br = '{}'
|
||||
append_to_result(f'"{i[0].strip()}": {br[0]}\n')
|
||||
brackets.append(br[1])
|
||||
else:
|
||||
def transform(s: str):
|
||||
try:
|
||||
return int(s)
|
||||
except:
|
||||
try:
|
||||
return float(s)
|
||||
except:
|
||||
if s.strip() in ["y", "Y", "yes", "Yes", "YES", "n", "N", "no", "No", "NO", "true", "True", "TRUE"]:
|
||||
return "true"
|
||||
elif s.strip() in ["false", "False", "FALSE", "on", "On", "ON", "off", "Off", "OFF"]:
|
||||
return "false"
|
||||
return '"' + s.replace("\\", "\\\\").replace("\"", "\\\"").strip() + '"'
|
||||
|
||||
|
||||
i = list(map(transform, i))
|
||||
append_to_result(f'{i[0]}: {i[1]},\n')
|
||||
if brackets is not None:
|
||||
for _ in range(len(brackets)):
|
||||
tab -= TAB_SIZE
|
||||
result = result[:len(result) - 2] + '\n'
|
||||
i = brackets.pop()
|
||||
append_to_result(i + ',\n')
|
||||
|
||||
result = result[:len(result) - 2] + '\n'
|
||||
result += '}'
|
||||
with open('output.json', 'w', encoding='UTF-8') as output:
|
||||
print(result)
|
||||
result = result.replace('\n', '')
|
||||
r = ''
|
||||
data = False
|
||||
for key, i in enumerate(result):
|
||||
if i != ' ' and not data:
|
||||
r += i
|
||||
if data:
|
||||
r += i
|
||||
if i == '"' and result[key - 1:key+1] != r'\"':
|
||||
data = not data
|
||||
output.write(r)
|
||||
print(r)
|
||||
|
||||
|
||||
t0 = time.perf_counter()
|
||||
for _ in range(100):
|
||||
task0()
|
||||
@ -151,3 +266,8 @@ t2 = time.perf_counter()
|
||||
for _ in range(100):
|
||||
task2()
|
||||
print('Задание 2: ', time.perf_counter() - t2)
|
||||
|
||||
t3 = time.perf_counter()
|
||||
for _ in range(100):
|
||||
task2()
|
||||
print('Задание 3: ', time.perf_counter() - t3)
|
||||
|
||||
@ -9,8 +9,8 @@ lessons:
|
||||
Hello: true
|
||||
weekday: Четверг
|
||||
time: 10:00-11:30
|
||||
audience: Ауд. 1410, Кронверкский пр., д.49, лит.А
|
||||
teacher: Айба Тамара Гурамовна
|
||||
audience: Ауд. 1410, Кро{нверкский пр., д.49, лит.А
|
||||
teacher: Айба Тамара Гурам}овна
|
||||
type: Практика
|
||||
format: Очно
|
||||
- subject: История России и мира в ХХ веке
|
||||
|
||||
@ -1 +1 @@
|
||||
{"lessons":[{"subject":"Ист\"ория России и мира в ХХ веке","hella":"7,3","chto":["a","b","c","d"],"Hello":true,"weekday":"Четверг","time":"10:00-11:30","audience":"Ауд. 1410, Кронверкский пр., д.49, лит.А","teacher":"Айба Тамара Гурамовна","type":"Практика","format":"Очно"},{"subject":"История России и мира в ХХ веке","weekday":"Четверг","time":"15:20-16:50","audience":"Ауд. Lemon Classroom (1419), Кронверкский пр., д.49, лит.А","teacher":"Пригодич Никита Дмитриевич","type":"Лекция","format":"Очно - дистанционный,","chot":true,"nea":33.6,"wawa":[{"mama":"mia","ech":"toumio"},{"mama":"mia","ech":"toumio"},{"mama":"mia","ech":"toumio"}]}]}
|
||||
{"lessons":[{"subject":"Ист\"ория России и мира в ХХ веке","hella":"7,3","chto":["a","b","c","d"],"Hello":true,"weekday":"Четверг","time":"10:00-11:30","audience":"Ауд. 1410, Кро{нверкский пр., д.49, лит.А","teacher":"Айба Тамара Гурам}овна","type":"Практика","format":"Очно"},{"subject":"История России и мира в ХХ веке","weekday":"Четверг","time":"15:20-16:50","audience":"Ауд. Lemon Classroom (1419), Кронверкский пр., д.49, лит.А","teacher":"Пригодич Никита Дмитриевич","type":"Лекция","format":"Очно - дистанционный,","chot":true,"nea":33.6,"wawa":[{"mama":"mia","ech":"toumio"},{"mama":"mia","ech":"toumio"},{"mama":"mia","ech":"toumio"}]}]}
|
||||
Loading…
Reference in New Issue
Block a user