From adsquaired at gmail.com Wed Feb 1 11:22:44 2017 From: adsquaired at gmail.com (ad^2) Date: Wed, 1 Feb 2017 11:22:44 -0500 Subject: [Tutor] Lock File Usage In-Reply-To: <85y3xsrruh.fsf@benfinney.id.au> References: <85y3xsrruh.fsf@benfinney.id.au> Message-ID: On Mon, Jan 30, 2017 at 7:33 PM, Ben Finney wrote: > "ad^2" writes: > > > So, IF: no lock file, create a lock file, execute, delete lock file > > when finished successfully. ElSE: the script is running, exit. Then, > > cron will try again an hour later. > > > > I do not necessarily require a "lock file" just looking for a > > recommendation on a best practice with low complexity to make this > > work. > > The third-party ?fasteners? library provides an API for locks > that you will want to > consider. > > Specifically, the interpprocess lock decorators > examples.html#lock-decorator> > seem to be what you want. > ------------------- > Thanks for the help guys. This is what I ended up going with that works. Requires: lockfile, syslog and os modules pid = ('/var/lock/' + os.path.splitext(os.path.basename(__file__))[0]) try: lock = filelock.FileLock(pid) lock.acquire(timeout = 1) # my code function 1 # my code function 2 # and so on.... lock.release(force = True) os.unlink(pid) except (IOError, OSError) as e: syslog.syslog('%s' % e) print e From jf_byrnes at comcast.net Wed Feb 1 16:18:18 2017 From: jf_byrnes at comcast.net (Jim) Date: Wed, 1 Feb 2017 15:18:18 -0600 Subject: [Tutor] How to interact with the result of subprocess.call() In-Reply-To: References: Message-ID: On 12/26/2016 04:48 AM, Peter Otten wrote: > Jim Byrnes wrote: > >> Is there a way to terminate subprocess and still keep LO open so >> pykeyboard can send it keystrokes from the script? > > In theory you can open Libre Office from another thread, wait a moment and > then send it keystrokes from the main thread. I managed to do this with the > script below. > > However, the procedure is very brittle; while experimenting I managed to > "press" the control key without releasing it afterwards. The interval from > doing it to realizing what was going on to "fixing" it (reboot) was an > interesting experience... > > from contextlib import contextmanager > import subprocess > import threading > import time > > import pykeyboard > > kb = pykeyboard.PyKeyboard() > > > @contextmanager > def press_key(key, kb=kb): > kb.press_key(key) > try: > yield > time.sleep(1) > finally: > kb.release_key(key) > > > def open_it(filename): > subprocess.call(["libreoffice", filename]) > print("exiting libreoffice thread") > > > LONG_ENOUGH = 15 # seconds > > # enter text into an existing odt file > filename = "demo.odt" > text = "hello and goodbye" > > opener = threading.Thread(target=open_it, args=(filename,)) > opener.start() > > time.sleep(LONG_ENOUGH) # for libreoffice to start and open the file > > kb.type_string(text) > > with press_key(kb.control_key): > kb.tap_key("s") > with press_key(kb.alt_key): > kb.tap_key(kb.function_keys[4]) > > > print("exiting main thread") > > Peter, My apologies for taking so long to get back and thank you for the code. With the holidays and moving to a new computer/OS I didn't have much time to work on my little project. I was able to use your code and send the necessary keystrokes to manipulate LO. Thanks again. Regards, Jim From ben+python at benfinney.id.au Wed Feb 1 17:10:32 2017 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 02 Feb 2017 09:10:32 +1100 Subject: [Tutor] Lock File Usage References: <85y3xsrruh.fsf@benfinney.id.au> Message-ID: <851svhsgtj.fsf@benfinney.id.au> "ad^2" writes: > Thanks for the help guys. This is what I ended up going with that works. > > Requires: lockfile, syslog and os modules You're probably aware, but for others reading this thread: The ?lockfile? library is explicitly deprecated by its maintainers . They have declared they're no longer maintaining it, and they recommend using a different library for locking. So long as you're aware that you are effectively accepting the burden of maintaining that library yourself, go ahead :-) -- \ ?I think it would be a good idea.? ?Mohandas K. Gandhi (when | `\ asked what he thought of Western civilization) | _o__) | Ben Finney From robertvstepp at gmail.com Thu Feb 2 21:22:12 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 2 Feb 2017 20:22:12 -0600 Subject: [Tutor] Best Python 3 module to create simple text editor in Windows command prompt? Message-ID: I wish to create a primitive text editor in Windows 7 command prompt to enable me to manually enter and solve simple substitution cryptograms by hand. I would want the cipher text to be one color and the plain text in another. My intent is to set up things this way in command prompt before creating a more interesting version (with increased features) in tkinter. What would be the best Python 3 module to best assist me in controlling the command prompt window display, font colors, positioning the cursor dynamically, etc.? TIA! -- boB From eryksun at gmail.com Thu Feb 2 21:35:33 2017 From: eryksun at gmail.com (eryk sun) Date: Fri, 3 Feb 2017 02:35:33 +0000 Subject: [Tutor] Best Python 3 module to create simple text editor in Windows command prompt? In-Reply-To: References: Message-ID: On Fri, Feb 3, 2017 at 2:22 AM, boB Stepp wrote: > What would be the best Python 3 module to best assist me in controlling > the command prompt window display, font colors, positioning the cursor > dynamically, etc.? Try using curses [1]. Christoph Gohlke has a port for Windows Python based on the PDCurses library [2]. [1]: https://docs.python.org/3/howto/curses.html [2]: http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses From robertvstepp at gmail.com Thu Feb 2 23:42:24 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Thu, 2 Feb 2017 22:42:24 -0600 Subject: [Tutor] Best Python 3 module to create simple text editor in Windows command prompt? In-Reply-To: References: Message-ID: On Thu, Feb 2, 2017 at 8:35 PM, eryk sun wrote: > On Fri, Feb 3, 2017 at 2:22 AM, boB Stepp wrote: >> What would be the best Python 3 module to best assist me in controlling >> the command prompt window display, font colors, positioning the cursor >> dynamically, etc.? > > Try using curses [1]. Christoph Gohlke has a port for Windows Python > based on the PDCurses library [2]. > > [1]: https://docs.python.org/3/howto/curses.html > [2]: http://www.lfd.uci.edu/~gohlke/pythonlibs/#curses Thanks, Eryk. I did not realize that curses had been ported to Windows. I thought it was for *nix only. -- boB From alan.gauld at yahoo.co.uk Fri Feb 3 05:47:44 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 3 Feb 2017 10:47:44 +0000 Subject: [Tutor] Best Python 3 module to create simple text editor in Windows command prompt? In-Reply-To: References: Message-ID: On 03/02/17 04:42, boB Stepp wrote: > On Thu, Feb 2, 2017 at 8:35 PM, eryk sun wrote: >>> >> Try using curses [1]. Christoph Gohlke has a port for Windows Python >> based on the PDCurses library [2]. > > Thanks, Eryk. I did not realize that curses had been ported to > Windows. I thought it was for *nix only. There have been several attempts at a Python curses for "DOS" over the years but none of them worked completely (except the Cygwin version of course*). I haven't tried the one that Eryk suggests, hopefully somebody finally got it working. Should that fail, or be overly complex, there are several terminal control libraries around, and if all else fails there are the so called "High Level Console Functions" in the Microsoft API: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682010(v=vs.85).aspx These should be accessible using ctypes if you are really stuck... They are not very "high level" though! (*) If you are just doing this for personal use then I strongly suggest installing cygwin and just running Python there. By far the simplest way to get a decent console on Windows and all the other *nix tools as well. -- 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 From poojabhalode11 at gmail.com Fri Feb 3 19:24:49 2017 From: poojabhalode11 at gmail.com (Pooja Bhalode) Date: Fri, 3 Feb 2017 19:24:49 -0500 Subject: [Tutor] Copy/Paste in Tkinter Message-ID: Hi, I am trying to write a simple Tkinter code in Python in order to demonstrate select/copy/paste/cut functionalities using keyboard keys and menu options. I tried looking up online, but I am finding examples of these in which they create an event and then it is used: eg.: import Tkinter def make_menu(w): global the_menu the_menu = Tkinter.Menu(w, tearoff=0) # the_menu.add_command(label="Select") the_menu.add_command(label="Cut") the_menu.add_command(label="Copy") the_menu.add_command(label="Paste") def show_menu(e): w = e.widget # the_menu.entryconfigure("Select",command=lambda: w.event_generate("<>")) > the_menu.entryconfigure("Cut",command=lambda: w.event_generate("<>")) > the_menu.entryconfigure("Copy",command=lambda: w.event_generate("<>")) > the_menu.entryconfigure("Paste",command=lambda: These should be in the create function. You don't need to configure the command every time you show the menu. > w.event_generate("<>")) > the_menu.tk.call("tk_popup", the_menu, e.x_root, e.y_root) You should only need to call the tk.call() method very, very rarely, there is usually a more direct way to do things. > root = Tkinter.Tk() > make_menu(root) > > e1 = Tkinter.Entry(); e1.pack() > e2 = Tkinter.Entry(); e2.pack() Shouldn't these Entries have a parent? -- 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 From alan.gauld at yahoo.co.uk Sat Feb 4 05:39:25 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 4 Feb 2017 10:39:25 +0000 Subject: [Tutor] Tkinter Copy/Paste/Cut functionalities In-Reply-To: References: Message-ID: On 04/02/17 02:34, Pooja Bhalode wrote: > I came across some snippets of the code to do this but using a self class. I'm not sure what you mean by a "self class"? > I was wondering if some one could help me create these functionalities in a > manner similar to the following code. (similar to the Quitcommand > function). See my reply to your other post. > Another question I had, was how to make the status bar text as a variable > such that it keeps on displaying what is going on in the program? Its always best to keep separate questions in separate posts - it makes searching the archives easier in the future. However,... That depends on what you want to show in the Status bar. A Status bar normally includes showing help text when the mouse moves over widgets and the status of any component that the cursor enters. That sounds different to what you want? You may be able to use a StriVar variable linked to an Entry on the status bar, or maybe even a Label(can a Label text link to a StrVar? I'm not sure). Alternatively ifv your code controls where the variable gets changed(as it should) then simply update the status bar every time you change that variable. This is one case where a setX type function becomes worthwhile. Then only change X by calling setX()... -- 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 From alan.gauld at yahoo.co.uk Sat Feb 4 13:47:16 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 4 Feb 2017 18:47:16 +0000 Subject: [Tutor] Copy/Paste in Tkinter In-Reply-To: References: Message-ID: <80190f93-14ea-7bea-f6be-403fe03dcabc@yahoo.co.uk> On 04/02/17 16:09, Pooja Bhalode wrote: > I do not have a text box, but a GUI similar to a word editor. That sounds like a text box to me!? What are you using to enter the text if not a Text widget? > I was learning from youtube videos about this. You can start with the GUI topic in my tutorial(see .sig) It contains links to a couple of other Tkinter tutorials on the web. The Python.org Tkinter page also has links. All of these cover text processing to some degree. Finally there are a couple of books to consider: 1) Python & Tkinter by Grayson on Manning Press 2) Programming Python 4th edition which has about 400 pages of Tkinter material, including a text editor. 3) And I can't resist a plug for my recent book "Python Projects" which has a more modest 30 page intro on Tkinter, but includes coverage of the get, delete and insert text methods, as well as how to set fonts and build menus :-) You may be able to get these via your local public/college library. -- 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 From juan0christian at gmail.com Sat Feb 4 12:44:57 2017 From: juan0christian at gmail.com (Juan C.) Date: Sat, 4 Feb 2017 15:44:57 -0200 Subject: [Tutor] Using venv In-Reply-To: References: Message-ID: On Fri, Jan 27, 2017 at 7:47 PM, Jim wrote: > > [...] This question seems a little dumb and maybe I am being a little dense, but then what? Imagine that you are working on 5 different Python projects, each using different packages with different versions. We can break this down in two situations: 1. No virtualenv 1.a. You only have one Lib/site-packages to hold all your packages 1.b. Project A uses package-abc 1.2 while Project B uses package-abc 1.5 1.c. You go to work on Project B using package-abc 1.5, but later on you have to do some changes on Project A so you must downgrade package-abc to 1.2. Too bad you have to fix some bugs on Project B the next day, need to update package-abc back to 1.5 2. Virtualenv 2.a. You had multiple virtualenvs, each of them having their own Lib/site-packages 2.b. Project A uses one virtualenv with package-abc 1.2 while Project B uses another virtualenv with package-abc 1.5 2.c. You go to work on Project B using package-abc 1.5, so you activate its venv, but later on you have to do some changes on Project A so you activate the other venv. Too bad you have to fix some bugs on Project B the next day, activate Project B venv. It's a simple command to activate/deactivate a venv vs all the work you have to do when not using venv. From poojabhalode11 at gmail.com Sat Feb 4 11:09:15 2017 From: poojabhalode11 at gmail.com (Pooja Bhalode) Date: Sat, 4 Feb 2017 11:09:15 -0500 Subject: [Tutor] Copy/Paste in Tkinter In-Reply-To: References: Message-ID: Hi Alan, Thank you so much for guiding me towards this. I am new to tkinter hence not aware of all these features. I am trying to find some simple examples, of how to use copy/paste/cut. I do not have a text box, but a GUI similar to a word editor. I was wondering if you could direct me towards or show me towards some simple examples of the same, so that it would be easier to understand. I was learning from youtube videos about this. It would be a huge help. Thank you so much Pooja On Sat, Feb 4, 2017 at 5:32 AM, Alan Gauld via Tutor wrote: > On 04/02/17 00:24, Pooja Bhalode wrote: > > > I am trying to write a simple Tkinter code in Python in order to > > demonstrate select/copy/paste/cut functionalities using keyboard keys and > > menu options. > > You are going a fairly long winded way about it. > Rather than generating events use the Text/Entry > widget methods directly. > > Check the documentation for the following methods: > > Text.get(....) # copy > Text.selection_get(...) # copy a selection > Text.delete(...) # cut > Text.insert(...) # paste > > To control selections look at the tag methods, > but that is more complex. > > > def show_menu(e): > > w = e.widget > > # the_menu.entryconfigure("Select",command=lambda: > > w.event_generate("<