Dwika Haryo Radithya
Dwika Haryo Radithya

Reputation: 67

Live Ever Changing Updating List in Python

I'm very new in python and it seems that after searching all over I can't seem to find a solution for this. So what I'm trying to do is have a blank list with nothing in it then have a variable that appends data that is always changing per day to that list. What I wanted to have is how do you update the list of changes like that happen. I'll try to explain it in this code

l = []
sum = data[0][1]
l.append(sum)

Now, this sum variable value is always changing every day or every second just like taking data from a JSON File that is constantly being updated. What I want to do is that the list just wouldn't change but adds the ever-changing sum each time it changes so if sum = 1 on the first day and then sum = 2 on the second day I want the list to be updated so it would be [1] on the first day then [1,2] on the second day. I'm really confused about which part of python I should learn just to be able to do this if anyone has any suggestions it would really help for my first project in python. Thank you.

Upvotes: 1

Views: 1289

Answers (2)

Alexis
Alexis

Reputation: 57

There are various ways to do what you are trying to do.

Let's first understand why your code is not working. Since Python does not pass by reference, when you are modifying sum, it won't affect your list. In fact Python does not pass by values either. (You can dive more into this here)

However you can use an object wrapper. Then the list will have an unchanged reference to the object. Finally you can modify the object.

class Sum:
  def __init__(self, value):
    self.value = value

l = []
sum = Sum(data[0][1])
l.append(sum) 

sum.value = ...

Afterwards you can init your list of sums and update them.

Upvotes: 0

Thomas
Thomas

Reputation: 899

This calls for use of a loop. Here is an example using an infinite loop (while True).

import time  # for "waiting" a day

N_SEC_IN_DAY = 60 * 60 * 24  # 1 day in seconds
list_ = []
while True:
    sum_ = getsum()  # defined elsewhere
    list_.append(sum_)
    time.sleep(N_SEC_IN_DAY)

Also, note the several stylistic changes I made.

  • list -> list_ to avoid clashes with built-in functions.

  • sum -> sum_ to avoid clashes with built-in functions.

If you want to access this list while the program is running, you might want to add a logger that logs the values in list_ every time a new sum_is added.

Add the following lines to the top of your code:

import logging
logging.basicConfig(filename='log', level=logging.INFO)

Add change the loop to say:

while True:
    sum_ = getsum()  # defined elsewhere
    list_.append(sum_)
    logging.info(str(list))  # LOGGING TAKES PLACE HERE
    time.sleep(N_SEC_IN_DAY)

Finally, I would like to point out that a better way to do this would be to use crontabs to schedule jobs. I'm not going to go in-depth about that because there are plenty of tutorials online, just google "crontab." (Thanks to commenter DevanshSoni for the reminder.)

Upvotes: 1

Related Questions