Created
August 21, 2019 09:13
-
-
Save rusnino/02d8eccc9e993b9ce0edcf5d8afc99fe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
''' | |
Задание 26.1 | |
Изменить класс Topology из задания 25.1x. | |
Добавить метод, который позволит выполнять сложение двух объектов (экземпляров) Topology. | |
В результате сложения должен возвращаться новый экземпляр класса Topology. | |
Создание двух топологий: | |
In [1]: t1 = Topology(topology_example) | |
In [2]: t1.topology | |
Out[2]: | |
{('R1', 'Eth0/0'): ('SW1', 'Eth0/1'), | |
('R2', 'Eth0/0'): ('SW1', 'Eth0/2'), | |
('R2', 'Eth0/1'): ('SW2', 'Eth0/11'), | |
('R3', 'Eth0/0'): ('SW1', 'Eth0/3'), | |
('R3', 'Eth0/1'): ('R4', 'Eth0/0'), | |
('R3', 'Eth0/2'): ('R5', 'Eth0/0')} | |
In [3]: topology_example2 = {('R1', 'Eth0/4'): ('R7', 'Eth0/0'), | |
('R1', 'Eth0/6'): ('R9', 'Eth0/0')} | |
In [4]: t2 = Topology(topology_example2) | |
In [5]: t2.topology | |
Out[5]: {('R1', 'Eth0/4'): ('R7', 'Eth0/0'), ('R1', 'Eth0/6'): ('R9', 'Eth0/0')} | |
Суммирование топологий: | |
In [6]: t3 = t1+t2 | |
In [7]: t3.topology | |
Out[7]: | |
{('R1', 'Eth0/0'): ('SW1', 'Eth0/1'), | |
('R1', 'Eth0/4'): ('R7', 'Eth0/0'), | |
('R1', 'Eth0/6'): ('R9', 'Eth0/0'), | |
('R2', 'Eth0/0'): ('SW1', 'Eth0/2'), | |
('R2', 'Eth0/1'): ('SW2', 'Eth0/11'), | |
('R3', 'Eth0/0'): ('SW1', 'Eth0/3'), | |
('R3', 'Eth0/1'): ('R4', 'Eth0/0'), | |
('R3', 'Eth0/2'): ('R5', 'Eth0/0')} | |
Проверка, что исходные топологии не изменились: | |
In [9]: t1.topology | |
Out[9]: | |
{('R1', 'Eth0/0'): ('SW1', 'Eth0/1'), | |
('R2', 'Eth0/0'): ('SW1', 'Eth0/2'), | |
('R2', 'Eth0/1'): ('SW2', 'Eth0/11'), | |
('R3', 'Eth0/0'): ('SW1', 'Eth0/3'), | |
('R3', 'Eth0/1'): ('R4', 'Eth0/0'), | |
('R3', 'Eth0/2'): ('R5', 'Eth0/0')} | |
In [10]: t2.topology | |
Out[10]: {('R1', 'Eth0/4'): ('R7', 'Eth0/0'), ('R1', 'Eth0/6'): ('R9', 'Eth0/0')} | |
''' | |
topology_example = {('R1', 'Eth0/0'): ('SW1', 'Eth0/1'), | |
('R2', 'Eth0/0'): ('SW1', 'Eth0/2'), | |
('R2', 'Eth0/1'): ('SW2', 'Eth0/11'), | |
('R3', 'Eth0/0'): ('SW1', 'Eth0/3'), | |
('R3', 'Eth0/1'): ('R4', 'Eth0/0'), | |
('R3', 'Eth0/2'): ('R5', 'Eth0/0'), | |
('SW1', 'Eth0/1'): ('R1', 'Eth0/0'), | |
('SW1', 'Eth0/2'): ('R2', 'Eth0/0'), | |
('SW1', 'Eth0/3'): ('R3', 'Eth0/0')} | |
topology_example2 = {('R1', 'Eth0/4'): ('R7', 'Eth0/0'), | |
('R1', 'Eth0/6'): ('R9', 'Eth0/0')} | |
class Topology: | |
def __init__(self, topology_dict): | |
self.topology = self._normalize(topology_dict) | |
def _normalize(self, topology_dict): | |
self.topology = {} | |
for key, value in topology_dict.items(): | |
if not self.topology.get(value) == key: | |
self.topology[key] = value | |
return self.topology | |
def delete_link(self, link_near_end, link_far_end): | |
if self.topology.get(link_near_end) == link_far_end: | |
del self.topology[link_near_end] | |
elif self.topology.get(link_far_end) == link_near_end: | |
del self.topology[link_far_end] | |
else: | |
print('Такого соединения нет') | |
def __add__(self, other): | |
result = self.topology.copy() | |
result.update(other.topology) | |
return Topology(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment