Guest User

Untitled

a guest
Dec 8th, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.02 KB | None | 0 0
  1. wget -O - "https://www.worldometers.info/coronavirus/worldwide-graphs/" | grep "Currently Infected" -A 4 | grep data: | sed 's/^[^\[]*//' | sed 's/[]].*}/]/' | ./percentagechange.py
  2.  
  3.  
  4.  
  5. #!/usr/bin/env python3
  6. import sys
  7. import json
  8.  
  9. # loop boilerplate
  10. def skipper(sk):
  11.     def s():
  12.         nonlocal sk
  13.         if (sk > 0):
  14.             sk -= 1
  15.             return True
  16.         return False
  17.     return s
  18.  
  19. # load data from stdin and parse as JSON
  20. sData = ""
  21. for line in sys.stdin:
  22.     sData += line
  23. jData = json.loads(sData)
  24.  
  25. # convert data and append to output array
  26. # calculate initial case outside loop
  27. skip = skipper(1)
  28. pData = [100] # for sake of argument, 0 --> whatever is a 100% increase
  29. last = jData[0]
  30. for i in jData:
  31.     if ( skip() ): continue #don't calculate initial case inside loop
  32.     percentageChange = ( ((i*100)/last) -100 )
  33.     percentageChange = round(percentageChange,2)
  34.     pData.append ( percentageChange)
  35.     last = i
  36.  
  37. # print output array, by coincidence this is valid JSON
  38. print (pData)
Advertisement
Add Comment
Please, Sign In to add comment