0

I'm writing a program in python which has three modules: 1. settings.py 2. init.py 3. main.py

settings.py just has one Boolean variable, the goal is to use it as a global variable(let's call it var, initialised as False ) across modules

init.py imports the var, and sets it to True after performing some actions in a function

main.py has to check whether var == True. If yes, proceed with the program else ask user to give some command line instructions (say, prg start) which will then call a function in init.py, which in turn will also set var==True

The problem is that since I have made a program that takes arguments from CLI, the var is always set to False. So the only command that CLI accepts is prg start else it prints the error message that I have written.

I need some method to let var stay True for rest of the session, and session actually translates to until computer is switched off.

code for main.py

first = sys.argv[0]
arguments = sys.argv[1:]

second = arguments[0]


if settings.var == False and second != "start":
    print "Error, type \'prg start\' to start"
    sys.exit(0)

if second == "start":
    init.start()

This is what I'm trying to accomplish:

pi@raspberrypi: ~$ prg start

pi@raspberrypi: ~$ prg set color 4

Success

what is happening right now:

pi@raspberrypi: ~$ prg start

pi@raspberrypi: ~$ prg set color 4

Error, type 'prg start' to start

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
Breakpoint
  • 428
  • 6
  • 19

2 Answers2

0

You can use a file to store the variable permanently!

Shubham Sharma
  • 714
  • 1
  • 8
  • 18
  • Do you mean that instead of using global variable I should use a file and read and write that? In that case, how can the variable become `False` automatically if I shutdown? – Breakpoint Mar 27 '19 at 06:42
  • Exactly as mentioned by @patrick-artner! Use the filename as the last boot time. That way, you have a new file for each boot. – Shubham Sharma Mar 27 '19 at 07:35
0

You need some kind of persistent storage that goes away on shutdown. It must also be persistent although your program is not currently running.

Solutions that come to mind:

File:

Write the last boot time into a file, also add last 'prg start time - figure out if latter came after former:

  • Linux: last reboot + datetime of your last 'prg start' call - you can figure out if reboot came before/after
  • Windows: systeminfo | find /i "Boot Time" (Or whatever it is called in your language), same principle

The file does not vanish, but your "last reboot time" will reset on reboot. You could simply get away with collecting'prg start' timestamps in your file and check "live" against the current value of "last reboot" if started with other parameters.

Server/Client:

Spawn an independent "server" process on first 'prg start' - check if that process runs using

The "server" process will vanish on reboot (or when manually killed - but so does the file if you delete it...)

Related:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Thanks @patrickartner. For this I'll need a python script that can automatically write `who` command and parse the `date and time` from it. Can you guide me in an appropriate direction? Also, how can I get `date-time` for `prg start`? – Breakpoint Mar 27 '19 at 08:54
  • @Total No I can not guide you. Lookup the [module datetime](https://docs.python.org/3/library/datetime.html) and its `.now()` method to get the current time. You can search if the systeminfo command is already python wrapped - if not, call it from your script and pipe the result into a file (f.e.) and read that in and get the stuff you need from it. Divide and conquer the tasks you need to accomplish - if you get stuck on one of them, ask a new question. – Patrick Artner Mar 27 '19 at 10:04