diff --git a/.gitignore b/.gitignore index 768b263..4106712 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.pdf venv -.idea \ No newline at end of file +.idea +*.docx diff --git a/3.py b/3.py index 726d000..fef9cf3 100644 --- a/3.py +++ b/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 diff --git a/4.py b/4.py index 25635ad..aed06cf 100644 --- a/4.py +++ b/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) diff --git a/input.yaml b/input.yaml index 7b68a81..662bf18 100644 --- a/input.yaml +++ b/input.yaml @@ -9,8 +9,8 @@ lessons: Hello: true weekday: Четверг time: 10:00-11:30 - audience: Ауд. 1410, Кронверкский пр., д.49, лит.А - teacher: Айба Тамара Гурамовна + audience: Ауд. 1410, Кро{нверкский пр., д.49, лит.А + teacher: Айба Тамара Гурам}овна type: Практика format: Очно - subject: История России и мира в ХХ веке diff --git a/output.json b/output.json index 7da6abb..e53d687 100644 --- a/output.json +++ b/output.json @@ -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"}]}]} \ No newline at end of file +{"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"}]}]} \ No newline at end of file