c-programming

Simple C programs
git clone git://git.laack.co/c-programming.git
Log | Files | Refs

gen.py (1813B)


      1 import random
      2 import hashlib
      3 import math
      4 import argparse
      5 
      6 def radians_from_degrees (deg):
      7     return 0.01745329251994329577 * deg;
      8 
      9 def square(x):
     10     return x**2
     11 
     12 # copied almost directly out of the perf aware programming listing
     13 def haversine(x0,x1,y0,y1, radius):
     14 
     15     lat1 = y0
     16     lat2 = y1
     17     lon1 = x0
     18     lon2 = x1
     19 
     20     dLat = radians_from_degrees(lat2 - lat1);
     21     dLon = radians_from_degrees(lon2 - lon1);
     22     lat1 = radians_from_degrees(lat1);
     23     lat2 = radians_from_degrees(lat2);
     24     
     25     a = square(math.sin(dLat/2.0)) + math.cos(lat1)*math.cos(lat2)*square(math.sin(dLon/2));
     26     c = 2.0*math.asin(math.sqrt(a));
     27     
     28     res = radius * c;
     29     return res;
     30 
     31 
     32 parser = argparse.ArgumentParser()
     33 parser.add_argument('filepath')
     34 parser.add_argument('num')
     35 args = parser.parse_args()
     36 
     37 def rnd_y():
     38     return ((random.random() * (90*2)) - 90)
     39 
     40 def rnd_x():
     41     return ((random.random() * (180*2)) - 180)
     42 
     43 rndx = []
     44 rndy = []
     45 hav = []
     46 
     47 def genJson():
     48     x0 = rnd_x()
     49     y0 = rnd_y()
     50     x1 = rnd_x()
     51     y1 = rnd_y()
     52 
     53     rndx.append(x0)
     54     rndx.append(x1)
     55 
     56     rndy.append(y0)
     57     rndy.append(y1)
     58 
     59     st = f"{{\"x0\" : {x0}, \"y0\" : {y0}, \"x1\" : {x1}, \"y1\" : {y1}}}"
     60     hav.append(haversine(x0,x1,y0,y1, 6372.8))
     61     return st
     62 
     63 with open(args.filepath, 'w') as f:
     64     genCount = int(args.num)
     65     lns = ["{\"pairs\": ["]
     66     for i in range(genCount - 1):
     67         lns.append(genJson() + ",")
     68     lns.append(genJson())
     69     lns.append("]}")
     70     f.writelines(lns)
     71 
     72 print("Avg x: " + str(sum(rndx) / len(rndx)))
     73 print("Avg y: " + str(sum(rndy) / len(rndy)))
     74 hvS = str(hav)
     75 
     76 with open(args.filepath + ".txt", 'w') as f:
     77     f.writelines([hvS])
     78 
     79 
     80 h = hashlib.new('sha256')
     81 h.update(hvS.encode())
     82 print(h.hexdigest())
     83 with open('artifacts/sha.txt', 'w') as f:
     84     f.writelines([str(h.hexdigest())])