Python Forum
Variables being overridden to initial values.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variables being overridden to initial values.
#5
Ok so I get all that, and changed my script, and yes it works. but I don't like it. BUT is works.

#!/usr/bin/python3*

'''
	Module name:
	Author:
	Contact: 
	Desc:
'''
__version__ = '0.0.1'

import shutil
import os, os.path
import datetime

TODAYS_DATE = str(datetime.date.today())
 
NUMBER_OF_BACKUP = 3
 
BACKUP_LOCATION = '/home/Superusr/Backup_Scripts/Test_01/Save_Location/'
FOLDER2BACKUP = '/home/Superusr/Backup_Scripts/Test_01/Sample_Folder'
 
CURRENT_BACKUP_NAME = FOLDER2BACKUP + "_" + TODAYS_DATE

CURRENT_LIST_OF_BACKUPS = []


def GET_MOST_RECENT_BACKUP_NAME():
	print("Step 2...")
	for names in os.listdir(BACKUP_LOCATION):
		CURRENT_LIST_OF_BACKUPS.append(names)
	
	CURRENT_LIST_OF_BACKUPS.sort()	
	
def SET_MOST_RECENT_BACKUP():
	print("Step 3...")
	RESULT = CURRENT_LIST_OF_BACKUPS[-1]
	return RESULT
		
def SET_OLDEST_BACKUP():
	print("Step 4...")
	RESULT = CURRENT_LIST_OF_BACKUPS[0]
	return  RESULT

def INITIAL_SETUP():
	print("Step 1...")
	GET_MOST_RECENT_BACKUP_NAME()
	
def main():
	INITIAL_SETUP()
	
	MOST_RECENT_BACKUP = SET_MOST_RECENT_BACKUP()
	OLDEST_BACKUP = SET_OLDEST_BACKUP()
	
	print("Most recent backup is: {}".format(MOST_RECENT_BACKUP))
		

if __name__ == "__main__": main()
I don't like it because it doesn't future proof the code, I know what I want to do next and I will have to switch it up.

Like, just moving "MOST_RECENT_BACKUP = SET_MOST_RECENT_BACKUP()" and "OLDEST_BACKUP = SET_OLDEST_BACKUP()"
out of the main() but after INITIAL_SETUP() function breaks it.

I guess it is a classic case of trying to run before you can walk.

Thanks again for your help.

After reviewing it some more, I understand why is breaks when the variable are breaking outside the main() function, because the intial_setup() function only get called then. Therefore not set before then.
Reply


Messages In This Thread
RE: Variables being overridden to initial values. - by p2bc - Oct-06-2020, 05:58 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Changing the initial worksheet name in an MS Excel file azizrasul 3 1,058 Oct-02-2022, 07:56 PM
Last Post: azizrasul
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,594 Jul-27-2022, 08:50 PM
Last Post: rob101
  How to move multiple columns to initial position SriRajesh 4 1,490 Jul-02-2022, 10:34 AM
Last Post: deanhystad
  Create array of values from 2 variables paulo79 1 1,177 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  variables vcnt, ocnt, and mcnt adding previous values and not resetting to 0 archanut 2 2,019 Feb-12-2021, 06:56 PM
Last Post: deanhystad
  Giving all possible values to four different variables quest_ 7 3,117 Jan-18-2021, 05:18 AM
Last Post: deanhystad
  Print variable values from a list of variables xnightwingx 3 2,729 Sep-01-2020, 02:56 PM
Last Post: deanhystad
  Assign dynamic values to Variables srikanthpython 6 3,560 Jun-06-2020, 03:36 PM
Last Post: srikanthpython
  Differential equations with initial condition in Python (change a working code) Euler2 1 1,900 May-29-2020, 04:06 PM
Last Post: Euler2
  Help with initial conditions Dantooine 1 1,409 Oct-18-2019, 07:22 PM
Last Post: UGuntupalli

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020