Re: [Tutor] Collecting output from Python scripts executed via Cron
2017-05-19 3:48 GMT+02:00 Leo Silver : > I have written a several Python scripts to collect data from external > sources (an email account and an sftp site). > > In development I run the scripts from IDLE or the command line and can view > the output of various print statements in the scripts which helps me to > monitor progress and confirm correct operation. > > However, in production I will be running the scripts via Cron. > > Is there a recommended/ elegant way to collect this output on the fly for > later review/ processing. > > Previously I have written bash scripts simply redirecting the standard > output to a text file and emailed this back to myself but I would like to > do this directly within Python rather than having to wrap the Python script > in a bash script. > > Thanks, Leo. > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Hi, There are two task here: as You wrote, firstly log must be written to files You could use the logging subsystem for this purpose https://docs.python.org/3/library/logging.html The second is to collect it and do someting. To do this You can use for example the scedule module https://docs.python.org/3/library/sched.html BR, George ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
for n in list(read_dictionary): > print(read_dictionary[n]) > if read_dictionary[n] == '5': > del read_dictionary[n] After doing this how do I save it back to the dictionary? then i ll do this numpy.save('loc_string_dictionary.npy', dictionary) On Thu, May 18, 2017 at 3:05 PM, Peter Otten <__pete...@web.de> wrote: > Michael C wrote: > > > I am trying to remove incorrect entries of my dictionary. > > I have multiple keys for the same value, > > > > ex, > > [111]:[5] > > [222]:[5] > > [333]:[5} > > > > and I have found out that some key:value pairs are incorrect, and the > best > > thing to do > > is to delete all entries who value is 5. So this is what I am doing: > > > > import numpy > > read_dictionary = numpy.load('loc_string_dictionary.npy').item() > > > > for n in read_dictionary: > > print(read_dictionary[n]) > > if read_dictionary[n] == '5': > > del read_dictionary[n] > > > > > > However, I get this error: > > RuntimeError: dictionary changed size during iteration > > > > and I can see why. > > > > What's the better thing to do? > > You can copy the keys into a list: > > for n in list(read_dictionary): > > print(read_dictionary[n]) > > if read_dictionary[n] == '5': > > del read_dictionary[n] > > As the list doesn't change size during iteration there'll be no error or > skipped key aka list item. > > If there are only a few items to delete build a list of keys and delete the > dict entries in a secend pass: > > delenda = [k for k, v in read_dictionary.items() if v == "5"] > for k in delenda: > del read_dictionary[k] > > If there are many items to delete or you just want to default to the most > idiomatic solution put the pairs you want to keep into a new dict: > > read_dictionary = {k: v for k, v in read_dictionary.items() if v != "5"} > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
Michael C wrote: > for n in list(read_dictionary): >> print(read_dictionary[n]) >> if read_dictionary[n] == '5': >> del read_dictionary[n] > > After doing this how do I save it back to the dictionary? > then i ll do this > numpy.save('loc_string_dictionary.npy', dictionary) Hm, the dict is called `read_dictionary`, so you save it with numpy.save('loc_string_dictionary.npy', read_dictionary) but I may have understood the question. If so, please clarify. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image Library
On 18/05/17 18:06, Alan Gauld via Tutor wrote: > Here is some untested Tkinter code to display an image > for 2 seconds: I tried this last night and it turned out to be harder than I expected. Eventually I got to bed at 3am! But here is something that seems to work for jpegs. I hope bmp files will too, I didn't have any to test... # import tkinter as tk from PIL import Image,ImageTk def showImage(imgName, delay=2000, wd=400, ht=400): '''Display the file imgName for delay milliseconds in a window sized wd x ht Close the window afterwards''' top = tk.Tk() top.withdraw() # hide the root window win = tk.Toplevel() PILimage = Image.open(imgName) TKimg = ImageTk.PhotoImage(PILimage) tk.Label(win, image=TKimg, width=wd, height=ht).pack() top.after(delay, lambda : (win.withdraw(), top.quit()) ) top.mainloop() ## Test code # import glob,os root = r"/full/path/to/your/files" for filename in glob.glob(root + "/*.jpg"): print(filename) showImage(filename) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image Library
On 19/05/17 10:29, Alan Gauld via Tutor wrote: > is something that seems to work for jpegs. I hope bmp files > will too, I didn't have any to test... I converted a few jpg to bmp. It does work but it turns out Pillow is quite fussy about the BMP format. I had to turn off colour space header info (in the GIMP export) to get them working. Others have had issues with unusual colour depths etc too. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image Library
Alan Gauld via Tutor wrote: > On 18/05/17 18:06, Alan Gauld via Tutor wrote: > >> Here is some untested Tkinter code to display an image >> for 2 seconds: > > I tried this last night and it turned out to be harder > than I expected. Eventually I got to bed at 3am! But here > is something that seems to work for jpegs. I hope bmp files > will too, I didn't have any to test... > > # > import tkinter as tk > from PIL import Image,ImageTk > > def showImage(imgName, delay=2000, wd=400, ht=400): >'''Display the file imgName > for delay milliseconds > in a window sized wd x ht > Close the window afterwards''' > >top = tk.Tk() >top.withdraw() # hide the root window >win = tk.Toplevel() >PILimage = Image.open(imgName) >TKimg = ImageTk.PhotoImage(PILimage) >tk.Label(win, image=TKimg, width=wd, height=ht).pack() >top.after(delay, lambda : (win.withdraw(), top.quit()) ) >top.mainloop() > > > ## Test code # > import glob,os > > root = r"/full/path/to/your/files" > > for filename in glob.glob(root + "/*.jpg"): > print(filename) > showImage(filename) I found your original sketch a bit more appealing and went to explore why it didn't work. The problem you already have fixed is passing a PhotoImage to the Label instead of the filename. The other seems to be that you need to call the destroy() rather than the quit() method. At least the following works on my Linux system, with jpegs: def show_image(imagename, delay=2000, width=None, height=None): root = tk.Tk() image = ImageTk.PhotoImage(Image.open(imagename)) tk.Label(root, image=image, width=width, height=height).pack() root.after(delay, root.destroy) root.mainloop() However, as your code gets away without calling destroy() I'm still puzzled... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Collecting output from Python scripts executed via Cron
On 2017-05-18 18:48, Leo Silver wrote: I have written a several Python scripts to collect data from external sources (an email account and an sftp site). In development I run the scripts from IDLE or the command line and can view the output of various print statements in the scripts which helps me to monitor progress and confirm correct operation. However, in production I will be running the scripts via Cron. Is there a recommended/ elegant way to collect this output on the fly for later review/ processing. From the python documentation of the print function: """ print([object, ...], *, sep=' ', end='\n', file=sys.stdout) Print object(s) to the stream file, separated by sep and followed by end. sep, end and file, if present, must be given as keyword arguments. All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no object is given, print() will just write end. The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. """ The following might work: with open("progress.txt", "a") as outf: print("what ever you used to print", file=outf) Previously I have written bash scripts simply redirecting the standard output to a text file and emailed this back to myself but I would like to do this directly within Python rather than having to wrap the Python script in a bash script. Thanks, Leo. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
list(read_dictionary) converts the dictionary into a list right? How can you save the list as a dictionary? Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Image Library
On 19/05/17 15:15, Peter Otten wrote: > call the destroy() rather than the quit() method. Nice! > > However, as your code gets away without calling destroy() I'm still > puzzled... withdraw hides the window then quit ends the mainloop so the procedure falls through to the end and everything gets garbage collected. Its brutal but it works. destroy combines the withdraw and quit effect in a much more elegant solution. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
On 19/05/17 15:23, Michael C wrote: > list(read_dictionary) converts the dictionary into a list right? How can > you save the list as a dictionary? Nope, list() produces a new list object containing the keys of the dictionary. In the old day(of python 2) you used to get the same effect using for key in mydict.keys() but keys() now returns a funky view of the original dict keys and I suspect you'd have the same problems when deleting items. So Peter's list() is probably the best option. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] deleting elements of a dictionary
On 05/19/2017 11:17 AM, Alan Gauld via Tutor wrote: > On 19/05/17 15:23, Michael C wrote: >> list(read_dictionary) converts the dictionary into a list right? How can >> you save the list as a dictionary? > > Nope, list() produces a new list object containing the > keys of the dictionary. In the old day(of python 2) you > used to get the same effect using > > for key in mydict.keys() > > but keys() now returns a funky view of the original dict > keys and I suspect you'd have the same problems when > deleting items. So Peter's list() is probably the best > option. > Or to take another related view: don't remove items from an iterable while iterating over it: del() is okay as long as you're not looping over the thing. Dictionaries have a method for this called pop(), but to my blushes, I don't really have a lot of experience with it. What I'd think of just off the bat is build a new dictionary on the fly, omitting the things you were trying to delete, and then if you like, save the new dict by the old name (which will cause the old one to have no references and be dropped. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] always on top
Hi all: I am running a code to examine another window's activities, while I use that window to do stuff. However, the python shell keeps grabbing the Topmost position so what I do on the other window, the one that has to stay on top the whole time, keeps getting into the shell window. Is making my task window the topmost window somehow the way to go? Or is there a way to suppress the shell so it doesn't grab the focus? Thanks! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] always on top
nvm, the problem went away on its own :) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor