Python Forum
Combining 2 or more scripts to one.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Combining 2 or more scripts to one.
#1
Hi,

Id like to know how i can make one script from 2 or more so i can just run one script with multiple functions.
Below are the 2 scripts which i want to have running in one script.
Both scripts have their own output file and time of execution.



import time
import schedule    
#starttime=time.time()
  
def task():  
  
#Input_file: "C:\\Campbellsci\\LoggerNet\\CR1000_Table1.dat"
#Output_file: "C:\\Users\\Makada\\Desktop\\CR1000_Table1 - kopie.dat"
  
 while True:
    """ Not to append duplicate data to output file"""
    existingLines = set(line.strip() for line in open("C:\\Users\\Makada\\Desktop\\CR1000_Table1 - kopie .dat"))
    outfile = open("C:\\Users\\Makada\\Desktop\\CR1000_Table1 - kopie .dat", "a+")
    for content in open("C:\\Campbellsci\\LoggerNet\\CR1000_Table1.dat", "r"):
        if content.strip() not in existingLines: # to void duplicate lines
            outfile.write(content)
            existingLines.add(content)
    outfile.close()
  
schedule.every().minute.at(":01").do(task)
while True:
    schedule.run_pending()
    time.sleep(1)
refresh()
import time
import schedule    
#starttime=time.time()
  
def task():  
  
#Input_file: "C:\\Campbellsci\\LoggerNet\\CR1000_Table1.dat"
#Output_file: "C:\\Users\\Makada\\Desktop\\CR1000_Table1 - kopie.dat"
  
 while True:
    """ Not to append duplicate data to output file"""
    existingLines = set(line.strip() for line in open("C:\\Users\\Makada\\Desktop\\CR1000_Table2 - kopie.dat"))
    outfile = open("C:\\Users\\Makada\\Desktop\\CR1000_Table2 - kopie.dat", "a+")
    for content in open("C:\\Campbellsci\\LoggerNet\\CR1000_Table2.dat", "r"):
        if content.strip() not in existingLines: # to void duplicate lines
            outfile.write(content)
            existingLines.add(content)
    outfile.close()
  
schedule.every().minute.at(":10").do(task)
while True:
    schedule.run_pending()
    time.sleep(1)
refresh()
Reply
#2
I'm not sure the ins and outs of those modules, but shoot for something like this:


import time
import schedule    
   
def task1():  
    existingLines = set(line.strip() for line in open("C:\\Users\\Makada\\Desktop\\CR1000_Table1 - kopie .dat"))
    outfile = open("C:\\Users\\Makada\\Desktop\\CR1000_Table1 - kopie .dat", "a+")
    for content in open("C:\\Campbellsci\\LoggerNet\\CR1000_Table1.dat", "r"):
        if content.strip() not in existingLines: # to void duplicate lines
            outfile.write(content)
            existingLines.add(content)
    outfile.close()
   
def task2():  
    existingLines = set(line.strip() for line in open("C:\\Users\\Makada\\Desktop\\CR1000_Table2 - kopie.dat"))
    outfile = open("C:\\Users\\Makada\\Desktop\\CR1000_Table2 - kopie.dat", "a+")
    for content in open("C:\\Campbellsci\\LoggerNet\\CR1000_Table2.dat", "r"):
        if content.strip() not in existingLines: # to void duplicate lines
            outfile.write(content)
            existingLines.add(content)
    outfile.close()

schedule.every().minute.at(":01").do(task1)  
schedule.every().minute.at(":10").do(task2)
while True:
    schedule.run_pending()
    time.sleep(1)
refresh()
Reply
#3
That is working fine, thanks alot Smile
Reply
#4
The last 3 scripts which i want to join to one script are a bit more difficult i think.

import requests
import csv
import hashlib
import time
import schedule
import urllib
from time import sleep
import sys
starttime=time.time()

def task():

    x = time.strftime("%Y%m%d%H%M%S")
    y = "Name"
    a = "Password"
    z = x + y + a

    rawdata = str(x) + y + a
    mdpass = hashlib.md5(str(rawdata).encode("utf-8")).hexdigest()


    WUurl = "https://www.windguru.cz/upload/api.php?"

    uid = "Name" 
    pwd = "Password" 
    WUcreds = "uid=" + y + "&salt=" + x + "&hash=" + mdpass
    action_str = ""


    WeatherUnderground= open("C:\\Campbellsci\\LoggerNet\\CR1000_upload.dat", "r")
    csvReader = csv.reader(WeatherUnderground)
    for field in csvReader:
        a = (field[18])
        b = (field[12])
        c = (field[4])
        d = (field[21])
        e = (field[8])
        wind_avg=a
        wind_direction=b
        temperature=c
        wind_max=d
        rh=e
        r= requests.get(
        WUurl +
        WUcreds +
        "&wind_avg=" +
        str(wind_avg)+
        "&wind_direction=" +
        str(wind_direction)+
        "&temperature=" +
        str(temperature)+
        "&wind_max=" +
        str(wind_max)+
        "&rh=" +
        str(rh)+        
        action_str)
        
    print("Received " + str(r.status_code) + " " + str(r.text), flush=True)
    print(z, flush=True)
    print(time.strftime("%Y%m%d%H%M%S"), flush=True)
    print(mdpass, flush=True)

while True:
 try:
     task()
     time.sleep(60 - time.time() % 60)
     refresh()
 except:
    pass
    time.sleep(1)

else:
 time.sleep(60)
 refresh()
     
import requests
import time
import schedule    
import csv
starttime=time.time()

WUurl = "https://weatherstation.wunderground.com/weatherstation\
/updateweatherstation.php?"
WU_station_id = "Name" # Replace XXXX with your PWS ID
WU_station_pwd = "Password" # Replace YYYY with your Password
WUcreds = "ID=" + WU_station_id + "&PASSWORD="+ WU_station_pwd
date_str = "&dateutc=now"
action_str = "&action=updateraw"

def task():
        WeatherUnderground= open("C:\\Campbellsci\\LoggerNet\\CR1000_upload.dat", "r")
        csvReader = csv.reader(WeatherUnderground)
        for field in csvReader:
         a = (field[12])
         b = (field[17])
         c = (field[19])
         d = (field[16])
         e = (field[8])
         f = (field[7])
         g = (field[5])
         h = (field[27])
         i = (field[25])
         j = (field[10])
         k = (field[23])
         l = (field[16])
        winddir=a
        windspeedmph=b
        windgustmph=c
        windgustdir=d
        humidity=e  
        dewptf=f
        tempf=g
        rainin=h
        dailyrainin=i
        baromin=j
        solarradiation=k
        softwaretype=1

        r= requests.get(
        WUurl +
        WUcreds +
        date_str +
        "&winddir=" +
        str(winddir)+
        "&windspeedmph=" +
        str(windspeedmph)+
        "&windgustmph=" +
        str(windgustmph)+
        "&windgustdir=" +
        str(windgustdir)+
        "&humidity=" +
        str(humidity)+
        "&dewptf=" +
        str(dewptf)+
        "&tempf=" +
        str(tempf)+
        "&rainin=" +
        str(rainin)+
        "&dailyrainin=" +
        str(dailyrainin)+
        "&baromin=" +
        str(baromin)+
        "&solarradiation=" +
        str(solarradiation)+

        str(softwaretype)+    
        action_str)
        timeout=60

        print (time.strftime("%a, %d %b %Y %H:%M:%S"), flush=True)
        print("Received " + str(r.status_code) + " " + str(r.text), flush=True)
        print ("winddir= " + a, flush=True)
        print ("windspeedmph= " + b, flush=True)
        print ("windgustmph= " + c, flush=True)
        print ("windgustdir= " + d, flush=True)
        print ("humidity= " + e, flush=True)
        print ("dewptf= " + f, flush=True)
        print ("tempf= " + g, flush=True)
        print ("rainin= " + h, flush=True)
        print ("dailyrainin= " + i, flush=True)
        print ("baromin= " + j, flush=True)
        print ("solarradiation= " + k, flush=True)
while True:
 try:
     task()
     time.sleep(60 - time.time() % 60)
     refresh()
 except:
    pass
    time.sleep(1)

else:
 time.sleep(60)
import requests
import time
import schedule    
import csv
import urllib

WUurl = "https://wow.metoffice.gov.uk/automaticreading?"
siteid = "xxxxx" 
siteAuthenticationKey = "xxxxx" 
WUcreds = "siteid=" + siteid + "&siteAuthenticationKey="+ siteAuthenticationKey
date_str = "&dateutc=now"
action_str = "&action=updateraw"

def task():
        WeatherUnderground= open("C:\\Campbellsci\\LoggerNet\\CR1000_upload.dat", "r")
        csvReader = csv.reader(WeatherUnderground)
        for field in csvReader:
         a = (field[12])
         b = (field[17])
         c = (field[19])
         d = (field[16])
         e = (field[8])
         f = (field[7])
         g = (field[5])
         h = (field[27])
         i = (field[25])
         j = (field[10])
         k = (field[23])
         l = (field[16])
        winddir=a
        windspeedmph=b
        windgustmph=c
        windgustdir=d
        humidity=e  
        dewptf=f
        tempf=g
        rainin=h
        dailyrainin=i
        baromin=j
        solarradiation=k
        softwaretype=1

        r= requests.get(
        WUurl +
        WUcreds +
        date_str +
        "&winddir=" +
        str(winddir)+
        "&windspeedmph=" +
        str(windspeedmph)+
        "&windgustmph=" +
        str(windgustmph)+
        "&windgustdir=" +
        str(windgustdir)+
        "&humidity=" +
        str(humidity)+
        "&dewptf=" +
        str(dewptf)+
        "&tempf=" +
        str(tempf)+
        "&rainin=" +
        str(rainin)+
        "&dailyrainin=" +
        str(dailyrainin)+
        "&baromin=" +
        str(baromin)+
        "&solarradiation=" +
        str(solarradiation)+
        str(softwaretype)+    
        action_str)
        
        print (time.strftime("%a, %d %b %Y %H:%M:%S"), flush=True)
        print("Received " + str(r.status_code) + " " + str(r.text), flush=True)
        print ("winddir= " + a, flush=True)
        print ("windspeedmph= " + b, flush=True)
        print ("windgustmph= " + c, flush=True)
        print ("windgustdir= " + d, flush=True)
        print ("humidity= " + e, flush=True)
        print ("dewptf= " + f, flush=True)
        print ("tempf= " + g, flush=True)
        print ("rainin= " + h, flush=True)
        print ("dailyrainin= " + i, flush=True)
        print ("baromin= " + j, flush=True)
        print ("solarradiation= " + k, flush=True)
schedule.every(5).minutes.do(task)

while True:
 try:
     schedule.run_pending()
     time.sleep(1)
     refresh()
 except:
    pass
    time.sleep(1)

else:
 time.sleep(60)
 refresh()
Reply
#5
You should avoid having globals really. Make modules of your useful functions and then combining them is really easy because you just have to import the functions and call them with the appropriate values.
Reply
#6
Hi,

I managed to get it partially working.
Task 1 and 2 are updating fine, but task 3(with other time.sleep) doesnt do anything.

This is the code regarding the Scheduler .
while True:
 try:

     task1()
     task2()
     time.sleep(60 - time.time() % 60)
     refresh()
     task3()
     time.sleep(300 - time.time() % 300)
     refresh()
 except:
    pass
    time.sleep(1)

else:
 time.sleep(60)
 refresh()
Reply
#7
do you have it scheduled? egschedule.every().minute.at(":10").do(task3)?
Reply
#8
Hi,

Thanks for your reply.
No, i found out the method below is more secure at every minute and if working, every 5 minuten.
But with the code below, the 5 minutes (task 3, 300 seconds) schedule isnt working Angry


while True:
 try:
 
     task1()
     task2()
     time.sleep(60 - time.time() % 60)
     refresh()
     task3()
     time.sleep(300 - time.time() % 300)
     refresh()
 except:
    pass
    time.sleep(1)
 
else:
 time.sleep(60)
 refresh()
Reply
#9
Hi,

What can i do to make the above working?
And how can i get the code below to have the seconds set to zero?

date_str = "&dateutc=now"
Thanks.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  interloper help appreciated combining two python3 scripts to add new conditions. Ricktoddfrank 0 1,441 Jun-22-2021, 10:24 PM
Last Post: Ricktoddfrank

Forum Jump:

User Panel Messages

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