-
Notifications
You must be signed in to change notification settings - Fork 0
/
primeslib.py
35 lines (30 loc) · 1.17 KB
/
primeslib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# DOCS
# When executed in IDLE, you can use the p variable.
# When imported, you must construct the class. To do this, use
# <variableName> = primeslib.Primes()
# You can provide an alternative source by passing it as an argument. It must
# be a JSON list of prime numbers.
#
# To get the primes as a list, do <variableName>.primes (when in IDLE, p.primes)
# To refresh the primes, do <variableName>.refresh()
import json
import urllib.request as url
import ssl
class Primes:
def __init__(self, src="http://ictman1076.eu.pythonanywhere.com/primes.txt"):
self.src = src
self.refresh()
def refresh(self, src="default"):
if src == "default":
src = self.src
# ssl stuff, so it works in areas with ssl interception - it's not like
# this is secure data so it doesn't matter about the cert.
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE
self.request = url.urlopen(src, context=ctx)
self.rawData = self.request.read()
self.data = self.rawData.decode()
self.primes = json.loads(self.data)
if __name__ == "__main__":
p = Primes()