Guest User

NYT account for rounding

a guest
Nov 12th, 2020
42
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. """This is just quickly put together; still not perfect (e.g., logic should be put in so that an (n)-digit number subtracted from an (n+1)-digit number resulting in an (n)-digit number rounds the result to have only two significant figure)."""
  2.  
  3. import json
  4. import requests
  5. import sys
  6. import math
  7.  
  8. round_to_n = lambda x, n: x if x == 0 else round(x, -int(math.floor(math.log10(abs(x)))) + (n - 1))
  9. statedata = {'Alabama': {'ts':[]},'Alaska': {'ts':[]},'Arizona': {'ts':[]},'Arkansas': {'ts':[]},'California': {'ts':[]},'Colorado': {'ts':[]},'Connecticut': {'ts':[]},'Delaware': {'ts':[]},'Florida': {'ts':[]},'Georgia': {'ts':[]},'Hawaii': {'ts':[]},'Idaho': {'ts':[]},'Illinois': {'ts':[]},'Indiana': {'ts':[]},'Iowa': {'ts':[]},'Kansas': {'ts':[]},'Kentucky': {'ts':[]},'Louisiana': {'ts':[]},'Maine': {'ts':[]},'Maryland': {'ts':[]},'Massachusetts': {'ts':[]},'Michigan': {'ts':[]},'Minnesota': {'ts':[]},'Mississippi': {'ts':[]},'Missouri': {'ts':[]},'Montana': {'ts':[]},'Nebraska': {'ts':[]},'Nevada': {'ts':[]},'New-Hampshire': {'ts':[]},'New-Jersey': {'ts':[]},'New-Mexico': {'ts':[]},'New-York': {'ts':[]},'North-Carolina': {'ts':[]},'North-Dakota': {'ts':[]},'Ohio': {'ts':[]},'Oklahoma': {'ts':[]},'Oregon': {'ts':[]},'Pennsylvania': {'ts':[]},'Rhode-Island': {'ts':[]},'South-Carolina': {'ts':[]},'South-Dakota': {'ts':[]},'Tennessee': {'ts':[]},'Texas': {'ts':[]},'Utah': {'ts':[]},'Vermont': {'ts':[]},'Virginia': {'ts':[]},'Washington': {'ts':[]},'West-Virginia': {'ts':[]},'Wisconsin': {'ts':[]},'Wyoming': {'ts':[]}}
  10.  
  11. def main():
  12.     for state in statedata:
  13.         url = "https://static01.nyt.com/elections-assets/2020/data/api/2020-11-03/race-page/" + state.lower() + "/president.json"
  14.         data = requests.get(url, allow_redirects=False)
  15.         with open(state + '.json', 'wb') as f:
  16.             f.write(data.content)
  17.         with open(state + '.json', encoding="utf8") as f:
  18.             statedata[state].update({'json': json.load(f)})
  19.         for i, ts in enumerate(statedata[state]['json']['data']['races'][0]['timeseries']):
  20.             statedata[state]['ts'].append({
  21.                     'votes': ts['votes'],
  22.                     'tshare': ts['vote_shares']['trumpd'],
  23.                     'bshare': ts['vote_shares']['bidenj'],
  24.                     'timestamp': ts['timestamp']
  25.                 })
  26.         for i, ts in enumerate(statedata[state]['ts']):
  27.             ts.update({
  28.                     'tvotes_crude': ts['votes']*ts['tshare'],
  29.                     'bvotes_crude': ts['votes']*ts['bshare']
  30.                 })
  31.             ts.update({
  32.                     'tvotes': round_to_n(ts['tvotes_crude'],3),
  33.                     'bvotes': round_to_n(ts['bvotes_crude'],3)
  34.                 })
  35.             if i == 0:
  36.                 ts.update({
  37.                         'tdelta_crude': 0,
  38.                         'bdelta_crude': 0,
  39.                         'tdelta': 0,
  40.                         'bdelta': 0
  41.                     })
  42.             else:
  43.                 ts.update({
  44.                         'tdelta_crude': ts['tvotes_crude'] - statedata[state]['ts'][i-1]['tvotes_crude'],
  45.                         'bdelta_crude': ts['bvotes_crude'] - statedata[state]['ts'][i-1]['bvotes_crude'],
  46.                         'tdelta': round_to_n(ts['tvotes'] - statedata[state]['ts'][i-1]['tvotes'],3),
  47.                         'bdelta': round_to_n(ts['bvotes'] - statedata[state]['ts'][i-1]['bvotes'],3)
  48.                     })
  49.             print(state + "[" + str(i) + "]\ttvotes: " + str(ts['tvotes']) + "\ttdelta: " + str(ts['tdelta']) + "\tbvotes: " + str(ts['bvotes']) + "\tbdelta: " + str(ts['bdelta']))
  50.  
  51. if __name__ == "__main__":
  52.     main()
  53.  
  54.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×