I would like to continue my python script after the schedule.every(x).seconds.until(x).do(job), but my program end the function until the x time and doesn't keep going after.
Here is my program :
import requests
import json
import pickle
import schedule
import sys
import time
import pandas as pd
from datetime import datetime, timedelta
from threading import Timer
import matplotlib
from datetime import datetime
import matplotlib.pyplot as plt
from colorama import Fore, Back, Style
import math
import numpy as np
import os
os.environ["PATH"] += os.pathsep + '/Library/TeX/texbin'
key = 'MY_API_KEY'
adress = 'https://api.openweathermap.org/data/2.5/weather'
params = {'appid':key, 'q': 'Lausanne', 'units':'metric'}
def somme(tab):
s=0
for i in range(len(tab)):
s=s+tab[i]
return s
def moyenne(tab):
return somme(tab)/len(tab)
tab=[]
tab2=[]
def function(tab,tab2):
response = requests.get(adress, params=params)
weather = response.json()
temp = weather['main']['temp']
print(temp)
tab.append(temp)
now = datetime.now()
time = now.strftime("%H:%M:%S")
tab2.append(time)
print(tab)
print(tab2)
print(moyenne(tab))
function(tab,tab2)
schedule.every(10).seconds.until("20:00").do(function, tab, tab2)
while True :
schedule.run_pending()
time.sleep(1)
fig = plt.figure(1,figsize=(9, 7))
ax = fig.add_subplot()
ax.set_xlabel('x')
ax.set_ylabel('y')
plt.plot(tab2,tab, 'b.')
plt.show()
I would like that my program plot the graph after it had done the schedule. Is it possible ?
Thank you for your help !
EDIT :
Thank you to Tim Roberts for the answer in the comments !
I just have to change my loop like this while datetime.now().hour < 20:
Related
Am trying to generate 3.3 million fake rows using python as below snippet.
generating the file is very very slow. any help speedup this?
Python version - 3.9.7
import os, csv, time, sys
from datetime import datetime
from faker import Faker
from time import sleep
from progress.bar import Bar
os.system('clear')
sCount = "distID.in"
fake = Faker()
startTime = datetime.now()
count = sum(1 for line in open(sCount))
fakeFile = open('fakeFile.csv', 'w')
bar = Bar('Processing', max=count)
with open(sCount) as piiFile:
i=666000000
for oldID in piiFile:
i=i+1
fn = fake.first_name()
ln = fake.last_name()
dob = (f'{fake.date_of_birth()}')
fakeFile.write(f'{i},{fn},{ln},{dob},{oldID}'+'\n')
bar.next()
fakeFile.close()
bar.finish()
When I try to profile my python code in this manner I get an output where it shows the time taken by the python lines only and not the c++ lines. I am using '%prun -s'command to help profile these lines. The code is written in Jupyter Notebook.
from Cython.Compiler.Options import get_directive_defaults
directive_defaults = get_directive_defaults()
directive_defaults['linetrace'] = True
directive_defaults['binding'] = True
%load_ext cython
%%cython
# cython: profile=True
from pygpb import libgpb
import numpy as np
from skimage import io, color
import matplotlib.pyplot as plt
from skimage import io
import os
import sys
from timeit import default_timer as timer
def main(path):
n_ori, Kmean_num, sigma_sm, sigma_lg = 8, 64, 2.0, np.sqrt(2)*2.0
# run it
gpb = libgpb.Gpb()
img = io.imread(path)
if(img.strides[1] > 3):
img = (255 * color.rgba2rgb(img)).astype(np.uint8)
gpb.test_np_mat(img, img)
# get texton
texton = gpb.texton(img, n_ori,
Kmean_num,
sigma_sm, sigma_lg)
start = timer();
gpb_res, ucm_res = gpb.run(img)
end = timer();
print("Time taken by demo is : " ,end-start, "seconds") #time in seconds:
[picture of the output][1]
[1]: https://i.stack.imgur.com/zlddP.jpg
I wrote a program and would like to give to the user the opportunity to run it either on compute with a graphical environment or not.
Currently through hard coding it I can do either one or the other by changing the matplotlib import at the top of my program file, before importing pyplot.
with graphical environment
import matplotlib
matplotlib.use("Qt4Agg")
import matplotlib.pyplot as plt
...
without graphical environment
import matplotlib
matplotlib.use("agg")
import matplotlib.pyplot as plt
...
the remaining code would look something like that:
...
import os, sys, argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--noX", action="store_true", dest="noX")
params = parser.parse_args()
data = [0,1,2,3,4,5]
fig, ax = plt.subplots()
ax.plot(data)
if not params.noX:
plt.show()
plt.savefig("foo.png")
sys.exit(0)
if __name__ == "__main__":
main()
Is it possible to change the backend based on the noX parameter value?
You can set the backend o a condition:
import matplotlib
if not params.noX:
matplotlib.use("agg")
else:
matplotlib.use("Qt4Agg")
import matplotlib.pyplot as plt
If you only plot inside main, move your import inside this function:
import argparse
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--noX", action="store_true", dest="noX", default=False)
params = parser.parse_args()
import matplotlib
if not params.noX:
matplotlib.use("agg")
else:
matplotlib.use("Qt4Agg")
import matplotlib.pyplot as plt
data = [0,1,2,3,4,5]
fig, ax = plt.subplots()
ax.plot(data)
if not params.noX:
plt.show()
plt.savefig("foo.png")
sys.exit(0)
if __name__ == "__main__":
main()
If you want to do plotting everywhere, use special function to parse the
command line arguments and call it only once.
import argparse
def _parse_cmd_args():
"""Parse command line args.
"""
parser = argparse.ArgumentParser()
parser.add_argument("--noX", action="store_true", dest="noX", default=False)
params = parser.parse_args()
return params
PARAMS = _parse_cmd_args()
# Want to prevent any further call to `_parse_cmd_args()`?
# Un-comment the following line:
# del _parse_cmd_args
import matplotlib
if not PARAMS.noX:
matplotlib.use("agg")
else:
matplotlib.use("Qt4Agg")
import matplotlib.pyplot as plt
def main():
data = [0,1,2,3,4,5]
fig, ax = plt.subplots()
ax.plot(data)
if not PARAMS.noX:
plt.show()
plt.savefig("foo.png")
sys.exit(0)
if __name__ == "__main__":
main()
Did you try? As a general rule you can't change the backend. So you will have to pospone the decision until you know the value of noX. Keep in mind that you can import at any time and any place. It is adviced to import at the beginning of the file but that's not always possible.
Ok so combining the idea of Mike with the global import of plt I come up with something like that to do the trick. Sorry for bothering you, it was actually simpler than first envisaged.
import os, sys, argparse
import matplotlib
matplotlib.use("agg") if "--noX" in sys.argv else matplotlib.use("Qt4Agg")
import matplotlib.pyplot as plt
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--noX", action="store_true", dest="noX", default=False)
params = parser.parse_args()
data = [0,1,2,3,4,5]
fig, ax = plt.subplots()
ax.plot(data)
if not params.noX:
plt.show()
plt.savefig("foo.png")
sys.exit(0)
if __name__ == "__main__":
main()
I would like to use this Linux Python script in Windows Python.
how to rewrite it ? The part to be rewritten in multiprocessing part.
from __future__ import print_function
from collections import Counter
import glob
import multiprocessing
import os
import re
import sys
import time
def create_data(filepath):
...
return values
filepaths = glob.glob('*/*.txt')
num_tasks = len(filepaths)
p = multiprocessing.Pool()
results = p.imap(create_data, filepaths)
while (True):
completed = results._index
print("\r--- Completed {:,} out of {:,}".format(completed, num_tasks), end='')
sys.stdout.flush()
time.sleep(1)
if (completed == num_tasks): break
p.close()
p.join()
df_full = pd.DataFrame(list(results))
print()
thanks for your help.
i wanted to create a django aplication which shows the graph using the data in the csv. I write a code in views file of the application repository. and also i placed the csv file along with views.py file. The codes are as follows
#from django.shortcuts import render
import sys
from sys import *
import numpy as np
import matplotlib.pyplot as xy
from datetime import datetime
from matplotlib.dates import YearLocator, MonthLocator, DateFormatter
from matplotlib.ticker import Formatter
import PIL
import PIL.Image
import StringIO
from django.http import HttpResponse
from django.template import RequestContext, loader
import csv
def graph():
a=[]
b=[]
c=[]
h=[]
a1=[]
b1=[]
c1=[]
j= open("Tier 1 Lake.csv")
for row in csv.reader(j):
a.append(row[8])
b.append(row[28])
c.append(row[2])
for i,v in enumerate(a):
if b[i]:
a1.append(a[i])
b1.append(b[i])
c1.append(c[i])
d=[item for item in range(len(c1)) if c1[item] == 'JOR-01-L']
e=[a1[i] for i in d]
f=[b1[i] for i in d]
FORMAT = '%m/%d/%Y'
s = sorted(zip(e,f), key = lambda x: datetime.strptime(x[0],'%m/%d/%Y'))
r= [x[1] for x in s]
t= [x[0] for x in s]
g= len(e)
for k in range(0,g):
h.append(k)
fig, ax = xy.subplots()
fig.autofmt_xdate()
xy.plot(h,r)
xy.xticks(h,t)
xy.plot(r, '-o', ms=10, lw=1, alpha=1, mfc='orange')
xy.xlabel('Sample Dates')
xy.ylabel('Air Temperature')
xy.title('Tier 1 Lake Graph (JOR-01-L)')
xy.grid(True)
xy.show()
buffer = StringIO.StringIO()
canvas = pylab.get_current_fig_manager().canvas
canvas.draw()
graphIMG = PIL.Image.fromstring("RGB", canvas.get_width_height(), canvas.tostring_rgb())
graphIMG.save(buffer,"PNG")
xy.close()
return HttpResponse(buffer.getvalue(), content_type="image/png")
Links and urls are functioning absolutely fine.
THE ERROR I AM GETTING IS " No such file or directory: 'Tier 1 Lake.csv'-- IO Error"