Advertisement
Guest User

Untitled

a guest
Dec 8th, 2017
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. import time
  2. import operator
  3. import cProfile
  4. import sys
  5.  
  6. def load_files():  
  7.     with open('vlans.csv') as f:
  8.         f.readline()
  9.         d = f.read()[:-1]
  10.         vlans = [l.split(",") for l in d.split("\n")]
  11.     with open('requests.csv') as f:
  12.         f.readline()
  13.         d = f.read()[:-1]
  14.         requests = [l.split(",") for l in d.split("\n")]
  15.     return vlans, requests
  16.  
  17. def process(vlans, requests, output_rows=0):
  18.     out = []
  19.     by_vlan_id = sorted(sorted(vlans, key=lambda x: int(x[0])), key=lambda x: int(x[2]))
  20.     # by_vlan_id = sorted(vlans, key=lambda x: (int(x[2]), int(x[0]))) # Slower than the above! But more readable.
  21.     red_vlans_iter = iter(by_vlan_id)
  22.     no_red_vlans_iter = iter(by_vlan_id)
  23.     for reqn, red in iter(requests):
  24.         if red == "1":
  25.             dev_id, vlan_id, primary, secondary = None, None, None, None
  26.             for info in red_vlans_iter:
  27.                 if not info[2]:
  28.                     continue
  29.                 if dev_id is None or info[0] != dev_id or info[2] != vlan_id:
  30.                     dev_id = info[0]
  31.                     vlan_id = info[2]
  32.                     primary, secondary = None, None
  33.                 if info[1] == "1":
  34.                     primary = info
  35.                 else:
  36.                     secondary = info
  37.                 if primary is not None and secondary is not None:
  38.                     primary[2] = 0
  39.                     secondary[2] = 0
  40.                     break
  41.             out.append("%s,%s,0,%s\n%s,%s,1,%s" % (reqn, dev_id, vlan_id, reqn, dev_id, vlan_id))
  42.         else:
  43.             for info in no_red_vlans_iter:
  44.                 if info[2] and info[1] == "1":
  45.                     out.append("%s,%s,1,%s" % (reqn, info[0], info[2]))
  46.                     info[2] = 0
  47.                     break
  48.     return out
  49.        
  50. def main(silent=False):
  51.     t0 = time.perf_counter()
  52.  
  53.     vlans, requests = load_files()
  54.     t1 = time.perf_counter()
  55.  
  56.     out = process(vlans, requests)
  57.     t2 = time.perf_counter()
  58.  
  59.     with open("output_py.csv", 'w') as f:
  60.         f.write('request_id,device_id,primary_port,vlan_id\n')
  61.         f.write("\n".join(out))
  62.     t3 = time.perf_counter()
  63.    
  64.     if not silent:
  65.         print("total %.2fms: reading: %dms, processing: %dms, writing: %dms" % (
  66.             (t3 - t0) * 1000, (t1 - t0) * 1000, (t2 - t1) * 1000, (t3 - t2) * 1000))
  67.     return (t3 - t0) * 1000
  68.  
  69. if __name__ == "__main__":
  70.     if len(sys.argv) > 1:
  71.         if sys.argv[1] == "profile":
  72.             sortby = sys.argv[2] if len(sys.argv) > 2 else 'tottime'
  73.             cProfile.run("main()", sort=sortby)
  74.         elif sys.argv[1] == "avg":
  75.             n = int(sys.argv[2]) if len(sys.argv) > 2 else 20
  76.             cumul = 0
  77.             for i in range(n):
  78.                 print("%d/%d\r" % (i, n), end="")
  79.                 cumul += main(True)
  80.             print("Average over %d executions: %.3fms" % (n, cumul / n))
  81.     else:
  82.         main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement