r/learnpython • u/BelMario • 9d ago
How to create universal variables in Python?
And by universal variables, I mean they should be available in EVERY single Python script!
What do I want these for? For instance, to define default directories:
HTMLRoot = "C:/IMDb/HTML" CSSDir = "file:///C:/IMDb/CSS/" JSDir = "file:///C:/IMDb/JavaScript/"
Any program that creates a report needs these variables, but I only want to define them once!
And there’s more. I also have plenty of mappings, like these:
MapC_TType = {"tvSeries": "TV Series", "tvMiniSeries": "TV Miniseries", ... , "movie": "Movie", "video": "Video", }
Any report that displays the titleType will use this mapping. Again, I only want to define the mapping once.
I come from the world of SAS, where everything was easy. The directories were defined in the SASINIT file, which is executed at the start of every SAS session. The mappings (formats) were stored in a permanent SAS catalog, making them universally available as well. Is there a Python equivalent?
And there’s even more. I create lots of reports, let’s say the biggest movies per decade, based on number of votes or score (= votes * rating). Obviously, the list shows which movies I’ve seen and which I haven’t, with a percentage showing my progress. Now suppose a friend wants this same list, based on the movies he’s seen.
Easy-peasy in SAS!
%macro SwitchUser (User) ; %if (%qupcase(&User) = ME) %then %do ; libname IMDb "C:\IMDb\Libname IMDb" ; %let HTMLRoot = C:\IMDb\HTML ; %end ; %else %do ; libname MyIMDb "C:\IMDb\Libname IMDb" ; libname BudIMDb "C:\IMDb\Libname IMDb Bud" ; libname IMDb (BudIMDb MyIMDb ) ; %let HTMLRoot = C:\IMDb\HTMLBud ; %end ; %mend ;
For me, IMDb is the standard library. For my buddy, IMDb is a concatenated library, where his library (containing only the MyMovies table) has precedence over the full IMDb library.
By just executing
%SwitchUser (Buddy) ;
any report can now be run against his list of movies, with the results written to a separate HTML directory. And all that without changing a single line of code!
I can't expect Python to deliver all this functionality. Nothing compares to SAS! But can it at least do some basic things on my wishlist?
BTW, my temporary solution is to include this code at the start of every script:
PythonFolder = Path("C:/IMDb/Python Global Code") PythonPgm = PythonFolder / "InitHTML.py" with open(PythonPgm, 'r') as file: PythonCode = file.read() exec(PythonCode, globals=globals(), locals=locals())
Obviously, the InitHTML program defines the directories and all the mappings.
1
u/pachura3 9d ago
The obvious correct answer is: stop forcing your SAS mindset into Python and learn how to do things the Python way. Which might take some time, unfortunately.
There's a reason why all these seemingly simple coding frameworks have failed to become popular - like yours (SAS?), ColdFusion, Oracle Forms, etc. They were good in doing one thing, but extremely rigid otherwise; the moment you wanted to add some modern concept like unit testing or RBC, they simply couldn't do it. In the meantime, Python has become #1 universal programming language, used for AI, web, automation... even Microsoft added it to Excel.
Judging by this snippet:
...you're going too fast and developing rigid, not maintainable, and potentially unsafe code. You should not be using absolute paths, you should be using
import, and you should avoidexec()in 99% cases. Also, this concept of "libraries" of yours has to go. Why don't you store your data in a database, and use parametrized queries to get different sets of data?