Advertisement
GeorgiLukanov87

Python OOP Exam - 16 Aug 2020 - System Split

Nov 16th, 2022
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.61 KB | None | 0 0
  1. # Python OOP Exam - 16 Aug 2020 - System Split
  2.  
  3. # https://judge.softuni.org/Contests/Practice/Index/2470#1
  4. =============================================================================================
  5. # file name: hardware.py
  6.  
  7. from abc import ABC
  8. from project.software.software import Software
  9.  
  10.  
  11. class Hardware(ABC):
  12.     def __init__(self, name: str, hardware_type: str, capacity: int, memory: int):
  13.         self.name = name
  14.         self.hardware_type = hardware_type
  15.         self.capacity = capacity
  16.         self.memory = memory
  17.         self.software_components = []  # All software's components installed on that hardware!
  18.  
  19.     def install(self, software: Software):
  20.         if self.capacity >= software.capacity_consumption and self.memory >= software.memory_consumption:
  21.             self.capacity -= software.capacity_consumption
  22.             self.memory -= software.memory_consumption
  23.             self.software_components.append(software)
  24.         else:
  25.             raise Exception("Software cannot be installed")
  26.  
  27.     def uninstall(self, software: Software):
  28.         if software in self.software_components:
  29.             self.software_components.remove(software)
  30.             self.capacity += software.capacity_consumption
  31.             self.memory += software.memory_consumption
  32.  
  33. =============================================================================================
  34. # file name: heavy_hardware.py
  35.  
  36. from project.hardware.hardware import Hardware
  37.  
  38.  
  39. class HeavyHardware(Hardware):
  40.     TYPE = 'Heavy'
  41.  
  42.     def __init__(self, name: str, capacity: int, memory: int):
  43.         super().__init__(name, self.TYPE, capacity, memory)
  44.         self.name = name
  45.         self.hardware_type = HeavyHardware.TYPE
  46.         self.capacity = capacity * 2
  47.         self.memory = int(memory * 0.75)
  48.  
  49.         self.initial_memory = self.memory
  50.         self.initial_cap = self.capacity
  51.  
  52. =============================================================================================
  53. # file name: power_hardware.py
  54.  
  55. from project.hardware.hardware import Hardware
  56.  
  57.  
  58. class PowerHardware(Hardware):
  59.     TYPE = 'Power'
  60.  
  61.     def __init__(self, name: str, capacity: int, memory: int):
  62.         super().__init__(name, self.TYPE, capacity, memory)
  63.         self.name = name
  64.         self.hardware_type = PowerHardware.TYPE
  65.         self.capacity = int(capacity * 0.25)
  66.         self.memory = int(memory * 1.75)
  67.  
  68.         self.initial_memory = self.memory
  69.         self.initial_cap = self.capacity
  70.  
  71. =============================================================================================
  72. # file name: software.py
  73.  
  74. from abc import ABC
  75.  
  76.  
  77. class Software(ABC):
  78.     def __init__(self, name: str, software_type: str, capacity_consumption: int, memory_consumption: int):
  79.         self.name = name
  80.         self.software_type = software_type
  81.         self.capacity_consumption = capacity_consumption
  82.         self.memory_consumption = memory_consumption
  83.  
  84. =============================================================================================
  85. # file name: express_software.py
  86.  
  87. from project.software.software import Software
  88.  
  89.  
  90. class ExpressSoftware(Software):
  91.     TYPE = 'Express'
  92.  
  93.     def __init__(self, name: str, capacity_consumption: int, memory_consumption: int):
  94.         super().__init__(name, self.TYPE, capacity_consumption, memory_consumption)
  95.         self.name = name
  96.         self.software_type = ExpressSoftware.TYPE
  97.         self.capacity_consumption = capacity_consumption
  98.         self.memory_consumption = memory_consumption * 2
  99.  
  100. =============================================================================================
  101. # file name: light_software.py
  102.  
  103. from project.software.software import Software
  104.  
  105.  
  106. class LightSoftware(Software):
  107.     TYPE = 'Light'
  108.  
  109.     def __init__(self, name: str, capacity_consumption: int, memory_consumption: int):
  110.         super().__init__(name, self.TYPE, capacity_consumption, memory_consumption)
  111.         self.name = name
  112.         self.software_type = LightSoftware.TYPE
  113.         self.capacity_consumption = int(capacity_consumption * 1.50)
  114.         self.memory_consumption = int(memory_consumption / 2)
  115.  
  116. =============================================================================================
  117.  
  118. # file name: system.py
  119.  
  120. from project.hardware.heavy_hardware import HeavyHardware
  121. from project.hardware.power_hardware import PowerHardware
  122. from project.software.express_software import ExpressSoftware
  123. from project.software.light_software import LightSoftware
  124.  
  125.  
  126. class System:
  127.     _hardware = []
  128.     _software = []
  129.  
  130.     def __init__(self):
  131.         ...
  132.  
  133.     @staticmethod
  134.     def register_power_hardware(name: str, capacity: int, memory: int):
  135.         hardware_to_add = PowerHardware(name, capacity, memory)
  136.         System._hardware.append(hardware_to_add)
  137.  
  138.     @staticmethod
  139.     def register_heavy_hardware(name: str, capacity: int, memory: int):
  140.         hardware_to_add = HeavyHardware(name, capacity, memory)
  141.         System._hardware.append(hardware_to_add)
  142.  
  143.     @staticmethod
  144.     def register_express_software(hardware_name: str, name: str, capacity_consumption: int, memory_consumption: int):
  145.         if hardware_name not in [h.name for h in System._hardware]:
  146.             return f"Hardware does not exist"
  147.         else:
  148.             hardware = [h for h in System._hardware if h.name == hardware_name][0]
  149.         new_express_software = ExpressSoftware(name, capacity_consumption, memory_consumption)
  150.         hardware.install(new_express_software)
  151.         System._software.append(new_express_software)
  152.  
  153.     @staticmethod
  154.     def register_light_software(hardware_name: str, name: str, capacity_consumption: int, memory_consumption: int):
  155.         if hardware_name not in [h.name for h in System._hardware]:
  156.             return f"Hardware does not exist"
  157.         else:
  158.             hardware = [h for h in System._hardware if h.name == hardware_name][0]
  159.         new_light_software = LightSoftware(name, capacity_consumption, memory_consumption)
  160.         hardware.install(new_light_software)
  161.         System._software.append(new_light_software)
  162.  
  163.     @staticmethod
  164.     def release_software_component(hardware_name: str, software_name: str):
  165.         if hardware_name in [h.name for h in System._hardware] and \
  166.                 software_name in [s.name for s in System._software]:
  167.             hardware = [h for h in System._hardware if h.name == hardware_name][0]
  168.             software = [s for s in System._software if s.name == software_name][0]
  169.             hardware.uninstall(software)
  170.             System._software.remove(software)
  171.         else:
  172.             return "Some of the components do not exist"
  173.  
  174.     @staticmethod
  175.     def analyze():
  176.         result = []
  177.         software_memory = 0
  178.         software_cap = 0
  179.  
  180.         for software_el in System._software:
  181.             software_memory += software_el.memory_consumption
  182.             software_cap += software_el.capacity_consumption
  183.  
  184.         total_memory = sum([m.initial_memory for m in System._hardware])
  185.         total_cap = sum([m.initial_cap for m in System._hardware])
  186.  
  187.         result.append("System Analysis")
  188.         result.append(f'Hardware Components: {len(System._hardware)}')
  189.         result.append(f'Software Components: {len(System._software)}')
  190.  
  191.         result.append(f"Total Operational Memory: {software_memory} / "
  192.                       f"{total_memory}")
  193.  
  194.         result.append(f'Total Capacity Taken: {software_cap} / '
  195.                       f'{total_cap}')
  196.  
  197.         return "\n".join(result)
  198.  
  199.     @staticmethod
  200.     def system_split():
  201.         result = []
  202.         for el in System._hardware:
  203.             result.append(f'Hardware Component - {el.name}')
  204.  
  205.             result.append(f'Express Software Components: '
  206.                           f'{len([x for x in el.software_components if type(x).__name__ == "ExpressSoftware"])}')
  207.  
  208.             result.append(f'Light Software Components: '
  209.                           f'{len([x for x in el.software_components if type(x).__name__ == "LightSoftware"])}')
  210.  
  211.             result.append(f'Memory Usage: '
  212.                           f'{sum([mc.memory_consumption for mc in el.software_components])} / {el.initial_memory}')
  213.  
  214.             result.append(f'Capacity Usage: '
  215.                           f'{sum([mc.capacity_consumption for mc in el.software_components])} / {el.initial_cap}')
  216.  
  217.             result.append(f'Type: {el.TYPE}')
  218.  
  219.             if not el.software_components:
  220.                 result.append(f'Software Components: None')
  221.             else:
  222.                 result.append(f'Software Components: {", ".join([n.name for n in el.software_components])}')
  223.  
  224.         return '\n'.join(result)
  225.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement