Commit 4ae89fb0 authored by Nane Kratzke's avatar Nane Kratzke
Browse files

Aktualisieren prime/rest.py

parent 98a1e924
......@@ -15,11 +15,11 @@ def lookup(key):
if not os.getenv('REDIS'):
return None
try:
data = redis.get(key)
if not data:
value = redis.get(key)
if not value:
log.info(f"Redis lookup of key {key} failed. No data.")
return None
return json.loads(data)
return value
except Exception as ex:
log.error(f"Redis lookup for {key} failed due to {ex}")
return None
......@@ -28,7 +28,6 @@ def cache(key, value):
if not os.getenv('REDIS'):
return None
try:
val = json.dumps(value, indent=3, sort_keys=True)
redis.set(key, val)
return val
except Exception as ex:
......@@ -39,13 +38,13 @@ def cache(key, value):
def number(number):
try:
n = int(number)
divisors = lookup(n)
if divisors:
return f"{ n } is not a prime number. It can be divided by {divisors} (from cache)"
divisors = [i for i in range(2, n) if n % i == 0]
cache(n, divisors)
if divisors:
return f"{ n } is not a prime number. It can be divided by {divisors} (processed)"
divisor = lookup(n)
if divisor:
return f"{ n } is not a prime number. It can be divided by {divisor} (from cache)"
for i in range(2, n // 2):
if n % i == 0:
cache(n, i)
return f"{ n } is not a prime number. It can be divided by { i } (processed)"
return f"Yes! random { n } is a prime number."
except Exception as ex:
log.error(f"Exception: { ex }")
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment