Question

Continue from the previous question, modify the following in the IP_address class. :

  • add a method named get_statistics(). The get_statistics() method return a list of all statistics data (i.e. the ip address, total sum of all packet-size, total frequency, average of all packet-size)

For example, consider the following code fragment

ip_key = '192.168.0.24'
data_list =[(0, 84), (1, 84), (2, 84), (3, 84), (4, 84), (5, 84), (6, 84), (7, 84), (8, 84), (9, 84), (10, 84), (11, 84), (12, 84), (13, 84), (14, 84), (15, 84), (16, 84), (17, 84), (18, 84), (19, 84), (20, 84)]
size = 3
ip = IP_address(key, data_list, size)

The data_list contains a list of tuple objects. Each tuple consists of the time-frame value and the packet-size. You should summarize this list and create a frequency list, a sum list and an average list based on the time-frame value. There are a lot of data for each time-frame value, we will group packet-size values together. For example: the above data_list will be divided into 3 groups:

  • (0, 84), (1, 84), (2, 84), (3, 84), (4, 84), (5, 84), (6, 84), (7, 84), (8, 84), (9, 84)
  • (10, 84), (11, 84), (12, 84), (13, 84), (14, 84), (15, 84), (16, 84), (17, 84), (18, 84), (19, 84)
  • (20, 84)Therefore, the ip object should contain: • ip address: 192.168.0.24 • a frequency list: [10, 10, 1] (i.e. there are 10 elemAnd,

    Continue from the previous question, modify the following in the IP_address class. :

  • add a method named __str__(). The _str__() method return a nicely formatted string representation of the object.
  • For example, consider the following code fragment:ip_key = 192.168.0.24 data_list =[(0, 84), (1, 84), (2, 84), (3, 84), (4, 84), (5, 84), (6, 84), (7, 84), (8, 84), (9, 84),

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Thanks for the question, here are the implemented functions - get_statistics() and __str()__

I have used the sample example to test these two functions and its working fine. Incase, any one test case fail do let me know so that I can change the code accordingly.

thanks a lot !

==================================================================

class IP_address():
    def __init__(self, ip, data_list, size):
        self.ip_address = ip
        self.data_list = data_list
        self.size = size


    def get_statistics(self):

        total_data_size = sum(packet[1] for packet in self.data_list)
        total_packets=len(self.data_list)
        average_packet_size=int(total_data_size/total_packets)
        return '{}, {}, {}, {}'.format(self.ip_address,total_data_size,total_packets,average_packet_size)

    def __str__(self):
        desc = self.ip_address
        statistics = []
        for i in range(0, len(self.data_list), 10):
            data = self.data_list[i: i + 10]
            frequency = len(data)
            sum_data = sum([packet[1] for packet in data])
            average = round(sum_data / frequency, 1)
            statistics.append([frequency, sum_data, average])
        desc += ':freq=[{}]'.format(', '.join([str(data[0]) for data in statistics]))
        desc += ',sum=[{}]'.format(', '.join([str(data[1]) for data in statistics]))
        desc += ',avg=[{}]'.format(', '.join([str(data[2]) for data in statistics]))
        return desc


ip_key = '192.168.0.24'
data_list = [(0, 84), (1, 84), (2, 84), (3, 84), (4, 84), (5, 84), (6, 84), (7, 84), (8, 84), (9, 84), (10, 84),
             (11, 84), (12, 84), (13, 84), (14, 84), (15, 84), (16, 84), (17, 84), (18, 84), (19, 84), (20, 84)]
size = 3
ip = IP_address(ip_key, data_list, size)
print(ip.get_statistics())
print(ip)

class IP_address(): definit_(self, ip, data_list, size): self.ip_address = ip self.data_list = data_list self.size = size def

1: Project untitled - [C:\Users\248341\Pycharm Projects\untitled] - ... powerball.py - PyCharm Community Edition 4.0.3 Eile E

thanks a lot !

Add a comment
Know the answer?
Add Answer to:
Continue from the previous question, modify the following in the IP_address class. : add a method...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT