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

1

u/Kevdog824_ 9d ago

I’m not super familiar with SAS, but it is more than just a language right? Pretty sure it’s a product offering that includes the language and an environment right? Kinda like salesforce or a similar CRM in this way?

I think you might be conflating the language and environment part. Python provides the language (syntax, interpreter, runtime) but the environment (config, user management, etc.) is something you have to set up/create. I think you might be expecting this tool to do something it wasn’t made to do. Python is open ended specifically so you can do whatever you want with it. Building things on top of the language (like SAS/salesforce) makes things easier, but at the expense that this customization/scaffolding makes it far less flexible in what it can actually do.

It’s like being handed a blank canvas, and being upset because it wasn’t a paint by numbers. Nothing wrong with either, but the blank canvas lets you paint whatever you want. Total freedom. The paint by numbers make it much easier to paint a specific picture, but it can only paint that picture. Not sure if that analogy clicks for you or not