Updating Value of previously created variable after certain Operations at the same place

Hi, I want value of variable previous to get changed after performing if condition to equal to len(data_json) for next run.

previous = 701
print(previous)
if len(data_json) > previous:
    new_data = len(data_json) - previous
    num = -new_data
    ids = ids[num:]

print(previous)

Hello, @ritika123, and welcome to Python Discourse!

Since you have not posted all of your code, we cannot see how the variable data_json was initialized. Please provide us with that information so we can help.

How would you like to save the state of your data between runs of the program? Is that data contained in another file?

If your program is too long to post in its entirety, please provide us with similar code in a shorter form that we can test.

Hi @Quercus,

Thanks for your response. So data_json variable contains the ticket details fetched from Connectwise Manage through API endpoint. and the count of ticket get increased after certain time, so the len(data_json) will be increased. and here is the code:

headers = {
    "Authorization": f"Basic {credentials}",
    "Accept": "application/vnd.connectwise.com+json; version=2022.1",
    "clientid":client_id
    # "Content-Type": "application/x-www-form-urlencoded"
}

## DETAILS OF TICKET

api_endpoint = "https://api-na.myconnectwise.net/v4_6_release/apis/3.0/service/tickets?page=399&pagesize=1000"
response = requests.get(api_endpoint, headers=headers)
# print(response.text)
data_str = response.content.decode('utf-8')
# print(data_str)
data_json = json.loads(data_str)

Where does the variable credentials referenced here get its value?:

    "Authorization": f"Basic {credentials}",

Why can’t you just add previous = new value inside the if?

1 Like

Are you familiar with while loops? It might be appropriate to loop until the condition in this if header is met:

if len(data_json) > previous:
1 Like

I don’t think that’s particularly relevant, plus it’s almost certainly going to include, well, some credentials :slight_smile:

That’s true; we could just use some placeholder during testing.

1 Like

My question regarding credentials was not stated well. What we really need is to find out about the intended flow of execution during a run of the program. For example, during an individual run, is there a single logging in event followed by repeated retrieval and checking of the data, or is there a different flow of events? With that information, we can figure out whether we need a loop, a saving of data in a local file, or other arrangements.

I suppose you could include the assignment as part of the expression in the if, as an assignment expression.

Maybe someone could figure out a better way, but I guess this could do

done = None
if (len(data_json) > previous) ^ ((previous := len(data_json)) is done):
  # Body of the IF

Wait! What do you mean by “after performing if condition”? Do you mean after the entire if (condition + body), or after evaluating the condition? The above is because I understood the latter.

The important question here is: what do you mean by “next run”? The code that is shown here will only run once, as is. If you have something outside of this code that will run this part multiple times… then the value of previous needs to be remembered outside of the part that runs multiple times.

I’ve refrenced that, above the code which I haven’t shared on portal

I want to schedule this code to run after every few minutes. and in every next run I want the value of previous to become equal to len(data_json) variable.

Do you know how to schedule a program to run periodically on your operating system? You will need to find out how to do that.

Each time the program runs, you will need to write out the value of len(data_json) into a file, so that it can be loaded next time the program runs.

Following is a simplified example of how to save a value from a run, so that it can be loaded for the next run:

# get the previous data
with open('previous_data.txt', 'r') as dfile:
    dfile = open("previous_data.txt", "r")
    previous = int(dfile.read())

# display the previous data
print(previous)

# write the updated previous data
previous += 1
with open('previous_data.txt', 'w') as dfile:
    dfile.write(str(previous))

Prior to the first time the above example is run, you will need to create the "previous_data.txt" file. You can put a 0 or any other number in it to get started. Subsequently, note how that number is updated each time the program is run.

You will, of course, need to revise the specifics of that example to adapt it to your needs.

EDITED to revise one of the comments in the code example.