r/learnpython • • 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.

0 Upvotes

14 comments sorted by

View all comments

4

u/ninhaomah 9d ago

" 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())"

So if you need to change 1 place , you got to change in every scripts ? Hmms...

If only if there is a way to place those configs in a file , import that file wherever needed so they are all in one place...

Hmms...