So I have been trying to figure out a function where I can compare two dictionary or list (Not sure what is more optimal in my case but basically what im trying to do is that I need to make a dictionary that will have each value i monitor (in my case the numbers), when something restocks it will check that dictionary and see if the item is in there, if it’s not it will add it. Once it’s added it will put in each number that restocked and a time stamp per number. Next run, if something restocks it will takes these numbers and check if they are in the dictionary, if they are it will compare the current timestamp with the ones inside the dictionary. Basically putting a cooldown per number.
What I have done so far is:
import random
import time
def randomNumber():
numbers = []
for x in range(3):
numbers.append(random.randint(1, 10))
return numbers
def demo():
firstList = randomNumber()
while True:
secondList = randomNumber()
print(secondList)
time.sleep(1)
demo()
and I wonder how I can be able to make a sort of comparing between timestamps?
EDIT:
Managed to play around with datetime:
import random
import time
import sys
from datetime import timedelta, datetime
def randomNumber():
numbers = []
for x in range(3):
numbers.append({random.randint(1, 10): datetime.now()})
time.sleep(random.randint(1,3))
return numbers
def demo():
firstList = randomNumber()
print(firstList)
for hehe in firstList:
for att, value in hehe.items():
timenow = datetime.now()
elapsed = timenow - value
if elapsed > timedelta(seconds=10):
print(f"tiemdelta {hehe}")
else:
print(f"Not in tiemdelta {hehe}")
time.sleep(1)
sys.exit()
while True:
secondList = randomNumber()
time_difference = a - b
time_difference_in_minutes = time_difference / timedelta(minutes=1)
sys.exit()
print(secondList)
time.sleep(1)
demo()
Aucun commentaire:
Enregistrer un commentaire