George1120901 / __2024_01_08_python_openAPI__

0 stars 0 forks source link

練習:儲存資料至redis,並可以取出全部資料 #5 #1

Open George1120901 opened 9 months ago

George1120901 commented 9 months ago

![Uploading 練習.png…]()

George1120901 commented 9 months ago
import redis
import time
from time import strftime
redis_conn = redis.Redis.from_url('rediss://red-xxxxxxxxxxxxxxxxx@singapore-redis.render.com:6379')

time_timeticks = []
lights = []
distances = []
lux = 1000

for i in range(0, n): #n->int 次數
    time_timeticks.append(strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    lights.append(round(lux/(i+1)))
    distances.append(round((i+1)*100))
    redis_conn.rpush('pico:timeticks', time_timeticks[i])
    redis_conn.hset('pico:lights', mapping={time_timeticks[i]: lights[i]})
    redis_conn.hset('pico:distances', mapping={
                    time_timeticks[i]: distances[i]})
    time.sleep(sec)

id_list = [item.decode()
           for item in redis_conn.lrange('pico:timeticks', 0, -1)]

for id in id_list:
    light = redis_conn.hget('pico:lights', id).decode()
    distance = redis_conn.hget('pico:distances', id).decode()
    print(f'Time:{id}\n亮度(lux):{light}\n距離(m):{distance}')
    print('--+++++++++++++++++++++++++--')

redis_conn.close()
George1120901 commented 9 months ago

![Uploading result.JPG…]()