0

I am new to python.

Tell me how to implement saving the value of a variable to a file so that you don't receive unnecessary notifications when the program is restarted.

The program pings the servers and sends a message when the status changes. When you restart the program, it does not save the history of its checks. It is very uncomfortable.

I hope I explained the problem correctly?

I need to constantly save the check result to a file and use this data when comparing new checks.

def ping_host(address):

    status = ping_url(address.address)

    if status != address.status:
        send_message(( "! " if status is None else "+ " if status else 
                     "- ") + address.comment)
        address.status = status

This function checks the status, if it has changed, then a new message is sent.

Alexandr
  • 9
  • 2
  • You should use: [python config files parser](https://docs.python.org/3/library/configparser.html) – Jean-Marc Volle Oct 28 '20 at 07:32
  • Did you google how to write something to a file? How to read something from a file? Your code does not show any attempt at writing/reading. 'There are already dozends of questions regarding file writing, f.e. [this](https://stackoverflow.com/questions/9536714/python-save-to-file/9536741) on SO. Persisting stuff to file is handled in the documentation here [Input&Output, reading and writing files](https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files). – Patrick Artner Oct 28 '20 at 08:19
  • Currently this question is a "give me code" request with no own efford spent and unclear where the real problem is - "I do not know how to do X" is no reason for a SO-Question. "I tried Y and this happened, I read up on it _here_ and that is my problem with it" would probably be one. – Patrick Artner Oct 28 '20 at 08:21

3 Answers3

1

If your file does not need to be portable the simplest solution is to use python pickling. The drawback is that you cannot inspect the file manually or modify it for debuging purpose vs text based saving (eg ini files, json or simple txt). The main advantage is the ease of use as you can serialyze this way any python basic type.

Here is a simple example on how to use it:

import pickle

def get_status():
    with open('status','rb') as f:
        status = pickle.load(f)
        return status
        
def set_status(status:bool):
    with open('status','wb') as f:
        pickle.dump(status,f)


set_status(True)
s = get_status()
assert s

set_status(False)
s = get_status()
assert not s
Jean-Marc Volle
  • 3,113
  • 1
  • 16
  • 20
0

You can make a file history.txt, and then on startup open it, and read the last state, and if its different overwrite that state in the file and save.

hrvoje hemen
  • 3
  • 1
  • 3
  • Yes, I agree, but the state may change during any check. So I need to save the changes every time, on every check. But how to implement it in the code? – Alexandr Oct 28 '20 at 07:38
0

from what you wrote in comments I would change it to this:

import json


ping_data = dict()
with open('C:\ping_data.json') as file:
    data = json.load(file)


def ping_host(address):
    status = ping_url(address.address)
    if data['address.status'] != status:
        ping_data['address.status'] = status 
        send_message(("! " if status is None else "+ " if status else "- ") + address.comment)


ping_host(youraddress)
with open('C:\ping_data.json', 'w') as file:
    json.dump(ping_data, file, indent=2)

the way I would do this is using json library

import json

next thing I would create a dictionary in your script

saved_data = dict()

then whenever I receive an update I would store the value in dictionary

saved_data['info'] = updated_info

and export? it to a json file

with open('saved_data.json', 'w') as file:
    json.dump(saved_data, file, indent=2)

now whenever I open the program it would read that file like this

with open('saved_data.json') as file:
    data = json.load(file)

and then I would access variable data as a dictionary

for k in data:
    for info in data[k]:
        if info != updated_info
            saved_data['info'] = updated_info
Matiiss
  • 5,970
  • 2
  • 12
  • 29
  • I will need more time to sort this out. Thanks. – Alexandr Oct 28 '20 at 07:44
  • Did I understand correctly the essence and syntax of writing? `def init(): ping_data = dict() with open('C:\ping_data.json') as file: data = json.load(file)` and `def ping_host(address): status = ping_url(address.address) for k in data: for address.status in data[k]: if address.status != status ping_data['address.status'] = status send_message(( "! " if status is None else "+ " if status else "- ") + address.comment) with open('C:\ping_data.json', 'w') as file: json.dump(ping_data, file, indent=2)` – Alexandr Oct 28 '20 at 10:49
  • Error- python C:\main_3.py File "C:\main_3.py", line 13 data = json.load(file) ^ IndentationError: unindent does not match any outer indentation level I don’t have such a file ... Now I don’t understand how to make it myself) Or make it empty? – Alexandr Oct 28 '20 at 11:34
  • Made an empty file - main_3.py", line 61 ping_data['address.status'] = status ^ TabError: inconsistent use of tabs and spaces in indentation – Alexandr Oct 28 '20 at 11:54
  • you should definitely delete the file you made and check for indents in your code they have to match – Matiiss Oct 28 '20 at 12:12