Home > front end >  How to transform current date and use it as a parameter in function?
How to transform current date and use it as a parameter in function?

Time:11-10

I want to create a script that run at a specific date everyday given the variable status as a parameter.

The status can take two entries: "past" or "present".

I want to save the current date as a parameter and use it after in a function to make different calls. So the current time becomes also a parameter.

For example, if I set the script to be running every day at 2:30 I want to be able to retrieve the date and use it as a parameter

I want to run the script with the status "present" with the day of today as a parameter and for the "past" for example, use the day of today minus 4 hours.

If my script is taking too long to run I want to use the date of today rounded to the last half hour.

So if I run my script the on the 8th of November 2022 at 5:32 I should get year=2022 month=11 day = 8 time =5:30 for the present and year=2022 month=11 day = 8 time =4:30 for the past

def function(past_or_present,year,month,day,time):
 if  past_or_present =="past":
   url = 'https://awebsite{year}/{month}/{day}/{time-1hour}  
   resp = requests.get(url=url).text 

 if  past_or_present =="present":
   url = 'https://awebsite{year}/{month}/{day}/{time} 
   resp = requests.get(url=url).text

    function("past")
    function("present")

CodePudding user response:

You'll probably want to use the datetime moduel python ships with.

import time
from datetime import date

today = date.today()

day = today.day
month = today.month
year = today.year

That in combination of the time moduel has some great built in functions for converting times from one to the other.

The datetime moduel even has a timedelta method for calculating the difference in time given two dates or times.

As far as

...script is taking too long to run I want to use the date of today rounded to the last half hour

That's going to get a bit more into the weeds. There's a few different methods of calculating how long a function takes, but to actively monitor and kill a process that takes too long is going to be tricky.

By default, Python is a single threaded application, meaning you can only have one thing run at the same time. So how would you be able to actively monitor if something is taking too long, stop that process, and start a new one with a different value? With another thread/process of course.

The multiprocessing library might be good if this function is expected to take a long time and hit the CPU hard with a crunchy calculation.

The threading moduel is great if the process is IO/network bound and you have a few cycles that you expect to just wait for data. You still have to deal with a Global Interpreter Lock though and that gets complicated. The short of the GIL is that Python locks the memory it works with so that multiple processes can't access and cause racing conditions.

There's also the option of asyncio.

The TL;DR, you need to have some form of processs/thread to spawn and then monitor the function you are trying to run, kill it if it takes too long, and then run another function.

Here is an example of what you might be looking for using the multiprocessing moduel.

Someone may have built a third party module that would do the timing and ending bit alot easier but these are the modules that ship with Python. If anyone knows of one, please leave a comment.

  • Related