Variable within a Variable in Python (3)

2024/10/7 10:22:08

My head is probably in the wrong place with this, but I want to put a variable within a variable.

My goal for this script is to compare current versions of clients software with current software versions that are available from the vendor. At this stage I just want to print out what's available.

I have some def's setup with:

def v80(program_1 = '80.24', program_2 = '80.5', program_3 = '80.16'):pass
def v81(program_1 = '81.16', program_2 = '81.7', program_3 = '81.14'):pass
def v82(program_1 = '82.15', program_2 = '82.4', program_3 = '82.9'):pass
def v83(program_1 = '83.01', program_2 = '83.0', program_3 = '83.1'):pass

I'm then reading all of the clients versions from a text file and doing comparisons.

One of the vars I'm generating is "program_main", currently I'm doing something like:

If program_main == "83":if program_1:if v83['program_1'] > float(program_1):print ("Update available", program_1, "-->", v83[program_1])if program_2:if v83['program_2'] > float(program_2):print ("Update available", program_2, "-->", v83[program_2])
if program_main == "82"if program_1:if v82['program_1'] > float(program_1):print ("Update available", program_1, "-->", v82[program_1])

etc etc

My train of though would be something like

if program_1:if v[program_main] > float(program_1):print('Update available", program_1, "-->", v[program_main])

etc etc

I'm sure there's a much better way to do this entire setup, but this is one of my first proper python scripts so I'm happy to chalk it up to noobish-ness, just wanted to know what the right way of doing what I'm trying to achieve is.

Answer

You can put your functions into a dictionary:

per_version = {'83': v83,'82': v82,
}

and simply use that to map string to function:

per_version[program_main]('program_1')

However, you may want to instead parameterise your version functions; make one function that takes the version as a parameter:

def program_check(version, program_1=None, program_2=None, program_3=None):# ...

which then looks up default values per program_x parameter based no the version, again from a dictionary perhaps.

https://en.xdnf.cn/q/118836.html

Related Q&A

selenium scraping data using children of elements

Hi im trying to scrape some data from a live stocks website. I want to display the companies name and stock price, %change ect. The details of 25 companies are shown per page, and these details follow …

Python - ETFs Daily Data Web Scraping

Im trying to web scrape some daily info of differents ETFs. I found that https://www.marketwatch.com/ have a accurate info. The most relevant info is the open Price, outstanding shares, NAV, total asse…

How to create DataFrame with columns based on scraped data?

import requests, re from bs4 import BeautifulSoupdata = []soup = BeautifulSoup(requests.get(https://www.booking.com/searchresults.html?label=gen173nr-1FCAEoggI46AdIM1gEaGyIAQGYATG4ARfIAQzYAQHoAQH4AQKI…

How do i change the colour of a button border tkinter

How do i change the colour of a border in tkinterI have looked at other solutions which recommended using highlightcolor and highlightbackground, however these did not work. excercises_button = Button(…

module object has no attribute Gridspec despite calling help(gridspec) revealing the Gridspec class

If I run the python console and doimport matplotlib matplotlib.__version__ import matplotlib.gridspec as gsI see that the matplotlib version is 1.2.1.If I do help(gs) I see the Gridspec class.However t…

Python division doesnt work as expected for large numbers [duplicate]

This question already has answers here:What class to use for money representation?(6 answers)Closed 9 months ago.I have three variables a, b and c. I want to make sure that after doing this: c -= a*bc…

working out an average of the values in a dictionary

My dictionary as of now is like this:class_1 = {Bob:[9,5,4,3,3,4], John:[5,5,7,3,6], Andy:[7,5,6,4,5], Harris:[3,4,2,3,2,3,2]}What i am trying to make it look like is this:class_1_average ={Bob:[averag…

getting an error when trying to import a list into a mysql table

whenever i try to add a list into the mysql table I get an error : ProgrammingError: Not all parameters were used in the SQL statementive tried to look online but all i could found is that i need to us…

Getting a view does not return a valid response error message on my flask chatbot [duplicate]

This question already has answers here:Flask view return error "View function did not return a response"(3 answers)Closed 3 years ago.Trying to create a whatsapp bot on Twilio that limits the…

Django how to add data to Object from queryset

I would like show list of clients and show tags assigned to them but I have problem because I have my tags in other table and I dont know how to connect data together. Clients can have couple of tags o…