Python - How do I.....

Author
Discussion

paulrockliffe

Original Poster:

15,956 posts

233 months

Saturday 7th January 2023
quotequote all
I'm in Visual Studio, I'm making some Python stuff, I've not done this before so I only vaguely know what I'm doing, but with the help of ChatGPT what I want to do is working. I have a load of scripts - are they called scripts? - that get data from an API and write to out to .json in a folder of my choosing. I have an authentication script too.

I want to set this all up so that it runs on my server - I think I can do that OK - but I want to control what happens when. I have a few scripts that I want to run daily, some scripts to run a few times a day and one script that will run every 5 minutes. The authentication script needs to run before all of the other scripts and each script needs to pickup a variable from the authentication. There's also a load of global variables that I need to set once and have used in all of the scripts.

What's the best-practice way of setting this up so I only have one copy of the shared variables and I only have one copy of the authentication script?


xeny

4,590 posts

84 months

Saturday 7th January 2023
quotequote all
Is the server Linux or Windows?

Crusoe

4,072 posts

237 months

Saturday 7th January 2023
quotequote all
If Windows you can convert the python into an exe which you can then use Windows scheduler or similar to trigger.

somouk

1,425 posts

204 months

Saturday 7th January 2023
quotequote all
Can you not write the auth script response to file for the other scripts to then utilise?

If the server is linux just set them up as cron jobs.

If it's a windows machine do you have python installed on it?

paulrockliffe

Original Poster:

15,956 posts

233 months

Saturday 7th January 2023
quotequote all
The server is Linux, it's running UnRaid, so I think there's a few options and I have briefly looked at a PLugin that is designed to run the Python Scripts and be schedulable.

I need to start with being able to understand how I pass variables between scripts, or building all the different elements in one script and being able to conditionally run them though. Like can a script scheduler contain the conditional logic and run different parts of the script, or is it more like an on-off switch and I need to build the logic into the script?

I can see how both approaches could work, but instinctively I feel like they should be in separate scripts, but don't know how to handle sharing variables between the scripts when done that way. Don't really know what to Google, so thought I'd ask for pointers.

paulrockliffe

Original Poster:

15,956 posts

233 months

Saturday 7th January 2023
quotequote all
somouk said:
Can you not write the auth script response to file for the other scripts to then utilise?

If the server is linux just set them up as cron jobs.

If it's a windows machine do you have python installed on it?
I'm doing the develepment on a Windows Machine, will have it over to Linux when I'm done. I did wonder about writing the responses out to files, but that doesn't sound like it would be the best way? Maybe it is, I don't know.

theaxe

3,566 posts

228 months

Saturday 7th January 2023
quotequote all
You might consider pickle for sharing variables/objects between scripts. To run them, cron should do what you need.

Another thought, you could use the auth script to set all of your variables as system environment variables...

Edited by theaxe on Saturday 7th January 17:13

Tycho

11,824 posts

279 months

Saturday 7th January 2023
quotequote all
I'm not a programmer but have tried a few scripts in python for work and I'd probably set the variables via a json file and then read it in with each script. Depending how large the authentication script is you might be better off just adding it into each script as a function and calling it that way.

paulrockliffe

Original Poster:

15,956 posts

233 months

Saturday 7th January 2023
quotequote all
Thanks, yeah I considered just doing authentication in each script, but I know that's likely to cause me problems as I'm implementing authentication in quite a basic way, enough to get this working with an occasional prod from me. Once it's up and running and my data is flowing as I need it to I'll eventually implement authentication in a slightly different way and it'll then run without a prod and conditionally so it doesn't bother the auth server when it already has valid credentials. It can gain valid credentials from any script, so I want to store the credential, store the expiry time and reference those on run to test whether it's needed or not.

I really should setup the file-write as a single module thing too eventually, which would need the same technique. I'll take a look at pickle while I'm researching.

selwonk

2,132 posts

231 months

Saturday 7th January 2023
quotequote all
You need to look to functionalise everything:

https://www.w3schools.com/python/python_functions....

Then, you can add all your functions to one module file that can be included across all your scripts OR your multiple scripts can be converted into one script.

For setting configuration variables and reusing them everywhere, the built in os.getenv() function will do what you need in a very straight forward way:

https://stackoverflow.com/questions/40216311/readi...

xeny

4,590 posts

84 months

Saturday 7th January 2023
quotequote all
paulrockliffe said:
I'm doing the develepment on a Windows Machine, will have it over to Linux when I'm done. I did wonder about writing the responses out to files, but that doesn't sound like it would be the best way? Maybe it is, I don't know.
I don't know if this is now considered a luddite approach, but why not simply use pipes to pass them between scripts?

paulrockliffe

Original Poster:

15,956 posts

233 months

Saturday 7th January 2023
quotequote all
Well I've never heard of pipes, so you tell me? 😀 Will Google it.

I tried using a .env file and it throws no errors, just prints "None". I've tried everything, but it basically can't see the file. ChatGPT says there nothing wrong. A little frustrating!

paulrockliffe

Original Poster:

15,956 posts

233 months

Sunday 8th January 2023
quotequote all
I decided to take a slightly different approach, I explained the programme to ChatGPT and asked it to give me a design. Then I asked it to write sample code for each element of the design. I told it which API I was calling. It's answer is that the API has a Python Library that I can import and the code it's written all looks great, really simple to manage and just call the library as required.

Except the library is a lie, being generous it's maybe assumed from my question that I'm referring to an existing library, but it's hubristically given me the name of the library with absolute certainty, so who knows.

So close to an easy solution!

BUT, on checking whether the Python library exists I discovered there is a .net library, so I presume rather than 'learning' python I just 'learn' .net instead. I'll see what ChatGPT has to say about the python library lie and then ask it to write the programme for .net instead.

I obviously have even less idea what I'm doing now, so all or some of the above may be rubbish, does switching to .net come with any problems? My server is running a Windows VM already, so if I have to run in Windows that's no bother.

theaxe

3,566 posts

228 months

Sunday 8th January 2023
quotequote all
What does this library do? There’s a good chance of a python equivalent.