daily pastebin goal
52%
SHARE
TWEET

Untitled

a guest Feb 15th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. """
  4. filename: descriptor_1_1.py
  5. description: python descriptor
  6. License: GPL V2
  7. Author: 天使de眼睛
  8.  
  9. 除特别声明,所有代码均是 python3.6 在 iOS 环境下编写测试。
  10.  
  11. 运行代码需要 pytest
  12.     pip install pytest
  13. """
  14. from ipaddress import ip_address
  15.  
  16. import pytest
  17.  
  18.  
  19. class Server:
  20.     def __init__(self, name, ip):
  21.         self._name = None
  22.         self._ip = None
  23.         self._memory = None
  24.         self.name = name
  25.         self.ip = ip
  26.        
  27.  
  28.     @property
  29.     def name(self):
  30.         return self._name
  31.  
  32.     @name.setter
  33.     def name(self, value):
  34.         if not isinstance(value, str):
  35.             raise TypeError(f'Expected {str}')
  36.         self._name = value.upper()
  37.  
  38.     @property
  39.     def ip(self):
  40.         return self._ip
  41.  
  42.     @ip.setter
  43.     def ip(self, value):
  44.         if not isinstance(value, str):
  45.             raise TypeError(f'Expected {str}')
  46.         self._ip = ip_address(value)
  47.  
  48.  
  49. class Protocol:
  50.     def __init__(self, protocol, source, destination):
  51.         self._protocol = None
  52.         self._source = None
  53.         self._destination = None
  54.         self.protocol = protocol
  55.         self.source = source
  56.         self.destination = destination
  57.  
  58.     @property
  59.     def protocol(self):
  60.         return self._protocol
  61.  
  62.     @protocol.setter
  63.     def protocol(self, value):
  64.         if not isinstance(value, str):
  65.             raise TypeError(f'Expected {str}')
  66.         self._protocol = value.upper()
  67.  
  68.     @property
  69.     def source(self):
  70.         return self._source
  71.  
  72.     @source.setter
  73.     def source(self, value):
  74.         self._source = ip_address(value)
  75.  
  76.     @property
  77.     def destination(self):
  78.         return self._destination
  79.  
  80.     @destination.setter
  81.     def destination(self, value):
  82.         self._destination = ip_address(value)
  83.  
  84.  
  85. class TestClasses:
  86.     def test_Server(self):
  87.         server = Server('myserver1', '192.168.1.1')
  88.         assert server.name == 'MYSERVER1'
  89.         assert server.ip == ip_address('192.168.1.1')
  90.         with pytest.raises(TypeError) as error:
  91.             server.name = 1234
  92.         assert str(error.value) == "Expected <class 'str'>"
  93.  
  94.     def test_Protocol(self):
  95.         logger = Protocol('tcp', '192.168.1.1', '10.1.1.1')
  96.         assert logger.protocol == 'TCP'
  97.         assert logger.source == ip_address('192.168.1.1')
  98.         assert logger.destination == ip_address('10.1.1.1')
  99.         with pytest.raises(TypeError) as error:
  100.             logger.protocol = ['tcp']
  101.         assert str(error.value) == "Expected <class 'str'>"
  102.  
  103.  
  104. if __name__ == '__main__':
  105.     import sys
  106.  
  107.     pytest.main(sys.argv + ['-v'])
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top