Python Forum
need coder to change to sched function in script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need coder to change to sched function in script
#1
Hi coders, The following code is supposed to run once per day. I need help changing the following code so that I can have it run every x hours. Also make sure it "keeps alive" after it has been initiated.

Since I'm new in this forum, I may need some guidance on how to proceed from here.
Thanks!

from __future__ import print_function
from requests import get
import json
from datetime import datetime,timedelta
from time import time,sleep
import time as tm
from os.path import isfile, join
import csv
import utils
from mysql.connector import connect
from urllib import urlencode

CREDS = {
    'user':'user',
    'password':'password',
    'host':'host',
    'database':'sentxawsdb',
    'raise_on_warnings': False
    }
HEADERS = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.90 Safari/537.36',
    }

def print_wait(hour,minute):
    now = datetime.now()
    nxt  = datetime.now().replace(hour=hour, minute=minute,second = 0)
    if ((now-nxt).total_seconds()) > 0 :
        nxt  += timedelta(day = 1 )

    while datetime.now() < nxt :
        dlt = nxt - datetime.now()
        print('NEXT RUN AT :: {}, IN :: {}'.format(nxt.strftime('%Y-%m-%d %H:%M:%S'),str(dlt).split('.')[0]), sep=' ', end='\r')  # No need for sep here, but okay :)
        sleep(1)
    print ('*'*60)

def read_input():
    with open('input.json') as f :
        inputs = json.load(f)
    return inputs

def insert_data(res):
    try :
        cnx = connect(**CREDS)
        check_tabes(cnx,res)

        query ='INSERT IGNORE INTO {} (TIME_,VALUE_,WEEK_DAY) VALUES (%s, %s, %s); '

        cursor = cnx.cursor()
        print ('Inserting data ...')
        for k in res.keys() :
            cursor.executemany(query.format(k),res[k])

        cnx.commit()
    except Exception as e :
        print (e)
    finally :
        cursor.close()
        cnx.close()

def check_tabes(cnx,res):
    print ('Checking tables ...')
    query = '''
    CREATE TABLE IF NOT EXISTS {} (
    TIME_ VARCHAR(30) PRIMARY KEY,
    VALUE_ VARCHAR(30),
    WEEK_DAY VARCHAR(30)
    )'''
    cursor = cnx.cursor()
    for k in res.keys():
        cursor.execute(query.format(k))

    cnx.commit()

def get_last_days(day_numbers=7,offset = 1,date_format = '%m/%d/%Y'):
    date = datetime.now()
    time_span = 3600*24*offset
    return [get_date_before(date,time_span+3600*24*i,date_format) for i in range(day_numbers)]

def get_date_before(date,time_span,date_format):
    delta = date-datetime.fromtimestamp(0)
    time_span = delta.total_seconds() - time_span
    return datetime.fromtimestamp(time_span).strftime(date_format)
Reply
#2
The code you shared is not responsible for scheduling, it just defines a function that is used to wait until a certain time (and that's a really bad way to do scheduling btw).
Also, it's probably not the best of ideas to share your database credentials on a public forum.
Reply
#3
oops. I wonder if I can edit/delete that post. if not I'll have to change credentials
Reply
#4
I think (not sure at all) that you have to have a certain number of posts to be able to edit them, so I've edited the details out for you.
Reply
#5
thank you so much! Does the code look like it is "writing" data to a mysql database?
Reply
#6
It defines a function insert_data() which writes data to a database, but that function isn't used in this file.
This code doesn't actually do anything, it just defines some constants and functions which are probably imported and used in another file.
Reply
#7
A parallel discussion in another sub-section of the forum
https://python-forum.io/Thread-change-ti...-py-script

I'm not sure if you want to keep this thread in Jobs section.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Coder/Tutor jpatierno 5 1,393 Nov-08-2023, 05:40 PM
Last Post: buran
  Hiring expierenced coder with instagram stab 0 2,723 Mar-15-2022, 02:03 AM
Last Post: stab
  looking for a python coder (speak german) micha854 1 2,191 Apr-16-2020, 05:10 PM
Last Post: DeaD_EyE
  In need of a python-coder - Pay: $200 HillTom1499 1 2,595 Sep-30-2019, 03:52 AM
Last Post: ray05
  Python Coder Amsterdam ISX4 0 2,528 May-16-2019, 07:35 AM
Last Post: ISX4
  [S] Coder for simple C Code implementation in python [O] Payment Sw1p3 4 3,880 Mar-07-2018, 08:40 PM
Last Post: metulburr
  Looking for electronic music python coder ZipSnipe 1 2,680 Dec-28-2017, 09:10 AM
Last Post: Smith1208

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020