kAldown

task1

May 13th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.50 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. # http://regexr.com/
  4.  
  5. import re
  6. import os
  7. import time
  8. import datetime
  9.  
  10. text = r'''123.124.554.123 this is 192.168.5.20 a
  11. 195 ip adress 200.5.3.125 and not an ip 344.45.12.145,
  12. 244.244.244.0 244.244.50.1
  13. time: 12:15:11 5:11:30 10:30:22
  14. date: 31/12/2015 8/13/2010 9/7/2010 1/1/2050
  15. bytes: 4444
  16.  
  17. to test sum:
  18.  
  19. 192.168.5.20
  20. 200.5.3.125
  21. 5556
  22. '''
  23. # date = %d/%m/%Y
  24. # time = %H:%M:%S
  25.  
  26. #with open(file_name) as f:
  27. #    text = f.read()
  28.  
  29. ips = re.compile(r'\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b', re.MULTILINE)
  30. times = re.compile(r'\b(?:[0-5]?[0-9]\:){2}[0-5]?[0-9]\b', re.MULTILINE)
  31. dates = re.compile(r'\b[0-3]?[0-9]\/(?:1[0-2]|[0-9])\/\d{4}\b', re.MULTILINE)
  32.  
  33.  
  34. bits = [int(i) for i in text.split() if i.isdigit()]
  35.    
  36. def make_ip_tuples(lst):
  37.     """
  38.    get list of all ips
  39.    return tuples from each line
  40.  
  41.    """
  42.     try:
  43.         assert len(lst) % 2 == 0
  44.     except AssertionError:
  45.         print 'lst length not even, try to add some IPs to make couples'
  46.         exit(1)
  47.    
  48.     result = []
  49.     for i in range(len(lst))[::2]:
  50.         ip_tuple = (lst[i], lst[i+1])
  51.         result.append(ip_tuple)
  52.     return result
  53.  
  54. def dump(ip_tuples, bts):
  55.     """
  56.    get list of ip tuples and list of bits for every occurence
  57.    return D: .keys = set uf tuples
  58.               .values = sum of bits
  59.    """
  60.     result = {}
  61.     for i in range(len(ip_tuples)):
  62.         ip_tuple = ip_tuples[i]
  63.         if ip_tuple not in result:
  64.             result[ip_tuple] = result.get(ip_tuple, bts[i])
  65.         else:
  66.             result[ip_tuple] += bts[i]
  67.     return result
  68.  
  69. def time_stamp(d, t):
  70.     ts = '%s %s' % (d, t)
  71.     result = time.mktime(datetime.datetime.strptime(ts, '%d/%m/%Y %H:%M:%S').timetuple())
  72.     return result
  73.  
  74. def make_output(input_list):
  75.     """
  76.    return dict with all columns in sorted order without dublicates
  77.    """
  78.     sorted_list = sorted(input_list, reverse=True, key=lambda x: time_stamp(x[0], x[1]))
  79.  
  80.     result = []
  81.     for i in sorted_list:
  82.         if i[2] not in [row[2] for row in result]:
  83.             result.append(list(i))
  84.         else:
  85.             idx = [row[2] for row in result].index(i[2])
  86.             result[idx][3] += i[3]
  87.     return result
  88.  
  89.  
  90. ips = ips.findall(text)
  91. time_list = times.findall(text)
  92. date_list = dates.findall(text)
  93. ip_tuples = make_ip_tuples(ips)
  94. ip_dump = dump(ip_tuples, bits)
  95.  
  96. inp = zip(date_list, time_list, ip_tuples, bits)
  97.  
  98. print make_output(inp)
Advertisement
Add Comment
Please, Sign In to add comment