From phil_lor at bigpond.com Mon May 1 00:55:56 2017 From: phil_lor at bigpond.com (Phil) Date: Mon, 1 May 2017 14:55:56 +1000 Subject: [Tutor] Another set question In-Reply-To: References: <20170429111543.637b450c@raspberrypi> <854lx86kgg.fsf@benfinney.id.au> <20170429140939.26883c38@raspberrypi> <85wpa35x16.fsf@benfinney.id.au> <20170430095847.362a1967@raspberrypi> Message-ID: <20170501145556.45a04b53@raspberrypi> On Sun, 30 Apr 2017 15:58:13 +0100 Alan Gauld via Tutor wrote: > I would probably combine both such that for each cell you > have a tuple containing the given number and the set of > candidates. In some cases the number may be a sentinel > (such as -1) to indicate no number yet, and for some > cells the set will be empty. > > But by always having both available your data handling > becomes consistent, you always know that you get a tuple > and you know can easily test the sentinel to see3 if > the value is set or not. And you never need to > test types. Thank you again Alan, During the intervening period between when my question appeared on the mailing list, plus our time zone difference, I had come to almost the same conclusion. All numbers are now sets, the likely candidates and the given numbers. No more fooling around with different types, they're all sets. I now have a working solution. Many of the methods have very similar code to each other and I'm currently working on making the code simpler and less confusing. After that I'll give programming a rest for awhile. -- Regards, Phil From alan.gauld at yahoo.co.uk Mon May 1 13:16:57 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 1 May 2017 18:16:57 +0100 Subject: [Tutor] Fwd: Re: Python 3.6 Extract Floating Point Data from a Text File In-Reply-To: <59074C35.20309@sbcglobal.net> References: <44dafee1-1d94-586a-b18b-ac48999d47ee@yahoo.co.uk> <16087630-63f4-d0ed-e918-71c00aa99d0c@yahoo.co.uk> <59074C35.20309@sbcglobal.net> Message-ID: On 01/05/17 15:54, Stephen P. Molnar wrote: > Unfortunately, I'm still missing something. Here is my latest attempt > to incorporate your solution: > name = input("Enter Molecule ID: ") > name = str(name) you don't need the str() since input() always returns whatever string the user enters. > name_in = [] > name_in = name[:]+'.lac.dat' You don't need the [:] either since that just makes a copy of name. So you can just use name_in = name + '.lac.dat' But notice that you have lost the list you created. name_in is now just a string. > atm_chg = [] This creates a new empty list. But... > with open(name_in) as f: > # skip two lines > f.readline() > f.readline() > for line in f.readlines(): You don't need readlines, you can just iterate over f: for line in f: atm_chg = float(line.split()[-1]) This loses the list you created earlier and replaces it with a float. I suspect you want to append this to your list? atm_chg.append(float(line.split()[-1]) > The error this attempt give me is attached. > > IndexError: list index out of range That's only the last line of the error. Please always post the full error text. There is a lot of useful data in there that we can't see. As a debug step you could try printing line to make sure it always contains multiple fields. But are you sure that you only want a list of floats out? Steven's solution based on a dictionary seemed like it might be more useful for you? -- 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 ian.monat at gmail.com Mon May 1 13:20:42 2017 From: ian.monat at gmail.com (Ian Monat) Date: Mon, 1 May 2017 10:20:42 -0700 Subject: [Tutor] Using Python to access .txt files stored behind a firewall as .exe files Message-ID: I've got a Python project that I'd love some help on from a Python developer who is well versed at web scraping or requests. I work for a supplier, and we use a distributor to sell our products to retailers. The distributor has a reporting website that requires a login. >From that home / login page, you land on a page with 1 link for each state in which we do business (12 states / links in total). >From the 'state' page, you click a state link, and are taken to a page with many data files for that state. The data files are neatly arranged .txt files displayed as links, with logical naming conventions. The problem is, when you click a link for a particular file, an .exe downloads to your local machine. Then you have you run the .exe which produces a zipped file, and inside the zipped file, is the .txt, which what I really want. There's no way the distributor will change anything about how they store files on their website for me. I've written a script using the requests module but I think a web scraper like Scrapy, Beautiful Soup or Selinium may be required. What would you do? Thanks for your time. -Ian From mysecretrobotfactory at gmail.com Mon May 1 14:12:42 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Mon, 1 May 2017 11:12:42 -0700 Subject: [Tutor] Press ESC to exit() Message-ID: Hi all, I am trying to write a script to exit itself when ESC is pressed. I have found several answers regarding it such as this: http://stackoverflow.com/questions/43709710/push-esc- to-terminate-python-script?noredirect=1#comment74464169_43709710 But those only work when the program goes to that point. Is there a way to write it so that it would run exit() even when the program is in the background? Thanks! From mysecretrobotfactory at gmail.com Mon May 1 14:28:30 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Mon, 1 May 2017 11:28:30 -0700 Subject: [Tutor] Press ESC to exit() Message-ID: Hi all, I found out that one way to press ESC to kill the script was to use my previous script language, AutoHotKey and this is how it works: AutoHotKey code ## function that kills the window with title '*Python 3.6.1 Shell*' kill() { WinKill, *Python 3.6.1 Shell* } ## When ESC is pressed, runs the function 'kill' Esc::kill() Yes, that's the entire script. Is there a way to write it in Python on windows? Thanks! From alan.gauld at yahoo.co.uk Mon May 1 17:40:11 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 1 May 2017 22:40:11 +0100 Subject: [Tutor] Press ESC to exit() In-Reply-To: References: Message-ID: On 01/05/17 19:28, Michael C wrote: > Hi all, I found out that one way to press ESC to kill the script was to > use my previous script language, AutoHotKey and this is how it works: > > ## When ESC is pressed, runs the function 'kill' > Esc::kill() > Is there a way to write it in Python on windows? Yes, of course but it is very OS dependant and you would need different code on Windows, MacOS and *nix. Its also an incredibly risky thing to do because you have to catch the keystroke at the highest level of the event management system which means that any program that uses the ESC key is now broken. To illustrate, lets assume you are running a tool that decides it wants to format your hard drive. It pops up a window saying it will do so in 5s unless you hit escape. You hit escape and your script dutifully intercepts it and kills your own app, meanwhile the tool sees no Escape and so proceeds to format your hard drive. Is that really what you want? There are usually better behaved ways of doing things which will obey the conventions of the OS. But if you are absolutely sure you want to take the risk of breaking your computer we can talk about how you might proceed. It will not be trivial however and nowhere near as short as your existing AutoHotKey code. Also it will depend to some extent on whether your program that you want to kill is itself a GUI or a CLI. -- 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 Mon May 1 17:44:16 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 1 May 2017 22:44:16 +0100 Subject: [Tutor] Using Python to access .txt files stored behind a firewall as .exe files In-Reply-To: References: Message-ID: On 01/05/17 18:20, Ian Monat wrote: > ... I've written a script using the requests module but I > think a web scraper like Scrapy, Beautiful Soup or Selinium may be > required. I'm not sure what you are looking for. Scrapy, BS etc will help you read the HTML but not to fetch the file. Also do you want to process the file (extract the text) in Python too, or is it enough to just fetch the file? If the problem is with reading the HTML then you need to give us more detail about the problem areas and HTML format. If the problem is fetching the file, it sounds like you have already done that and it should be a case of fine tuning/tidying up the code you've written. What kind of help exactly are you asking for? -- 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 ian.monat at gmail.com Mon May 1 19:30:04 2017 From: ian.monat at gmail.com (Ian Monat) Date: Mon, 1 May 2017 16:30:04 -0700 Subject: [Tutor] Using Python to access .txt files stored behind a firewall as .exe files In-Reply-To: References: Message-ID: Hi Alan, thanks for the reply. My goal is to automatically via Python download the .exe, unzip it, and place the new .txt in a folder on my OneDrive. Then I have another visualization program that loads all the .txt files in that folder and displays them in a web-dashboard. My sales team has access to the dashboard through Sharepoint. So, I'm trying to automate the input to the dashboard so the team is always updated, without taking any of my time. Thanks for your time and thoughts -Ian On Mon, May 1, 2017 at 2:44 PM, Alan Gauld via Tutor wrote: > On 01/05/17 18:20, Ian Monat wrote: > > ... I've written a script using the requests module but I > > think a web scraper like Scrapy, Beautiful Soup or Selinium may be > > required. > > I'm not sure what you are looking for. Scrapy, BS etc will > help you read the HTML but not to fetch the file. Also do > you want to process the file (extract the text) in Python > too, or is it enough to just fetch the file? > > If the problem is with reading the HTML then you need to > give us more detail about the problem areas and HTML > format. > > If the problem is fetching the file, it sounds like you > have already done that and it should be a case of fine > tuning/tidying up the code you've written. > > What kind of help exactly are you asking for? > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mats at wichmann.us Mon May 1 19:14:51 2017 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 1 May 2017 17:14:51 -0600 Subject: [Tutor] Using Python to access .txt files stored behind a firewall as .exe files In-Reply-To: References: Message-ID: <56bd6a3a-fb01-c44f-2af7-d950c5d2c29d@wichmann.us> On 05/01/2017 03:44 PM, Alan Gauld via Tutor wrote: > On 01/05/17 18:20, Ian Monat wrote: >> ... I've written a script using the requests module but I >> think a web scraper like Scrapy, Beautiful Soup or Selinium may be >> required. > > I'm not sure what you are looking for. Scrapy, BS etc will > help you read the HTML but not to fetch the file. Also do > you want to process the file (extract the text) in Python > too, or is it enough to just fetch the file? > > If the problem is with reading the HTML then you need to > give us more detail about the problem areas and HTML > format. > > If the problem is fetching the file, it sounds like you > have already done that and it should be a case of fine > tuning/tidying up the code you've written. > > What kind of help exactly are you asking for? > This is a completely non-Python, non-Tutor response to part of this: The self-extracting archive. Convenience, at a price: running executables of unverified reliability is just a terrible idea. I know you said your disty won't change their website, but you should tell them they should: a tremendous number of organizations have policies that don't just allow pulling down and running an exe file from a website. Even if that's not currently the case for you, you could say that you're not allowed, and get someone in your management chain to promise to support that if there's a question - should not be hard. It may be wired into the distributor's content delivery system, but that's a stupid choice on their part. "Then you have you run the .exe which produces a zipped file" Don't do this ("run"), unless there's a way you trust to be able to verify the security of what is offered. Just about any payload could be buried in the exe, especially if someone broke in to the distributor's site. Possibly slightly pythonic: if it is really just a wrapper for a zipfile (i.e. the aforementioned self-extracting archive), you should be able to open it in 7zip or similar, and extract the zipfile, without ever "running" it. And if that is the case, you should be able to script extracting the zipfile from the .exe, and then extracting the text file from the zipfile, using Python (or other scripting languages: that's not particularly Python-specific). From ian.monat at gmail.com Mon May 1 20:12:40 2017 From: ian.monat at gmail.com (Ian Monat) Date: Mon, 1 May 2017 17:12:40 -0700 Subject: [Tutor] Using Python to access .txt files stored behind a firewall as .exe files In-Reply-To: <56bd6a3a-fb01-c44f-2af7-d950c5d2c29d@wichmann.us> References: <56bd6a3a-fb01-c44f-2af7-d950c5d2c29d@wichmann.us> Message-ID: Thank you for the reply Mats. I agree the fact that files are wrapped in an .exe is ridiculous. We're talking about a $15B company that is doing this by the way, not a ma and pa shop. Anyways... If I understand you correctly, you're saying I can: 1) Use Python to download the file from the web (but not by using a webscraper, according to Alan) 2) Simply ignore the .exe wrapper and use, maybe Windows Task Manager, to unzip the file and place the .txt file in the desired folder Am I understanding you correctly? Thank you -Ian On Mon, May 1, 2017 at 4:14 PM, Mats Wichmann wrote: > On 05/01/2017 03:44 PM, Alan Gauld via Tutor wrote: > > On 01/05/17 18:20, Ian Monat wrote: > >> ... I've written a script using the requests module but I > >> think a web scraper like Scrapy, Beautiful Soup or Selinium may be > >> required. > > > > I'm not sure what you are looking for. Scrapy, BS etc will > > help you read the HTML but not to fetch the file. Also do > > you want to process the file (extract the text) in Python > > too, or is it enough to just fetch the file? > > > > If the problem is with reading the HTML then you need to > > give us more detail about the problem areas and HTML > > format. > > > > If the problem is fetching the file, it sounds like you > > have already done that and it should be a case of fine > > tuning/tidying up the code you've written. > > > > What kind of help exactly are you asking for? > > > > This is a completely non-Python, non-Tutor response to part of this: > > The self-extracting archive. Convenience, at a price: running > executables of unverified reliability is just a terrible idea. > > I know you said your disty won't change their website, but you should > tell them they should: a tremendous number of organizations have > policies that don't just allow pulling down and running an exe file from > a website. Even if that's not currently the case for you, you could say > that you're not allowed, and get someone in your management chain to > promise to support that if there's a question - should not be hard. It > may be wired into the distributor's content delivery system, but that's > a stupid choice on their part. > > "Then you have you run the .exe which produces a zipped file" > > Don't do this ("run"), unless there's a way you trust to be able to > verify the security of what is offered. Just about any payload could be > buried in the exe, especially if someone broke in to the distributor's > site. > > Possibly slightly pythonic: > > if it is really just a wrapper for a zipfile (i.e. the aforementioned > self-extracting archive), you should be able to open it in 7zip or > similar, and extract the zipfile, without ever "running" it. And if > that is the case, you should be able to script extracting the zipfile > from the .exe, and then extracting the text file from the zipfile, using > Python (or other scripting languages: that's not particularly > Python-specific). > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Mon May 1 20:36:06 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 2 May 2017 01:36:06 +0100 Subject: [Tutor] Using Python to access .txt files stored behind a firewall as .exe files In-Reply-To: References: <56bd6a3a-fb01-c44f-2af7-d950c5d2c29d@wichmann.us> Message-ID: On 02/05/17 01:12, Ian Monat wrote: > 1) Use Python to download the file from the web (but not by using a > webscraper, according to Alan) Things like BeautifulSoup will help you read the HTML and extract links etc but they won't help you actually fetch the file/documents from the web site. A package like requests is the correct tool for that. > 2) Simply ignore the .exe wrapper and use, maybe Windows Task Manager, to > unzip the file and place the .txt file in the desired folder I don't think Task Manager will do it but third party tools exist that can and they cope with self executing zip files too. And you can execute those programs from Python rather than directly executing the unsafe download and, if its not a legitimate zip file they will usually just issue a warning. Once you have a regular zip file the standard Python zip module can extract the text file. -- 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 eryksun at gmail.com Mon May 1 23:02:05 2017 From: eryksun at gmail.com (eryk sun) Date: Tue, 2 May 2017 03:02:05 +0000 Subject: [Tutor] [ctypes-users] Press ESC to exit() In-Reply-To: References: Message-ID: On Mon, May 1, 2017 at 6:28 PM, Michael C wrote: > Hi all, I found out that one way to press ESC to kill the script was to use > my previous > script language, AutoHotKey and this is how it works: > > AutoHotKey code > ## function that kills the window with title '*Python 3.6.1 Shell*' > > kill() > { > WinKill, *Python 3.6.1 Shell* > } > > ## When ESC is pressed, runs the function 'kill' > Esc::kill() > > Yes, that's the entire script. > Is there a way to write it in Python on windows? Instead of using a hotkey, you can set a global low-level keyboard hook. Then optionally you can check for a particular foreground window before handling the key. For example: import ctypes import win32con import win32gui from ctypes import wintypes user32 = ctypes.WinDLL('user32', use_last_error=True) VK_ESCAPE = 0x1B LLKHF_EXTENDED = 0x00000001 LLKHF_LOWER_IL_INJECTED = 0x00000002 LLKHF_INJECTED = 0x00000010 LLKHF_ALTDOWN = 0x00000020 LLKHF_UP = 0x00000080 ULONG_PTR = wintypes.WPARAM class KBDLLHOOKSTRUCT(ctypes.Structure): _fields_ = (('vkCode', wintypes.DWORD), ('scanCode', wintypes.DWORD), ('flags', wintypes.DWORD), ('time', wintypes.DWORD), ('dwExtraInfo', ULONG_PTR)) HOOKPROC = ctypes.WINFUNCTYPE(wintypes.LPARAM, ctypes.c_int, wintypes.WPARAM, wintypes.LPARAM) user32.SetWindowsHookExW.restype = wintypes.HHOOK user32.SetWindowsHookExW.argtypes = ( ctypes.c_int, # _In_ idHook HOOKPROC, # _In_ lpfn wintypes.HINSTANCE, # _In_ hMod wintypes.DWORD) # _In_ dwThreadId user32.CallNextHookEx.restype = wintypes.LPARAM user32.CallNextHookEx.argtypes = ( wintypes.HHOOK, # _In_opt_ hhk ctypes.c_int, # _In_ nCode wintypes.WPARAM, # _In_ wParam wintypes.LPARAM) # _In_ lParam def keyboard_hook(handler, hwnd=None): @HOOKPROC def hookfunc(nCode, wParam, lParam): event = KBDLLHOOKSTRUCT.from_address(lParam) if hwnd is not None and win32gui.GetForegroundWindow() == hwnd: handler(event) return user32.CallNextHookEx(hHook, nCode, wParam, lParam) hHook = user32.SetWindowsHookExW(win32con.WH_KEYBOARD_LL, hookfunc, None, 0) if not hHook: raise ctypes.WinError(ctypes.get_last_error()) win32gui.PumpMessages() if __name__ == '__main__': import msvcrt import threading import win32console print('Press escape to quit') def escape_handler(event): if event.vkCode == VK_ESCAPE: # kill the hook thread win32gui.PostQuitMessage(0) elif not (event.flags & LLKHF_UP): print('Virtual Key Code: {:#04x} [{}]'.format(event.vkCode, event.time)) t = threading.Thread(target=keyboard_hook, args=(escape_handler, win32console.GetConsoleWindow()), daemon=True) t.start() # consume keyboard input while t.is_alive(): if msvcrt.kbhit(): msvcrt.getwch() The __main__ demo should be run as a console script. The keyboard hook filters on the console window handle from GetConsoleWindow(). From eryksun at gmail.com Tue May 2 01:56:37 2017 From: eryksun at gmail.com (eryk sun) Date: Tue, 2 May 2017 05:56:37 +0000 Subject: [Tutor] [ctypes-users] Press ESC to exit() In-Reply-To: References: Message-ID: On Tue, May 2, 2017 at 3:03 AM, Michael C wrote: > holy cow The code for a global keyboard hook is a bit complex - mostly because I had to use ctypes (properly instead of an unreliable hack). Normally an application has one or more windows and a message loop, in which case there's no need for such an awkward approach. But you're asking to do this from a console application, which doesn't own a window. The window procedure that gets keyboard input from the system is in the console host process (conhost.exe). It processes input as a sequence of events that it stores in its input buffer. Thus you could use the input buffer instead of a global keyboard hook, but that comes with its own set of problems. You're no longer getting notified as soon as the character is typed in the window. Instead, the escape-key monitor has to share the input buffer with the main application. Events can be read without removing them via PeekConsoleInput, or read and removed via ReadConsoleInput. They can also be flushed (discarded) via FlushConsoleInputBuffer. If your script doesn't read from the console and the escape-key monitory only peeks the input buffer, it will grow without bound. However, it shouldn't read or flush input events that the main task needs, and the main task shouldn't read events that causes the escape-key monitoring thread to miss the user pressing escape. It needs an integrated design. From dkwolfe at gmail.com Mon May 1 20:42:01 2017 From: dkwolfe at gmail.com (David Wolfe) Date: Mon, 1 May 2017 20:42:01 -0400 Subject: [Tutor] Pycharm Edu Lesson 3, Task 7 Message-ID: Good Evening; I'm working through Pycharm Edu, and I'm stuck on the following: phrase = """ It is a really long string triple-quoted strings are used to define multi-line strings """ first_half = phrase[:len(phrase)//2] print(first_half) The instructions are: The len() function is used to count how many characters a string contains. Get the first half of the string stored in the variable phrase. Note: Remember about type conversion. and my output is: It is a really long string triple-quoted st which is what I'm supposed to get, but I keep getting the error message "Too short string in the output" Any suggestions? Thanks, David Virus-free. www.avg.com <#DAB4FAD8-2DD7-40BB-A1B8-4E2AA1F9FDF2> From mats at wichmann.us Mon May 1 20:47:25 2017 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 1 May 2017 18:47:25 -0600 Subject: [Tutor] Using Python to access .txt files stored behind a firewall as .exe files In-Reply-To: References: <56bd6a3a-fb01-c44f-2af7-d950c5d2c29d@wichmann.us> Message-ID: On 05/01/2017 06:12 PM, Ian Monat wrote: > Thank you for the reply Mats. > > I agree the fact that files are wrapped in an .exe is ridiculous. We're > talking about a $15B company that is doing this by the way, not a ma and pa > shop. Anyways... > > If I understand you correctly, you're saying I can: > > 1) Use Python to download the file from the web (but not by using a > webscraper, according to Alan) > 2) Simply ignore the .exe wrapper and use, maybe Windows Task Manager, to > unzip the file and place the .txt file in the desired folder > > Am I understanding you correctly? > > Thank you -Ian Once you figure out the filename to download - and this may require some scraping of the page - I'm thinking something like: import subprocess downloaded_filename = "something.exe" # whatever you found to download cmd = "7z x " + downloaded_filename res = subprocess.check_output(cmd) examine res to make sure there was something to extract then go on and fish file(s) out of the zipfile I have nothing to experiment with this on, so it's just "thinking out loud". I have at some point used 7z (and most of the other windows archivers that you'd consider "third party" can probably do something like) interactively to fish a zip out of an exe, but there's no reason it wouldn't work from the command line that I know of. From mysecretrobotfactory at gmail.com Mon May 1 23:03:27 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Tue, 02 May 2017 03:03:27 +0000 Subject: [Tutor] [ctypes-users] Press ESC to exit() In-Reply-To: References: Message-ID: holy cow On Mon, May 1, 2017 at 8:02 PM eryk sun wrote: > On Mon, May 1, 2017 at 6:28 PM, Michael C > wrote: > > Hi all, I found out that one way to press ESC to kill the script was to > use > > my previous > > script language, AutoHotKey and this is how it works: > > > > AutoHotKey code > > ## function that kills the window with title '*Python 3.6.1 Shell*' > > > > kill() > > { > > WinKill, *Python 3.6.1 Shell* > > } > > > > ## When ESC is pressed, runs the function 'kill' > > Esc::kill() > > > > Yes, that's the entire script. > > Is there a way to write it in Python on windows? > > Instead of using a hotkey, you can set a global low-level keyboard > hook. Then optionally you can check for a particular foreground window > before handling the key. For example: > > import ctypes > import win32con > import win32gui > > from ctypes import wintypes > > user32 = ctypes.WinDLL('user32', use_last_error=True) > > VK_ESCAPE = 0x1B > > LLKHF_EXTENDED = 0x00000001 > LLKHF_LOWER_IL_INJECTED = 0x00000002 > LLKHF_INJECTED = 0x00000010 > LLKHF_ALTDOWN = 0x00000020 > LLKHF_UP = 0x00000080 > > ULONG_PTR = wintypes.WPARAM > class KBDLLHOOKSTRUCT(ctypes.Structure): > _fields_ = (('vkCode', wintypes.DWORD), > ('scanCode', wintypes.DWORD), > ('flags', wintypes.DWORD), > ('time', wintypes.DWORD), > ('dwExtraInfo', ULONG_PTR)) > > HOOKPROC = ctypes.WINFUNCTYPE(wintypes.LPARAM, ctypes.c_int, > wintypes.WPARAM, wintypes.LPARAM) > > user32.SetWindowsHookExW.restype = wintypes.HHOOK > user32.SetWindowsHookExW.argtypes = ( > ctypes.c_int, # _In_ idHook > HOOKPROC, # _In_ lpfn > wintypes.HINSTANCE, # _In_ hMod > wintypes.DWORD) # _In_ dwThreadId > > user32.CallNextHookEx.restype = wintypes.LPARAM > user32.CallNextHookEx.argtypes = ( > wintypes.HHOOK, # _In_opt_ hhk > ctypes.c_int, # _In_ nCode > wintypes.WPARAM, # _In_ wParam > wintypes.LPARAM) # _In_ lParam > > def keyboard_hook(handler, hwnd=None): > @HOOKPROC > def hookfunc(nCode, wParam, lParam): > event = KBDLLHOOKSTRUCT.from_address(lParam) > if hwnd is not None and win32gui.GetForegroundWindow() == hwnd: > handler(event) > return user32.CallNextHookEx(hHook, nCode, wParam, lParam) > > hHook = user32.SetWindowsHookExW(win32con.WH_KEYBOARD_LL, hookfunc, > None, 0) > if not hHook: > raise ctypes.WinError(ctypes.get_last_error()) > > win32gui.PumpMessages() > > if __name__ == '__main__': > import msvcrt > import threading > import win32console > > print('Press escape to quit') > > def escape_handler(event): > if event.vkCode == VK_ESCAPE: > # kill the hook thread > win32gui.PostQuitMessage(0) > elif not (event.flags & LLKHF_UP): > print('Virtual Key Code: {:#04x} [{}]'.format(event.vkCode, > event.time)) > > t = threading.Thread(target=keyboard_hook, args=(escape_handler, > win32console.GetConsoleWindow()), daemon=True) > t.start() > > # consume keyboard input > while t.is_alive(): > if msvcrt.kbhit(): > msvcrt.getwch() > > The __main__ demo should be run as a console script. The keyboard hook > filters on the console window handle from GetConsoleWindow(). > From alan.gauld at yahoo.co.uk Tue May 2 04:27:56 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 2 May 2017 09:27:56 +0100 Subject: [Tutor] Pycharm Edu Lesson 3, Task 7 In-Reply-To: References: Message-ID: On 02/05/17 01:42, David Wolfe wrote: > I'm working through Pycharm Edu, and I'm stuck on the following: > > phrase = """ > It is a really long string > triple-quoted strings are used > to define multi-line strings > """ > first_half = phrase[:len(phrase)//2] > print(first_half) > > > The instructions are: > The len() function is used to count how many characters a string contains. > > Get the first half of the string stored in the variable phrase. > Note: Remember about type conversion. I'm assuming this is only part of the story since the instructions don't make sense. There is no need for type conversion here. > and my output is: > > It is a really long string > triple-quoted st > > which is what I'm supposed to get, but I keep getting the error message > "Too short string in the output" I don't know the PyCharm IDE but is that all it says? I'd expect a bit more information about where the error lies. It certainly isn't obvious and I don't get any such error when I execute your code in IDLE. -- 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 Tue May 2 04:29:47 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 2 May 2017 09:29:47 +0100 Subject: [Tutor] Press ESC to exit() In-Reply-To: References: Message-ID: On 02/05/17 04:03, Michael C wrote: > holy cow I did warn you it was non trivial :-) Basically Eryksun has done what your scripting tool is doing in the background. -- 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 steve at pearwood.info Tue May 2 12:44:12 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Wed, 3 May 2017 02:44:12 +1000 Subject: [Tutor] Using Python to access .txt files stored behind a firewall as .exe files In-Reply-To: References: Message-ID: <20170502164412.GQ22525@ando.pearwood.info> On Mon, May 01, 2017 at 10:20:42AM -0700, Ian Monat wrote: [...] > Then you have you run the .exe which produces a zipped file, and inside the > zipped file, is the .txt, which what I really want. There's no way the > distributor will change anything about how they store files on their > website for me. I've written a script using the requests module but I > think a web scraper like Scrapy, Beautiful Soup or Selinium may be > required. > > What would you do? Find another distributor. (Its this sort of business to business incompetence that makes me laugh when people say that private industry is always more efficient than the alternatives. Did I say laugh? I meant cry.) Seriously, can't you tell them that your anti-virus blocks the .exe files, and if they want you to use their system, they'll have to provide text files as text files? Or tell them that you're using Apple Macs and the .exe files don't run under Mac. I guess it depends on whether you need them more than they need you. In any case, this isn't a problem that can be solved by a web scraper. The distributor's website provides .exe files. There's nothing you can do about that except complain or leave. The website gives you a .exe file, so that's what you receive. However, once you have the .exe file in your possession, you *may* be able to hack open the file and extract the .zip file without running it. That will require detailed knowledge of how the .exe file does its job, but it is conceivable that it will work. A good low-level hacker could probably determine whether the zip file is embedded in the .exe or if it is generated on the fly. That's beyond my skills though. If it is generated on the fly, you're screwed. You have no choice but to run the .exe, until you do the zip doesn't even exist. But if it is embedded, it can be extracted, and once the zip file is extracted, Python can easily unzip it. -- Steve From ian.monat at gmail.com Tue May 2 14:06:40 2017 From: ian.monat at gmail.com (Ian Monat) Date: Tue, 2 May 2017 11:06:40 -0700 Subject: [Tutor] Using Python to access .txt files stored behind a firewall as .exe files In-Reply-To: <20170502164412.GQ22525@ando.pearwood.info> References: <20170502164412.GQ22525@ando.pearwood.info> Message-ID: Hi Steven, Thanks for your commentary, made me laugh, I wish switching distributors were that easy. I could give them reasons why .exe files won't work for me but they don't really care if I take the data files on their site or not. So I guess to answer your question, we need them more. That said, I think my plan is to use requests to pull the .exe file down and and then try to write a python script to extract the .zip without running the .exe. (maybe with pandas?) I'm a beginner with python so we'll see how it goes! Thanks for your help -Ian On Tue, May 2, 2017 at 9:44 AM, Steven D'Aprano wrote: > On Mon, May 01, 2017 at 10:20:42AM -0700, Ian Monat wrote: > [...] > > Then you have you run the .exe which produces a zipped file, and inside > the > > zipped file, is the .txt, which what I really want. There's no way the > > distributor will change anything about how they store files on their > > website for me. I've written a script using the requests module but I > > think a web scraper like Scrapy, Beautiful Soup or Selinium may be > > required. > > > > What would you do? > > Find another distributor. > > (Its this sort of business to business incompetence that makes me laugh > when people say that private industry is always more efficient than the > alternatives. Did I say laugh? I meant cry.) > > Seriously, can't you tell them that your anti-virus blocks the .exe > files, and if they want you to use their system, they'll have to provide > text files as text files? > > Or tell them that you're using Apple Macs and the .exe files don't run > under Mac. > > I guess it depends on whether you need them more than they need you. > > In any case, this isn't a problem that can be solved by a web scraper. > The distributor's website provides .exe files. There's nothing you can > do about that except complain or leave. The website gives you a .exe > file, so that's what you receive. > > However, once you have the .exe file in your possession, you *may* be > able to hack open the file and extract the .zip file without running it. > That will require detailed knowledge of how the .exe file does its job, > but it is conceivable that it will work. A good low-level hacker could > probably determine whether the zip file is embedded in the .exe or if it > is generated on the fly. That's beyond my skills though. > > If it is generated on the fly, you're screwed. You have no choice but to > run the .exe, until you do the zip doesn't even exist. But if it is > embedded, it can be extracted, and once the zip file is extracted, > Python can easily unzip it. > > > > -- > Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mysecretrobotfactory at gmail.com Tue May 2 14:09:01 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Tue, 2 May 2017 11:09:01 -0700 Subject: [Tutor] Hi all: How do I save a file in a designated folder? Message-ID: from PIL import Image from PIL import ImageGrab # takes the screenshot screenshot = ImageGrab.grab() # display the screenshot screenshot.show() # save the screenshot screenshot.save("\test\missed.png") This is my current code, using Python Image Library! What I would like to get help with is: 1. How to name the file with time stamp. e.g. 05012017.png and so forth. 2. How to save it in a designated folder like C:\test\test2\ and so forth. P.S. I am on windows 10 Thanks! From alan.gauld at yahoo.co.uk Tue May 2 15:36:37 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 2 May 2017 20:36:37 +0100 Subject: [Tutor] Using Python to access .txt files stored behind a firewall as .exe files In-Reply-To: References: <20170502164412.GQ22525@ando.pearwood.info> Message-ID: On 02/05/17 19:06, Ian Monat wrote: > I could give them reasons why .exe files won't work for me but they don't > really care if I take the data files on their site or not. But do they care about their reputation? The biggest issue here is not the technical one but the security one, they could be laying themselves open to serious problems if those exe files are ever tampered with either by a "hacker" or by a disgruntled employee. Most large organizations are contemptuous of technical concerns but brand image is everything! > That said, I think my plan is to use requests to pull the .exe file down > and and then try to write a python script to extract the .zip without > running the .exe. (maybe with pandas?) I suspect not with pandas. pandas is a statistical data processing app and while it can read several file formats I doubt if self extracting zip files is one of them! I think your best6 bet is one of the PC zip archivers such as the one mentioned by Mats, specifically one with a command line interface. Then use the subprocess module in Python to run the extractor and finally unzip the 5text file either via Pythons zip module or via the extractor program again. Once you have the text file pandas may come into play to load and analyze the data. -- 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 Tue May 2 19:01:55 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 3 May 2017 00:01:55 +0100 Subject: [Tutor] Hi all: How do I save a file in a designated folder? In-Reply-To: References: Message-ID: On 02/05/17 19:09, Michael C wrote: > from PIL import Image > from PIL import ImageGrab > > screenshot = ImageGrab.grab() > screenshot.show() > screenshot.save("\test\missed.png") > > This is my current code, using Python Image Library! You should probably investigate Pillow, I believe development of PIL has now ceased in favour of Pillow. Pillow is backwardly compatible with PIL so your existing code should still work. > What I would like to get help with is: > > 1. How to name the file with time stamp. e.g. 05012017.png and so forth. And so forth? I assume you mean MMDDYYYY.png format? You should read about the strftime function in the time (and datetime) module. Other functions thee will find the current date/time for you. Thee are examples on the documentation pages but if you get stuck come back with specific questioons. > 2. How to save it in a designated folder like C:\test\test2\ and so forth. That is just string handling. thee is nothing special about a file path(*), it is just a string. Store the folder as one string then construct your file path with filepath = folder + filename (*)Having said that there is nothing special there is a dedicated module for working with paths - os.path - that has convenience functions for constructing and de-constructing path strings. You might find it useful. > P.S. I am on windows 10 The biggest issue with Windows is its use of \ as a separator. Be sure to use raw strings ( ie prefix an r: r'....') or just use a Unix style [path with / instead of \. Or use os.path which will intelligently decide which separator to use. HTH -- 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 cs at zip.com.au Tue May 2 19:28:10 2017 From: cs at zip.com.au (Cameron Simpson) Date: Wed, 3 May 2017 09:28:10 +1000 Subject: [Tutor] Hi all: How do I save a file in a designated folder? In-Reply-To: References: Message-ID: <20170502232810.GA93088@cskk.homeip.net> On 03May2017 00:01, Alan Gauld wrote: >On 02/05/17 19:09, Michael C wrote: >> 1. How to name the file with time stamp. e.g. 05012017.png and so forth. > >And so forth? I assume you mean > >MMDDYYYY.png format? > >You should read about the strftime function in the time >(and datetime) module. Other functions thee will find >the current date/time for you. Thee are examples on the >documentation pages but if you get stuck come back >with specific questioons. Further to this, I would also advocate that you consider writing the timestamp from largest unit to smallest unit, like an ISO8601 timestamp, which typically looks like: YYYYMMDD.png See: https://www.iso.org/iso-8601-date-and-time-format.html https://en.wikipedia.org/wiki/ISO_8601 This has the advantage that a lexical sort (as in a typical directory listing or file glob) automatically arranges names in date order. I'd also remark that the USA centric format (MMDDYYYY) that Alan inferred is particularly unfortunate in that (a) the rest of the world generally use DDMMYYYY so that the units come in increasing size and (b) doesn't sort very nicely at all. Of course, you might have meant 5th December 2017 above, so who knows? But the very ambiguity makes this one to avoid. For numeric timestamps like yours I only ever use YYYYMMDD (or the obvious variants like YYYY-MM-DD etc). If I use another, I make the month a word (lousy for sorting and parsing, but clear to the human reader); I only do that in prose, not in filenames. Cheers, Cameron Simpson From eryksun at gmail.com Tue May 2 21:08:50 2017 From: eryksun at gmail.com (eryk sun) Date: Wed, 3 May 2017 01:08:50 +0000 Subject: [Tutor] Hi all: How do I save a file in a designated folder? In-Reply-To: References: Message-ID: On Tue, May 2, 2017 at 6:09 PM, Michael C wrote: > screenshot.save("\test\missed.png") You probably know that "\t" represents a tab in a string literal, but there's something about working with a path that causes people to overlook this. Windows won't overlook it. Control characters, i.e. characters with an ordinal value below 32, are forbidden in Windows file names. Also, you're depending on whatever drive or UNC path is the current directory. For example: >>> os.chdir('C:\\') >>> os.path.abspath('/test/missed.png') 'C:\\test\\missed.png' >>> os.chdir('\\\\localhost\C$') >>> os.path.abspath('/test/missed.png') '\\\\localhost\\C$\\test\\missed.png' A fully-qualified path must start with the drive or UNC share. For example: >>> os.path.abspath('C:/test/missed.png') 'C:\\test\\missed.png' >>> os.path.abspath('//localhost/C$/test/missed.png') '\\\\localhost\\C$\\test\\missed.png' In this case I'm using abspath() to normalize the path, which first has Windows itself normalize the path via GetFullPathName(). This shows what Windows will actually try to open or create, given that it implements legacy DOS rules. For example, it ignores trailing spaces and dots in the final path component: >>> os.path.abspath('C:/test/missed.png ... ') 'C:\\test\\missed.png' and DOS devices are virtually present in every directory: >>> os.path.abspath('C:/test/nul.txt') '\\\\.\\nul' From skgoyal721 at gmail.com Wed May 3 01:22:43 2017 From: skgoyal721 at gmail.com (shubham goyal) Date: Wed, 3 May 2017 10:52:43 +0530 Subject: [Tutor] creating a cluster Message-ID: Hello tutors, here i am trying to create a cluster on cloud using json data but its giving some error can you walk me through it. thankyou import urllib2 import requests data="""{ "spark_version": null, "presto_version": null, "label": [ "test" ], "disallow_cluster_termination": false, "force_tunnel": false, "enable_ganglia_monitoring": false, "node_bootstrap_file": null, "tunnel_server_ip": null, "ec2_settings": { "aws_preferred_availability_zone": "Any", "aws_region": "us-east-1", "compute_validated": false, "vpc_id": null, "subnet_id": null, "bastion_node_public_dns": null, "master_elastic_ip": null, "instance_tenancy": null, "compute_secret_key": "", "compute_access_key": "AKIAITFMYWE7A3BG5BDQ", "use_account_compute_creds": true }, "hadoop_settings": { "use_hbase": null, "use_spark": false, "custom_config": null, "use_hadoop2": false, "use_qubole_placement_policy": true, "is_ha": null, "enable_rubix": false, "fairscheduler_settings": { "default_pool": null } }, "node_configuration": { "master_instance_type": "m1.large", "slave_instance_type": "m1.xlarge", "initial_nodes": 1, "max_nodes": 1, "idle_cluster_timeout": null, "slave_request_type": "spot", "fallback_to_ondemand": null, "spot_instance_settings": { "maximum_bid_price_percentage": 100, "timeout_for_request": 10, "maximum_spot_instance_percentage": 50 }, "cluster_name": "qa_qbol_acc5572_cl67507" }, "security_settings": { "encrypted_ephemerals": false }, "presto_settings": { "enable_presto": false, "custom_config": null }, "spark_settings": { "custom_config": null }, "errors": [ "invalid_credentials" ], "datadog_settings": { "datadog_api_token": null, "datadog_app_token": null }, "spark_s3_package_name": null, "zeppelin_s3_package_name": null, "engine_config": {}, "zeppelin_interpreter_mode": null }""" u='auth_token' p='d3d62d04584d49bbaf2a2430a453f3314fb03b38adcd4e58ac671f40638ac6cd' url='https://qa.qubole.net/api/v1.3/clusters' print url headers={'u':'p'} req = requests.post(url, data,headers) #base64string = base64.encodestring('%s:%s' % (u, p)).replace('\n', '') #req.add_header("Authorization", "Basic %s" %base64string) for line in req: print line #f.close() Output: https://qubole.net/api/v1.3/clusters Traceback (most recent call last): File "createCluster.py", line 83, in f = urllib2.urlopen(req) File "/usr/lib/python2.7/urllib2.py", line 154, in urlopen return opener.open(url, data, timeout) File "/usr/lib/python2.7/urllib2.py", line 431, in open response = self._open(req, data) File "/usr/lib/python2.7/urllib2.py", line 449, in _open '_open', req) File "/usr/lib/python2.7/urllib2.py", line 409, in _call_chain result = func(*args) File "/usr/lib/python2.7/urllib2.py", line 1240, in https_open context=self._context) File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open raise URLError(err) urllib2.URLError: From alan.gauld at yahoo.co.uk Wed May 3 02:30:10 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 3 May 2017 07:30:10 +0100 Subject: [Tutor] Hi all: How do I save a file in a designated folder? In-Reply-To: <20170502232810.GA93088@cskk.homeip.net> References: <20170502232810.GA93088@cskk.homeip.net> Message-ID: On 03/05/17 00:28, Cameron Simpson wrote: >> And so forth? I assume you mean >> >> MMDDYYYY.png format? >> >> You should read about the strftime function in the time > > Further to this, I would also advocate that you consider writing the timestamp > from largest unit to smallest unit, like an ISO8601 timestamp, which typically > looks like: > > YYYYMMDD.png Yes, I absolutely second this advice and should have included it. ISO dates make programming life so much easier! -- 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 Wed May 3 02:40:32 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 3 May 2017 07:40:32 +0100 Subject: [Tutor] creating a cluster In-Reply-To: References: Message-ID: On 03/05/17 06:22, shubham goyal wrote: > Hello tutors, > > here i am trying to create a cluster on cloud using json data but its > giving some error > File "/usr/lib/python2.7/urllib2.py", line 1197, in do_open > raise URLError(err) > urllib2.URLError: It says connection refused. Do you have access to the URL? Can you connect using telnet/ssh or a browser? This looks like a networking issue rather than a Python one. -- 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 nathan.delboux at gmail.com Wed May 3 05:40:28 2017 From: nathan.delboux at gmail.com (Nathan D'Elboux) Date: Wed, 03 May 2017 09:40:28 +0000 Subject: [Tutor] Python Bind DNS Zone config Message-ID: Hi all, I am new to python and have been studying Python 3 at uni and in my spare time for the past 6 months or so. I could probably do this in BASH given its on a linux system but am really incorporating python into my daily work as much as i can, so i have chosen python3 to implement this What i am trying to do is have the below syntax somehow stored into a block of strings exactly in this syntax. I dont know what data type i should be trying to store this as, a string obviously but how would i preserve the syntax so it outputs in the identical way? The zone "." { 6 type master; 7 //type hint; 8 file "/etc/bind/db.root.honeypot"; 9 }; In between the "." I will be asking a user for input to insert a domain in plain ASCII and place it in that location of the zone file config and print it to STD OUT on the terminal. Perhaps later another version of this i will include writing directly to a file but for now i want to ensure the inserting of string into specific position works and just print it out. So im thinking of something along the lines of User_input_Zone = str(input("Enter the domain you wish to block: ") Def zone_blacklist New_zone = "zone "{}" {, user_input_zone 6 type master; 7 //type hint; 8 file "/etc/bind/db.root.honeypot"; 9 };" Print(new_zone) What i was thinking of doing is having a place holder of {} in place of where i want the domain to be inserted into but i dont know how to structure the syntax of a zone file in a function within I apologise if its basic, i don't necessarily want the answer just given to me but more of a clue of how someone else may structure it and i will research it myself. Just want some guidance. Thanks, Nathan From robertvstepp at gmail.com Wed May 3 08:24:01 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 3 May 2017 07:24:01 -0500 Subject: [Tutor] Python Bind DNS Zone config In-Reply-To: References: Message-ID: Hello Nathan! On Wed, May 3, 2017 at 4:40 AM, Nathan D'Elboux wrote: > > What i am trying to do is have the below syntax somehow stored into a block > of strings exactly in this syntax. I dont know what data type i should be > trying to store this as, a string obviously but how would i preserve the > syntax so it outputs in the identical way? The > > zone "." { > 6 type master; > 7 //type hint; > 8 file "/etc/bind/db.root.honeypot"; > 9 > > > > > }; Do you know about triple quotes in Python? They can be used when you wish to have a multi-line string. You can use either single quotes: py3: my_string = '''Line one... ... Line two... ... Line three!''' py3: print(my_string) Line one... Line two... Line three! Or you could use three double quotes: py3: my_string = """Line one... ... Line two... ... Line three!""" py3: print(my_string) Line one... Line two... Line three! > In between the "." I will be asking a user for input to insert a domain in > plain ASCII and place it in that location of the zone file config and print > it to STD OUT on the terminal. Perhaps later another version of this i will > include writing directly to a file but for now i want to ensure the > inserting of string into specific position works and just print it out. > > So im thinking of something along the lines of > > User_input_Zone = str(input("Enter the domain you wish to block: ") Note: The str() you use with input() is unnecessary. input() always returns a string; no need to convert it to a string. On the other hand if the input was an integer or a float, then you would need to use int() or float(), respectively. > Def zone_blacklist > New_zone = "zone "{}" {, user_input_zone > 6 type master; > 7 //type hint; > 8 file "/etc/bind/db.root.honeypot"; > 9 };" > Print(new_zone) > > What i was thinking of doing is having a place holder of {} in place of > where i want the domain to be inserted into but i dont know how to > structure the syntax of a zone file in a function within Have you looked into string formatting? See: https://docs.python.org/3/tutorial/inputoutput.html in the Python Tutorial. What you appear to be interested in falls a bit farther down the page. And you can always google Python 3 string formatting. HTH! -- boB From s.molnar at sbcglobal.net Wed May 3 19:32:53 2017 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Wed, 3 May 2017 19:32:53 -0400 Subject: [Tutor] Index Out of Range?List Message-ID: <590A68A5.10606@sbcglobal.net> I have a section of Python 3 code that is intended to be part of a larger program which essentially inputs the file: LOEWDIN ATOMIC CHARGES ---------------------- 0 C : -0.780631 1 H : 0.114577 2 Br: 0.309802 3 Cl: 0.357316 4 F : -0.001065 The code is: import numpy as np name = input("Enter Molecule ID: ") name = str(name) name_in =name[:]+'.lac.dat' print(name_in) atm_chg = [] """ atm_chg = open(name_in,'r') for line in atm_chg: print(line, end=' ') """ #all_contents = [] with open(name_in) as f: # skip two lines f.readline() f.readline() for line in f.readlines(): atm_chg.append(float( line.split()[-1] )) p.asarray({atm_chg}) When it is run I get: IndexError: list index out of range However, the Variable Explorer shows: [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065] [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065] [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065] [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065] One line of which is exactly what I want as input to the next step in the larger calculation. Now, my question is how do I extract just one line of this file? Thanks in advance. -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 From steve at pearwood.info Wed May 3 20:32:24 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 4 May 2017 10:32:24 +1000 Subject: [Tutor] Index Out of Range?List In-Reply-To: <590A68A5.10606@sbcglobal.net> References: <590A68A5.10606@sbcglobal.net> Message-ID: <20170504003222.GW22525@ando.pearwood.info> On Wed, May 03, 2017 at 07:32:53PM -0400, Stephen P. Molnar wrote: [...] > When it is run I get: > > IndexError: list index out of range That alone is useless to us. Please post the full traceback, starting from the line Traceback (most recent call last): Among other things, it will show us the line of code that causes the error. Otherwise, we're just guessing. You might also like to consider a simple debugging technique: print the index and the length of the list just before trying to use them. E.g. if you want to extract the nth item of a list, write: print(n, len(a_list)) value = a_list[n] (And yes, I completely agree that the error message should show that information, but it currently doesn't.) -- Steve From alan.gauld at yahoo.co.uk Wed May 3 20:51:41 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 4 May 2017 01:51:41 +0100 Subject: [Tutor] Index Out of Range?List In-Reply-To: <590A68A5.10606@sbcglobal.net> References: <590A68A5.10606@sbcglobal.net> Message-ID: On 04/05/17 00:32, Stephen P. Molnar wrote: > import numpy as np > > name = input("Enter Molecule ID: ") > name = str(name) You don't need the str(), input always returns a string. > name_in =name[:]+'.lac.dat' And you don't need the [:]. Just use name_in = name + '.lac.dat' > print(name_in) > > atm_chg = [] > """ > atm_chg = open(name_in,'r') > for line in atm_chg: > print(line, end=' ') > """ This creates a 3 line string which is not assigned to any object. It is not executable code and will not be executed. Maybe you are doing it as a way of commenting out a block? If so it would be better in a post to just delete it, it just adds confusion otherwise. (Well, it confused me! :-) > with open(name_in) as f: > # skip two lines > f.readline() > f.readline() > for line in f.readlines(): > atm_chg.append(float( line.split()[-1] )) > > p.asarray({atm_chg}) Where did p come from? Should it be np? asarray() sounds like it might be a numpy thing. And I'm not sure what the {atm_chg} is supposed to do - create a single element set using your list maybe? I get an "unhashable" error if I try it at the >>>> prompt. > When it is run I get: > > IndexError: list index out of range I'm pretty sure you get more than that, please post the full error text, it's much harder to diagnose problems with just the summary. Since the only indexing you do is in this line > atm_chg.append(float( line.split()[-1] )) I'll assume that's where the problem lies. Try checking if the string is not empty before using it: for line in f.readlines(): if line: atm_chg.append(float( line.split()[-1] )) > However, the Variable Explorer shows: I have no idea what the Variable Explorer is? Is it part of your IDE? Or of numpy? If the IDE which IDE are you using? > [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065] > [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065] > [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065] > [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065] > > One line of which is exactly what I want as input to the next step in > the larger calculation. > > Now, my question is how do I extract just one line of this file? Any particular line? And which file are you talking about? The data should be in the list variable, atm_chg. In which case the first line is therefore: atm_chg[0] Or you can process each line using the usual for loop: for line in atm_chg: # use line here.... -- 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 nathan.delboux at gmail.com Wed May 3 23:11:19 2017 From: nathan.delboux at gmail.com (Nathan D'Elboux) Date: Thu, 4 May 2017 13:11:19 +1000 Subject: [Tutor] Python Bind DNS Zone config In-Reply-To: References: Message-ID: Thanks so much for your response. Ok so i now have the following code new_domain = input("Enter domain you wish to blacklist: ") print("""zone {} IN { type master; file "zones/1.2.3.4.zone"; allow-transfer { none; }; allow-query { test-test; }; };""".format(new_domain)) when i run the above code it prompts me to enter the domain so i do, then the out put is a "Value error: Unexpected '{' in field name So i comment out the .format statement and i get the exact output i want but nothing in the {} statement I need to either escape the opening { after the IN statement as the .format(new_domain)) statement doesnt seem to be detecting i have the {} ready to take the parameter new_domain? Thanks for the link it certainly helped On Wed, May 3, 2017 at 10:24 PM, boB Stepp wrote: > Hello Nathan! > > On Wed, May 3, 2017 at 4:40 AM, Nathan D'Elboux > wrote: > >> >> What i am trying to do is have the below syntax somehow stored into a block >> of strings exactly in this syntax. I dont know what data type i should be >> trying to store this as, a string obviously but how would i preserve the >> syntax so it outputs in the identical way? The >> >> zone "." { >> 6 type master; >> 7 //type hint; >> 8 file "/etc/bind/db.root.honeypot"; >> 9 >> >> >> >> >> }; > > Do you know about triple quotes in Python? They can be used when you > wish to have a multi-line string. You can use either single quotes: > > py3: my_string = '''Line one... > ... Line two... > ... Line three!''' > py3: print(my_string) > Line one... > Line two... > Line three! > > Or you could use three double quotes: > > py3: my_string = """Line one... > ... Line two... > ... Line three!""" > py3: print(my_string) > Line one... > Line two... > Line three! > >> In between the "." I will be asking a user for input to insert a domain in >> plain ASCII and place it in that location of the zone file config and print >> it to STD OUT on the terminal. Perhaps later another version of this i will >> include writing directly to a file but for now i want to ensure the >> inserting of string into specific position works and just print it out. >> >> So im thinking of something along the lines of >> >> User_input_Zone = str(input("Enter the domain you wish to block: ") > > Note: The str() you use with input() is unnecessary. input() always > returns a string; no need to convert it to a string. On the other > hand if the input was an integer or a float, then you would need to > use int() or float(), respectively. > >> Def zone_blacklist >> New_zone = "zone "{}" {, user_input_zone >> 6 type master; >> 7 //type hint; >> 8 file "/etc/bind/db.root.honeypot"; >> 9 };" >> Print(new_zone) >> >> What i was thinking of doing is having a place holder of {} in place of >> where i want the domain to be inserted into but i dont know how to >> structure the syntax of a zone file in a function within > > Have you looked into string formatting? See: > > https://docs.python.org/3/tutorial/inputoutput.html > > in the Python Tutorial. What you appear to be interested in falls a > bit farther down the page. And you can always google Python 3 string > formatting. > > HTH! > > -- > boB From alan.gauld at yahoo.co.uk Thu May 4 05:50:07 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 4 May 2017 10:50:07 +0100 Subject: [Tutor] Python Bind DNS Zone config In-Reply-To: References: Message-ID: On 04/05/17 04:11, Nathan D'Elboux wrote: > when i run the above code it prompts me to enter the domain so i do, > then the out put is a "Value error: Unexpected '{' in field name Always, always, always post the full error message. Do not shorten it to the summary line. We can't see the location information so er have to guess and that's never reliable. > So i comment out the .format statement and i get the exact output i > want but nothing in the {} statement The problem could be avoided by not having the format markers in the string. This is easily done by using traditional formatting using % characters instead of the {} style. print("""zone {%s} IN { type master; file "zones/1.2.3.4.zone"; allow-transfer { none; }; allow-query { test-test; }; };""" % new_domain) I'm sure there is a way to do it using format() but it probably involves a lot of escape characters etc. I'd take the easy route and use printf style % markers. -- 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 s.molnar at sbcglobal.net Thu May 4 08:21:04 2017 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Thu, 4 May 2017 08:21:04 -0400 Subject: [Tutor] Index Out of Range?List In-Reply-To: <20170504003222.GW22525@ando.pearwood.info> References: <590A68A5.10606@sbcglobal.net> <20170504003222.GW22525@ando.pearwood.info> Message-ID: <590B1CB0.4050401@sbcglobal.net> On 05/03/2017 08:32 PM, Steven D'Aprano wrote: > On Wed, May 03, 2017 at 07:32:53PM -0400, Stephen P. Molnar wrote: > > [...] >> When it is run I get: >> >> IndexError: list index out of range > > That alone is useless to us. Please post the full traceback, starting > from the line > > Traceback (most recent call last): > > Among other things, it will show us the line of code that causes the > error. Otherwise, we're just guessing. > > You might also like to consider a simple debugging technique: print > the index and the length of the list just before trying to use them. > E.g. if you want to extract the nth item of a list, write: > > print(n, len(a_list)) > value = a_list[n] > > > (And yes, I completely agree that the error message should show that > information, but it currently doesn't.) > > Thanks for the reply. Here are the error messages: Enter Molecule ID: A A.lac.dat Traceback (most recent call last): File "", line 1, in runfile('/home/comp/Apps/Python/Testing/ReadFile_1.py', wdir='/home/comp/Apps/Python/Testing') File "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile execfile(filename, namespace) File "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "/home/comp/Apps/Python/Testing/ReadFile_1.py", line 32, in atm_chg.append(float( line.split()[-1] )) IndexError: list index out of range -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 From alan.gauld at yahoo.co.uk Thu May 4 09:44:38 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 4 May 2017 14:44:38 +0100 Subject: [Tutor] Index Out of Range?List In-Reply-To: <590B1CB0.4050401@sbcglobal.net> References: <590A68A5.10606@sbcglobal.net> <20170504003222.GW22525@ando.pearwood.info> <590B1CB0.4050401@sbcglobal.net> Message-ID: On 04/05/17 13:21, Stephen P. Molnar wrote: > Here are the error messages: > > Enter Molecule ID: A > A.lac.dat > Traceback (most recent call last): > > File "", line 1, in > runfile('/home/comp/Apps/Python/Testing/ReadFile_1.py', > wdir='/home/comp/Apps/Python/Testing') There appears to be a missing opening paren? Or a surplus closing one? Either way this line is not in the code you sent us. It may be coming from your IDE - which seems to be spyder. Can you run the code from a command prompt so we see the unpolluted Python output. IDEs are useful but they sometimes add in extra layers that obfuscate things. > "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", > line 102, in execfile > exec(compile(f.read(), filename, 'exec'), namespace) > > File "/home/comp/Apps/Python/Testing/ReadFile_1.py", line 32, in > atm_chg.append(float( line.split()[-1] )) > > IndexError: list index out of range But it does look like it's this line that causes the error so I'd try the check for emptiness as described in my previous post. -- 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 s.molnar at sbcglobal.net Thu May 4 08:50:28 2017 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Thu, 4 May 2017 08:50:28 -0400 Subject: [Tutor] Index Out of Range?List In-Reply-To: References: <590A68A5.10606@sbcglobal.net> Message-ID: <590B2394.40204@sbcglobal.net> On 05/03/2017 08:51 PM, Alan Gauld via Tutor wrote: > On 04/05/17 00:32, Stephen P. Molnar wrote: > >> import numpy as np >> >> name = input("Enter Molecule ID: ") >> name = str(name) > > You don't need the str(), input always returns a string. > >> name_in =name[:]+'.lac.dat' > > And you don't need the [:]. Just use > > name_in = name + '.lac.dat' > >> print(name_in) >> >> atm_chg = [] > >> """ >> atm_chg = open(name_in,'r') >> for line in atm_chg: >> print(line, end=' ') >> """ > This creates a 3 line string which is not assigned to any object. > It is not executable code and will not be executed. Maybe you > are doing it as a way of commenting out a block? If so it would be > better in a post to just delete it, it just adds confusion > otherwise. (Well, it confused me! :-) > >> with open(name_in) as f: >> # skip two lines >> f.readline() >> f.readline() >> for line in f.readlines(): >> atm_chg.append(float( line.split()[-1] )) >> >> p.asarray({atm_chg}) > > Where did p come from? > Should it be np? asarray() sounds like it might be a numpy thing. > And I'm not sure what the {atm_chg} is supposed to do - create > a single element set using your list maybe? I get an "unhashable" > error if I try it at the >>>> prompt. > >> When it is run I get: >> >> IndexError: list index out of range > > I'm pretty sure you get more than that, please post the full > error text, it's much harder to diagnose problems with just > the summary. > > Since the only indexing you do is in this line > >> atm_chg.append(float( line.split()[-1] )) > > I'll assume that's where the problem lies. > Try checking if the string is not empty before using it: > > for line in f.readlines(): > if line: > atm_chg.append(float( line.split()[-1] )) > > >> However, the Variable Explorer shows: > > I have no idea what the Variable Explorer is? > Is it part of your IDE? Or of numpy? > If the IDE which IDE are you using? > >> [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065] >> [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065] >> [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065] >> [-0.780631, 0.114577, 0.309802, 0.357316, -0.001065] >> >> One line of which is exactly what I want as input to the next step in >> the larger calculation. >> >> Now, my question is how do I extract just one line of this file? > > Any particular line? And which file are you talking about? > The data should be in the list variable, atm_chg. > In which case the first line is therefore: atm_chg[0] > > Or you can process each line using the usual for loop: > > for line in atm_chg: > # use line here.... > Thanks for your reply. The Variable Explorer is part of the Spyder IDE. I have edited the code: import numpy as np name = input("Enter Molecule ID: ") #name = str(name) name_in =name[:]+'.lac.dat' print(name_in) atm_chg = [] with open(name_in) as f: # skip two lines f.readline() f.readline() for line in f.readlines(): atm_chg.append(float( line.split()[-1] )) np.asarray({atm_chg}) Execution still generates the errors: runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', wdir='/home/comp/Apps/Python/Testing') Enter Molecule ID: A A.lac.dat Traceback (most recent call last): File "", line 1, in runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', wdir='/home/comp/Apps/Python/Testing') File "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile execfile(filename, namespace) File "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "/home/comp/Apps/Python/Testing/ReadFile_2.py", line 27, in atm_chg.append(float( line.split()[-1] )) IndexError: list index out of range from the input file: runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', wdir='/home/comp/Apps/Python/Testing') Enter Molecule ID: A A.lac.dat Traceback (most recent call last): File "", line 1, in runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', wdir='/home/comp/Apps/Python/Testing') File "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile execfile(filename, namespace) File "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "/home/comp/Apps/Python/Testing/ReadFile_2.py", line 27, in atm_chg.append(float( line.split()[-1] )) IndexError: list index out of range Finally, here is the code for which I need input data for the calculation of the Integrated Charge Transform: #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Apr 8 15:17:07 2017 @author: comp Copyright (c) 2017 Stephen P. Molnar, Ph.D. """ import matplotlib.pyplot as plt import numpy as np # you don't need pandas, np.genfromtxt() reads this type of txt files. plt.ion() # interactive plotting, stopps plt.show() from blocking. #import math # you won't need math if you have numpy start=1 finish=31 points=300 s = np.linspace(start, finish, points) np.savetxt('s',s) name = input("Enter Molecule ID: ") name = str(name) print(name) name_in = name+'.dat' print(name) dtype = [('NO', int), ('LB', 'S2'), ('ZA', float), ('FRAG', int), ('MASS', float), ('X', float), ('Y', float), ('Z', float)] data = np.genfromtxt(name_in, dtype=dtype, skip_header=3) N = data.shape[0] #number of atoms in molecule a = np.array([data['X'], data['Y'], data['Z']]) #atomic coordinates #dist.squareform(dist.pdist(a, "euclidean")) anrows, ancols = np.shape(a) a_new = a.reshape(anrows, 1, ancols) diff = a_new - a D = (diff ** 2).sum(2) D = np.sqrt(D) r = D def eq7(a, s, Z): """ Computes equation 7 in Molnar & King (2001) """ N = r.shape[0] I = np.zeros(s.shape) for i in range(1, N): for j in range(i): I += Z[i] * Z[j] * np.sin(s * r[i, j])/(s * r[i, j]) return I I = eq7(r, s, data['ZA']) name = 'I_z-'+str(name) np.savetxt(name,I) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(s.T, I) plt.title("Molecular Transform: ${}$".format(name)) fig.gca().set_xlabel("Distance (?)") plt.ylabel('Atomic Number Transform (I$_z$)') plt.show() I_sq = [] I_sq = I**2 Area = [] Area = np.trapz(I_sq,x=None,dx=0.01,axis=-1) np.savetxt('I_sq',I_sq) I_z = np.sqrt(Area) print('FTz: ',I_z) #-------------------------------------------------------------------------- # MASS Mollecular Transform FT_m I = eq7(r, s, data['MASS']) name = 'I_m-'+str(name) np.savetxt(name,I) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(s.T, I) plt.title("Molecular Transform: ${}$".format(name)) fig.gca().set_xlabel("Distance (?)") plt.ylabel('MASS Transform (I$_m$)') plt.show() I_sq = I**2 Area = np.trapz(I_sq,x=None,dx=0.01,axis=-1) np.savetxt('I_sq',I_sq,delimiter=' ') I_z = np.sqrt(Area) I_m = I_z print('FT_m: ',I_m) #------------------------------------------------------------------------------ #Integrated Charge Transform print(name) -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 From dkwolfe at gmail.com Thu May 4 12:51:34 2017 From: dkwolfe at gmail.com (David Wolfe) Date: Thu, 4 May 2017 12:51:34 -0400 Subject: [Tutor] Pycharm Edu Lesson 3, Task 7 In-Reply-To: References: Message-ID: Thanks all for the help. Pycharm just posted an update to their Pycharm Edu product, and when I re-ran the script, everything was fine. Best, David On Tue, May 2, 2017 at 4:27 AM, Alan Gauld via Tutor wrote: > On 02/05/17 01:42, David Wolfe wrote: > > > I'm working through Pycharm Edu, and I'm stuck on the following: > > > > phrase = """ > > It is a really long string > > triple-quoted strings are used > > to define multi-line strings > > """ > > first_half = phrase[:len(phrase)//2] > > print(first_half) > > > > > > The instructions are: > > The len() function is used to count how many characters a string > contains. > > > > Get the first half of the string stored in the variable phrase. > > Note: Remember about type conversion. > > I'm assuming this is only part of the story since the instructions don't > make sense. There is no need for type conversion here. > > > and my output is: > > > > It is a really long string > > triple-quoted st > > > > which is what I'm supposed to get, but I keep getting the error message > > "Too short string in the output" > > I don't know the PyCharm IDE but is that all it says? > I'd expect a bit more information about where the error lies. > It certainly isn't obvious and I don't get any such error when > I execute your code in IDLE. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mats at wichmann.us Thu May 4 13:24:16 2017 From: mats at wichmann.us (Mats Wichmann) Date: Thu, 4 May 2017 11:24:16 -0600 Subject: [Tutor] Index Out of Range?List In-Reply-To: <590B2394.40204@sbcglobal.net> References: <590A68A5.10606@sbcglobal.net> <590B2394.40204@sbcglobal.net> Message-ID: <63e7eb4e-b317-7820-f0d7-f02e98eac401@wichmann.us> > atm_chg.append(float( line.split()[-1] )) > > > np.asarray({atm_chg}) > > Execution still generates the errors: > > runfile('/home/comp/Apps/Python/Testing/ReadFile_2.py', > wdir='/home/comp/Apps/Python/Testing') that means you have a blank line it's reading, the result of splitting an empty line is an empty list, which you can't index since it has no members. $ python Python 2.7.13 (default, Jan 12 2017, 17:59:37) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> ''.split() [] >>> ''.split()[-1] Traceback (most recent call last): File "", line 1, in IndexError: list index out of range >>> you're going to need to do a little input validation, always a good idea anyway. that is inside this loop: for line in f.readlines(): atm_chg.append(float( line.split()[-1] )) check there's something usable in line before you split-and-use it. From alan.gauld at yahoo.co.uk Thu May 4 13:31:05 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 4 May 2017 18:31:05 +0100 Subject: [Tutor] Index Out of Range?List In-Reply-To: <590B2394.40204@sbcglobal.net> References: <590A68A5.10606@sbcglobal.net> <590B2394.40204@sbcglobal.net> Message-ID: On 04/05/17 13:50, Stephen P. Molnar wrote: >> I'll assume that's where the problem lies. >> Try checking if the string is not empty before using it: >> >> for line in f.readlines(): >> if line: >> atm_chg.append(float( line.split()[-1] )) > I have edited the code: > > for line in f.readlines(): > atm_chg.append(float( line.split()[-1] )) > Execution still generates the errors: That's because you didn't include the change that I thought might fix the error. See above. -- 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 timeofsands at gmail.com Thu May 4 14:44:33 2017 From: timeofsands at gmail.com (Palm Tree) Date: Thu, 4 May 2017 22:44:33 +0400 Subject: [Tutor] selenium, automated testing and python In-Reply-To: References: Message-ID: Sorry i'm a bit new to automated testing. I have explored python wide and deep but can someone just tell me why as a coder i need automated testing? i have tried some googling but the explanations are a bit crazy. thanks. From alan.gauld at yahoo.co.uk Thu May 4 17:09:56 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 4 May 2017 22:09:56 +0100 Subject: [Tutor] selenium, automated testing and python In-Reply-To: References: Message-ID: On 04/05/17 19:44, Palm Tree wrote: > Sorry i'm a bit new to automated testing. > > I have explored python wide and deep but can someone just tell me why as a > coder i need automated testing? You don't, you can test it all by hand. But it will take you much longer, especially after you've done, say, the 50th update to your program and had to retest every feature for the 50th time. Automated testing means you can type a command and wait while the PC does the work for you, usually only a matter of a few minutes to an hour - much less than the several hours it would take you by hand to do the very same tests. And the automated tool doesn't forget or use the wrong data with the wrong test etc. So its more consistent. And as you add new features you only need to add a few new tests and those features get tested each time too. When you are starting out it seems like you spend more time writing test cases than code 9and sometimes its even true!) but if your project survives for more than a few iterations its well worth the investment. And of course the corollary to that is that if its a one-off tool that is only for you and you'll "never use again" manual testing may be adequate. (But its surprising how many of those tools do get used again!) > i have tried some googling but the explanations are a bit crazy. Really? what kind of crazy? There are several Youtube videos that cover the reasons why, as well as the how to do it. They are mostly short too. And the reasons above are only scratching the surface, there are many more gains that apply to testing in general but are only practical if testing is automated. -- 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 Thu May 4 19:59:49 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 5 May 2017 00:59:49 +0100 Subject: [Tutor] selenium, automated testing and python In-Reply-To: References: Message-ID: On 04/05/17 22:09, Alan Gauld via Tutor wrote: > And the reasons above are only scratching the > surface, there are many more gains that apply to > testing in general but are only practical if > testing is automated. One thing I meant to add is that "automated" does not necessarily mean using a framework like unittest or nose etc. You can create your own dedicated test harness and use that. (In fact that's exactly what we all did for every project before generic test frameworks were invented around the early 1990s) For some types of application that can be easier than trying to force a framework to do your bidding. It's still automated testing. But, for most of the time, unittest and its cousins are a convenient approach that is well understood and, importantly, supported. And in python, as a bare minimum, consider doctest. It is by far the simplest to use, although also the most limited but it's still a lot better than nothing! -- 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 rafael.knuth at gmail.com Fri May 5 09:45:00 2017 From: rafael.knuth at gmail.com (Rafael Knuth) Date: Fri, 5 May 2017 15:45:00 +0200 Subject: [Tutor] General question rgrd. usage of libraries Message-ID: Hi there, I just recently learned how to build a basic web scraper with Python 3.5 (I am learning Python for data analytics purposes). Being new to coding, I have a question: How do I know which libraries I need to perform a certain task? For example, in case of this web scraper (which I built with help of a tutorial on YouTube) I need to have urrlib and Beautiful Soup import urllib import urllib.request from bs4 import BeautifulSoup theurl = "https://twitter.com/rafaelknuth" thepage = urllib.request.urlopen(theurl) soup = BeautifulSoup(thepage, "html.parser") print(soup.title.text) i = 1 for tweets in soup.findAll("div",{"class":"content"}): print(i) print(tweets.find("p").text) i = i + 1 Is there a way I can figure out which libraries I need when drafting my code? Can you share your experiences? Right now, if I wanted for example to populate a Google Sheet with my scraped web content - how would I know which libraries I would need to actually make this happen? I am trying wondering if there is a process to figure out what I exactly need library-wise. Thank you, Rafael From alan.gauld at yahoo.co.uk Fri May 5 14:01:23 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 5 May 2017 19:01:23 +0100 Subject: [Tutor] General question rgrd. usage of libraries In-Reply-To: References: Message-ID: On 05/05/17 14:45, Rafael Knuth wrote: > How do I know which libraries I need to perform a certain task? Mostly youn learn by experience, but otherwise google (or other search engine) is your friend. It doesn't do any harm to read the Library Reference document on the python.org web site too, it gives a good overview of the most common ones. But for stuff outside the standard library its pretty much down to google and asking on fora like the tutor list or a more topic related one. > populate a Google Sheet with my scraped web content One thing that will definitely help when asking on forums is to identify the correct (or at least the common) terminology. For example I've no idea what a "Google Sheet" is - that may be the correct term - but but I recognize the term I might be able to offer help. If I don't recognize the term I'll assume I don't know the answer. So some research before asking definitely helps. And don't forget PyPI which has libraries categorised to make browsing/searching easier. -- 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 jf_byrnes at comcast.net Fri May 5 16:56:21 2017 From: jf_byrnes at comcast.net (Jim) Date: Fri, 5 May 2017 15:56:21 -0500 Subject: [Tutor] General question rgrd. usage of libraries In-Reply-To: References: Message-ID: On 05/05/2017 08:45 AM, Rafael Knuth wrote: > Hi there, > > I just recently learned how to build a basic web scraper with Python > 3.5 (I am learning Python for data analytics purposes). Being new to > coding, I have a question: > > How do I know which libraries I need to perform a certain task? > For example, in case of this web scraper (which I built with help of a > tutorial on YouTube) I need to have urrlib and Beautiful Soup > > import urllib > import urllib.request > from bs4 import BeautifulSoup > > theurl = "https://twitter.com/rafaelknuth" > thepage = urllib.request.urlopen(theurl) > soup = BeautifulSoup(thepage, "html.parser") > > print(soup.title.text) > > i = 1 > for tweets in soup.findAll("div",{"class":"content"}): > print(i) > print(tweets.find("p").text) > i = i + 1 > > Is there a way I can figure out which libraries I need when drafting my code? > Can you share your experiences? Right now, if I wanted for example to > populate a Google Sheet with my scraped web content - how would I know > which libraries I would need to actually make this happen? I am trying > wondering if there is a process to figure out what I exactly need > library-wise. > > There is a Python API to google sheets but when I had a look, it seemed fairly complex. I haven't tried it yet but depending on what you need to do this library may be what you need: https://pypi.python.org/pypi/gspread. Regards, Jim From timeofsands at gmail.com Fri May 5 23:12:27 2017 From: timeofsands at gmail.com (Palm Tree) Date: Sat, 6 May 2017 07:12:27 +0400 Subject: [Tutor] General question rgrd. usage of libraries In-Reply-To: References: Message-ID: Hum i also suggest you get more experience with python think of a project and learn while doing it. thus you'll get motivation while at the same time doing something useful which you could reuse in the future. else, me for bs4 i googled what i needed. I also put an increasing variable to ease web scraping tasks. like var =0 ... print(var, element) also, i suggest you decode to unicode as you'll get crazy hex stuffs if you don't .decode("utf-8") i scrape websites written in french, so i always need unicode. else the .text is very helpful like 'p' gives you the element but 'p.text' gives you the content To find suitable libraries i suggest you become good at doing the desired task by hand as far as possible, so you'll know your job well. Then you identify boring, impossible or tiring tasks. Then you google like .. python module or just python how to and see how they did it or what module they used to do it. Hope it helps, Abdur-Rahmaan Janhangeer, Mauritius On 6 May 2017 00:56, "Jim" wrote: > On 05/05/2017 08:45 AM, Rafael Knuth wrote: > >> Hi there, >> >> I just recently learned how to build a basic web scraper with Python >> 3.5 (I am learning Python for data analytics purposes). Being new to >> coding, I have a question: >> >> How do I know which libraries I need to perform a certain task? >> For example, in case of this web scraper (which I built with help of a >> tutorial on YouTube) I need to have urrlib and Beautiful Soup >> >> import urllib >> import urllib.request >> from bs4 import BeautifulSoup >> >> theurl = "https://twitter.com/rafaelknuth" >> thepage = urllib.request.urlopen(theurl) >> soup = BeautifulSoup(thepage, "html.parser") >> >> print(soup.title.text) >> >> i = 1 >> for tweets in soup.findAll("div",{"class":"content"}): >> print(i) >> print(tweets.find("p").text) >> i = i + 1 >> >> Is there a way I can figure out which libraries I need when drafting my >> code? >> Can you share your experiences? Right now, if I wanted for example to >> populate a Google Sheet with my scraped web content - how would I know >> which libraries I would need to actually make this happen? I am trying >> wondering if there is a process to figure out what I exactly need >> library-wise. >> >> >> > There is a Python API to google sheets but when I had a look, it seemed > fairly complex. I haven't tried it yet but depending on what you need to do > this library may be what you need: > https://pypi.python.org/pypi/gspread. > > Regards, Jim > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From jojo.mwebaze at gmail.com Sun May 7 11:23:58 2017 From: jojo.mwebaze at gmail.com (Jojo Mwebaze) Date: Sun, 7 May 2017 18:23:58 +0300 Subject: [Tutor] best Python Web-Development Applications framework. Message-ID: Dear All, I am trying to figure out the best Python Web-Development Applications framework. I trying to get started with building Web-Based Applications. Kindly give advise which one is the best for a novice user Cheers, Johnson From alan.gauld at yahoo.co.uk Sun May 7 12:49:01 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 7 May 2017 17:49:01 +0100 Subject: [Tutor] best Python Web-Development Applications framework. In-Reply-To: References: Message-ID: On 07/05/17 16:23, Jojo Mwebaze wrote: > I am trying to figure out the best Python Web-Development Applications > framework. I trying to get started with building Web-Based Applications. > Kindly give advise which one is the best for a novice user It depends on several factors which you haven't told us about. The only thing we know is you describe yourself as a "novice user". What kind of web applications do you want to build? Different frameworks suit different application areas better than others. What kind of end result are you aiming for? An industrial strength app that you can deploy into a public data centre with many visitors? (How many? How many transactions per day/hour?) What kind of security requirements do you have? Will this hold personal data? Financial information? Or other critical data? Or is it public domain with very little security needs, not even user logins? What kind of processing will it be doing? Heavy CPU data analysis? Or database searches? Highly interactive network intensive? Or mainly displaying static web pages? There are other things we could consider. But these are all things that will make a difference to which framework is best for you. In general the bigger the ambition the bigger the framework you need. The good news is that they are all fairly easy to get started with (except maybe Zope). If you plan on going public then the choice will likely be driven by what your web hosting service supports. (And many don't support Python at all!) I've used a couple of very simple frameworks and can't really recommend one above the other: Pylons and Flask. As a default start with one of these and see how far they take you, the basic principles will apply to the bigger boys too. Of the mid-range frameworks I've used both Turbo-Gears and Django. Django has a very definite way of working, TG is more flexible (IMHO) but that implies more experience from the user. Django has better support and tutorials etc. Of the heavyweight frameworks, I've not used any of them, although I did look at Zope, but my hosting options required Java so I went down that route. The final thing to consider is how much Javascript and browser-side work you intend to do. Almost all modern web apps rely on a lot of Javascript code in the browser for the user experience side of things. And that changes the whole approach to server side development too - the server becomes much more of a data oriented server with very little HTML/CSS processing going on. -- 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 rafael.knuth at gmail.com Mon May 8 11:23:15 2017 From: rafael.knuth at gmail.com (Rafael Knuth) Date: Mon, 8 May 2017 17:23:15 +0200 Subject: [Tutor] urllib ... lost novice's question Message-ID: Which package should I use to fetch and open an URL? I am using Python 3.5 and there are presently 4 versions: urllib2 urllib3 urllib4 urllib5 Common sense is telling me to use the latest version. Not sure if my common sense is fooling me here though ;-) Then, there is another package, along with a dozen other urllib-related packages (such as aiourllib). I thought this one is doing what I need: urllib.request The latter I found on http://docs.python-requests.org along with these encouraging words: "Warning: Recreational use of the Python standard library for HTTP may result in dangerous side-effects, including: security vulnerabilities, verbose code, reinventing the wheel, constantly reading documentation, depression, headaches, or even death." How do I know where to find the right package - on python.org or elsewhere? I found some code samples that show how to use urllib.request, now I am trying to understand why I should use urllib.request. Would it be also doable to do requests using urllib5 or any other version? Like 2 or 3? Just trying to understand. I am lost here. Feeback appreciated. Thank you! BTW, here's some (working) exemplary code I have been using for educational purposes: import urllib.request from bs4 import BeautifulSoup theurl = "https://twitter.com/rafaelknuth" thepage = urllib.request.urlopen(theurl) soup = BeautifulSoup(thepage, "html.parser") print(soup.title.text) i = 1 for tweets in soup.findAll("div",{"class":"content"}): print(i) print(tweets.find("p").text) i = i + 1 I am assuming there are different solutions for fetching and open URLs? Or is the above the only viable solution? From alan.gauld at yahoo.co.uk Mon May 8 21:06:45 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 9 May 2017 02:06:45 +0100 Subject: [Tutor] urllib ... lost novice's question In-Reply-To: References: Message-ID: On 08/05/17 16:23, Rafael Knuth wrote: > Which package should I use to fetch and open an URL? > I am using Python 3.5 and there are presently 4 versions: > > urllib2 > urllib3 > urllib4 > urllib5 I don't know where you are getting those from but the standard install of Python v3.6 only has urllib. This is a package with various modules inside. ISTR there was a urllib2 in Python 2 for a while but I've never heard of any 3,4, or 5. > Then, there is another package, along with a dozen other > urllib-related packages (such as aiourllib). Again, where are you finding these? They are not in the standard library. Have you been installing other packages that may have their own versions maybe? > urllib.request > > The latter I found on http://docs.python-requests.org along with these > encouraging words: > > "Warning: Recreational use of the Python standard library for HTTP may > result in dangerous side-effects, including: security vulnerabilities, > verbose code, reinventing the wheel, constantly reading documentation, > depression, headaches, or even death." That's true of almost any package used badly. Remember that this is "marketing" propaganda from an alternative package maintainer. And while most folks (including me)seem to agree that Requests is easier to use than the standard library, the standard library version works just fine if you take sensible care. > How do I know where to find the right package There is no right package, just the one you find most effective. Most folks would say that Requests is easier to use than the standard library, if you are doing anything non-trivial I'd second that opinion. > I found some code samples that show how to use urllib.request, now I > am trying to understand why I should use urllib.request. Because as part of the standard library you can be sure it will be thee, whereas Requests is a third party module that needs to be downloaded/installed and therefore may not be present (or even allowed by the server admins) Or maybe because you found some old code written before Requests became popular and you need to integrate with it or reuse it. -- 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 arj.python at gmail.com Tue May 9 03:05:26 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Tue, 9 May 2017 11:05:26 +0400 Subject: [Tutor] urllib ... lost novice's question In-Reply-To: References: Message-ID: As a side note see a tutorial on urllib and requests and try them at the same time see for python 3.x; 3.4 or 3.6 also see the data type received by the different combinations, when you should use .read() etc also use utf-8 or unicode like .decode("utf8") Well play around fool mess with it, feel free as when you'll do serious stuffs you won't need to test to know what should be done or not, what breaks it or not. summary : learn it well from the begining Finding the right package. Hum either in your beginner learning path you learn popular third party modules or You find how the people round the net did what you are doing, see how they did it and what modules they used or google "module " or browse pypi or _long term_ never stop reading about python. so you'll constantly discover new things and reduce the probability of you not knowing how to do something. Hope it helps, Abdur-Rahmaan Janhangeer Vacoas, Mauritius https://abdurrahmaanjanhangeer.wordpress.com/ From mats at wichmann.us Mon May 8 21:01:31 2017 From: mats at wichmann.us (Mats Wichmann) Date: Mon, 08 May 2017 19:01:31 -0600 Subject: [Tutor] urllib ... lost novice's question In-Reply-To: References: Message-ID: <0DFA9D82-59D2-4C8B-9685-219021D681D8@wichmann.us> this is one of those things where if what you want is simple, they're all usable, and easy. if not, some are frankly horrid. requests is the current hot module. go ahead and try it. (urllib.request is not from requests, it's from urllib) On May 8, 2017 9:23:15 AM MDT, Rafael Knuth wrote: >Which package should I use to fetch and open an URL? >I am using Python 3.5 and there are presently 4 versions: > >urllib2 >urllib3 >urllib4 >urllib5 > >Common sense is telling me to use the latest version. >Not sure if my common sense is fooling me here though ;-) > >Then, there is another package, along with a dozen other >urllib-related packages (such as aiourllib). I thought this one is >doing what I need: > >urllib.request > >The latter I found on http://docs.python-requests.org along with these >encouraging words: > >"Warning: Recreational use of the Python standard library for HTTP may >result in dangerous side-effects, including: security vulnerabilities, >verbose code, reinventing the wheel, constantly reading documentation, >depression, headaches, or even death." > >How do I know where to find the right package - on python.org or >elsewhere? >I found some code samples that show how to use urllib.request, now I >am trying to understand why I should use urllib.request. >Would it be also doable to do requests using urllib5 or any other >version? Like 2 or 3? Just trying to understand. > >I am lost here. Feeback appreciated. Thank you! > >BTW, here's some (working) exemplary code I have been using for >educational purposes: > >import urllib.request >from bs4 import BeautifulSoup > >theurl = "https://twitter.com/rafaelknuth" >thepage = urllib.request.urlopen(theurl) >soup = BeautifulSoup(thepage, "html.parser") > >print(soup.title.text) > >i = 1 >for tweets in soup.findAll("div",{"class":"content"}): > print(i) > print(tweets.find("p").text) > i = i + 1 > >I am assuming there are different solutions for fetching and open URLs? >Or is the above the only viable solution? >_______________________________________________ >Tutor maillist - Tutor at python.org >To unsubscribe or change subscription options: >https://mail.python.org/mailman/listinfo/tutor -- Sent from my Android device with K-9 Mail. Please excuse my brevity. From rafael.knuth at gmail.com Wed May 10 12:06:56 2017 From: rafael.knuth at gmail.com (Rafael Knuth) Date: Wed, 10 May 2017 18:06:56 +0200 Subject: [Tutor] urllib ... lost novice's question In-Reply-To: References: Message-ID: >> Then, there is another package, along with a dozen other >> urllib-related packages (such as aiourllib). > > Again, where are you finding these? They are not in > the standard library. Have you been installing other > packages that may have their own versions maybe? they are all available via PyCharm EDU From joe at tele-magic.co.uk Wed May 10 11:17:16 2017 From: joe at tele-magic.co.uk (CHERRY PHOUTHAVONG) Date: Wed, 10 May 2017 10:17:16 -0500 Subject: [Tutor] (no subject) Message-ID: <3wNN2D2BKDzFqnd@mail.python.org> http://asciigraphics.carltoncurryhouse.com/style.php Cherry Phouthavong From rikudou__sennin at live.com Tue May 9 19:25:55 2017 From: rikudou__sennin at live.com (adil gourinda) Date: Tue, 9 May 2017 23:25:55 +0000 Subject: [Tutor] Flowchart Message-ID: As you requested, I uploaded my flowcharts on a web storage link as: "https://app.box.com/s/camaahvmd1yroutfu9ff6xpte44oj1fn" thanks ? From alan.gauld at yahoo.co.uk Wed May 10 19:20:40 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 May 2017 00:20:40 +0100 Subject: [Tutor] urllib ... lost novice's question In-Reply-To: References: Message-ID: On 10/05/17 17:06, Rafael Knuth wrote: >>> Then, there is another package, along with a dozen other >>> urllib-related packages (such as aiourllib). >> >> Again, where are you finding these? They are not in >> the standard library. Have you been installing other >> packages that may have their own versions maybe? > > they are all available via PyCharm EDU It looks like PyCharm may be adding extra packages to the standard library. Thats OK, both ActiveState and Anaconda (and others) do the same, but it does mean you need to check on python.org to see what is and what isn't "approved". If it's not official content then you need to ask on a PyCharm forum about the preferred choices. The fact they are included suggests that somebody has tested them and found them useful in some way, but you would need to ask them why they chose those packages and when they would be more suitable than the standard versions. These bonus packages are often seen as a valuable extra, but they do carry a burden of responsibility for the user to identify which is best for them, and that's not always easy to assess, especially for a beginner. -- 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 Wed May 10 19:26:05 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 May 2017 00:26:05 +0100 Subject: [Tutor] Flowchart In-Reply-To: References: Message-ID: On 10/05/17 00:25, adil gourinda wrote: > As you requested, I uploaded my flowcharts on a web storage link as: Thanks, although it would have helped if you included your original message since I am the only person to see it! It never got sent to the rest of the list. > "https://app.box.com/s/camaahvmd1yroutfu9ff6xpte44oj1fn" They look very pretty and, so far as I can tell at a glance, the if and while loop ones are accurate. I'm not quite sure what the control one is trying to say. It mentions a for loop but you don't provide a pdf for that? And the block of code is of course only one subset of a nearly infinite number of possibilities. -- 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 steve at pearwood.info Wed May 10 19:34:52 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 11 May 2017 09:34:52 +1000 Subject: [Tutor] Flowchart In-Reply-To: References: Message-ID: <20170510233452.GJ22525@ando.pearwood.info> On Tue, May 09, 2017 at 11:25:55PM +0000, adil gourinda wrote: > As you requested, I uploaded my flowcharts on a web storage link as: > > "https://app.box.com/s/camaahvmd1yroutfu9ff6xpte44oj1fn" I didn't request anything. What is this? Do you have a question or are you just sharing? Adil, I'm sure that the context is really clear to you, but to everyone else, it is a mystery. This looks like an attempt to get people to click on a link that will install a virus on their computer. Say something that proves you're a person with a question about Python, and not a bot trying to spread malware. -- Steve From steve at pearwood.info Wed May 10 20:02:34 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Thu, 11 May 2017 10:02:34 +1000 Subject: [Tutor] (no subject) In-Reply-To: <3wNN2D2BKDzFqnd@mail.python.org> References: <3wNN2D2BKDzFqnd@mail.python.org> Message-ID: <20170511000234.GA23636@ando.pearwood.info> Looks like spam to me. A link to a mystery URL with no context, by somebody signing their email with a radically different name from the email address used, sending to ten different addresses, one of which is the sender. I'm not sure that we care about a URL for some curry house in Carlton, especially since its probably got malware on it. On Wed, May 10, 2017 at 10:17:16AM -0500, CHERRY PHOUTHAVONG wrote: > > http:// ...] .php (URL redacted). -- Steve From alan.gauld at yahoo.co.uk Wed May 10 20:09:18 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 May 2017 01:09:18 +0100 Subject: [Tutor] Flowchart In-Reply-To: <20170510233452.GJ22525@ando.pearwood.info> References: <20170510233452.GJ22525@ando.pearwood.info> Message-ID: On 11/05/17 00:34, Steven D'Aprano wrote: > I didn't request anything. What is this? Do you have a question or are > you just sharing? It was me did the requesting. Adil originally posted his pdf's as attachments so I rejected the post, requesting he use a link instead. Unfortunately he left out his original request to review the images off his re-post... -- 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 mats at wichmann.us Wed May 10 19:54:26 2017 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 10 May 2017 17:54:26 -0600 Subject: [Tutor] Flowchart In-Reply-To: References: Message-ID: On 05/10/2017 05:26 PM, Alan Gauld via Tutor wrote: > On 10/05/17 00:25, adil gourinda wrote: >> As you requested, I uploaded my flowcharts on a web storage link as: > > Thanks, although it would have helped if you included > your original message since I am the only person to > see it! It never got sent to the rest of the list. > >> "https://app.box.com/s/camaahvmd1yroutfu9ff6xpte44oj1fn" > > They look very pretty and, so far as I can tell at a > glance, the if and while loop ones are accurate. > > I'm not quite sure what the control one is trying to say. > It mentions a for loop but you don't provide a pdf for > that? And the block of code is of course only one subset > of a nearly infinite number of possibilities. and it turns out some people don't think flowcharts are really a useful model... of course, it's a world where there are a zillion different opinions on things. I had to hunt a bit to find this one I had read at some point in the past, take it for whatever it's worth: https://www.quora.com/Why-is-using-a-flowchart-bad-practice-in-programming From alan.gauld at yahoo.co.uk Wed May 10 20:18:35 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 May 2017 01:18:35 +0100 Subject: [Tutor] (no subject) In-Reply-To: <20170511000234.GA23636@ando.pearwood.info> References: <3wNN2D2BKDzFqnd@mail.python.org> <20170511000234.GA23636@ando.pearwood.info> Message-ID: On 11/05/17 01:02, Steven D'Aprano wrote: > Looks like spam to me. A link to a mystery URL with no context, by > somebody signing their email with a radically different name from the > email address used, And the address is not subscribed to the list so the message should have gone to moderation. I'm trying to find out what happened here... -- 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 leamhall at gmail.com Wed May 10 20:27:13 2017 From: leamhall at gmail.com (leam hall) Date: Wed, 10 May 2017 20:27:13 -0400 Subject: [Tutor] (no subject) In-Reply-To: References: <3wNN2D2BKDzFqnd@mail.python.org> <20170511000234.GA23636@ando.pearwood.info> Message-ID: Oh...Spam with Curry! On Wed, May 10, 2017 at 8:18 PM, Alan Gauld via Tutor wrote: > On 11/05/17 01:02, Steven D'Aprano wrote: >> Looks like spam to me. A link to a mystery URL with no context, by >> somebody signing their email with a radically different name from the >> email address used, > > And the address is not subscribed to the list so the message > should have gone to moderation. > > I'm trying to find out what happened here... > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Wed May 10 20:30:17 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 May 2017 01:30:17 +0100 Subject: [Tutor] Flowchart In-Reply-To: References: Message-ID: On 11/05/17 00:54, Mats Wichmann wrote: > and it turns out some people don't think flowcharts are really a useful > model... Yes, but as usual it depends. They are quite good for documenting human based processes, which is what they were originally designed for. They work very well for documenting the design of assembler code and even reverse engineering assembler code. They don't work so well for structured code. But many modern variations exist that overcome these issues. UML Activity charts are just jazzed up flow charts. SDL(the design language not the graphics library) is another jazzed up version (and now mostly incorporated into UML2) My own experience is that flowcharts are only good for assembler level code and business processes documentation. Activity charts are likewise best at higher level design like business processes. For software design message sequence charts combined with pseudo-code are hard to beat. However, the OP was using them to aid his own understanding and asking us if he got it right. (except he didn't actually post the question). So, if they work for him, then they are a good choice. > https://www.quora.com/Why-is-using-a-flowchart-bad-practice-in-programming In my experience Quora is good for getting opinions, not so great at getting objective facts! :-) -- 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 rskovron at gmail.com Wed May 10 21:50:43 2017 From: rskovron at gmail.com (Rafael Skovron) Date: Wed, 10 May 2017 18:50:43 -0700 Subject: [Tutor] Simple while loop question Message-ID: I dont understand why j can have any value other than zero in this: for i in range(1, 5): j = 0 while j < i: print(j, end = " ") j += 1 From rskovron at gmail.com Wed May 10 21:53:21 2017 From: rskovron at gmail.com (Rafael Skovron) Date: Wed, 10 May 2017 18:53:21 -0700 Subject: [Tutor] While Loop Question Message-ID: Sorry I left out the indents in my previous email. It seems like j is always reset to zero. Why does j vary? Are there two different instances of j going on? for i in range(1, 5): j=0 while j < i: print(j, end = " ") j += 1 From alan.gauld at yahoo.co.uk Thu May 11 04:22:09 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 May 2017 09:22:09 +0100 Subject: [Tutor] Simple while loop question In-Reply-To: References: Message-ID: On 11/05/17 02:50, Rafael Skovron wrote: > I dont understand why j can have any value other than zero in this: > > for i in range(1, 5): j = 0 while j < i: print(j, end = " ") j += 1 Because i can go as high as 4 (from range(1,5)->1,2,3,4). So lets consider that final case: i->4 j->0 j True print 0 j->1 jTrue print 1 j->2 jTrue j->3 print 3 j->4 j<4->False So you'd expect the output to go like this: i=1, j->0 i=2, j->0,1 i=3, j->0,1,2 i=4, j->0,1,2,3 And since you set the print end to be a space it will all run into one line: 0 0 1 0 1 2 0 1 2 3 Is that what you see? -- 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 steve at pearwood.info Thu May 11 10:15:15 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Fri, 12 May 2017 00:15:15 +1000 Subject: [Tutor] While Loop Question In-Reply-To: References: Message-ID: <20170511141514.GA24625@ando.pearwood.info> On Wed, May 10, 2017 at 06:53:21PM -0700, Rafael Skovron wrote: > Sorry I left out the indents in my previous email. It seems like j is > always reset to zero. Why does j vary? Because you run code that adds 1 to j. > Are there two different instances of j going on? No. Try to run the code in your head and see what happens. See below: > for i in range(1, 5): > j=0 > while j < i: > print(j, end = " ") > j += 1 We start by setting i = 1, then the body of the for-loop begins. That sets j = 0, and since 0 < 1, we enter the while-loop. Inside the while-loop, we print j (0), then add 1 to j which makes it 1. Then we return to the top of the while-loop. Since 1 is NOT less than 1, we exit the while-loop and return to the top of the for-loop. Now we set i = 2, and continue into the body of the for-loop. That sets j = 0 (again!) and since 0 < 2, we enter the while-loop. Inside the while-loop, we print j (0), then add 1 to j which makes it 1. Then we return to the top of the while-loop. Since 1 < 2, we continue inside the body, print j (1), then add 1 to j which makes it 2. Since 2 is not LESS than 2, we exit the while-loop and return to the top of the for-loop. Now we set i = 3, and continue into the body of the for-loop. That sets j = 0 (again!) and since 0 < 3, we enter the while-loop. Inside the while-loop, we follow the same steps and print 0, then 1, then 2, then exit the while-loop and return to the top of the for-loop. Now we set i = 4, and again continue into the while-loop to print 0, 1, 2 and finally 3, then exit the while-loop and return to the top of the for-loop, which is now complete. -- Steve From arj.python at gmail.com Thu May 11 04:29:09 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Thu, 11 May 2017 12:29:09 +0400 Subject: [Tutor] Fwd: Re: While Loop Question In-Reply-To: References: Message-ID: ---------- Forwarded message ---------- From: "Abdur-Rahmaan Janhangeer" Date: 11 May 2017 12:26 pm Subject: Re: [Tutor] While Loop Question To: "Rafael Skovron" Cc: i modified your code to make it look like that : for i in range(1, 5): j=0 print("outer, i=",i) while j < i: print("inner, j=",j) j += 1 so it shows that each time the outer loop is reexecuted, i is set to zero. putting j at the begining completely, it is not reset to zero Abdur-Rahmaan Janhangeer, Mauritius https://abdurrahmaanjanhangeer.wordpress.com From arj.python at gmail.com Thu May 11 04:37:47 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Thu, 11 May 2017 12:37:47 +0400 Subject: [Tutor] Fwd: Re: While Loop Question In-Reply-To: References: Message-ID: sorry j is set to zero. grave typo Abdur-Rahmaan Janhangeer, Mauritius https://abdurrahmaanjanhangeer.wordpress.com On 11 May 2017 12:29 pm, "Abdur-Rahmaan Janhangeer" wrote: > > > ---------- Forwarded message ---------- > From: "Abdur-Rahmaan Janhangeer" > Date: 11 May 2017 12:26 pm > Subject: Re: [Tutor] While Loop Question > To: "Rafael Skovron" > Cc: > > i modified your code to make it look like that : > > for i in range(1, 5): > > j=0 > print("outer, i=",i) > while j < i: > print("inner, j=",j) > > j += 1 > > so it shows that each time the outer loop is reexecuted, i is set to zero. > > putting j at the begining completely, it is not reset to zero > > Abdur-Rahmaan Janhangeer, > Mauritius > https://abdurrahmaanjanhangeer.wordpress.com > > > > From george at fischhof.hu Thu May 11 04:21:43 2017 From: george at fischhof.hu (George Fischhof) Date: Thu, 11 May 2017 10:21:43 +0200 Subject: [Tutor] While Loop Question In-Reply-To: References: Message-ID: 2017-05-11 3:53 GMT+02:00 Rafael Skovron : > Sorry I left out the indents in my previous email. It seems like j is > always reset to zero. Why does j vary? > > Are there two different instances of j going on? > > > for i in range(1, 5): > > j=0 > > while j < i: > > print(j, end = " ") > > j += 1 > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > Hi, As there is a j = 0 statement in the for cycle, it will be set to 0 every time the while cycle finishes. (The while cycle will then increment j, then if j == i j will be set to 0 again). George From rskovron at gmail.com Thu May 11 15:55:52 2017 From: rskovron at gmail.com (Rafael Skovron) Date: Thu, 11 May 2017 19:55:52 +0000 Subject: [Tutor] Fwd: Re: While Loop Question In-Reply-To: References: Message-ID: Thank you all I finally understood that the while code traps j and runs independently of the for loop until while is false and a new i is picked. On Thu, May 11, 2017 at 10:19 AM Abdur-Rahmaan Janhangeer < arj.python at gmail.com> wrote: > sorry j is set to zero. grave typo > > Abdur-Rahmaan Janhangeer, > Mauritius > https://abdurrahmaanjanhangeer.wordpress.com > > On 11 May 2017 12:29 pm, "Abdur-Rahmaan Janhangeer" > wrote: > > > > > > > ---------- Forwarded message ---------- > > From: "Abdur-Rahmaan Janhangeer" > > Date: 11 May 2017 12:26 pm > > Subject: Re: [Tutor] While Loop Question > > To: "Rafael Skovron" > > Cc: > > > > i modified your code to make it look like that : > > > > for i in range(1, 5): > > > > j=0 > > print("outer, i=",i) > > while j < i: > > print("inner, j=",j) > > > > j += 1 > > > > so it shows that each time the outer loop is reexecuted, i is set to > zero. > > > > putting j at the begining completely, it is not reset to zero > > > > Abdur-Rahmaan Janhangeer, > > Mauritius > > https://abdurrahmaanjanhangeer.wordpress.com > > > > > > > > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From s.molnar at sbcglobal.net Thu May 11 14:26:39 2017 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Thu, 11 May 2017 14:26:39 -0400 Subject: [Tutor] python 3 np.savetxt(name_s, I_z) produces IndexError: tuple index out of range Message-ID: <5914ACDF.7060508@sbcglobal.net> 0 down vote favorite I am using Spyder3.1.4 with Python 3.6.0 | Anaconda 4.3.0 (64-bit) | and IPython 6.0.0. My script is rather long and calculates a series of molecular indices using the same formula and a series of different coefficients. The script uses the np.savetxt() quite a few times, but one use generates the title error. The offending line is np.savetxt(name_s,I_z) where I_z is a single number of type float64 with a size of 1. If I comment out the line containing the np.savetxt statement the script runs to completion and np.savetxt(name_s,I_oe) where I_oe is a float64 number except it has a size of (1,). Now, it would seems to me that the different size of the name in the np.savetxt statement causes the error, but how do I correct it? Google has not resulted in a solution. Here is an abbreviated copy of the script: #!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Sat Apr 8 15:17:07 2017 @author: comp Copyright (c) 2017 Stephen P. Molnar, Ph.D. """ import matplotlib.pyplot as plt import numpy as np start=1 finish=31 points=300 s = np.linspace(start, finish, points) np.savetxt('s',s) name = input("Enter Molecule ID: ") name_in = name+'.dat' dtype = [('NO', int), ('LB', 'S2'), ('ZA', float), ('FRAG', int), ('MASS', float), ('X', float), ('Y', float), ('Z', float)] data = np.genfromtxt(name_in, dtype=dtype, skip_header=3) N = data.shape[0] a = np.array([data['X'], data['Y'], data['Z']]) anrows, ancols = np.shape(a) a_new = a.reshape(anrows, 1, ancols) diff = a_new - a D = (diff ** 2).sum(2) D = np.sqrt(D) r = D def eq7(a, s, Z): """ Computes equation 7 in Molnar & King (2001) """ N = r.shape[0] I = np.zeros(s.shape) for i in range(1, N): for j in range(i): I += Z[i] * Z[j] * np.sin(s * r[i, j])/(s * r[i, j]) return I I = eq7(r, s, data['ZA']) name_s = name+'-Iz' np.savetxt(name_s,I) I_sq = [] I_sq = I**2 Area = [] name_s = name+'-Iz' Area = np.trapz(I_sq,x=None,dx=0.01,axis=-1) I_z = np.sqrt(Area) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(s.T, I) fig.gca().set_xlabel("Distance (?)") plt.ylabel('Atomic Number Transform (FT$_z$)') plt.show() print('FTz: ',I_z) name_s = name+'-FTz' print(name_s) np.savetxt(name_s,I_z) #-------------------------------------------------------------------------- # MASS Mollecular Transform FT_m I = eq7(r, s, data['MASS']) name_s = name+'-Im' np.savetxt(name_s,I) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(s.T, I) fig.gca().set_xlabel("Distance (?)") plt.ylabel('MASS Transform (FT$_m$)') plt.show() I_sq = I**2 Area = np.trapz(I_sq,x=None,dx=0.01,axis=-1) np.savetxt('I_m',I,delimiter=' ') I_m = np.sqrt([Area]) print('FTm: ',I_m) name_s = name+'-FTm' np.savetxt(name_s,I_m) """ #------------------------------------------------------------------------------ name_in = name+'.mpa.dat' dtype_1 = [('NO', int), ('ATOM', 'S2'), ('NA', float), ('ZA', float), ('QA', float), ('VA', float), ('BVA', float), ('FA', float)] data_1 = np.genfromtxt(name_in, dtype=dtype_1, skip_header=11) qa = [] qa = np.array(data_1['QA']) va = [] va = np.array([data_1['VA']]) bva =[] bva = np.array([data_1['BVA']]) fa = [] fa = np.array([data_1['FA']]) #------------------------------------------------------------------------------ #Charge Molecular Transform FT_c I = eq7(r, s, data_1['QA']) I_c = I name_s = name+'-Ic' np.savetxt(name_s,I) fig = plt.figure() ax = fig.add_subplot(111) ax.plot(s.T, I) fig.gca().set_xlabel("Distance (?)") plt.ylabel('Charge Transform (FT$_C$)') plt.show() I_sq = I_c**2 Area = np.trapz(I_sq,x=None,dx=0.01,axis=-1) np.savetxt(name_s,I,delimiter=' ') I_c = np.sqrt([Area]) print('FTc: ',I_c) name_s = name+'-FTc' np.savetxt(name_s,I_c) """ And here is the input file: CARTESIAN COORDINATES (A.U.) ---------------------------- NO LB ZA FRAG MASS X Y Z 0 C 6.0000 0 12.011 0.000000 0.000000 0.000000 1 H 1.0000 0 1.008 2.059801 0.000000 0.000000 2 Br 35.0000 0 79.900 -1.203126 3.402953 0.000000 3 Cl 17.0000 0 35.453 -1.108639 -1.567853 2.715601 4 F 9.0000 0 18.998 -0.938564 -1.327330 -2.299003 The last line in the script is causing the problem. A pointer towards the solution to the problem will be much apprecialted. Thanks in advance. -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 From s.molnar at sbcglobal.net Thu May 11 15:46:18 2017 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Thu, 11 May 2017 15:46:18 -0400 Subject: [Tutor] python 3 np.savetxt(name_s, I_z) produces IndexError: tuple index out of range In-Reply-To: <5914ACDF.7060508@sbcglobal.net> References: <5914ACDF.7060508@sbcglobal.net> Message-ID: <5914BF8A.6000109@sbcglobal.net> On 05/11/2017 02:26 PM, Stephen P. Molnar wrote: > 0 > down vote > favorite > > > I am using Spyder3.1.4 with Python 3.6.0 | Anaconda 4.3.0 (64-bit) | and > IPython 6.0.0. > > My script is rather long and calculates a series of molecular indices > using the same formula and a series of different coefficients. The > script uses the np.savetxt() quite a few times, but one use generates > the title error. The offending line is np.savetxt(name_s,I_z) where I_z > is a single number of type float64 with a size of 1. > > If I comment out the line containing the np.savetxt statement the script > runs to completion and np.savetxt(name_s,I_oe) where I_oe is a float64 > number except it has a size of (1,). > > Now, it would seems to me that the different size of the name in the > np.savetxt statement causes the error, but how do I correct it? Google > has not resulted in a solution. > > Here is an abbreviated copy of the script: > > #!/usr/bin/env python3 > # -*- coding: utf-8 -*- > """ > Created on Sat Apr 8 15:17:07 2017 > > @author: comp > > Copyright (c) 2017 Stephen P. Molnar, Ph.D. > > """ > import matplotlib.pyplot as plt > import numpy as np > > > start=1 > finish=31 > points=300 > > s = np.linspace(start, finish, points) > np.savetxt('s',s) > > name = input("Enter Molecule ID: ") > > name_in = name+'.dat' > > dtype = [('NO', int), ('LB', 'S2'), ('ZA', float), ('FRAG', int), > ('MASS', float), ('X', float), ('Y', float), ('Z', float)] > data = np.genfromtxt(name_in, dtype=dtype, skip_header=3) > > N = data.shape[0] > a = np.array([data['X'], data['Y'], data['Z']]) > anrows, ancols = np.shape(a) > a_new = a.reshape(anrows, 1, ancols) > > diff = a_new - a > > D = (diff ** 2).sum(2) > D = np.sqrt(D) > r = D > > def eq7(a, s, Z): > """ > Computes equation 7 in Molnar & King (2001) > """ > N = r.shape[0] > I = np.zeros(s.shape) > for i in range(1, N): > for j in range(i): > I += Z[i] * Z[j] * np.sin(s * r[i, j])/(s * r[i, j]) > > return I > > I = eq7(r, s, data['ZA']) > > name_s = name+'-Iz' > np.savetxt(name_s,I) > > I_sq = [] > I_sq = I**2 > Area = [] > > name_s = name+'-Iz' > > Area = np.trapz(I_sq,x=None,dx=0.01,axis=-1) > I_z = np.sqrt(Area) > > fig = plt.figure() > ax = fig.add_subplot(111) > ax.plot(s.T, I) > fig.gca().set_xlabel("Distance (?)") > plt.ylabel('Atomic Number Transform (FT$_z$)') > plt.show() > print('FTz: ',I_z) > name_s = name+'-FTz' > print(name_s) > np.savetxt(name_s,I_z) > > > #-------------------------------------------------------------------------- > # MASS Mollecular Transform FT_m > > > I = eq7(r, s, data['MASS']) > > name_s = name+'-Im' > > np.savetxt(name_s,I) > fig = plt.figure() > ax = fig.add_subplot(111) > ax.plot(s.T, I) > fig.gca().set_xlabel("Distance (?)") > plt.ylabel('MASS Transform (FT$_m$)') > plt.show() > > I_sq = I**2 > Area = np.trapz(I_sq,x=None,dx=0.01,axis=-1) > np.savetxt('I_m',I,delimiter=' ') > I_m = np.sqrt([Area]) > > print('FTm: ',I_m) > name_s = name+'-FTm' > np.savetxt(name_s,I_m) > > """ > #------------------------------------------------------------------------------ > > > name_in = name+'.mpa.dat' > > dtype_1 = [('NO', int), ('ATOM', 'S2'), ('NA', float), ('ZA', float), > ('QA', float), ('VA', float), ('BVA', float), ('FA', float)] > > data_1 = np.genfromtxt(name_in, dtype=dtype_1, skip_header=11) > > qa = [] > qa = np.array(data_1['QA']) > va = [] > va = np.array([data_1['VA']]) > bva =[] > bva = np.array([data_1['BVA']]) > fa = [] > fa = np.array([data_1['FA']]) > > #------------------------------------------------------------------------------ > > #Charge Molecular Transform FT_c > > I = eq7(r, s, data_1['QA']) > I_c = I > > name_s = name+'-Ic' > > np.savetxt(name_s,I) > fig = plt.figure() > ax = fig.add_subplot(111) > ax.plot(s.T, I) > fig.gca().set_xlabel("Distance (?)") > plt.ylabel('Charge Transform (FT$_C$)') > plt.show() > > I_sq = I_c**2 > Area = np.trapz(I_sq,x=None,dx=0.01,axis=-1) > np.savetxt(name_s,I,delimiter=' ') > I_c = np.sqrt([Area]) > > print('FTc: ',I_c) > name_s = name+'-FTc' > > np.savetxt(name_s,I_c) > > > """ > > And here is the input file: > > CARTESIAN COORDINATES (A.U.) > ---------------------------- > NO LB ZA FRAG MASS X Y Z > 0 C 6.0000 0 12.011 0.000000 0.000000 0.000000 > 1 H 1.0000 0 1.008 2.059801 0.000000 0.000000 > 2 Br 35.0000 0 79.900 -1.203126 3.402953 0.000000 > 3 Cl 17.0000 0 35.453 -1.108639 -1.567853 2.715601 > 4 F 9.0000 0 18.998 -0.938564 -1.327330 -2.299003 > > The last line in the script is causing the problem. > > A pointer towards the solution to the problem will be much apprecialted. > > Thanks in advance. > Problem solved: np.savetxt(name_s,I_z) should be np.savetxt(name_s,[I_z]) -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 From alan.gauld at yahoo.co.uk Thu May 11 16:59:30 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 11 May 2017 21:59:30 +0100 Subject: [Tutor] python 3 np.savetxt(name_s, I_z) produces IndexError: tuple index out of range In-Reply-To: <5914BF8A.6000109@sbcglobal.net> References: <5914ACDF.7060508@sbcglobal.net> <5914BF8A.6000109@sbcglobal.net> Message-ID: On 11/05/17 20:46, Stephen P. Molnar wrote: >> script uses the np.savetxt() quite a few times, but one use generates >> the title error. The offending line is np.savetxt(name_s,I_z) where I_z >> is a single number of type float64 with a size of 1. I'm glad you found the problem but please, when you post, do NOT summarize the error. Always, always, send the full error trace, there is a wealth of data in those messages and if we only get your best guess at what they mean we are deprived of essential clues in diagnosing problems. > Problem solved: np.savetxt(name_s,I_z) should be np.savetxt(name_s,[I_z]) -- 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 mrzenwiz at gmail.com Fri May 12 15:44:44 2017 From: mrzenwiz at gmail.com (MR ZenWiz) Date: Fri, 12 May 2017 12:44:44 -0700 Subject: [Tutor] cffi version mismatch stalling pip install pygit2 Message-ID: How do I fix this? We are using a newer version of libgit2 than the standard release (libgit2.0.25.0 instead of libgit2.0.24.0 - some hard dependency in our code). However, after I install libgit2 (25), I get this error chain: pip install pygit2 Collecting pygit2 Using cached pygit2-0.25.1.tar.gz Complete output from command python setup.py egg_info: /usr/lib/python2.7/site-packages/setuptools/version.py:1: UserWarning: Module cffi was already imported from /usr/lib64/python2.7/site-packages/cffi/__init__.pyc, but /tmp/easy_install-HY89WT/cffi-1.9.1 is being added to sys.path import pkg_resources Installed /tmp/pip-build-OX9KGK/pygit2/.eggs/cffi-1.9.1-py2.7-linux-x86_64.egg /usr/lib/python2.7/site-packages/setuptools/dist.py:378: UserWarning: Module cffi was already imported from /usr/lib64/python2.7/site-packages/cffi/__init__.pyc, but /tmp/pip-build-OX9KGK/pygit2/.eggs/cffi-1.9.1-py2.7-linux-x86_64.egg is being added to sys.path pkg_resources.working_set.add(dist, replace=True) Traceback (most recent call last): File "", line 1, in File "/tmp/pip-build-OX9KGK/pygit2/setup.py", line 210, in **extra_args) File "/usr/lib64/python2.7/distutils/core.py", line 112, in setup _setup_distribution = dist = klass(attrs) File "/usr/lib/python2.7/site-packages/setuptools/dist.py", line 321, in __init__ _Distribution.__init__(self, attrs) File "/usr/lib64/python2.7/distutils/dist.py", line 287, in __init__ self.finalize_options() File "/usr/lib/python2.7/site-packages/setuptools/dist.py", line 390, in finalize_options ep.load()(self, ep.name, value) File "/usr/lib64/python2.7/site-packages/cffi/setuptools_ext.py", line 188, in cffi_modules add_cffi_module(dist, cffi_module) File "/usr/lib64/python2.7/site-packages/cffi/setuptools_ext.py", line 49, in add_cffi_module execfile(build_file_name, mod_vars) File "/usr/lib64/python2.7/site-packages/cffi/setuptools_ext.py", line 25, in execfile exec(code, glob, glob) File "pygit2/_run.py", line 67, in ffi = FFI() File "/usr/lib64/python2.7/site-packages/cffi/api.py", line 54, in __init__ backend.__version__, backend.__file__)) Exception: Version mismatch: this is the 'cffi' package version 1.10.0, located in '/usr/lib64/python2.7/site-packages/cffi/api.pyc'. When we import the top-level '_cffi_backend' extension module, we get version 1.9.1, located in '/tmp/pip-build-OX9KGK/pygit2/.eggs/cffi-1.9.1-py2.7-linux-x86_64.egg/_cffi_backend.so'. The two versions should be equal; check your installation. ---------------------------------------- Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-OX9KGK/pygit2/ Thanks. MR (long time s/w professional but python newb) From s.molnar at sbcglobal.net Sat May 13 09:33:19 2017 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Sat, 13 May 2017 09:33:19 -0400 Subject: [Tutor] Subtract a 1D Array from a 2D Array Message-ID: <59170B1F.6030003@sbcglobal.net> I am using Python 3.6 in Anaconda3 Spyder IDE. I have two arrays: X Y Z a 0 0 0 2.059801 0 0 -1.203126 3.402953 0 -1.108639 -1.567853 2.715601 -0.938564 -1.32733 -2.299003 a_c 0.4283375 0.91755 0.208299 and want to subtract the value of the value of each column or the array a_c from the each value in the corresponding column of array c. Trying a_mm = a - a_c where a_mm is the new array results in the error message: ValueError: operands could not be broadcast together with shapes (3,5) (1,3) My search for a solution has not been successful. -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 From eryksun at gmail.com Sat May 13 11:16:06 2017 From: eryksun at gmail.com (eryk sun) Date: Sat, 13 May 2017 15:16:06 +0000 Subject: [Tutor] Subtract a 1D Array from a 2D Array In-Reply-To: <59170B1F.6030003@sbcglobal.net> References: <59170B1F.6030003@sbcglobal.net> Message-ID: On Sat, May 13, 2017 at 1:33 PM, Stephen P. Molnar wrote: > I am using Python 3.6 in Anaconda3 Spyder IDE. > > I have two arrays: > > X Y Z > a > 0 0 0 > 2.059801 0 0 > -1.203126 3.402953 0 > -1.108639 -1.567853 2.715601 > -0.938564 -1.32733 -2.299003 > a_c > 0.4283375 0.91755 0.208299 > > and want to subtract the value of the value of each column or the array a_c > from the each value in the corresponding column of array c. > > Trying a_mm = a - a_c where a_mm is the new array results in the error > message: > > ValueError: operands could not be broadcast together with shapes (3,5) (1,3) > > My search for a solution has not been successful. You've presented array `a` as if it's 5x3, but the error says it's 3x5. If it were actually 5x3, there would be no problem: import numpy as np a = np.array([[0, 0, 0], [2.059801, 0, 0], [-1.203126, 3.402953, 0], [-1.108639, -1.567853, 2.715601], [-0.938564, -1.32733, -2.299003]]) a_c = np.array([[0.4283375, 0.91755, 0.208299]]) >>> a.shape (5, 3) >>> a_c.shape (1, 3) >>> a - a_c array([[-0.4283375, -0.91755 , -0.208299 ], [ 1.6314635, -0.91755 , -0.208299 ], [-1.6314635, 2.485403 , -0.208299 ], [-1.5369765, -2.485403 , 2.507302 ], [-1.3669015, -2.24488 , -2.507302 ]]) You can use the transpose of either array. For example, use `a.T` to get the same number of columns, or use `a_c.T` to get the same number of rows. From steve at pearwood.info Sat May 13 11:33:36 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 14 May 2017 01:33:36 +1000 Subject: [Tutor] Subtract a 1D Array from a 2D Array In-Reply-To: <59170B1F.6030003@sbcglobal.net> References: <59170B1F.6030003@sbcglobal.net> Message-ID: <20170513153336.GE24625@ando.pearwood.info> On Sat, May 13, 2017 at 09:33:19AM -0400, Stephen P. Molnar wrote: > I have two arrays: > > X Y Z > a > 0 0 0 > 2.059801 0 0 > -1.203126 3.402953 0 > -1.108639 -1.567853 2.715601 > -0.938564 -1.32733 -2.299003 > a_c > 0.4283375 0.91755 0.208299 I'm having trouble interpreting what that means. What are X, Y, Z? Are they part of the array? Which array? Why is there a lone "a" in the middle of row 2, and a lone "a_c" in the middle of row 8? Can you give some Python code that creates the two arrays? And then show us what result you expect to get? Since the actual values in the array are not important, please use SIMPLE values. We shouldn't have to do detailed mental arithmetic with seven-significant figures to understand what you are trying to do. You should use nice simple single-digit (or at most two-digit) values for your examples. My *guess* is that you have an array called "a" with 3 columns and 5 rows, and a second array called "a_m" with 3 columns and 1 row, and you want to subtract a_m from each row of a. a = [[ 9 8 7 ] [ 5 5 5 ] [ 4 4 4 ] [ 3 3 3 ] [ 2 2 2 ]] a_m = [[ 1 2 3 ]] And you want the result of a - a_m to be: [[ 8 6 4 ] [ 4 3 2 ] [ 3 2 1 ] [ 2 1 0 ] [ 1 0 -1 ]] Am I close? If this is not what you want, please explain what you actually do want. -- Steve From s.molnar at sbcglobal.net Sat May 13 12:59:19 2017 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Sat, 13 May 2017 12:59:19 -0400 Subject: [Tutor] Subtract a 1D Array from a 2D Array In-Reply-To: References: <59170B1F.6030003@sbcglobal.net> Message-ID: <59173B67.6060001@sbcglobal.net> On 05/13/2017 11:16 AM, eryk sun wrote: > On Sat, May 13, 2017 at 1:33 PM, Stephen P. Molnar > wrote: >> I am using Python 3.6 in Anaconda3 Spyder IDE. >> >> I have two arrays: >> >> X Y Z >> a >> 0 0 0 >> 2.059801 0 0 >> -1.203126 3.402953 0 >> -1.108639 -1.567853 2.715601 >> -0.938564 -1.32733 -2.299003 >> a_c >> 0.4283375 0.91755 0.208299 >> >> and want to subtract the value of the value of each column or the array a_c >> from the each value in the corresponding column of array c. >> >> Trying a_mm = a - a_c where a_mm is the new array results in the error >> message: >> >> ValueError: operands could not be broadcast together with shapes (3,5) (1,3) >> >> My search for a solution has not been successful. > > You've presented array `a` as if it's 5x3, but the error says it's > 3x5. If it were actually 5x3, there would be no problem: > > import numpy as np > > a = np.array([[0, 0, 0], > [2.059801, 0, 0], > [-1.203126, 3.402953, 0], > [-1.108639, -1.567853, 2.715601], > [-0.938564, -1.32733, -2.299003]]) > > a_c = np.array([[0.4283375, 0.91755, 0.208299]]) > > >>> a.shape > (5, 3) > >>> a_c.shape > (1, 3) > >>> a - a_c > array([[-0.4283375, -0.91755 , -0.208299 ], > [ 1.6314635, -0.91755 , -0.208299 ], > [-1.6314635, 2.485403 , -0.208299 ], > [-1.5369765, -2.485403 , 2.507302 ], > [-1.3669015, -2.24488 , -2.507302 ]]) > > You can use the transpose of either array. For example, use `a.T` to > get the same number of columns, or use `a_c.T` to get the same number > of rows. > That solved the problem. Many thanks. -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 From mats at wichmann.us Sat May 13 13:38:53 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 13 May 2017 11:38:53 -0600 Subject: [Tutor] cffi version mismatch stalling pip install pygit2 In-Reply-To: References: Message-ID: <87f9afb4-bd21-b922-c7f6-3e8a3030053c@wichmann.us> On 05/12/2017 01:44 PM, MR ZenWiz wrote: > How do I fix this? > > We are using a newer version of libgit2 than the standard release > (libgit2.0.25.0 instead of libgit2.0.24.0 - some hard dependency in > our code). > > However, after I install libgit2 (25), I get this error chain: If you use a virtualenv (google for instructions if you're not familiar), you can have all the versions be what they need to be for this install, without impacting your system versions of things - that's about the only advice from here. Although, I tried to install pygit2 in a virtualenv and it failed, though a bit of a different error, so it may be the pygit2 project itself is having some issues getting their dependencies right. I got this: raise DistutilsError("Setup script exited with %s" % (v.args[0],)) distutils.errors.DistutilsError: Setup script exited with error: command 'gcc' failed with exit status 1 without any hint of *why* gcc failed, probably it was a missing C header file but haven't researched. > > pip install pygit2 > Collecting pygit2 > Using cached pygit2-0.25.1.tar.gz > Complete output from command python setup.py egg_info: > /usr/lib/python2.7/site-packages/setuptools/version.py:1: > UserWarning: Module cffi was already imported from > /usr/lib64/python2.7/site-packages/cffi/__init__.pyc, but > /tmp/easy_install-HY89WT/cffi-1.9.1 is being added to sys.path > import pkg_resources > > Installed /tmp/pip-build-OX9KGK/pygit2/.eggs/cffi-1.9.1-py2.7-linux-x86_64.egg > /usr/lib/python2.7/site-packages/setuptools/dist.py:378: > UserWarning: Module cffi was already imported from > /usr/lib64/python2.7/site-packages/cffi/__init__.pyc, but > /tmp/pip-build-OX9KGK/pygit2/.eggs/cffi-1.9.1-py2.7-linux-x86_64.egg > is being added to sys.path > pkg_resources.working_set.add(dist, replace=True) > Traceback (most recent call last): > File "", line 1, in > File "/tmp/pip-build-OX9KGK/pygit2/setup.py", line 210, in > **extra_args) > File "/usr/lib64/python2.7/distutils/core.py", line 112, in setup > _setup_distribution = dist = klass(attrs) > File "/usr/lib/python2.7/site-packages/setuptools/dist.py", line > 321, in __init__ > _Distribution.__init__(self, attrs) > File "/usr/lib64/python2.7/distutils/dist.py", line 287, in __init__ > self.finalize_options() > File "/usr/lib/python2.7/site-packages/setuptools/dist.py", line > 390, in finalize_options > ep.load()(self, ep.name, value) > File "/usr/lib64/python2.7/site-packages/cffi/setuptools_ext.py", > line 188, in cffi_modules > add_cffi_module(dist, cffi_module) > File "/usr/lib64/python2.7/site-packages/cffi/setuptools_ext.py", > line 49, in add_cffi_module > execfile(build_file_name, mod_vars) > File "/usr/lib64/python2.7/site-packages/cffi/setuptools_ext.py", > line 25, in execfile > exec(code, glob, glob) > File "pygit2/_run.py", line 67, in > ffi = FFI() > File "/usr/lib64/python2.7/site-packages/cffi/api.py", line 54, > in __init__ > backend.__version__, backend.__file__)) > Exception: Version mismatch: this is the 'cffi' package version > 1.10.0, located in '/usr/lib64/python2.7/site-packages/cffi/api.pyc'. > When we import the top-level '_cffi_backend' extension module, we get > version 1.9.1, located in > '/tmp/pip-build-OX9KGK/pygit2/.eggs/cffi-1.9.1-py2.7-linux-x86_64.egg/_cffi_backend.so'. > The two versions should be equal; check your installation. > > ---------------------------------------- > Command "python setup.py egg_info" failed with error code 1 in > /tmp/pip-build-OX9KGK/pygit2/ > > Thanks. > MR (long time s/w professional but python newb) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From s.shall at virginmedia.com Sun May 14 14:03:02 2017 From: s.shall at virginmedia.com (Sydney Shall) Date: Sun, 14 May 2017 19:03:02 +0100 Subject: [Tutor] How to write the __str__ function Message-ID: <295f494c-c6c3-632c-44ce-147c1e42acff@virginmedia.com> I need some advice that I have been embarrased to ask for, because I think that my error is so elementary. I have written, as advised by the tutors, a complex program in a topic that interests me. The program works well and the tests are OK. Now I want to add a __str__ function, which I thought would be straightforward. But I cannot get it right. The code that I have so far is as folows: def __str__(self): return("\n" " Output from __str__ of POCWP. " "\n" "\n After the first turnover, during the " "'Population Of Capitals Init' cycle," "\n the productivities were raised from 1.0 " "\n to a specific Unit Constant Capital (UCC) " "for each specific capital: " "\n The input value for the mean of UCC " "was %7.5f" % (self.ucc), "\n The fractional sigma (FractionalSTD)" " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) The error message is: TypeError: __str__ returned non-string (type tuple) When I omit either or both of the objects; self.ucc or self.fractsigma_ucc, the code works fine. So, I guess my code has an error. But I cannot detect the error. Guidance would be much appreciated. -- Sydney From martin at linux-ip.net Sun May 14 14:59:13 2017 From: martin at linux-ip.net (Martin A. Brown) Date: Sun, 14 May 2017 11:59:13 -0700 Subject: [Tutor] How to write the __str__ function In-Reply-To: <295f494c-c6c3-632c-44ce-147c1e42acff@virginmedia.com> References: <295f494c-c6c3-632c-44ce-147c1e42acff@virginmedia.com> Message-ID: Hello and greetings, > I need some advice that I have been embarrased to ask for, because > I think that my error is so elementary. Well, there are two benefits to trying to write down questions like this when you encounter them. 1) Rubber Duck debugging (if you have not heard of it); sometimes the act of describing the problem / question is enough for you to figure it out in the process 2) If you don't quite get there, then you have a description that somebody can easily review and respond to. > I have written, as advised by the tutors, a complex program in a > topic that interests me. The program works well and the tests are > OK. Right on! > Now I want to add a __str__ function, which I thought would be > straightforward. But I cannot get it right. > > The code that I have so far is as folows: > > def __str__(self): > return("\n" > " Output from __str__ of POCWP. " > "\n" > "\n After the first turnover, during the " > "'Population Of Capitals Init' cycle," > "\n the productivities were raised from 1.0 " > "\n to a specific Unit Constant Capital (UCC) " > "for each specific capital: " > "\n The input value for the mean of UCC " > "was %7.5f" % (self.ucc), > "\n The fractional sigma (FractionalSTD)" > " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) > > The error message is: > > TypeError: __str__ returned non-string (type tuple) > > When I omit either or both of the objects; self.ucc or > self.fractsigma_ucc, the code works fine. > > So, I guess my code has an error. But I cannot detect the error. It is not the omission of the objects, but rather the comma which is posing you this puzzle. > Guidance would be much appreciated. So, the error message tells you that you are creating a tuple, so let's look at why / how you are creating a tuple. I'm going to remove your variable names and use x and y. x, y = 5, 7 thing = ("var x=%s" % (x,), "var y=%s" % (y,)) # -- your code type(thing) # you will see: But, this is what you are doing when you remove one of the variables: thing = ("var x=unknown var y=%s" % (y,)) type(thing) # you will see: What's going on here? thing = ("no comma here") type(thing) # you will see: thing = ("no comma here",) type(thing) # you will see: So, when you are using parentheses, you can create a tuple, but you must include a comma, otherwise, you are not actually creating a tuple. Of course, you wish to create a string, not a tuple, so you want to bear in mind that you are creating a tuple when you include the comma in the line that ends with: (self.ucc), I have, therefore, a few small suggestions: 1. Put all of the variables replacements at the end. thing = ("var x=%s\nvar y=%s" % (x,y)) 2. When creating the replacements, also use tuples (see next point, too): "was %7.5f" % (self.ucc,) 3. Use separate statements for creating and returning the string. See below where I'm using triple-quoted strings for easier editing [0].) Good luck with Python, -Martin =================== def thing(): x, y = 3.141592653589793, 2.718281828459045 text = '''\n Output from __str__ of POCWP. After the first turnover, during the 'Population Of Capitals Init' cycle, the productivities were raised from 1.0 to a specific Unit Constant Capital (UCC) for each specific capital: The input value for the mean of UCC was %7.5f The fractional sigma (FractionalSTD) of UCC that was input was %7.5f''' return text % (x, y) =================== [0] https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str -- Martin A. Brown http://linux-ip.net/ From nulla.epistola at web.de Sun May 14 15:44:27 2017 From: nulla.epistola at web.de (Sibylle Koczian) Date: Sun, 14 May 2017 21:44:27 +0200 Subject: [Tutor] How to write the __str__ function In-Reply-To: References: <295f494c-c6c3-632c-44ce-147c1e42acff@virginmedia.com> Message-ID: Am 14.05.2017 um 20:59 schrieb Martin A. Brown: > > Hello and greetings, > >> I need some advice that I have been embarrased to ask for, because >> I think that my error is so elementary. > > Well, there are two benefits to trying to write down questions like > this when you encounter them. > > 1) Rubber Duck debugging (if you have not heard of it); sometimes > the act of describing the problem / question is enough for you > to figure it out in the process > > 2) If you don't quite get there, then you have a description that > somebody can easily review and respond to. > >> I have written, as advised by the tutors, a complex program in a >> topic that interests me. The program works well and the tests are >> OK. > > Right on! > >> Now I want to add a __str__ function, which I thought would be >> straightforward. But I cannot get it right. >> >> The code that I have so far is as folows: >> >> def __str__(self): >> return("\n" >> " Output from __str__ of POCWP. " >> "\n" >> "\n After the first turnover, during the " >> "'Population Of Capitals Init' cycle," >> "\n the productivities were raised from 1.0 " >> "\n to a specific Unit Constant Capital (UCC) " >> "for each specific capital: " >> "\n The input value for the mean of UCC " >> "was %7.5f" % (self.ucc), >> "\n The fractional sigma (FractionalSTD)" >> " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) >> >> The error message is: >> >> TypeError: __str__ returned non-string (type tuple) >> ... > I have, therefore, a few small suggestions: > > 1. Put all of the variables replacements at the end. > > thing = ("var x=%s\nvar y=%s" % (x,y)) > > 2. When creating the replacements, also use tuples (see next > point, too): > > "was %7.5f" % (self.ucc,) > If you put this into your original string, it won't work. Try this very simple example: st = "abc %d" % 7 "def" # <- SyntaxError The replacements definitely belong at the end, all together. But an additional question: do you really want to get this very long text every time you need an instance of your class as a string? Even if it's put into another sentence using string formatting? Like this for example (assuming your class is called MyClass): x = MyClass(some, args) s = """This is an instance of MyClass with the value %s. It was initialized with the values some = %s, args = %s.""" % (x, some, args) print(s) Greetings, Sibylle From alan.gauld at yahoo.co.uk Sun May 14 20:27:22 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 15 May 2017 01:27:22 +0100 Subject: [Tutor] How to write the __str__ function In-Reply-To: <295f494c-c6c3-632c-44ce-147c1e42acff@virginmedia.com> References: <295f494c-c6c3-632c-44ce-147c1e42acff@virginmedia.com> Message-ID: On 14/05/17 19:03, Sydney Shall wrote: > The code that I have so far is as folows: > > def __str__(self): > return("\n" > " Output from __str__ of POCWP. " > "\n" > "\n After the first turnover, during the " > "'Population Of Capitals Init' cycle," > "\n the productivities were raised from 1.0 " > "\n to a specific Unit Constant Capital (UCC) " > "for each specific capital: " > "\n The input value for the mean of UCC " > "was %7.5f" % (self.ucc), Here endeth the first string > "\n The fractional sigma (FractionalSTD)" > " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) And here the second. Returning two strings separated by a comma makes it a tuple. Instead put the two values in a tuple at the end of a single concatenated string. And while at it make life easier for your self and use triple quotes: def __str__(self): return """ Output from __str__ of POCWP. After the first turnover, during the 'Population Of Capitals Init' cycle, the productivities were raised from 1.0 to a specific Unit Constant Capital (UCC) for each specific capital: The input value for the mean of UCC was %7.5f The fractional sigma (FractionalSTD) of UCC that was input was %7.5f """ % ( self.ucc, self.fractsigma_ucc) Tweak the formatting to suit. However, I'm not sure thats really a good use of __str__, I might be tempted to make that an explicit method that's called pprint() - for pretty-print - or somesuch. __str__() methods are usually a fairly cocise depiction of the objects state that you can embed in a bigger string. Maybe pprint() would look like def pprint(self): return """ Output from __str__ of POCWP. After the first turnover, during the 'Population Of Capitals Init' cycle, the productivities were raised from 1.0 to a specific Unit Constant Capital (UCC) for each specific capital: %s""" % self And __str__() def __str__(self): return """ The input value for the mean of UCC was %7.5f The fractional sigma (FractionalSTD) of UCC that was input was %7.5f """ % (self.ucc, self.fractsigma_ucc) Thus pprint() uses str() to create the long version while str() just gives the bare bones result. Just a thought. -- 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 shulatif at gmail.com Sun May 14 14:51:46 2017 From: shulatif at gmail.com (shu latif) Date: Sun, 14 May 2017 11:51:46 -0700 Subject: [Tutor] How to write the __str__ function In-Reply-To: <295f494c-c6c3-632c-44ce-147c1e42acff@virginmedia.com> References: <295f494c-c6c3-632c-44ce-147c1e42acff@virginmedia.com> Message-ID: <5D6445EE-D836-4119-B9AF-7128A7E0CBBF@gmail.com> Not exactly sure what you're trying to do here but maybe you want to join those two things instead? Because of course it's a tuple the way you have it now. Shu Sent from my iTypo (with enhanced embarrassing auto-correcting) > On May 14, 2017, at 11:03 AM, Sydney Shall wrote: > > I need some advice that I have been embarrased to ask for, because I think that my error is so elementary. > > I have written, as advised by the tutors, a complex program in a topic that interests me. The program works well and the tests are OK. > > Now I want to add a __str__ function, which I thought would be straightforward. But I cannot get it right. > > The code that I have so far is as folows: > > def __str__(self): > return("\n" > " Output from __str__ of POCWP. " > "\n" > "\n After the first turnover, during the " > "'Population Of Capitals Init' cycle," > "\n the productivities were raised from 1.0 " > "\n to a specific Unit Constant Capital (UCC) " > "for each specific capital: " > "\n The input value for the mean of UCC " > "was %7.5f" % (self.ucc), > "\n The fractional sigma (FractionalSTD)" > " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) > > The error message is: > > TypeError: __str__ returned non-string (type tuple) > > When I omit either or both of the objects; self.ucc or self.fractsigma_ucc, the code works fine. > > So, I guess my code has an error. But I cannot detect the error. > > Guidance would be much appreciated. > > -- > Sydney > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From jf_byrnes at comcast.net Sun May 14 23:57:57 2017 From: jf_byrnes at comcast.net (Jim) Date: Sun, 14 May 2017 22:57:57 -0500 Subject: [Tutor] No file or directory error using subprocess and Popen Message-ID: I am running this on Mint 18. This is the third script I have written to open and position windows in workspaces. The first two work, but trying to open ebook-viewe r (calibre) with a specific book produces the following error. If I run the same command in the terminal it works without an error. Exception in thread Thread-4: Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(*self._args, **self._kwargs) File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 24, in open_it subprocess.call([self.program]) File "/usr/lib/python3.5/subprocess.py", line 557, in call with Popen(*popenargs, **kwargs) as p: File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ restore_signals, start_new_session) File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child raise child_exception_type(errno_num, err_msg) FileNotFoundError: [Errno 2] No such file or directory: 'ebook-viewer /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub' Code: # place_windows_OO_WS3.py import subprocess from subprocess import Popen,PIPE import threading import time class Place(): def __init__(self): self.programs = ['jedit', 'google-chrome', 'doublecmd', 'ebook-viewer /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub'] self.classname = {'jedit' : 'sun-awt-X11-XFramePeer', 'google-chrome':'google-chrome', 'doublecmd':'doublecmd', 'calibre-ebook-viewer': 'libprs500'} self.open_and_move() def open_it(self): subprocess.call([self.program]) def open_and_move(self): for self.program in self.programs: opener = threading.Thread(target=self.open_it) opener.start() time.sleep(2) p = Popen(['xdotool', 'search', '--classname', self.classname[self.program]], stdout=subprocess.PIPE) if self.classname[self.program] == 'sun-awt-X11-XFramePeer': wid = str(p.stdout.read()) wid = wid[len(wid) - 11 : len(wid) - 3] x = '0' y = '0' print('***jedit***') elif self.classname[self.program] == 'google-chrome': wid = str(p.stdout.read()) wid = wid[len(wid) - 11 : len(wid) - 3] x = '1924' y = '0' print('***google***') elif self.classname[self.program] == 'doublecmd': wid = str(p.stdout.read()) wid = wid[len(wid) - 11 : len(wid) - 3] x = '1924' y = '537' print('***double***') else: wid = str(p.stdout.read()) wid = wid[len(wid) - 11 : len(wid) - 3] x = '2540' #'1924' y = '537' print('***calibre***') subprocess.call(['xdotool', 'windowmove', str(wid), x, y]) I did some googling and it seems that subprocess does not have a length limit in linux. Could someone tell me how to correct the above error. Thanks, Jim From robertvstepp at gmail.com Mon May 15 00:19:12 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sun, 14 May 2017 23:19:12 -0500 Subject: [Tutor] No file or directory error using subprocess and Popen In-Reply-To: References: Message-ID: On Sun, May 14, 2017 at 10:57 PM, Jim wrote: > I am running this on Mint 18. > This is the third script I have written to open and position windows in > workspaces. The first two work, but trying to open ebook-viewe r (calibre) > with a specific book produces the following error. > If I run the same command in the terminal it works without an error. > > > Exception in thread Thread-4: > Traceback (most recent call last): > File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner > self.run() > File "/usr/lib/python3.5/threading.py", line 862, in run > self._target(*self._args, **self._kwargs) > File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 24, in > open_it > subprocess.call([self.program]) > File "/usr/lib/python3.5/subprocess.py", line 557, in call > with Popen(*popenargs, **kwargs) as p: > File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ > restore_signals, start_new_session) > File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child > raise child_exception_type(errno_num, err_msg) > FileNotFoundError: [Errno 2] No such file or directory: 'ebook-viewer > /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub' > > Code: > > # place_windows_OO_WS3.py > > import subprocess > from subprocess import Popen,PIPE > import threading > import time > > class Place(): > > def __init__(self): > self.programs = ['jedit', 'google-chrome', 'doublecmd', > 'ebook-viewer > /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub'] > self.classname = {'jedit' : 'sun-awt-X11-XFramePeer', > 'google-chrome':'google-chrome', > 'doublecmd':'doublecmd', > 'calibre-ebook-viewer': 'libprs500'} > self.open_and_move() > > def open_it(self): > subprocess.call([self.program]) I'm not very familiar with using the subprocess module yet, but when the above call to "subprocess.call([self.program])" occurs, isn't subprocess.call() expecting a list like ['ebook-viewer', '/home/jfb ...'] ? Hope I am not off-track here. boB From steve at pearwood.info Mon May 15 03:48:50 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 15 May 2017 17:48:50 +1000 Subject: [Tutor] No file or directory error using subprocess and Popen In-Reply-To: References: Message-ID: <20170515074850.GB30777@ando.pearwood.info> On Sun, May 14, 2017 at 10:57:57PM -0500, Jim wrote: > I am running this on Mint 18. > This is the third script I have written to open and position windows in > workspaces. The first two work, but trying to open ebook-viewe r > (calibre) with a specific book produces the following error. > If I run the same command in the terminal it works without an error. I think your problem is that you're telling subprocess to run a command called: ebook-viewer /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub with no arguments. What you want is a command called: ebook-viewer and a single argument: /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub I think (but haven't tried it) that the simplest way to fix that is to change the entry in self.programs from: > self.programs = ['jedit', 'google-chrome', 'doublecmd', > 'ebook-viewer > /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub'] to: path_to_file = '/home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub' self.programs = ['jedit', 'google-chrome', 'doublecmd', ['ebook-viewer', path_to_file], ] and see if that fixes it. (It may not be enough, or the right approach, but at least you'll get a different error if it is wrong :-) The difference is that the shell automatically splits things on spaces, so it sees the space between ebook-viewer and the long path, and treats the first word as the executable and the second as an argument. But Python treats the whole string, spaces and quotes included, as the executable. -- Steve From __peter__ at web.de Mon May 15 08:03:02 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 15 May 2017 14:03:02 +0200 Subject: [Tutor] No file or directory error using subprocess and Popen References: Message-ID: Jim wrote: > I am running this on Mint 18. > This is the third script I have written to open and position windows in > workspaces. The first two work, but trying to open ebook-viewe r > (calibre) with a specific book produces the following error. > If I run the same command in the terminal it works without an error. > > > Exception in thread Thread-4: > Traceback (most recent call last): > File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner > self.run() > File "/usr/lib/python3.5/threading.py", line 862, in run > self._target(*self._args, **self._kwargs) > File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 24, in > open_it > subprocess.call([self.program]) > File "/usr/lib/python3.5/subprocess.py", line 557, in call > with Popen(*popenargs, **kwargs) as p: > File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ > restore_signals, start_new_session) > File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child > raise child_exception_type(errno_num, err_msg) > FileNotFoundError: [Errno 2] No such file or directory: 'ebook-viewer > /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub' > > Code: > > # place_windows_OO_WS3.py > > import subprocess > from subprocess import Popen,PIPE > import threading > import time > > class Place(): > > def __init__(self): > self.programs = ['jedit', 'google-chrome', 'doublecmd', > 'ebook-viewer > /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub'] > self.classname = {'jedit' : 'sun-awt-X11-XFramePeer', > 'google-chrome':'google-chrome', > 'doublecmd':'doublecmd', > 'calibre-ebook-viewer': 'libprs500'} > self.open_and_move() > > def open_it(self): > subprocess.call([self.program]) > > def open_and_move(self): > for self.program in self.programs: > opener = threading.Thread(target=self.open_it) > opener.start() > time.sleep(2) > p = Popen(['xdotool', 'search', '--classname', > self.classname[self.program]], stdout=subprocess.PIPE) > > if self.classname[self.program] == 'sun-awt-X11-XFramePeer': > wid = str(p.stdout.read()) > wid = wid[len(wid) - 11 : len(wid) - 3] > x = '0' > y = '0' > print('***jedit***') > elif self.classname[self.program] == 'google-chrome': > wid = str(p.stdout.read()) > wid = wid[len(wid) - 11 : len(wid) - 3] > x = '1924' > y = '0' > print('***google***') > elif self.classname[self.program] == 'doublecmd': > wid = str(p.stdout.read()) > wid = wid[len(wid) - 11 : len(wid) - 3] > x = '1924' > y = '537' > print('***double***') > else: > wid = str(p.stdout.read()) > wid = wid[len(wid) - 11 : len(wid) - 3] > x = '2540' #'1924' > y = '537' > print('***calibre***') > subprocess.call(['xdotool', 'windowmove', str(wid), x, y]) > > I did some googling and it seems that subprocess does not have a length > limit in linux. Could someone tell me how to correct the above error. Not directly related to your question, but I think you should have one instance of your Place class per program. That way you keep related information together and avoid the need to look it up in a dict or find it with a long chain of if ... elif ... tests. For example: # untested import subprocess import threading import time DELAY = 3.0 # seconds class Place: def __init__(self, cmd, classname=None, name=None, x=0, y=0): if classname is None: classname = cmd[0] if name is None: name = cmd[0] self.cmd = cmd self.classname = classname self.name = name self.x = x self.y = y def open_it(self): subprocess.call(self.cmd) def open_and_move(self): opener = threading.Thread(target=self.open_it) opener.start() time.sleep(DELAY) output = subprocess.Popen( ['xdotool', 'search', '--classname', self.classname], stdout=subprocess.PIPE ).communicate()[0] wid = output.split()[-1].decode() subprocess.call( [ 'xdotool', 'windowmove', wid, str(self.x), str(self.y) ] ) print("***{}***".format(self.name)) EBOOK = ( "/home/jfb/Documents/eBooks/Javascript/" "GOOGLE_SHEETS/googlespreadsheetprogramming.epub" ) programs = [ Place(["jedit"], classname="sun-awt-X11-XFramePeer"), Place(["google-chrome"], x=1924), Place(["doublecmd"], x=1924, y=537), Place( ["ebook-viewer", EBOOK], classname="libprs500", name="calibre", x=2540, y=537 ) ] for program in programs: program.open_and_move() Now when you add a new program you just append a new `Place` instance to the `programs` list. From phil_lor at bigpond.com Mon May 15 02:11:03 2017 From: phil_lor at bigpond.com (Phil) Date: Mon, 15 May 2017 16:11:03 +1000 Subject: [Tutor] Tkinter + frame + grid question Message-ID: <20170515161103.14dbf4f4@raspberrypi> Thank you for reading this. I'd like to place a red frame in the centre of the main window. I've discovered two ways to achieve this using place() and pack(padx, pady). Grid() does not give the desired result. Next I'd like to place some widgets on the frame. Again place() does what I want and pack() almost does except the red frame is the size of the padding. It now seems to me that this is the way it's supposed to work. I'd like to use grid(), but so far, all that I can achieve is to place widgets on the main window and not the frame. The frame does not display if I use grid(). I've spent hours on this and an Internet search has not helped at all. However, I did learn that grid() and pack() are not compatible, could this be the problem? Perhaps I have to let go of the familiar grid() method and use pack() instead? Place() does exactly what I want but it's a bit cumbersome to use. The more I think about it, the more I think pack() is the go. Still, I'd like to know, what's the best method? The following is a summary of what I've tried: from tkinter import * class App: def __init__(self, master): master.geometry('400x400+50+50') frame = Frame(master, width = 300, height = 300, bg = 'red') frame.pack(pady = 50) #frame.place(x = 50, y = 50) Entry(frame).place(x = 100, y = 100, anchor = CENTER) #Entry(frame).pack(padx = 20, pady = 20) #Entry(frame).grid(row = 10, column = 1) #Entry2 = Entry #Entry2(frame).grid(row = 20, column = 1) root = Tk() app = App(root) root.mainloop() -- Regards, Phil From __peter__ at web.de Mon May 15 10:14:48 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 15 May 2017 16:14:48 +0200 Subject: [Tutor] Tkinter + frame + grid question References: <20170515161103.14dbf4f4@raspberrypi> Message-ID: Phil wrote: > Thank you for reading this. > > I'd like to place a red frame in the centre of the main window. > > I've discovered two ways to achieve this using place() and pack(padx, > pady). Grid() does not give the desired result. > > Next I'd like to place some widgets on the frame. Again place() does what > I want and pack() almost does except the red frame is the size of the > padding. It now seems to me that this is the way it's supposed to work. > I'd like to use grid(), but so far, all that I can achieve is to place > widgets on the main window and not the frame. The frame does not display > if I use grid(). > > I've spent hours on this and an Internet search has not helped at all. > However, I did learn that grid() and pack() are not compatible, could this > be the problem? Perhaps I have to let go of the familiar grid() method and > use pack() instead? Place() does exactly what I want but it's a bit > cumbersome to use. The more I think about it, the more I think pack() is > the go. > > Still, I'd like to know, what's the best method? place() is a pain if the screen or font size differs from what you expect. I use grid() almost exclusively, but I have to admit that I always have to play around a bit until I get something close to what I want. > > The following is a summary of what I've tried: > > from tkinter import * > > class App: > > def __init__(self, master): > master.geometry('400x400+50+50') > frame = Frame(master, width = 300, height = 300, bg = 'red') > > frame.pack(pady = 50) > #frame.place(x = 50, y = 50) > > > Entry(frame).place(x = 100, y = 100, anchor = CENTER) > #Entry(frame).pack(padx = 20, pady = 20) > #Entry(frame).grid(row = 10, column = 1) > > #Entry2 = Entry > #Entry2(frame).grid(row = 20, column = 1) Columns and rows are logical columns; typically you will use 0 and 1 rather than 10 and 20. > > > root = Tk() > app = App(root) > root.mainloop() > Here's a grid()-based contender: class App: def __init__(self, master): master.columnconfigure(0, weight=1) master.rowconfigure(0, weight=1) frame = Frame(master, bg="red") frame.grid(row=0, column=0, padx=30, pady=30, sticky="nsew") frame.rowconfigure(0, weight=1) entry = Entry(frame) entry.grid(row=0, column=0, padx=30, pady=30) From alan.gauld at yahoo.co.uk Mon May 15 11:08:07 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 15 May 2017 16:08:07 +0100 Subject: [Tutor] Tkinter + frame + grid question In-Reply-To: <20170515161103.14dbf4f4@raspberrypi> References: <20170515161103.14dbf4f4@raspberrypi> Message-ID: On 15/05/17 07:11, Phil wrote: > I'd like to place a red frame in the centre of the main window. What do you want surrounding it? You are going to have to use whichever layout manager fits the scenario. Remember you can use different layout managers in each frame in your app - and you will usually have several frames. Lets assume you fill your main window with a single frame(via pack) then use a 3x3 grid inside that frame and put your red frame in the centre cell of your grid. That will give you the centre red frame plus space for 8 surrounding components or frames > ...I did learn that grid() and pack() are not compatible I don't know what you mean by that. You can freely mix frames with grid/pack/place layout managers but only one manager can be used at a time per frame. So if you adopted the scheme above the top level window uses pack() the enclosed frame uses grid. The red frame can use place, or pack or grid (or any of the other layout managers available, for example, Tix provides a Form manager) Sketching out how your frames and components will be laid out on paper is often a good idea. That will help you decide how many frames you need and which layout manager to use per frame. I rend to use a standard layout of MainWindow Menubar(not allocated by layout manager0 Frame single row grid for toolbar buttons Frame main grid of controls/subframes (usually there will be 3 or 4 subframes holding the actual components, these subframes will usually use pack()) Frame status bar labels The three top level frames are packed in the mainwindow The top frame uses pack for the buttons(but could use a 1 row grid instead) The centre frame tends to use Grid(but might use Form if I'm using tix) The bottom frame will use pack (but again, could be a single row grid) HTH -- 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 s.shall at virginmedia.com Mon May 15 11:43:33 2017 From: s.shall at virginmedia.com (Sydney Shall) Date: Mon, 15 May 2017 16:43:33 +0100 Subject: [Tutor] How to write the __str__ function In-Reply-To: References: <295f494c-c6c3-632c-44ce-147c1e42acff@virginmedia.com> Message-ID: <4e29f64a-f2f3-00f3-59ff-fd752a31daea@virginmedia.com> On 15/05/2017 01:27, Alan Gauld via Tutor wrote: > On 14/05/17 19:03, Sydney Shall wrote: > >> The code that I have so far is as folows: >> >> def __str__(self): >> return("\n" >> " Output from __str__ of POCWP. " >> "\n" >> "\n After the first turnover, during the " >> "'Population Of Capitals Init' cycle," >> "\n the productivities were raised from 1.0 " >> "\n to a specific Unit Constant Capital (UCC) " >> "for each specific capital: " >> "\n The input value for the mean of UCC " >> "was %7.5f" % (self.ucc), > > Here endeth the first string > >> "\n The fractional sigma (FractionalSTD)" >> " of UCC that was input was %7.5f " % (self.fractsigma_ucc)) > > And here the second. Returning two strings separated > by a comma makes it a tuple. Instead put the two values in a tuple at > the end of a single concatenated string. > > And while at it make life easier for your self and use triple quotes: > > def __str__(self): > return """ > Output from __str__ of POCWP. > > After the first turnover, during the > 'Population Of Capitals Init' cycle, > the productivities were raised from 1.0 > to a specific Unit Constant Capital (UCC) > for each specific capital: > The input value for the mean of UCC was %7.5f > The fractional sigma (FractionalSTD) of UCC that was input was %7.5f > """ % ( self.ucc, self.fractsigma_ucc) > > Tweak the formatting to suit. > > However, I'm not sure thats really a good use of __str__, > I might be tempted to make that an explicit method that's > called pprint() - for pretty-print - or somesuch. > __str__() methods are usually a fairly cocise depiction > of the objects state that you can embed in a bigger string. > > Maybe pprint() would look like > > def pprint(self): > return """ > Output from __str__ of POCWP. > > After the first turnover, during the > 'Population Of Capitals Init' cycle, > the productivities were raised from 1.0 > to a specific Unit Constant Capital (UCC) > for each specific capital: %s""" % self > > And __str__() > > def __str__(self): > return """ > The input value for the mean of UCC was %7.5f > The fractional sigma (FractionalSTD) of UCC that was input was %7.5f """ > % (self.ucc, self.fractsigma_ucc) > > Thus pprint() uses str() to create the long version while str() > just gives the bare bones result. > > Just a thought. > Here endeth the second (! -presumptious?) string. """I would like to thank you all for the five lessons. The advice of course gave me several useful ways of outputting data. I believe I now have a better understanding of output.""" Many thanks. -- Sydney From arj.python at gmail.com Mon May 15 15:06:04 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Mon, 15 May 2017 23:06:04 +0400 Subject: [Tutor] Tkinter + frame + grid question In-Reply-To: <20170515161103.14dbf4f4@raspberrypi> References: <20170515161103.14dbf4f4@raspberrypi> Message-ID: Hum i understand your frustration. however, whatever you use it is ok, just don't mix them at the same time. either use grid or use pack or use place grid is best for me else try some tricks here and there like try adding transparent images in the grid manager use PIL module by specifying the image sizes you place the frame where you want a tip: UI design is always a bit frustrating, even in Android coding, but some tricks help like the image trick i learnt there . . . kind of hack or work around. Coding is not straight forward but authors of languages try as much as possible to cover the needs of users. we just have to extend sometimes somewhere Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com From phil_lor at bigpond.com Mon May 15 19:52:02 2017 From: phil_lor at bigpond.com (Phil) Date: Tue, 16 May 2017 09:52:02 +1000 Subject: [Tutor] Tkinter + frame + grid question In-Reply-To: References: <20170515161103.14dbf4f4@raspberrypi> Message-ID: <20170516095202.720ecd09@raspberrypi> On Mon, 15 May 2017 16:14:48 +0200 Peter Otten <__peter__ at web.de> wrote: Thank you Peter, Alan and Abdur-Rahmaan for your replies. After a night's sleep I'm now well on the way to beautifying a Lotto checking program that I wrote a couple of years ago. The need for and the use of frames is now clearer to me now, plus I now see why I should include the use of columnconfigure and sticky. I knew about tix but hadn't investigated it until now. I'll have to play with it. -- Regards, Phil From jf_byrnes at comcast.net Mon May 15 21:33:51 2017 From: jf_byrnes at comcast.net (Jim) Date: Mon, 15 May 2017 20:33:51 -0500 Subject: [Tutor] No file or directory error using subprocess and Popen In-Reply-To: <20170515074850.GB30777@ando.pearwood.info> References: <20170515074850.GB30777@ando.pearwood.info> Message-ID: On 05/15/2017 02:48 AM, Steven D'Aprano wrote: > On Sun, May 14, 2017 at 10:57:57PM -0500, Jim wrote: >> I am running this on Mint 18. This is the third script I have >> written to open and position windows in workspaces. The first two >> work, but trying to open ebook-viewe r (calibre) with a specific >> book produces the following error. If I run the same command in the >> terminal it works without an error. > > I think your problem is that you're telling subprocess to run a > command called: > > ebook-viewer > /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub > > with no arguments. What you want is a command called: > > ebook-viewer > > and a single argument: > > /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub > > I think (but haven't tried it) that the simplest way to fix that is > to change the entry in self.programs from: > >> self.programs = ['jedit', 'google-chrome', 'doublecmd', >> 'ebook-viewer >> /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub'] > >> > to: > > path_to_file = > '/home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub' > > self.programs = ['jedit', > 'google-chrome', 'doublecmd', ['ebook-viewer', path_to_file], ] I made the changes you suggested. def __init__(self): path_to_book = '/home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub' self.programs = ['jedit', 'google-chrome', 'doublecmd', ['ebook-viewer', path_to_book ]] self.classname = {'jedit' : 'sun-awt-X11-XFramePeer', 'google-chrome':'google-chrome', 'doublecmd':'doublecmd', 'calibre-ebook-viewer': 'libprs500'} self.open_and_move() I noticed you have a , between the last two ]],s. I don't think you meant that but I tried it both ways just incase. > > and see if that fixes it. (It may not be enough, or the right > approach, but at least you'll get a different error if it is wrong > :-) Unfortunately you are correct, I did get a different error message. Exception in thread Thread-4: Traceback (most recent call last): File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner self.run() File "/usr/lib/python3.5/threading.py", line 862, in run self._target(*self._args, **self._kwargs) File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 24, in open_it subprocess.call([self.program]) File "/usr/lib/python3.5/subprocess.py", line 557, in call with Popen(*popenargs, **kwargs) as p: File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ restore_signals, start_new_session) File "/usr/lib/python3.5/subprocess.py", line 1474, in _execute_child executable = os.fsencode(executable) File "/usr/lib/python3.5/os.py", line 862, in fsencode raise TypeError("expect bytes or str, not %s" % type(filename).__name__) TypeError: expect bytes or str, not list Traceback (most recent call last): File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 78, in Place() File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 21, in __init__ self.open_and_move() File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 31, in open_and_move p = Popen(['xdotool', 'search', '--classname', self.classname[self.program]], stdout=subprocess.PIPE) TypeError: unhashable type: 'list' Regards, Jim > The difference is that the shell automatically splits things on > spaces, so it sees the space between ebook-viewer and the long path, > and treats the first word as the executable and the second as an > argument. But Python treats the whole string, spaces and quotes > included, as the executable. > > From jf_byrnes at comcast.net Mon May 15 21:39:12 2017 From: jf_byrnes at comcast.net (Jim) Date: Mon, 15 May 2017 20:39:12 -0500 Subject: [Tutor] No file or directory error using subprocess and Popen In-Reply-To: References: Message-ID: On 05/15/2017 07:03 AM, Peter Otten wrote: > Jim wrote: > >> I am running this on Mint 18. >> This is the third script I have written to open and position windows in >> workspaces. The first two work, but trying to open ebook-viewe r >> (calibre) with a specific book produces the following error. >> If I run the same command in the terminal it works without an error. >> >> >> Exception in thread Thread-4: >> Traceback (most recent call last): >> File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner >> self.run() >> File "/usr/lib/python3.5/threading.py", line 862, in run >> self._target(*self._args, **self._kwargs) >> File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 24, in >> open_it >> subprocess.call([self.program]) >> File "/usr/lib/python3.5/subprocess.py", line 557, in call >> with Popen(*popenargs, **kwargs) as p: >> File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ >> restore_signals, start_new_session) >> File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child >> raise child_exception_type(errno_num, err_msg) >> FileNotFoundError: [Errno 2] No such file or directory: 'ebook-viewer >> > /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub' >> >> Code: >> >> # place_windows_OO_WS3.py >> >> import subprocess >> from subprocess import Popen,PIPE >> import threading >> import time >> >> class Place(): >> >> def __init__(self): >> self.programs = ['jedit', 'google-chrome', 'doublecmd', >> 'ebook-viewer >> > /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub'] >> self.classname = {'jedit' : 'sun-awt-X11-XFramePeer', >> 'google-chrome':'google-chrome', >> 'doublecmd':'doublecmd', >> 'calibre-ebook-viewer': 'libprs500'} >> self.open_and_move() >> >> def open_it(self): >> subprocess.call([self.program]) >> >> def open_and_move(self): >> for self.program in self.programs: >> opener = threading.Thread(target=self.open_it) >> opener.start() >> time.sleep(2) >> p = Popen(['xdotool', 'search', '--classname', >> self.classname[self.program]], stdout=subprocess.PIPE) >> >> if self.classname[self.program] == 'sun-awt-X11-XFramePeer': >> wid = str(p.stdout.read()) >> wid = wid[len(wid) - 11 : len(wid) - 3] >> x = '0' >> y = '0' >> print('***jedit***') >> elif self.classname[self.program] == 'google-chrome': >> wid = str(p.stdout.read()) >> wid = wid[len(wid) - 11 : len(wid) - 3] >> x = '1924' >> y = '0' >> print('***google***') >> elif self.classname[self.program] == 'doublecmd': >> wid = str(p.stdout.read()) >> wid = wid[len(wid) - 11 : len(wid) - 3] >> x = '1924' >> y = '537' >> print('***double***') >> else: >> wid = str(p.stdout.read()) >> wid = wid[len(wid) - 11 : len(wid) - 3] >> x = '2540' #'1924' >> y = '537' >> print('***calibre***') >> subprocess.call(['xdotool', 'windowmove', str(wid), x, y]) >> >> I did some googling and it seems that subprocess does not have a length >> limit in linux. Could someone tell me how to correct the above error. > > Not directly related to your question, but I think you should have one > instance of your Place class per program. That way you keep related > information together and avoid the need to look it up in a dict or find it > with a long chain of if ... elif ... tests. For example: > > # untested > import subprocess > import threading > import time > > DELAY = 3.0 # seconds > > > class Place: > def __init__(self, cmd, classname=None, name=None, x=0, y=0): > if classname is None: > classname = cmd[0] > if name is None: > name = cmd[0] > > self.cmd = cmd > self.classname = classname > self.name = name > self.x = x > self.y = y > > def open_it(self): > subprocess.call(self.cmd) > > def open_and_move(self): > opener = threading.Thread(target=self.open_it) > opener.start() > time.sleep(DELAY) > output = subprocess.Popen( > ['xdotool', 'search', '--classname', self.classname], > stdout=subprocess.PIPE > ).communicate()[0] > wid = output.split()[-1].decode() > subprocess.call( > [ > 'xdotool', 'windowmove', > wid, str(self.x), str(self.y) > ] > ) > print("***{}***".format(self.name)) > > EBOOK = ( > "/home/jfb/Documents/eBooks/Javascript/" > "GOOGLE_SHEETS/googlespreadsheetprogramming.epub" > ) > > programs = [ > Place(["jedit"], classname="sun-awt-X11-XFramePeer"), > Place(["google-chrome"], x=1924), > Place(["doublecmd"], x=1924, y=537), > Place( > ["ebook-viewer", EBOOK], > classname="libprs500", name="calibre", > x=2540, y=537 > ) > ] > > for program in programs: > program.open_and_move() > > Now when you add a new program you just append a new `Place` instance to the > `programs` list. > Peter, Thanks for the code. Once I get it working I will definitely look at changing my code to follow your guidelines. Thanks, Jim From jf_byrnes at comcast.net Mon May 15 21:37:10 2017 From: jf_byrnes at comcast.net (Jim) Date: Mon, 15 May 2017 20:37:10 -0500 Subject: [Tutor] No file or directory error using subprocess and Popen In-Reply-To: References: Message-ID: On 05/14/2017 11:19 PM, boB Stepp wrote: > On Sun, May 14, 2017 at 10:57 PM, Jim wrote: >> I am running this on Mint 18. >> This is the third script I have written to open and position windows in >> workspaces. The first two work, but trying to open ebook-viewe r (calibre) >> with a specific book produces the following error. >> If I run the same command in the terminal it works without an error. >> >> >> Exception in thread Thread-4: >> Traceback (most recent call last): >> File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner >> self.run() >> File "/usr/lib/python3.5/threading.py", line 862, in run >> self._target(*self._args, **self._kwargs) >> File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 24, in >> open_it >> subprocess.call([self.program]) >> File "/usr/lib/python3.5/subprocess.py", line 557, in call >> with Popen(*popenargs, **kwargs) as p: >> File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ >> restore_signals, start_new_session) >> File "/usr/lib/python3.5/subprocess.py", line 1551, in _execute_child >> raise child_exception_type(errno_num, err_msg) >> FileNotFoundError: [Errno 2] No such file or directory: 'ebook-viewer >> /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub' >> >> Code: >> >> # place_windows_OO_WS3.py >> >> import subprocess >> from subprocess import Popen,PIPE >> import threading >> import time >> >> class Place(): >> >> def __init__(self): >> self.programs = ['jedit', 'google-chrome', 'doublecmd', >> 'ebook-viewer >> /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub'] >> self.classname = {'jedit' : 'sun-awt-X11-XFramePeer', >> 'google-chrome':'google-chrome', >> 'doublecmd':'doublecmd', >> 'calibre-ebook-viewer': 'libprs500'} >> self.open_and_move() >> >> def open_it(self): >> subprocess.call([self.program]) > > I'm not very familiar with using the subprocess module yet, but when > the above call to "subprocess.call([self.program])" occurs, isn't > subprocess.call() expecting a list like > > ['ebook-viewer', '/home/jfb ...'] > > ? > > Hope I am not off-track here. > > boB Bob, I thought you were on to something, especially since Steven suggested about the same thing. See my reply to Steven. It seems to be looking for a str or byte not a list. Thanks, Jim From jf_byrnes at comcast.net Mon May 15 22:17:03 2017 From: jf_byrnes at comcast.net (Jim) Date: Mon, 15 May 2017 21:17:03 -0500 Subject: [Tutor] No file or directory error using subprocess and Popen In-Reply-To: References: <20170515074850.GB30777@ando.pearwood.info> Message-ID: On 05/15/2017 08:33 PM, Jim wrote: > On 05/15/2017 02:48 AM, Steven D'Aprano wrote: >> On Sun, May 14, 2017 at 10:57:57PM -0500, Jim wrote: >>> I am running this on Mint 18. This is the third script I have >>> written to open and position windows in workspaces. The first two >>> work, but trying to open ebook-viewe r (calibre) with a specific >>> book produces the following error. If I run the same command in the >>> terminal it works without an error. >> >> I think your problem is that you're telling subprocess to run a >> command called: >> >> ebook-viewer >> /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub >> >> >> with no arguments. What you want is a command called: >> >> ebook-viewer >> >> and a single argument: >> >> /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub >> >> >> I think (but haven't tried it) that the simplest way to fix that is >> to change the entry in self.programs from: >> >>> self.programs = ['jedit', 'google-chrome', 'doublecmd', >>> 'ebook-viewer >>> /home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub'] >>> >> >>> >> to: >> >> path_to_file = >> '/home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub' >> >> > >> > self.programs = ['jedit', >> 'google-chrome', 'doublecmd', ['ebook-viewer', path_to_file], ] > > I made the changes you suggested. > > def __init__(self): > path_to_book = > '/home/jfb/Documents/eBooks/Javascript/GOOGLE_SHEETS/googlespreadsheetprogramming.epub' > > self.programs = ['jedit', 'google-chrome', 'doublecmd', > ['ebook-viewer', path_to_book ]] > self.classname = {'jedit' : 'sun-awt-X11-XFramePeer', > 'google-chrome':'google-chrome', > 'doublecmd':'doublecmd', > 'calibre-ebook-viewer': 'libprs500'} > self.open_and_move() > > I noticed you have a , between the last two ]],s. I don't think you > meant that but I tried it both ways just incase. > >> >> and see if that fixes it. (It may not be enough, or the right >> approach, but at least you'll get a different error if it is wrong >> :-) > > Unfortunately you are correct, I did get a different error message. > > Exception in thread Thread-4: > Traceback (most recent call last): > File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner > self.run() > File "/usr/lib/python3.5/threading.py", line 862, in run > self._target(*self._args, **self._kwargs) > File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 24, in > open_it > subprocess.call([self.program]) > File "/usr/lib/python3.5/subprocess.py", line 557, in call > with Popen(*popenargs, **kwargs) as p: > File "/usr/lib/python3.5/subprocess.py", line 947, in __init__ > restore_signals, start_new_session) > File "/usr/lib/python3.5/subprocess.py", line 1474, in _execute_child > executable = os.fsencode(executable) > File "/usr/lib/python3.5/os.py", line 862, in fsencode > raise TypeError("expect bytes or str, not %s" % > type(filename).__name__) > TypeError: expect bytes or str, not list > > Traceback (most recent call last): > File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 78, in > > Place() > File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 21, in > __init__ > self.open_and_move() > File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 31, in > open_and_move > p = Popen(['xdotool', 'search', '--classname', > self.classname[self.program]], stdout=subprocess.PIPE) > TypeError: unhashable type: 'list' > > Regards, Jim > Replying to myself to report that I got it working. I changed open_it to: def open_it(self): if self.program == 'ebook-viewer': subprocess.call([self.program, self.path_to_book]) else: subprocess.call([self.program]) After looking at it, I realized I should be providing the path when I was actually opening the file in open_it not further down in open_and_move where I was positioning the windows. Also I realize open_and_move is a poor name and needs to be changed to move_it or something. I still get the following error but it does not seem to effect the programs operation. So for now I am just glad it is working. Traceback (most recent call last): File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 84, in Place() File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 21, in __init__ self.open_and_move() File "/home/jfb/MyProgs/Scripts/place_windows_OO_WS3.py", line 37, in open_and_move p = Popen(['xdotool', 'search', '--classname', self.classname[self.program]], stdout=subprocess.PIPE) KeyError: 'ebook-viewer' Thanks for everyones help, Jim From mysecretrobotfactory at gmail.com Tue May 16 14:49:28 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Tue, 16 May 2017 11:49:28 -0700 Subject: [Tutor] how do i open picture files without knowing the file name? Message-ID: I am running this code so I can do some image manipulation with them. The thing is, I have a lot of pictures to go through, so I can't type the file names one by one in the code. However, the order of the files to be processed doesn't matter, so getting them in a random fashion is alright! How do I make the python code open picture files? thanks! ## need to open random png. Any png. from PIL import Image ## need to load picture so I can process them. Any pictures is fine as ## I need to process every picture in the folder im = Image.open('1.png') ## do my things... From alan.gauld at yahoo.co.uk Tue May 16 15:51:53 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 16 May 2017 20:51:53 +0100 Subject: [Tutor] how do i open picture files without knowing the file name? In-Reply-To: References: Message-ID: On 16/05/17 19:49, Michael C wrote: > I am running this code so I can do some image manipulation with them. > The thing is, I have a lot of pictures to go through, so I can't type the > file names Do you know the folder where they live? If so os.listdir() will give you a list of filenames. If there are subfolders then os.walk() will traverse them. If you don't understand that come back with more specific questions about what puzzles you and we will expand as necessary. -- 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 mysecretrobotfactory at gmail.com Tue May 16 16:01:13 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Tue, 16 May 2017 13:01:13 -0700 Subject: [Tutor] how do i open picture files without knowing the file name? In-Reply-To: References: Message-ID: i ll look into it! thx for now! On Tue, May 16, 2017 at 12:51 PM, Alan Gauld via Tutor wrote: > On 16/05/17 19:49, Michael C wrote: > > I am running this code so I can do some image manipulation with them. > > The thing is, I have a lot of pictures to go through, so I can't type the > > file names > > Do you know the folder where they live? > If so os.listdir() will give you a list of filenames. > If there are subfolders then os.walk() will traverse them. > > If you don't understand that come back with more specific > questions about what puzzles you and we will expand > as necessary. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From robertvstepp at gmail.com Tue May 16 22:49:50 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Tue, 16 May 2017 21:49:50 -0500 Subject: [Tutor] Given a string, call a function of that name Message-ID: My son (Now 13 years old.) is merrily programming away in Python 3 (Of course!) and has many projects he is working on. Today he resumed work on his efforts to create a natural language parser, which he hopes will enable people to type in commands to a DnD-like game using natural English. An ambitious project to be sure! He asked me if there was any way that if he identified a word in the user's input corresponding to one of his functions or methods, if he could use that word to run a function of the same name. I said I had done something once where I used the word as a dictionary key, which was then used to call the function. After fumbling around a bit I came up with the following program for him to play around with: ========================================================================== #!/usr/bin/env python3 def spam(phrase): print('spam function said: ', phrase) def ham(phrase): print('ham function said: ', phrase) def eggs(phrase): print('eggs function said: ', phrase) def get_input(): function = input('Which do you want: spam, ham or eggs?\n') phrase = input('\nHow do you want your food to be prepared?\n') return function, phrase def check_fcn_input(function): valid_fcns = ['spam', 'ham', 'eggs'] if function in valid_fcns: return True else: return False def run_fcn(function, phrase): fcn_dict = {'spam': spam, 'ham': ham, 'eggs': eggs} fcn_dict[function](phrase) def main(): function, phrase = get_input() while not check_fcn_input(function): print("You made an invalid food choice! Let's try this again!") function, phrase = get_input() run_fcn(function, phrase) if __name__ == '__main__': main() ========================================================================== This works, but I cannot but help wondering if there is a more direct approach? Given the above three functions spam(), ham() and eggs(), and given a string 'ham', what is the quickest, most direct way to run that function? Or similarly for the other two? Oh, and I suppose I should ask for a critique of the code as written for appropriate Python style, proper targeted function use, etc. I am always eager to learn! However, I did not use doc strings for the functions or write tests. If I were actually planning on using this for real, I would have done so. Hmm. It bothers me that in check_fcn_input() I have a list valid_fcns and in run_fcn() I have a dictionary fcn_dict. These contain essentially the same information. Would this be a case for a global function dictionary (Which I could also use to check for valid functions.) or perhaps a class which only exists to have this function dictionary? TIA! -- boB From __peter__ at web.de Wed May 17 02:42:40 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 17 May 2017 08:42:40 +0200 Subject: [Tutor] Given a string, call a function of that name References: Message-ID: boB Stepp wrote: > My son (Now 13 years old.) is merrily programming away in Python 3 (Of > course!) and has many projects he is working on. Today he resumed > work on his efforts to create a natural language parser, which he > hopes will enable people to type in commands to a DnD-like game using > natural English. An ambitious project to be sure! He asked me if > there was any way that if he identified a word in the user's input > corresponding to one of his functions or methods, if he could use that > word to run a function of the same name. I said I had done something > once where I used the word as a dictionary key, which was then used to > call the function. After fumbling around a bit I came up with the > following program for him to play around with: > > ========================================================================== > #!/usr/bin/env python3 > > def spam(phrase): > print('spam function said: ', phrase) > > def ham(phrase): > print('ham function said: ', phrase) > > def eggs(phrase): > print('eggs function said: ', phrase) > > def get_input(): > function = input('Which do you want: spam, ham or eggs?\n') > phrase = input('\nHow do you want your food to be prepared?\n') > return function, phrase > > def check_fcn_input(function): > valid_fcns = ['spam', 'ham', 'eggs'] > if function in valid_fcns: > return True > else: > return False > > def run_fcn(function, phrase): > fcn_dict = {'spam': spam, 'ham': ham, 'eggs': eggs} > fcn_dict[function](phrase) > > def main(): > function, phrase = get_input() > while not check_fcn_input(function): > print("You made an invalid food choice! Let's try this again!") > function, phrase = get_input() > run_fcn(function, phrase) > > if __name__ == '__main__': > main() > ========================================================================== > > This works, but I cannot but help wondering if there is a more direct > approach? Given the above three functions spam(), ham() and eggs(), > and given a string 'ham', what is the quickest, most direct way to run > that function? Or similarly for the other two? If you are not bothered about security, getattr(module, function_name)() or even eval(). But usually the approach you have chosen is preferrable. > Oh, and I suppose I should ask for a critique of the code as written > for appropriate Python style, proper targeted function use, etc. I am > always eager to learn! > if function in valid_fcns: > return True > else: > return False should really be spelt return function in valid_fcns and personally I wouldn't mind to type the few extra chars to make it return function in valid_functions ;) > However, I did not use doc strings for the > functions or write tests. If I were actually planning on using this > for real, I would have done so. > > Hmm. It bothers me that in check_fcn_input() I have a list valid_fcns > and in run_fcn() I have a dictionary fcn_dict. These contain > essentially the same information. Would this be a case for a global > function dictionary (Which I could also use to check for valid > functions.) or perhaps a class which only exists to have this function > dictionary? A global is indeed better than the duplicate information in your list and dict. Here's another option, return the function instead of information about its existence: def find_function(function): fcn_dict = {'spam': spam, 'ham': ham, 'eggs': eggs} return fcn_dict.get(function) def main(): while True: function_name, phrase = get_input() function = find_function(function_name) if function is not None: break print("You made an invalid food choice! Let's try this again!") function(phrase) If want to try the class-based approach you can steal from the cmd module: class Lookup: prefix = "do_" def do_spam(self, phrase): print('spam function said: ', phrase) def do_ham(self, phrase): print('ham function said: ', phrase) def do_eggs(self, phrase): print('eggs function said: ', phrase) def known_names(self): offset = len(self.prefix) return sorted( name[offset:] for name in dir(self) if name.startswith(self.prefix) ) def get_input(self): names = self.known_names() names = ", ".join(names[:-1]) + " or " + names[-1] function = input('Which do you want: {}?\n'.format(names)) phrase = input('\nHow do you want your food to be prepared?\n') return function, phrase def find_function(self, function): return getattr(self, self.prefix + function, None) def one_cmd(self): while True: function_name, phrase = self.get_input() function = self.find_function(function_name) if function is not None: break print("You made an invalid food choice! Let's try this again!") function(phrase) def main(): main = Lookup() main.one_cmd() The name mangling with the do_-prefix prevents that the user can invoke arbitrary methods. From gsanford at wesleyan.edu Tue May 16 21:10:26 2017 From: gsanford at wesleyan.edu (Grace Sanford) Date: Tue, 16 May 2017 21:10:26 -0400 Subject: [Tutor] turtle question Message-ID: how do you reset the turtle's position without drawing a line from where it last was? From __peter__ at web.de Wed May 17 04:10:19 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 17 May 2017 10:10:19 +0200 Subject: [Tutor] turtle question References: Message-ID: Grace Sanford wrote: > how do you reset the turtle's position without drawing a line from where > it last was? Call turtle.penup() when you want to prevent it from drawing and pendown() to have it draw again. Example: import turtle for i in range(10): # draw a 20 units line turtle.forward(20) # lift the pen turtle.penup() # move 10 units forward without drawing turtle.forward(10) # put the pen back on the paper turtle.pendown() turtle.mainloop() From alan.gauld at yahoo.co.uk Wed May 17 04:34:13 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 17 May 2017 09:34:13 +0100 Subject: [Tutor] Given a string, call a function of that name In-Reply-To: References: Message-ID: On 17/05/17 03:49, boB Stepp wrote: > corresponding to one of his functions or methods, if he could use that > word to run a function of the same name. I said I had done something > once where I used the word as a dictionary key, which was then used to > call the function. That's the usual approach. > def check_fcn_input(function): > valid_fcns = ['spam', 'ham', 'eggs'] > if function in valid_fcns: > return True > else: > return False I would rewrite this to return the function or None def get_input_function(name): return functions.get(name) You can then use the result as a boolean in a test or simply call it directly using Pythons "ask forgiveness" approach: name,phrase = get_input() try: get_input_function(name)(phrase) except TypeError: pass > This works, but I cannot but help wondering if there is a more direct > approach? Not really. Remember that Python itself uses a dictionary to translate names to function calls. If its good enough for the interpreter its probably good enough for you! :-) > Hmm. It bothers me that in check_fcn_input() I have a list valid_fcns > and in run_fcn() I have a dictionary fcn_dict. A global dict wins here. You could of course make it a class but I'm not sure that actually makes anything clearer in this case. HTH -- 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 gsanford at wesleyan.edu Wed May 17 09:20:54 2017 From: gsanford at wesleyan.edu (Grace Sanford) Date: Wed, 17 May 2017 09:20:54 -0400 Subject: [Tutor] Help writing a function Message-ID: I need suggestions/help for writing the following function: import turtle import time import random # This list represents the board. It's a list # of nine strings, each of which is either # "X", "O", "_", representing, respectively, # a position occupied by an X, by an O, and # an unoccupied position. The first three # elements in the list represent the first row, # and so on. Initially, all positions are # unoccupied. the_board = [ "_", "_", "_", "_", "_", "_", "_", "_", "_"] def do_user_move(board, x, y): """ signature: list(str), int, int -> bool Given a list representing the state of the board and an x,y screen coordinate pair indicating where the user clicked, update the board with an O in the corresponding position. The function returns a bool indicated if the operation was successful: if the user clicks on a position that is already occupied or outside of the board area, the move is invalid, and the function should return False, otherise True. """ From gsanford at wesleyan.edu Wed May 17 09:31:33 2017 From: gsanford at wesleyan.edu (Grace Sanford) Date: Wed, 17 May 2017 09:31:33 -0400 Subject: [Tutor] Help finishing a function Message-ID: I am wondering if someone can help/advise me on finishing the code for this function: import turtle import time import random # This list represents the board. It's a list # of nine strings, each of which is either # "X", "O", "_", representing, respectively, # a position occupied by an X, by an O, and # an unoccupied position. The first three # elements in the list represent the first row, # and so on. Initially, all positions are # unoccupied. the_board = [ "_", "_", "_", "_", "_", "_", "_", "_", "_"] def do_user_move(board, x, y): """ signature: list(str), int, int -> bool Given a list representing the state of the board and an x,y screen coordinate pair indicating where the user clicked, update the board with an O in the corresponding position. The function returns a bool indicated if the operation was successful: if the user clicks on a position that is already occupied or outside of the board area, the move is invalid, and the function should return False, otherise True. """ print("user clicked at "+str(x)+","+str(y)) width = turtle.window_width () height = turtle.window_height () #Given coordinates of user click, update board with "O" in corresponding position if x<(-width/2)+(width/3): column = 0 elif x>(-width/2)+(width/3) and x<(width/2)-(width/3): column = 1 elif x>(width/2)-(width/3): column = 2 if y>(height/2)-(height/3): row = 0 elif y<(height/2)-(height/3) and y>(-height/2)+(height/3): row = 1 elif y<(-height/2)+(height/3): row = 2 p = row * 3 + column for board[p]=="_": pass #code here #Check if user clicks on a position that is already occupied pass #code here #Check if user clicks outside the board area if x<(-width/2) or x>(width/2) or y<(-height/2) or y>(height/2): return False From poojabhalode11 at gmail.com Wed May 17 10:07:00 2017 From: poojabhalode11 at gmail.com (Pooja Bhalode) Date: Wed, 17 May 2017 10:07:00 -0400 Subject: [Tutor] Matplotlib error with Python Message-ID: *Hi, * *I have been working on a graphic user interface with matplotlib and suddenly(Earlier, I didnt get these errors, and when I opened the files now, I am) getting these errors: * */Users/poojabhalode/.bash_profile: line 1: .bashrc: No such file or directory* *[[ 2 -1 0]* * [-1 2 -1]* * [ 0 -1 2]]* *[[ 3.41421356e+00 8.32667268e-17 -6.37995760e-17]* * [ 0.00000000e+00 2.00000000e+00 1.35170527e-16]* * [ 0.00000000e+00 0.00000000e+00 5.85786438e-01]]* *8.32667268469e-17* *8.32667268469e-17* *-6.37995760397e-17* *0.0* *1.35170526715e-16* *0.0* *0.0* *[[ 3.41421356 0. 0. ]* * [ 0. 2. 0. ]* * [ 0. 0. 0.58578644]]* *Traceback (most recent call last):* * File "/Users/poojabhalode/Google Drive/PYTHON/files/1sttest/May17.py", line 7, in * * import matplotlib* * File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.py", line 947, in * * rcParams = rc_params()* * File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.py", line 856, in rc_params* * fname = matplotlib_fname()* * File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.py", line 726, in matplotlib_fname* * configdir = _get_configdir()* * File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.py", line 597, in _get_configdir* * return _get_config_or_cache_dir(_get_xdg_config_dir())* * File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.py", line 566, in _get_config_or_cache_dir* * if not _is_writable_dir(p):* * File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.py", line 228, in _is_writable_dir* * t = tempfile.TemporaryFile(dir=p)* *AttributeError: 'module' object has no attribute 'TemporaryFile'* *[Finished in 0.2s with exit code 1]* *[shell_cmd: python -u "/Users/poojabhalode/Google Drive/PYTHON/files/1sttest/May17.py"]* *[dir: /Users/poojabhalode/Google Drive/PYTHON/files/1sttest]* *[path: /usr/bin:/bin:/usr/sbin:/sbin]* *My code gives errors at the import statement itself: * *Code::* *import matplotlib* *ERROR indicated here. * *matplotlib.use("TkAgg")* *from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg* *from matplotlib.figure import Figure* *Can someone please let me know what is going wrong with this?* *Thank you. I would really appreciate it.* *Thanks!* From alan.gauld at yahoo.co.uk Wed May 17 11:18:36 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 17 May 2017 16:18:36 +0100 Subject: [Tutor] Help finishing a function In-Reply-To: References: Message-ID: On 17/05/17 14:31, Grace Sanford wrote: > I am wondering if someone can help/advise me on finishing the code for this > function: Please only send one email for a given i8ssue it gets confusing when people start responding to two different threads about the same question. Also please give us more information. We won;t do your homework for you so you need to tell us specifically where you need help. The assignment gives you lots of hints about what to do. Which bits are you struggling with? That having been said, there is much I don't understand myself, see below... > the_board = [ "_", "_", "_", > "_", "_", "_", > "_", "_", "_"] > > def do_user_move(board, x, y): > """ > signature: list(str), int, int -> bool > Given a list representing the state of the board > and an x,y screen coordinate pair indicating where > the user clicked, update the board > with an O in the corresponding position. The board above is a list of strings and not represented on any kind of "screen"? I assume there is some other code somewhere that creates a grid on screen that the user then clicks on and you need to map the cell to the string table? > The function returns a bool indicated if > the operation was successful: if the user > clicks on a position that is already occupied > or outside of the board area, the move is > invalid, and the function should return False, > otherise True. > """ > print("user clicked at "+str(x)+","+str(y)) > width = turtle.window_width () > height = turtle.window_height () > #Given coordinates of user click, update board with "O" in > corresponding position > if x<(-width/2)+(width/3): > column = 0 > elif x>(-width/2)+(width/3) and x<(width/2)-(width/3): > column = 1 > elif x>(width/2)-(width/3): > column = 2 This seems like a complicated way to get the cell! > if y>(height/2)-(height/3): > row = 0 > elif y<(height/2)-(height/3) and y>(-height/2)+(height/3): > row = 1 > elif y<(-height/2)+(height/3): > row = 2 > p = row * 3 + column > for board[p]=="_": > pass #code here This is not valid Python code. The for loop requires a sequence of some kind (to be technical an iterable). This is a boolean test so you should get an error. > #Check if user clicks on a position that is already occupied > pass #code here Assuming you get the code above to work and you have a row/column [pair do you know how to map that onto your 9 element string list? If so put the code here. > #Check if user clicks outside the board area > if x<(-width/2) or x>(width/2) or y<(-height/2) or y>(height/2): > return False I'd have thought thus test should be right at the top before attempting all the other stuff! So in conclusion it looks like we only have half the story (where is the missing screen?) and the code as provided has at least one bug (the for loop). But otherwise the task is fairly clear, so what bit are you stuck with? -- 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 __peter__ at web.de Wed May 17 11:26:30 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 17 May 2017 17:26:30 +0200 Subject: [Tutor] Help finishing a function References: Message-ID: Grace Sanford wrote: > I am wondering if someone can help/advise me on finishing the code for > this function: > > import turtle > import time > import random > > # This list represents the board. It's a list > # of nine strings, each of which is either > # "X", "O", "_", representing, respectively, > # a position occupied by an X, by an O, and > # an unoccupied position. The first three > # elements in the list represent the first row, > # and so on. Initially, all positions are > # unoccupied. > the_board = [ "_", "_", "_", > "_", "_", "_", > "_", "_", "_"] > > def do_user_move(board, x, y): > """ > signature: list(str), int, int -> bool > Given a list representing the state of the board > and an x,y screen coordinate pair indicating where > the user clicked, update the board > with an O in the corresponding position. > The function returns a bool indicated if > the operation was successful: if the user > clicks on a position that is already occupied > or outside of the board area, the move is > invalid, and the function should return False, > otherise True. > """ > print("user clicked at "+str(x)+","+str(y)) > width = turtle.window_width () > height = turtle.window_height () Hello Grace! While I can understand that you want to have something visibible you are clearly overambitious here. Forget about the turtle for now and try to implement exactly what the docstring is asking for. Indices are zero-based in Python and for a fresh board the function should return False when any of the x and y arguments is outside the half-open interval 0 to 3 and True when both are inside. "Half-open" means that 0, 1, 2 are a legal values, but 3 is not. Examples: >>> do_user_move(the_board, 0, 0) True >>> do_user_move(the_board, 2, 0) True >>> do_user_move(the_board, 3, 0) False >>> do_user_move(the_board, -1, 0) False >>> do_user_move(the_board, 2, 2) True >>> do_user_move(the_board, 2, 3) False Once you have that part working you can tackle the problem of converting x and y to an index into the `the_board` list. Examples: x = 0, y = 0 --> index = 0 x = 1, y = 0 --> index = 1 x = 0, y = 1 --> index = 3 x = 1, y = 2 --> index = 7 Then, finally, you can think about how to render the logical board as a pretty image on screen. > #Given coordinates of user click, update board with "O" in > corresponding position > if x<(-width/2)+(width/3): > column = 0 > elif x>(-width/2)+(width/3) and x<(width/2)-(width/3): > column = 1 > elif x>(width/2)-(width/3): > column = 2 > if y>(height/2)-(height/3): > row = 0 > elif y<(height/2)-(height/3) and y>(-height/2)+(height/3): > row = 1 > elif y<(-height/2)+(height/3): > row = 2 > p = row * 3 + column > for board[p]=="_": > pass #code here > #Check if user clicks on a position that is already occupied > pass #code here > #Check if user clicks outside the board area > if x<(-width/2) or x>(width/2) or y<(-height/2) or y>(height/2): > return False > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Wed May 17 11:27:00 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 17 May 2017 16:27:00 +0100 Subject: [Tutor] Matplotlib error with Python In-Reply-To: References: Message-ID: On 17/05/17 15:07, Pooja Bhalode wrote: > */Users/poojabhalode/.bash_profile: line 1: .bashrc: No such file or > directory* I'd start with this one. Where is the bash script error coming from? Is there really no .bashrc? If not what is the impact of not having it? I'm also not clear what is calling bashrc... > *[[ 2 -1 0]* > * [-1 2 -1]* I have no idea what these numbers are. > *Traceback (most recent call last):* > * File "/Users/poojabhalode/Google Drive/PYTHON/files/1sttest/May17.py", > line 7, in * > * import matplotlib* > * File > "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.py", > line 947, in * > * rcParams = rc_params()* > * File > "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.py", > line 856, in rc_params* > * fname = matplotlib_fname()* > * File > "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.py", > line 726, in matplotlib_fname* > * configdir = _get_configdir()* > * File > "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.py", > line 597, in _get_configdir* > * return _get_config_or_cache_dir(_get_xdg_config_dir())* > * File > "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.py", > line 566, in _get_config_or_cache_dir* > * if not _is_writable_dir(p):* > * File > "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/matplotlib/__init__.py", > line 228, in _is_writable_dir* > * t = tempfile.TemporaryFile(dir=p)* > *AttributeError: 'module' object has no attribute 'TemporaryFile'* You don't happen to have a file called tempfile.py in your folder by any chance? It could be that the code is picking it up instead of the standard library version? -- 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 gsanford at wesleyan.edu Wed May 17 12:26:39 2017 From: gsanford at wesleyan.edu (Grace Sanford) Date: Wed, 17 May 2017 12:26:39 -0400 Subject: [Tutor] Bug Message-ID: Theoretically, the following code is suppose to check if the user has won a tic tac toe game by checking if there are all "X"s in either the horizontal, vertical, or diagonal lines of a grid (represented by a list with "board" with elements 0-8). If one of these is the case, it is suppose to print the "You won" string. Nevertheless, when I change list variable to reflect one of these conditions, there is no printing occurring. I cannot figure out why. if board[0:3]==["X", "X", "X"] or board[3:6]==["X", "X", "X"] or board[6:9]==["X", "X", "X"] or \ [board[0],board[3],board[6]]==["X", "X", "X"] or [board[1],board[4],board[7]]==["X", "X", "X"] or [board[2],board[5],board[8]] ==["X", "X", "X"] or \ [board[0],board[4],board[8]]==["X", "X", "X"] or [board[2],board[4],board[6]]==["X", "X", "X"]: From kquach at rogers.com Wed May 17 14:17:10 2017 From: kquach at rogers.com (keith quach) Date: Wed, 17 May 2017 14:17:10 -0400 Subject: [Tutor] Help - What is the best package to learn programming in Python? Message-ID: Hi, I hope you could help. I am new to the Python community. I am looking for your recommendation for a Windows 10 (64 bit) Python 3.6 distribution package that covers all the necessary installtions/files. Thanks, Keith From poojabhalode11 at gmail.com Wed May 17 11:36:54 2017 From: poojabhalode11 at gmail.com (Pooja Bhalode) Date: Wed, 17 May 2017 11:36:54 -0400 Subject: [Tutor] Matplotlib error with Python In-Reply-To: References: Message-ID: Hi Alan, Yes, that was the issue, I had a tempfile.py created my me some time back. That was interfering with this one. Thanks a lot for your input. Hope you have a great day! Pooja On Wed, May 17, 2017 at 11:27 AM, Alan Gauld via Tutor wrote: > On 17/05/17 15:07, Pooja Bhalode wrote: > > > */Users/poojabhalode/.bash_profile: line 1: .bashrc: No such file or > > directory* > > I'd start with this one. > Where is the bash script error coming from? > Is there really no .bashrc? > If not what is the impact of not having it? > > I'm also not clear what is calling bashrc... > > > *[[ 2 -1 0]* > > * [-1 2 -1]* > > I have no idea what these numbers are. > > > *Traceback (most recent call last):* > > * File "/Users/poojabhalode/Google Drive/PYTHON/files/1sttest/ > May17.py", > > line 7, in * > > * import matplotlib* > > * File > > "/System/Library/Frameworks/Python.framework/Versions/2.7/ > Extras/lib/python/matplotlib/__init__.py", > > line 947, in * > > * rcParams = rc_params()* > > * File > > "/System/Library/Frameworks/Python.framework/Versions/2.7/ > Extras/lib/python/matplotlib/__init__.py", > > line 856, in rc_params* > > * fname = matplotlib_fname()* > > * File > > "/System/Library/Frameworks/Python.framework/Versions/2.7/ > Extras/lib/python/matplotlib/__init__.py", > > line 726, in matplotlib_fname* > > * configdir = _get_configdir()* > > * File > > "/System/Library/Frameworks/Python.framework/Versions/2.7/ > Extras/lib/python/matplotlib/__init__.py", > > line 597, in _get_configdir* > > * return _get_config_or_cache_dir(_get_xdg_config_dir())* > > * File > > "/System/Library/Frameworks/Python.framework/Versions/2.7/ > Extras/lib/python/matplotlib/__init__.py", > > line 566, in _get_config_or_cache_dir* > > * if not _is_writable_dir(p):* > > * File > > "/System/Library/Frameworks/Python.framework/Versions/2.7/ > Extras/lib/python/matplotlib/__init__.py", > > line 228, in _is_writable_dir* > > * t = tempfile.TemporaryFile(dir=p)* > > *AttributeError: 'module' object has no attribute 'TemporaryFile'* > > You don't happen to have a file called tempfile.py in your > folder by any chance? It could be that the code is picking > it up instead of the standard library version? > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From s.molnar at sbcglobal.net Wed May 17 15:50:24 2017 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Wed, 17 May 2017 15:50:24 -0400 Subject: [Tutor] Pyuthon 3 Combine 1 D Arrays Message-ID: <591CA980.10705@sbcglobal.net> I'm beginning to think that I don't know how to ask the question as Google and Stack Overflow have resulted in nothing. All of the results seem to deal with integers. I have a number of single column floating point arrays each containing 300 entries that I want to combine into a n by 300 array where n is the number of single column arrays. I have tried zip, np.concatenate etc with only failure. Thanks in advance. -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 -------------- next part -------------- 1.000000000000000000e+00 1.100334448160535050e+00 1.200668896321070322e+00 1.301003344481605373e+00 1.401337792642140423e+00 1.501672240802675695e+00 1.602006688963210745e+00 1.702341137123745796e+00 1.802675585284280846e+00 1.903010033444816118e+00 2.003344481605351390e+00 2.103678929765885997e+00 2.204013377926421491e+00 2.304347826086956541e+00 2.404682274247491591e+00 2.505016722408027086e+00 2.605351170568561692e+00 2.705685618729097186e+00 2.806020066889632236e+00 2.906354515050167286e+00 3.006688963210702337e+00 3.107023411371237387e+00 3.207357859531772437e+00 3.307692307692307931e+00 3.408026755852842982e+00 3.508361204013378032e+00 3.608695652173913082e+00 3.709030100334448132e+00 3.809364548494983183e+00 3.909698996655518677e+00 4.010033444816054171e+00 4.110367892976588777e+00 4.210702341137123383e+00 4.311036789297658878e+00 4.411371237458194372e+00 4.511705685618728978e+00 4.612040133779264472e+00 4.712374581939799967e+00 4.812709030100334573e+00 4.913043478260869179e+00 5.013377926421404673e+00 5.113712374581940168e+00 5.214046822742474774e+00 5.314381270903010268e+00 5.414715719063544874e+00 5.515050167224080369e+00 5.615384615384615863e+00 5.715719063545150469e+00 5.816053511705685963e+00 5.916387959866220569e+00 6.016722408026756064e+00 6.117056856187290670e+00 6.217391304347826164e+00 6.317725752508361658e+00 6.418060200668896265e+00 6.518394648829431759e+00 6.618729096989966365e+00 6.719063545150501859e+00 6.819397993311037354e+00 6.919732441471571960e+00 7.020066889632107454e+00 7.120401337792642060e+00 7.220735785953177555e+00 7.321070234113712161e+00 7.421404682274247655e+00 7.521739130434783149e+00 7.622073578595317755e+00 7.722408026755853250e+00 7.822742474916387856e+00 7.923076923076923350e+00 8.023411371237457956e+00 8.123745819397992562e+00 8.224080267558528945e+00 8.324414715719063551e+00 8.424749163879599934e+00 8.525083612040134540e+00 8.625418060200669146e+00 8.725752508361203752e+00 8.826086956521738358e+00 8.926421404682274741e+00 9.026755852842809347e+00 9.127090301003343953e+00 9.227424749163880335e+00 9.327759197324414941e+00 9.428093645484949548e+00 9.528428093645485930e+00 9.628762541806020536e+00 9.729096989966555142e+00 9.829431438127089748e+00 9.929765886287626131e+00 1.003010033444816074e+01 1.013043478260869534e+01 1.023076923076923173e+01 1.033110367892976633e+01 1.043143812709030094e+01 1.053177257525083554e+01 1.063210702341137193e+01 1.073244147157190653e+01 1.083277591973244114e+01 1.093311036789297752e+01 1.103344481605351213e+01 1.113377926421404673e+01 1.123411371237458134e+01 1.133444816053511772e+01 1.143478260869565233e+01 1.153511705685618693e+01 1.163545150501672332e+01 1.173578595317725792e+01 1.183612040133779253e+01 1.193645484949832891e+01 1.203678929765886352e+01 1.213712374581939812e+01 1.223745819397993273e+01 1.233779264214046911e+01 1.243812709030100372e+01 1.253846153846153832e+01 1.263879598662207471e+01 1.273913043478260931e+01 1.283946488294314392e+01 1.293979933110367853e+01 1.304013377926421491e+01 1.314046822742474951e+01 1.324080267558528412e+01 1.334113712374582050e+01 1.344147157190635511e+01 1.354180602006688972e+01 1.364214046822742432e+01 1.374247491638796070e+01 1.384280936454849531e+01 1.394314381270902992e+01 1.404347826086956630e+01 1.414381270903010090e+01 1.424414715719063551e+01 1.434448160535117012e+01 1.444481605351170650e+01 1.454515050167224111e+01 1.464548494983277571e+01 1.474581939799331209e+01 1.484615384615384670e+01 1.494648829431438131e+01 1.504682274247491591e+01 1.514715719063545230e+01 1.524749163879598690e+01 1.534782608695652151e+01 1.544816053511705789e+01 1.554849498327759250e+01 1.564882943143812710e+01 1.574916387959866171e+01 1.584949832775919809e+01 1.594983277591973270e+01 1.605016722408026908e+01 1.615050167224080369e+01 1.625083612040133829e+01 1.635117056856187290e+01 1.645150501672240750e+01 1.655183946488294566e+01 1.665217391304347672e+01 1.675250836120401488e+01 1.685284280936454948e+01 1.695317725752508409e+01 1.705351170568561869e+01 1.715384615384615330e+01 1.725418060200668791e+01 1.735451505016722606e+01 1.745484949832776067e+01 1.755518394648829528e+01 1.765551839464882988e+01 1.775585284280936449e+01 1.785618729096989910e+01 1.795652173913043370e+01 1.805685618729097186e+01 1.815719063545150647e+01 1.825752508361204107e+01 1.835785953177257568e+01 1.845819397993311028e+01 1.855852842809364489e+01 1.865886287625417950e+01 1.875919732441471766e+01 1.885953177257525226e+01 1.895986622073578687e+01 1.906020066889632147e+01 1.916053511705685608e+01 1.926086956521739069e+01 1.936120401337792529e+01 1.946153846153846345e+01 1.956187290969899806e+01 1.966220735785953266e+01 1.976254180602006727e+01 1.986287625418060188e+01 1.996321070234113648e+01 2.006354515050167109e+01 2.016387959866220925e+01 2.026421404682274385e+01 2.036454849498327846e+01 2.046488294314381307e+01 2.056521739130434767e+01 2.066555183946488228e+01 2.076588628762541688e+01 2.086622073578595504e+01 2.096655518394648965e+01 2.106688963210702425e+01 2.116722408026755886e+01 2.126755852842809347e+01 2.136789297658862807e+01 2.146822742474916268e+01 2.156856187290970084e+01 2.166889632107023544e+01 2.176923076923077005e+01 2.186956521739130466e+01 2.196989966555183926e+01 2.207023411371237387e+01 2.217056856187290848e+01 2.227090301003344663e+01 2.237123745819398124e+01 2.247157190635451585e+01 2.257190635451505045e+01 2.267224080267558506e+01 2.277257525083611966e+01 2.287290969899665782e+01 2.297324414715719243e+01 2.307357859531772704e+01 2.317391304347826164e+01 2.327424749163879625e+01 2.337458193979933085e+01 2.347491638795986546e+01 2.357525083612040362e+01 2.367558528428093823e+01 2.377591973244147283e+01 2.387625418060200744e+01 2.397658862876254204e+01 2.407692307692307665e+01 2.417725752508361126e+01 2.427759197324414941e+01 2.437792642140468402e+01 2.447826086956521863e+01 2.457859531772575323e+01 2.467892976588628784e+01 2.477926421404682245e+01 2.487959866220735705e+01 2.497993311036789521e+01 2.508026755852842982e+01 2.518060200668896442e+01 2.528093645484949903e+01 2.538127090301003363e+01 2.548160535117056824e+01 2.558193979933110285e+01 2.568227424749164101e+01 2.578260869565217561e+01 2.588294314381271022e+01 2.598327759197324482e+01 2.608361204013377943e+01 2.618394648829431404e+01 2.628428093645484864e+01 2.638461538461538680e+01 2.648494983277592141e+01 2.658528428093645601e+01 2.668561872909699062e+01 2.678595317725752523e+01 2.688628762541805983e+01 2.698662207357859444e+01 2.708695652173913260e+01 2.718729096989966720e+01 2.728762541806020181e+01 2.738795986622073642e+01 2.748829431438127102e+01 2.758862876254180563e+01 2.768896321070234023e+01 2.778929765886287839e+01 2.788963210702341300e+01 2.798996655518394761e+01 2.809030100334448221e+01 2.819063545150501682e+01 2.829096989966555142e+01 2.839130434782608603e+01 2.849163879598662419e+01 2.859197324414715879e+01 2.869230769230769340e+01 2.879264214046822801e+01 2.889297658862876261e+01 2.899331103678929722e+01 2.909364548494983183e+01 2.919397993311036998e+01 2.929431438127090459e+01 2.939464882943143920e+01 2.949498327759197380e+01 2.959531772575250841e+01 2.969565217391304301e+01 2.979598662207357762e+01 2.989632107023411578e+01 2.999665551839465039e+01 3.009698996655518499e+01 3.019732441471571960e+01 3.029765886287625420e+01 3.039799331103678881e+01 3.049832775919732342e+01 3.059866220735786158e+01 3.069899665551839618e+01 3.079933110367893079e+01 3.089966555183946539e+01 3.100000000000000000e+01 -------------- next part -------------- -4.989135702304388786e+01 -3.782702353676535267e+01 -2.008998234748863965e+01 -1.175585807041829689e+00 1.495434549948340575e+01 2.550093979056171634e+01 2.917475012237327192e+01 2.622356379754077338e+01 1.817069168253399880e+01 7.345881526907726666e+00 -3.677555051582719337e+00 -1.262019789896069355e+01 -1.790803103486884851e+01 -1.889400988133026260e+01 -1.587058246803304407e+01 -9.897237492450921437e+00 -2.500084773596451981e+00 4.682126372205683396e+00 1.023064561450653720e+01 1.319721486329215310e+01 1.323539046705397304e+01 1.060301269904798716e+01 6.051249999601910012e+00 6.341703826502502750e-01 -4.517548148775482275e+00 -8.414540919032189592e+00 -1.038102230706289575e+01 -1.015611406484396539e+01 -7.913296397590167786e+00 -4.201242756955583246e+00 1.785298279918888786e-01 4.332880458660905099e+00 7.447949758708277912e+00 8.940032579983979488e+00 8.560492841533653419e+00 6.436541501867912629e+00 3.040553897794865179e+00 -9.068354826443663086e-01 -4.582287017053754852e+00 -7.226522262205014258e+00 -8.299629590407503699e+00 -7.593162878671786409e+00 -5.274801125640029120e+00 -1.854444361800865693e+00 1.923071609230374124e+00 5.237348627399976309e+00 7.370359649206132957e+00 7.864130136125616843e+00 6.622670479887682760e+00 3.934266672284844724e+00 4.077929786020433323e-01 -3.163690167628180117e+00 -5.981741511658563937e+00 -7.423101260868043560e+00 -7.180412361830405210e+00 -5.330386530332558159e+00 -2.313470535455159016e+00 1.170203270497526482e+00 4.324077117774246304e+00 6.439201324092779011e+00 7.055859640294457336e+00 6.065205495232201649e+00 3.727888097550572599e+00 6.065828388800000859e-01 -2.570231071429938119e+00 -5.079938441808319105e+00 -6.371891499094652467e+00 -6.189208132926637518e+00 -4.618998589077998318e+00 -2.061431107185877298e+00 8.725148831895208623e-01 3.508301860599581268e+00 5.264171452208674218e+00 5.779925872301565626e+00 4.989506557476382831e+00 3.123132435671212903e+00 6.418420976045284831e-01 -1.877167725201108528e+00 -3.875614681232903092e+00 -4.937740513278311205e+00 -4.875106169157246150e+00 -3.755791979992229024e+00 -1.874952908618830349e+00 3.228638943953915930e-01 2.349151420698369908e+00 3.779488292529795679e+00 4.340447467099695089e+00 3.958182610498339304e+00 2.761275010752416215e+00 1.040889976732125799e+00 -8.199352900050015380e-01 -2.430700614654627056e+00 -3.473202709520770703e+00 -3.761036003152107288e+00 -3.268795670835793032e+00 -2.127835400479471950e+00 -5.920976891535016939e-01 1.017262976897724824e+00 2.376732789515100031e+00 3.222402009520803468e+00 3.398156342555897602e+00 2.882273933877695704e+00 1.788600245299090563e+00 3.427764212087292539e-01 -1.161942804505055715e+00 -2.421404300166859702e+00 -3.179723150680448640e+00 -3.280107306958649271e+00 -2.697572695020084232e+00 -1.546589720280284741e+00 -6.104130067127117032e-02 1.450863489880372725e+00 2.668816743935060742e+00 3.327733381934794288e+00 3.276148788813820545e+00 2.512589904975988997e+00 1.190749302234919371e+00 -4.093491685369414768e-01 -1.939478156129876618e+00 -3.058910310942971833e+00 -3.511234936570373222e+00 -3.184669457558185979e+00 -2.141224729732431609e+00 -6.067406698560582345e-01 1.077265214735538601e+00 2.529872204123308155e+00 3.418014299597285532e+00 3.534395923893328462e+00 2.847389163177406868e+00 1.510488990747854920e+00 -1.716432118824457731e-01 -1.813116170434613483e+00 -3.036500097081449745e+00 -3.561073701117846380e+00 -3.268568214609918421e+00 -2.230636392922966671e+00 -6.914474825626369947e-01 9.903758057635186907e-01 2.426472148751638969e+00 3.289751924046178111e+00 3.389988222788530070e+00 2.716206492133223183e+00 1.436294053935290860e+00 -1.453453427516132601e-01 -1.661247335774635125e+00 -2.767416209865317445e+00 -3.222216597033965790e+00 -2.939295079264343524e+00 -2.002905406785845610e+00 -6.432798897798408522e-01 8.208107915725221249e-01 2.057735513455910770e+00 2.798679707788371385e+00 2.895890778016051659e+00 2.350929798774065826e+00 1.307925491766929627e+00 1.513417952543061329e-02 -1.234834797450475641e+00 -2.171537527998177008e+00 -2.604753674797276375e+00 -2.461764881373474267e+00 -1.796986371775219471e+00 -7.736541002590879845e-01 3.766228426727505352e-01 1.406312727778275873e+00 2.104530218704414146e+00 2.338780468925277134e+00 2.078082818130412512e+00 1.394125636611494023e+00 4.417810805520490103e-01 -5.757548923244035777e-01 -1.449415507927338309e+00 -2.005727384908674082e+00 -2.139544883573424539e+00 -1.832437448307795425e+00 -1.154007569128849831e+00 -2.465376640805917452e-01 7.037776787068863449e-01 1.503547901117399554e+00 1.990094800960211963e+00 2.063585803763686943e+00 1.707340217900452428e+00 9.922175301855484797e-01 6.377406446418681929e-02 -8.859233276457338935e-01 -1.656843594583504276e+00 -2.082536516631678047e+00 -2.066220463253735318e+00 -1.603531726748722575e+00 -7.863705466838405433e-01 2.139830876743204491e-01 1.181849965836944794e+00 1.903371416161150353e+00 2.214140175478783501e+00 2.037512082442650474e+00 1.404421925848377883e+00 4.493492035501511217e-01 -6.175965568550870755e-01 -1.556876467874403147e+00 -2.153847915692830295e+00 -2.268725290745527534e+00 -1.870279985834191194e+00 -1.044886599643395142e+00 2.181281516773631024e-02 1.086849635264148128e+00 1.905935581012842217e+00 2.290424845648891150e+00 2.151677539398523020e+00 1.522130301644184502e+00 5.478901780874073646e-01 -5.455002361241902964e-01 -1.506002303683851462e+00 -2.114084665926062812e+00 -2.233713637232671356e+00 -1.843251737490468223e+00 -1.039111181511425785e+00 -1.162895424148999157e-02 1.000701565910979474e+00 1.767428482319533645e+00 2.119282857281788335e+00 1.985811476229744299e+00 1.409056664009087356e+00 5.307640873288060890e-01 -4.430572057560616672e-01 -1.291188991760533256e+00 -1.827980330175278390e+00 -1.944038476388533221e+00 -1.627900502119618453e+00 -9.645913691093188636e-01 -1.125213313683188221e-01 7.349268967876437797e-01 1.393640146355626275e+00 1.728621019644615453e+00 1.680862409526334922e+00 1.276154060716046867e+00 6.150870624459470193e-01 -1.523050246231151261e-01 -8.607731282357500291e-01 -1.365088101611864335e+00 -1.568880284208032938e+00 -1.441655602933981317e+00 -1.021459293334454843e+00 -4.037576592336105108e-01 2.802521472924186519e-01 8.909763747892321950e-01 1.307876264087727058e+00 1.451974503927710547e+00 1.299638720924972679e+00 8.856344810278431456e-01 2.953568305569998498e-01 -3.520735363627967018e-01 -9.267365045781900124e-01 -1.312814665654407298e+00 -1.431079212632464071e+00 -1.254866385230449577e+00 -8.164051472209996607e-01 -2.019031024125218510e-01 4.642305029210396228e-01 1.043535102910351897e+00 1.411669861094379641e+00 1.485060982108963756e+00 1.240321296198652279e+00 7.217990910938736349e-01 3.482132345932920092e-02 -6.748152414637516028e-01 -1.251397639131106354e+00 -1.563607647443729487e+00 -1.534981574010484406e+00 -1.163292289252247036e+00 -5.237694701672012121e-01 2.454068656409528759e-01 9.727760229707326234e-01 1.492110774765444514e+00 1.681033253682105100e+00 1.490578595879253898e+00 9.583635384639688226e-01 2.017062311537572150e-01 -6.082285425803166667e-01 -1.285557492408472946e+00 -1.673011611368314222e+00 -1.679176407734167764e+00 -1.300616669241415169e+00 -6.233799622599560664e-01 1.966998562010519591e-01 9.703449050824082267e-01 1.519201899215782525e+00 1.717638833982976232e+00 1.521980530255498731e+00 9.802448039257359858e-01 2.201759094837304476e-01 -5.813860565326754992e-01 -1.240307542400615359e+00 -1.608064556042943183e+00 -1.605576301467637412e+00 -1.240160460568219936e+00 -6.019932368522818988e-01 1.585857130479550459e-01 8.670869717510122365e-01 1.365406137799051622e+00 1.547317989731859189e+00 From mysecretrobotfactory at gmail.com Wed May 17 15:09:06 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Wed, 17 May 2017 12:09:06 -0700 Subject: [Tutor] copy/paste/move files Message-ID: Hi all, How do I move files to a designated folder or copy/paste? thanks! From mysecretrobotfactory at gmail.com Wed May 17 16:24:41 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Wed, 17 May 2017 13:24:41 -0700 Subject: [Tutor] Python Image Library Message-ID: from PIL import Image im = Image.open('pic.bmp') im.show() I ran this code and it not only opened the picture in paint, which is what I want, but it also opens a CMD.exe console window! how do I prevent that from happening? thanks! From mysecretrobotfactory at gmail.com Wed May 17 16:33:05 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Wed, 17 May 2017 13:33:05 -0700 Subject: [Tutor] How do I display a picture? Message-ID: Hi all, How do I display a picture? From mysecretrobotfactory at gmail.com Wed May 17 17:01:21 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Wed, 17 May 2017 14:01:21 -0700 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: in fact, when I ran this: import os os.system("mspaint 1.bmp") It also opened a cmd.exe window and the script wouldn't continue until I closed the cmd window! On Wed, May 17, 2017 at 1:24 PM, Michael C wrote: > from PIL import Image > > > im = Image.open('pic.bmp') > im.show() > > > I ran this code and it not only opened the picture in paint, which is what > I want, > but it also opens a CMD.exe console window! how do I prevent that from > happening? > > thanks! > From eryksun at gmail.com Wed May 17 18:30:10 2017 From: eryksun at gmail.com (eryk sun) Date: Wed, 17 May 2017 22:30:10 +0000 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: On Wed, May 17, 2017 at 8:24 PM, Michael C wrote: > from PIL import Image > > im = Image.open('pic.bmp') > im.show() > > I ran this code and it not only opened the picture in paint, which is what > I want, but it also opens a CMD.exe console window! how do I prevent that from > happening? You're probably running a .py script that's associated with py.exe or python.exe. These executables create a new console (i.e. an instance of the console host process, conhost.exe), if they don't inherit one from their parent process. The new console defaults to creating a visible window. Change the file extension to .pyw. This file extension should be associated with pyw.exe or pythonw.exe, which will not create a console. FYI, the cmd shell is unrelated to that console window. Windows users often confuse the cmd shell with the console window that it uses. I suppose it's less confusing on Linux. Like cmd, bash will inherit the parent's terminal/console; however, it doesn't automatically spawn a terminal on Linux if the parent doesn't have one. (Getting that behavior on Windows requires the DETACHED_PROCESS creation flag.) Since Windows users typically run cmd.exe to get a command-line shell for running programs, they associate cmd.exe with the console window. It isn't obvious that other programs create consoles without any associated instance of cmd.exe. From mysecretrobotfactory at gmail.com Wed May 17 18:33:26 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Wed, 17 May 2017 15:33:26 -0700 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: Actually, that is the whole script! I didn't get used to have the cmd.exe window pop up at all, could it be something I did? Or, is there a way to suppress that from showing up? thanks! On Wed, May 17, 2017 at 3:30 PM, eryk sun wrote: > On Wed, May 17, 2017 at 8:24 PM, Michael C > wrote: > > from PIL import Image > > > > im = Image.open('pic.bmp') > > im.show() > > > > I ran this code and it not only opened the picture in paint, which is > what > > I want, but it also opens a CMD.exe console window! how do I prevent > that from > > happening? > > You're probably running a .py script that's associated with py.exe or > python.exe. These executables create a new console (i.e. an instance > of the console host process, conhost.exe), if they don't inherit one > from their parent process. The new console defaults to creating a > visible window. Change the file extension to .pyw. This file extension > should be associated with pyw.exe or pythonw.exe, which will not > create a console. > > FYI, the cmd shell is unrelated to that console window. Windows users > often confuse the cmd shell with the console window that it uses. I > suppose it's less confusing on Linux. Like cmd, bash will inherit the > parent's terminal/console; however, it doesn't automatically spawn a > terminal on Linux if the parent doesn't have one. (Getting that > behavior on Windows requires the DETACHED_PROCESS creation flag.) > Since Windows users typically run cmd.exe to get a command-line shell > for running programs, they associate cmd.exe with the console window. > It isn't obvious that other programs create consoles without any > associated instance of cmd.exe. > From alan.gauld at yahoo.co.uk Wed May 17 19:04:02 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 May 2017 00:04:02 +0100 Subject: [Tutor] Bug In-Reply-To: References: Message-ID: On 17/05/17 17:26, Grace Sanford wrote: > with "board" with elements 0-8). If one of these is the case, it is > suppose to print the "You won" string. Nevertheless, when I change list > variable to reflect one of these conditions, there is no printing > occurring. I cannot figure out why. You need to show us the whole code including the print statements. Also, do you get an error message? If so please include the full message. As it stands the formatting is all messed up but unless you have it all on one line (or 3 lines, I just noticed the \ chars...) I suspect you will get a syntax error? To fix that you can put parentheses round the expression: if ( board[0:3]==["X", "X", "X"] or board[3:6]==["X", "X", "X"] or board[6:9]==["X", "X", "X"] or [board[0],board[3],board[6]]==["X", "X", "X"] or [board[1],board[4],board[7]]==["X", "X", "X"] or [board[2],board[5],board[8]]==["X", "X", "X"] or [board[0],board[4],board[8]]==["X", "X", "X"] or [board[2],board[4],board[6]]==["X", "X", "X"] ): Have you tried breaking it down in the interpreter? Try an if statement with just the first three lines? Then the last two? etc That will help identify where the problem lies. At a casual glance I can't see any issues with the code above. -- 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 Wed May 17 19:09:20 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 May 2017 00:09:20 +0100 Subject: [Tutor] Help - What is the best package to learn programming in Python? In-Reply-To: References: Message-ID: On 17/05/17 19:17, keith quach wrote: > I hope you could help. I am new to the Python community. I am looking > for your recommendation for a Windows 10 (64 bit) Python 3.6 > distribution package that covers all the necessary installtions/files. It depends on what you want to do. There is no single package I know of that includes *all* the Python modules available - there are too many and many are out of sync with different versions. If you do scientific/numeric computations you probably want something like Anaconda or Enthought. If you do media/video work you might want some of the distros targetting Maya or similar. Of the "standard" distributions I usually recommend the ActiveState.com distro because it includes some extra Windows goodies and integrates with the help system better. If you need an IDE you will need to check those out separately, there are at least half a dozen, some free, some commercial. IDEs are a very personal choice, many Python programmers prefer not to use one but work with multiple open windows instead. -- 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 Wed May 17 19:11:51 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 May 2017 00:11:51 +0100 Subject: [Tutor] copy/paste/move files In-Reply-To: References: Message-ID: On 17/05/17 20:09, Michael C wrote: > How do I move files to a designated folder or copy/paste? copy/paste is a UI specific thing, you don't do that in Python code. What you do is either copy a file or move it. The shutil module provides easy functions for both. See the documentation for the module. -- 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 Wed May 17 19:13:37 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 May 2017 00:13:37 +0100 Subject: [Tutor] How do I display a picture? In-Reply-To: References: Message-ID: On 17/05/17 21:33, Michael C wrote: > How do I display a picture? What do you mean? What kind of picture? Where do you want it displayed (what kind of window/screen)? There are a dozen possible ways to "display a picture" depending on what you specifically want. -- 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 akleider at sonic.net Wed May 17 19:11:35 2017 From: akleider at sonic.net (Alex Kleider) Date: Wed, 17 May 2017 16:11:35 -0700 Subject: [Tutor] copy/paste/move files In-Reply-To: References: Message-ID: On 2017-05-17 12:09, Michael C wrote: > Hi all, > > How do I move files to a designated folder or copy/paste? > The first hit when I googled "how to move a file using python" was http://stackoverflow.com/questions/8858008/how-to-move-a-file-in-python which in turn suggests the use of: os.rename() or shutil.move() depending on whether you need only to rename or to actually move a file. (The unix mv utility is used to do both but the 'twofor' is not available in Python to my knowledge.) Don't forget to import os or import shutil depending on your needs. From alan.gauld at yahoo.co.uk Wed May 17 19:38:41 2017 From: alan.gauld at yahoo.co.uk (Alan G) Date: Thu, 18 May 2017 00:38:41 +0100 Subject: [Tutor] Fwd: Re: How do I display a picture? Message-ID: Please always use reply all, or reply list when responding to list messages. I think eryksun may have already answered your question, if not give more info about how you run your code. -------- Original Message -------- Subject: Re: [Tutor] How do I display a picture? From: Michael C To: Alan Gauld CC: tutor at python.org I use python image library and apparently when I do im = Image.open('1.png') im.show() show() actually opens a empty console window that doesn't do anything and as long as it's around the script hangs. Currently I have to close it by clicking on the close button and it makes my script useless. so **i am trying find either a way to prevent that from poping up or to close it automatically somehow or to find another way to display the picture without using python image library. On Wed, May 17, 2017 at 4:13 PM, Alan Gauld via Tutor <[1]tutor at python.org> wrote: On 17/05/17 21:33, Michael C wrote: > How do I display a picture? What do you mean? What kind of picture? Where do you want it displayed (what kind of window/screen)? There are a dozen possible ways to "display a picture" depending on what you specifically want. -- Alan G Author of the Learn to Program web site [2]http://www.alan-g.me.uk/ [3]http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: [4]http://www.flickr.com/photos/alangauldphotos _______________________________________________ Tutor maillist** -** [5]Tutor at python.org To unsubscribe or change subscription options: [6]https://mail.python.org/mailman/listinfo/tutor References Visible links 1. mailto:tutor at python.org 2. http://www.alan-g.me.uk/ 3. http://www.amazon.com/author/alan_gauld 4. http://www.flickr.com/photos/alangauldphotos 5. mailto:Tutor at python.org 6. https://mail.python.org/mailman/listinfo/tutor From alan.gauld at yahoo.co.uk Wed May 17 18:54:52 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 17 May 2017 23:54:52 +0100 Subject: [Tutor] Help finishing a function In-Reply-To: References: Message-ID: Please use Reply-All or Reply-List when replying to the list, otherwise it only goes to me. On 17/05/17 17:21, Grace Sanford wrote: > Syntactically speaking, how can I check if an element in the list > "board" at position p equals "_" and then change that element to "0"? You can use the == operator: if board[index] == "_": board[index] = "O" else: # report an error? return false? -- 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 mysecretrobotfactory at gmail.com Wed May 17 19:17:20 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Wed, 17 May 2017 16:17:20 -0700 Subject: [Tutor] copy/paste/move files In-Reply-To: References: Message-ID: Ok! On Wed, May 17, 2017 at 4:11 PM, Alan Gauld via Tutor wrote: > On 17/05/17 20:09, Michael C wrote: > > > How do I move files to a designated folder or copy/paste? > > copy/paste is a UI specific thing, you don't do that in Python code. > What you do is either copy a file or move it. > > The shutil module provides easy functions for both. > See the documentation for the module. > > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mysecretrobotfactory at gmail.com Wed May 17 19:24:52 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Wed, 17 May 2017 16:24:52 -0700 Subject: [Tutor] How do I display a picture? In-Reply-To: References: Message-ID: I use python image library and apparently when I do im = Image.open('1.png') im.show() show() actually opens a empty console window that doesn't do anything and as long as it's around the script hangs. Currently I have to close it by clicking on the close button and it makes my script useless. so i am trying find either a way to prevent that from poping up or to close it automatically somehow or to find another way to display the picture without using python image library. On Wed, May 17, 2017 at 4:13 PM, Alan Gauld via Tutor wrote: > On 17/05/17 21:33, Michael C wrote: > > > How do I display a picture? > > What do you mean? What kind of picture? > Where do you want it displayed (what kind > of window/screen)? > > There are a dozen possible ways to "display a picture" > depending on what you specifically want. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Wed May 17 20:53:31 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 May 2017 01:53:31 +0100 Subject: [Tutor] Fwd: Re: How do I display a picture? In-Reply-To: References: Message-ID: On 18/05/17 00:38, Alan G via Tutor wrote: > Please always use reply all, or reply list when responding to list Oops, it seems like you did that already, my apologies. -- 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 Wed May 17 21:03:58 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 May 2017 02:03:58 +0100 Subject: [Tutor] How do I display a picture? In-Reply-To: References: Message-ID: On 18/05/17 00:24, Michael C wrote: > or to find another way to display the picture without using python image > library. There are lots of ways it depends on what you actually want to do with the image. For example you can run your favourite image viewer program(that looks like what PIL is doing) Or you could create a temporary html file with an img tag pointing at the image, then open your browser on that file. Or you can create a simple GUI and put the image into a canvas component. Or you could use a different image editing module like the ImageMagik module(forgotten the name) or Blender. It just depends what you need it for which solution suits. -- 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 Wed May 17 21:06:05 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 May 2017 02:06:05 +0100 Subject: [Tutor] Pyuthon 3 Combine 1 D Arrays In-Reply-To: <591CA980.10705@sbcglobal.net> References: <591CA980.10705@sbcglobal.net> Message-ID: On 17/05/17 20:50, Stephen P. Molnar wrote: > I'm beginning to think that I don't know how to ask the question as > Google and Stack Overflow have resulted in nothing. All of the results > seem to deal with integers. Have you tried asking on the scipy forum? https://www.scipy.org/scipylib/mailing-lists.html -- 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 cs at zip.com.au Wed May 17 18:00:22 2017 From: cs at zip.com.au (Cameron Simpson) Date: Thu, 18 May 2017 08:00:22 +1000 Subject: [Tutor] Bug In-Reply-To: References: Message-ID: <20170517220022.GA56595@cskk.homeip.net> On 17May2017 12:26, Grace Sanford wrote: >Theoretically, the following code is suppose to check if the user has won a >tic tac toe game by checking if there are all "X"s in either the >horizontal, vertical, or diagonal lines of a grid (represented by a list >with "board" with elements 0-8). If one of these is the case, it is >suppose to print the "You won" string. Nevertheless, when I change list >variable to reflect one of these conditions, there is no printing >occurring. I cannot figure out why. > >if board[0:3]==["X", "X", "X"] or board[3:6]==["X", "X", "X"] or >board[6:9]==["X", "X", "X"] or \ > [board[0],board[3],board[6]]==["X", "X", "X"] or >[board[1],board[4],board[7]]==["X", "X", "X"] or >[board[2],board[5],board[8]] ==["X", "X", "X"] or \ > [board[0],board[4],board[8]]==["X", "X", "X"] or >[board[2],board[4],board[6]]==["X", "X", "X"]: Please post complete code, and the output (I accept that in your case the output is empty). For example: board = [ "X", "X", "X", "", "", "", "", "", "" ] if board[0:3]==["X", "X", "X"] or board[3:6]==["X", "X", "X"] or board[6:9]==["X", "X", "X"] or \ [board[0],board[3],board[6]]==["X", "X", "X"] or [board[1],board[4],board[7]]==["X", "X", "X"] or [board[2],board[5],board[8]] ==["X", "X", "X"] or \ [board[0],board[4],board[8]]==["X", "X", "X"] or [board[2],board[4],board[6]]==["X", "X", "X"]: print("ROW!") so that proeple can reproduce your problem. For example, it may be that some winning positions do work and some don't, and you've tested only a failing combination. The example I have above prints "ROW!" for me, and it is just your own code with a specific combination. Cheers, Cameron Simpson From eryksun at gmail.com Wed May 17 21:51:41 2017 From: eryksun at gmail.com (eryk sun) Date: Thu, 18 May 2017 01:51:41 +0000 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: On Wed, May 17, 2017 at 10:33 PM, Michael C wrote: > On Wed, May 17, 2017 at 3:30 PM, eryk sun wrote: > >> You're probably running a .py script that's associated with py.exe or >> python.exe. These executables create a new console (i.e. an instance >> of the console host process, conhost.exe), if they don't inherit one >> from their parent process. The new console defaults to creating a >> visible window. Change the file extension to .pyw. This file extension >> should be associated with pyw.exe or pythonw.exe, which will not >> create a console. >> >> FYI, the cmd shell is unrelated to that console window. Windows users >> often confuse the cmd shell with the console window that it uses. I >> suppose it's less confusing on Linux. Like cmd, bash will inherit the >> parent's terminal/console; however, it doesn't automatically spawn a >> terminal on Linux if the parent doesn't have one. (Getting that >> behavior on Windows requires the DETACHED_PROCESS creation flag.) >> Since Windows users typically run cmd.exe to get a command-line shell >> for running programs, they associate cmd.exe with the console window. >> It isn't obvious that other programs create consoles without any >> associated instance of cmd.exe. > > Actually, that is the whole script! I didn't get used to have the cmd.exe > window pop up at all, could it be something I did? Changing the script's extension to .pyw didn't prevent the console from appearing? Or did you not try it? Also, to reiterate, the cmd shell doesn't create or own any window, and unless something really weird is going on, there's no cmd.exe instance associated with the console window that you're seeing. cmd can use a console via standard I/O File handles, and usually does, but not always. It's no different from python.exe, powershell.exe, or any other console application. Really, there is no such thing as a "cmd window", "python window", or "PowerShell window". Because these are long-running shell processes (e.g. Python's REPL), people are inclined to think in those terms, and that's fine, but would you call it a "where.exe window", "chcp.com window", "doskey.exe window", "whoami.exe window", "findstr.exe window"? I think not. It's clear that these programs simply use the console; they don't own it. Neither do shells, but in the case of shells and other long-running console processes, we're loose with language for convenience -- as long as it doesn't confuse our understanding of how things really work. From eryksun at gmail.com Wed May 17 22:52:42 2017 From: eryksun at gmail.com (eryk sun) Date: Thu, 18 May 2017 02:52:42 +0000 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: On Thu, May 18, 2017 at 1:58 AM, Michael C wrote: > when I run this, while it's called test.pyw, this pops up > > from PIL import Image > > im = Image.open('1.bmp') > im.show() Ok, I stand corrected. It's something weird, and most likely due to Windows support here being an afterthought throw in just for coverage, instead of something anyone actually cared about. The code does indeed use os.system to show the file, which is just about the worst thing one can do here for a Windows viewer. >>> print(inspect.getsource(ImageShow.WindowsViewer)) class WindowsViewer(Viewer): format = "BMP" def get_command(self, file, **options): return ('start "Pillow" /WAIT "%s" ' '&& ping -n 2 127.0.0.1 >NUL ' '&& del /f "%s"' % (file, file)) >>> print(inspect.getsource(ImageShow.Viewer.show_file)) def show_file(self, file, **options): """Display given file""" os.system(self.get_command(file, **options)) return 1 The WindowsViewer class preferrably should override show_file to call ShellExecuteExW directly via ctypes, and wait (if possible) to delete the temporary file. Or at least use subprocess.call with shell=True, which will hide the console window. Note that being able to wait on the image viewer is not guaranteed. For example, the image viewer in Windows 10 is an app that cannot be waited on. Since you already have a file on disk, you could skip PIL completely. Just use os.startfile('1.bmp'). From robertvstepp at gmail.com Wed May 17 23:03:34 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Wed, 17 May 2017 22:03:34 -0500 Subject: [Tutor] Given a string, call a function of that name In-Reply-To: References: Message-ID: On Wed, May 17, 2017 at 1:42 AM, Peter Otten <__peter__ at web.de> wrote: > boB Stepp wrote: >> Oh, and I suppose I should ask for a critique of the code as written >> for appropriate Python style, proper targeted function use, etc. I am >> always eager to learn! > >> if function in valid_fcns: >> return True >> else: >> return False > > should really be spelt > > return function in valid_fcns > > and personally I wouldn't mind to type the few extra chars to make it > > return function in valid_functions [snip] >> Hmm. It bothers me that in check_fcn_input() I have a list valid_fcns >> and in run_fcn() I have a dictionary fcn_dict. These contain >> essentially the same information. Would this be a case for a global >> function dictionary (Which I could also use to check for valid >> functions.) or perhaps a class which only exists to have this function >> dictionary? > > A global is indeed better than the duplicate information in your list and > dict. Here's another option, return the function instead of information > about its existence: I keep forgetting that this is doable! I like this idea and implemented it. Here -- for posterity and/or further critiquing -- is my revised code: ========================================================================= #!/usr/bin/env python3 def spam(phrase): print('spam function said: ', phrase) def ham(phrase): print('ham function said: ', phrase) def eggs(phrase): print('eggs function said: ', phrase) def get_input(): function_name = input('Which do you want: spam, ham or eggs?\n') phrase = input('\nHow do you want your food to be prepared?\n') return function_name, phrase def find_function(function_name): valid_functions = {'spam': spam, 'ham': ham, 'eggs': eggs} function = valid_functions.get(function_name) return function def main(): while True: function_name, phrase = get_input() function = find_function(function_name) if function: break print("You made an invalid food choice! Let's try this again!") function(phrase) if __name__ == '__main__': main() ========================================================================= I cleaned up the naming a bit. I think it reads more clearly now, and spells out "function" fully. Also, the overall LOC is fewer, while retaining the same functionality. Thanks Peter and Alan!! BTW, my son is now merrily calling his functions using this dictionary technique. He seems to like it and said that he now understands both functions, returns and dictionaries better after adapting these ideas to his own code. Now if I can only get him to adopt unit testing and version control! -- boB From __peter__ at web.de Thu May 18 03:07:22 2017 From: __peter__ at web.de (Peter Otten) Date: Thu, 18 May 2017 09:07:22 +0200 Subject: [Tutor] Python 3 Combine 1 D Arrays References: <591CA980.10705@sbcglobal.net> Message-ID: Stephen P. Molnar wrote: > I'm beginning to think that I don't know how to ask the question as > Google and Stack Overflow have resulted in nothing. All of the results > seem to deal with integers. > > I have a number of single column floating point arrays each containing > 300 entries that I want to combine into a n by 300 array where n is the > number of single column arrays. > > I have tried zip, np.concatenate etc with only failure. > > Thanks in advance. > Googling for numpy combine 1d arrays into 2d array I get http://stackoverflow.com/questions/21322564/numpy-list-of-1d-arrays-to-2d-array as the first hit. Does that help? The second hit http://stackoverflow.com/questions/17710672/create-2-dimensional-array-with-2-one-dimensional-array should also work, and the best approach is probably #3: https://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.column_stack.html >>> import numpy as np >>> a = np.array([1.2, 3.4, 5.6]) >>> b = np.array([10.11, 12.13, 14.15]) >>> np.column_stack((a, b)) array([[ 1.2 , 10.11], [ 3.4 , 12.13], [ 5.6 , 14.15]]) No rocket science ;) From arj.python at gmail.com Thu May 18 01:34:36 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Thu, 18 May 2017 09:34:36 +0400 Subject: [Tutor] Help - What is the best package to learn programming in Python? In-Reply-To: References: Message-ID: for IDE, you have pycharm which works well i use Wing IDE personal. Nice for me. Don't forget to check the in-built IDLE tutorialspoint for python 2 or 3 is a nice quick reference. Learning how to do things the python way helps to ease jobs. else, python has become more than an average general purpose lang. It even has an astrophysics library (astropy), as well as data science and image processing libraries. It is used in industries for internal workings and production as well . So, investing in python really pays off ! P.s. Learn it from the begining well. e.g. the print in python also takes optional arguments as well. . . ..what to print _sep _end _std output _flush print('to print', sep=' ', end=' ', file=open('file.txt','w'), flush=true) so, better learn python well ! Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 18 May 2017 03:09, "Alan Gauld via Tutor" wrote: > On 17/05/17 19:17, keith quach wrote: > > > I hope you could help. I am new to the Python community. I am looking > > for your recommendation for a Windows 10 (64 bit) Python 3.6 > > distribution package that covers all the necessary installtions/files. > > It depends on what you want to do. There is no single package I > know of that includes *all* the Python modules available - there > are too many and many are out of sync with different versions. > > If you do scientific/numeric computations you probably want > something like Anaconda or Enthought. If you do media/video > work you might want some of the distros targetting Maya or > similar. > > Of the "standard" distributions I usually recommend the > ActiveState.com distro because it includes some extra > Windows goodies and integrates with the help system better. > > If you need an IDE you will need to check those out separately, > there are at least half a dozen, some free, some commercial. > IDEs are a very personal choice, many Python programmers > prefer not to use one but work with multiple open windows > instead. > > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mysecretrobotfactory at gmail.com Wed May 17 21:15:54 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Wed, 17 May 2017 18:15:54 -0700 Subject: [Tutor] How do I display a picture? In-Reply-To: References: Message-ID: If all PIL does with .show() is to invoke the system default image viewer, why would it pop an empty console window? the existence of this window causes the script to hang, so i have to so far manually close it before the script would continue to run. On Wed, May 17, 2017 at 6:03 PM, Alan Gauld via Tutor wrote: > On 18/05/17 00:24, Michael C wrote: > > > or to find another way to display the picture without using python image > > library. > > > There are lots of ways it depends on what you actually want > to do with the image. For example you can run your favourite > image viewer program(that looks like what PIL is doing) > > Or you could create a temporary html file with an img tag > pointing at the image, then open your browser on that file. > > Or you can create a simple GUI and put the image into a > canvas component. > > Or you could use a different image editing module like > the ImageMagik module(forgotten the name) or Blender. > > It just depends what you need it for which solution suits. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mysecretrobotfactory at gmail.com Wed May 17 21:58:41 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Wed, 17 May 2017 18:58:41 -0700 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: when I run this, while it's called test.pyw, this pops up from PIL import Image im = Image.open('1.bmp') im.show() [image: Inline image 1] On Wed, May 17, 2017 at 6:51 PM, eryk sun wrote: > On Wed, May 17, 2017 at 10:33 PM, Michael C > wrote: > > On Wed, May 17, 2017 at 3:30 PM, eryk sun wrote: > > > >> You're probably running a .py script that's associated with py.exe or > >> python.exe. These executables create a new console (i.e. an instance > >> of the console host process, conhost.exe), if they don't inherit one > >> from their parent process. The new console defaults to creating a > >> visible window. Change the file extension to .pyw. This file extension > >> should be associated with pyw.exe or pythonw.exe, which will not > >> create a console. > >> > >> FYI, the cmd shell is unrelated to that console window. Windows users > >> often confuse the cmd shell with the console window that it uses. I > >> suppose it's less confusing on Linux. Like cmd, bash will inherit the > >> parent's terminal/console; however, it doesn't automatically spawn a > >> terminal on Linux if the parent doesn't have one. (Getting that > >> behavior on Windows requires the DETACHED_PROCESS creation flag.) > >> Since Windows users typically run cmd.exe to get a command-line shell > >> for running programs, they associate cmd.exe with the console window. > >> It isn't obvious that other programs create consoles without any > >> associated instance of cmd.exe. > > > > Actually, that is the whole script! I didn't get used to have the cmd.exe > > window pop up at all, could it be something I did? > > Changing the script's extension to .pyw didn't prevent the console > from appearing? Or did you not try it? > > Also, to reiterate, the cmd shell doesn't create or own any window, > and unless something really weird is going on, there's no cmd.exe > instance associated with the console window that you're seeing. cmd > can use a console via standard I/O File handles, and usually does, but > not always. It's no different from python.exe, powershell.exe, or any > other console application. Really, there is no such thing as a "cmd > window", "python window", or "PowerShell window". Because these are > long-running shell processes (e.g. Python's REPL), people are inclined > to think in those terms, and that's fine, but would you call it a > "where.exe window", "chcp.com window", "doskey.exe window", > "whoami.exe window", "findstr.exe window"? I think not. It's clear > that these programs simply use the console; they don't own it. Neither > do shells, but in the case of shells and other long-running console > processes, we're loose with language for convenience -- as long as it > doesn't confuse our understanding of how things really work. > From alan.gauld at yahoo.co.uk Thu May 18 04:55:20 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 May 2017 09:55:20 +0100 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: On 18/05/17 02:58, Michael C wrote: > when I run this, while it's called test.pyw, this pops up > > from PIL import Image > > im = Image.open('1.bmp') > im.show() One suggestion is to use Pillow instead of PIL. So far as I know PIL is frozen and all new development is on Pillow. It is backwardly compatible although for this case that hardly matters! But it's just possible that this is fixed in Pillow? But if this is all you are using PIL for then its overkill and you would be much better off sing a different approach. But that depends on what you are trying to achieve. -- 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 s.molnar at sbcglobal.net Thu May 18 08:07:04 2017 From: s.molnar at sbcglobal.net (Stephen P. Molnar) Date: Thu, 18 May 2017 08:07:04 -0400 Subject: [Tutor] Pyuthon 3 Combine 1 D Arrays In-Reply-To: <995f1213-1a12-1d3f-8f4f-f14c25053a94@gmail.com> References: <591CA980.10705@sbcglobal.net> <995f1213-1a12-1d3f-8f4f-f14c25053a94@gmail.com> Message-ID: <591D8E68.9080409@sbcglobal.net> On 05/17/2017 06:15 PM, Luis JM Amoreira wrote: > Hi, > If your arrays have shape (300,1) (single column, 300 rows right?) then > M = numpy.hstack((array1, array2)) is an array with shape (300, 2) (300 > rows, 2 columns) and M.T is 2 by 300 [shape (2, 300)]. Take an example: > > In [11]: import numpy as np > > In [12]: a1=np.random.rand(3,1) > > In [13]: a1 > Out[13]: > array([[ 0.09042866], > [ 0.63008665], > [ 0.99106757]]) > > In [14]: a2=np.random.rand(3,1) > > In [15]: np.hstack((a1,a2)) > Out[15]: > array([[ 0.09042866, 0.99965848], > [ 0.63008665, 0.12334957], > [ 0.99106757, 0.43502637]]) > > In [16]: np.hstack((a1,a2)).T > Out[16]: > array([[ 0.09042866, 0.63008665, 0.99106757], > [ 0.99965848, 0.12334957, 0.43502637]]) > > Hope this helps! > ze > > > On 05/17/2017 08:50 PM, Stephen P. Molnar wrote: >> I'm beginning to think that I don't know how to ask the question as >> Google and Stack Overflow have resulted in nothing. All of the >> results seem to deal with integers. >> >> I have a number of single column floating point arrays each containing >> 300 entries that I want to combine into a n by 300 array where n is >> the number of single column arrays. >> >> I have tried zip, np.concatenate etc with only failure. >> >> Thanks in advance. >> >> >> >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > Thanks for your reply. Here's what I get: Traceback (most recent call last): File "", line 1, in runfile('/home/comp/Apps/Python/Molecular_Transforms/Basic/MolT_app_1.py', wdir='/home/comp/Apps/Python/Molecular_Transforms/Basic') File "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 880, in runfile execfile(filename, namespace) File "/home/comp/Apps/anaconda3/lib/python3.6/site-packages/spyder/utils/site/sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace) File "/home/comp/Apps/Python/Molecular_Transforms/Basic/MolT_app_1.py", line 289, in f.write("\n".join("".join(map(str, x)) for x in (name_I_app))) File "/home/comp/Apps/Python/Molecular_Transforms/Basic/MolT_app_1.py", line 289, in f.write("\n".join("".join(map(str, x)) for x in (name_I_app))) TypeError: 'numpy.float64' object is not iterable However, numpy.column_stack works. Problem solved. -- Stephen P. Molnar, Ph.D. Life is a fuzzy set www.molecular-modeling.net Stochastic and multivariate (614)312-7528 (c) Skype: smolnar1 From mysecretrobotfactory at gmail.com Thu May 18 11:14:00 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 18 May 2017 08:14:00 -0700 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: Did you go into the source code of PIL/Pillow? Awesome!!! On Wed, May 17, 2017 at 7:52 PM, eryk sun wrote: > On Thu, May 18, 2017 at 1:58 AM, Michael C > wrote: > > when I run this, while it's called test.pyw, this pops up > > > > from PIL import Image > > > > im = Image.open('1.bmp') > > im.show() > > Ok, I stand corrected. It's something weird, and most likely due to > Windows support here being an afterthought throw in just for coverage, > instead of something anyone actually cared about. The code does indeed > use os.system to show the file, which is just about the worst thing > one can do here for a Windows viewer. > > >>> print(inspect.getsource(ImageShow.WindowsViewer)) > class WindowsViewer(Viewer): > format = "BMP" > > def get_command(self, file, **options): > return ('start "Pillow" /WAIT "%s" ' > '&& ping -n 2 127.0.0.1 >NUL ' > '&& del /f "%s"' % (file, file)) > > >>> print(inspect.getsource(ImageShow.Viewer.show_file)) > def show_file(self, file, **options): > """Display given file""" > os.system(self.get_command(file, **options)) > return 1 > > The WindowsViewer class preferrably should override show_file to call > ShellExecuteExW directly via ctypes, and wait (if possible) to delete > the temporary file. Or at least use subprocess.call with shell=True, > which will hide the console window. Note that being able to wait on > the image viewer is not guaranteed. For example, the image viewer in > Windows 10 is an app that cannot be waited on. > > Since you already have a file on disk, you could skip PIL completely. > Just use os.startfile('1.bmp'). > From mysecretrobotfactory at gmail.com Thu May 18 11:14:29 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 18 May 2017 08:14:29 -0700 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: Oh I have been using Pillow 4.0 the whole time alright, sorry I forgot to mention it. On Thu, May 18, 2017 at 1:55 AM, Alan Gauld via Tutor wrote: > On 18/05/17 02:58, Michael C wrote: > > when I run this, while it's called test.pyw, this pops up > > > > from PIL import Image > > > > im = Image.open('1.bmp') > > im.show() > > One suggestion is to use Pillow instead of PIL. > So far as I know PIL is frozen and all new development > is on Pillow. It is backwardly compatible although for > this case that hardly matters! But it's just possible > that this is fixed in Pillow? > > But if this is all you are using PIL for then its > overkill and you would be much better off sing a > different approach. But that depends on what you > are trying to achieve. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mysecretrobotfactory at gmail.com Thu May 18 11:43:38 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 18 May 2017 08:43:38 -0700 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: os.startfile('1.bmp') works like a charm! Now I need to figure out how to close this window once I finish with it! On Thu, May 18, 2017 at 8:14 AM, Michael C wrote: > Oh I have been using Pillow 4.0 the whole time alright, sorry I forgot to > mention it. > > On Thu, May 18, 2017 at 1:55 AM, Alan Gauld via Tutor > wrote: > >> On 18/05/17 02:58, Michael C wrote: >> > when I run this, while it's called test.pyw, this pops up >> > >> > from PIL import Image >> > >> > im = Image.open('1.bmp') >> > im.show() >> >> One suggestion is to use Pillow instead of PIL. >> So far as I know PIL is frozen and all new development >> is on Pillow. It is backwardly compatible although for >> this case that hardly matters! But it's just possible >> that this is fixed in Pillow? >> >> But if this is all you are using PIL for then its >> overkill and you would be much better off sing a >> different approach. But that depends on what you >> are trying to achieve. >> >> -- >> 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 at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > > From alan.gauld at yahoo.co.uk Thu May 18 13:06:35 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 18 May 2017 18:06:35 +0100 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: On 18/05/17 16:43, Michael C wrote: > os.startfile('1.bmp') > > works like a charm! > > Now I need to figure out how to close this window once I finish with it! Sadly that is manual. Unless you care to write code to search for the process and use the Windows API to kill it off. If you really want the image under your programs control you need to build a GUI, even if a very basic one and display the image in a window. A very simplistic Tkinter app will do for simply splatting an image on screen then closing it later. But it all depends what else you need the app to do how complex it gets after that. Here is some untested Tkinter code to display an image for 2 seconds: import tkinter as tk top = tk.Tk() tk.Label(top, image='1.bmp').pack() top.after(2000, top.quit) top.mainloop() If not that, something quite like it... -- 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 mysecretrobotfactory at gmail.com Thu May 18 12:55:12 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 18 May 2017 09:55:12 -0700 Subject: [Tutor] deleting elements of a dictionary Message-ID: 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? thanks! From mysecretrobotfactory at gmail.com Thu May 18 13:13:29 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 18 May 2017 10:13:29 -0700 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: I'll use it when I get to it! Thanks! For now, I use this, as suggested by eryk sun: os.startfile('1.bmp') it doesn't pop the window. Thanks Alan! On Thu, May 18, 2017 at 10:06 AM, Alan Gauld via Tutor wrote: > On 18/05/17 16:43, Michael C wrote: > > os.startfile('1.bmp') > > > > works like a charm! > > > > Now I need to figure out how to close this window once I finish with it! > > Sadly that is manual. Unless you care to write code to search for the > process and use the Windows API to kill it off. > > If you really want the image under your programs control you > need to build a GUI, even if a very basic one and display > the image in a window. A very simplistic Tkinter app will > do for simply splatting an image on screen then closing > it later. But it all depends what else you need the app to > do how complex it gets after that. > > Here is some untested Tkinter code to display an image > for 2 seconds: > > import tkinter as tk > > top = tk.Tk() > tk.Label(top, image='1.bmp').pack() > top.after(2000, top.quit) > > top.mainloop() > > If not that, something quite like it... > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From __peter__ at web.de Thu May 18 18:05:22 2017 From: __peter__ at web.de (Peter Otten) Date: Fri, 19 May 2017 00:05:22 +0200 Subject: [Tutor] deleting elements of a dictionary References: Message-ID: 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"} From leo.silver at soholinux.com.au Thu May 18 21:48:01 2017 From: leo.silver at soholinux.com.au (Leo Silver) Date: Fri, 19 May 2017 11:48:01 +1000 Subject: [Tutor] Collecting output from Python scripts executed via Cron Message-ID: 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. From george at fischhof.hu Fri May 19 03:25:45 2017 From: george at fischhof.hu (George Fischhof) Date: Fri, 19 May 2017 09:25:45 +0200 Subject: [Tutor] Collecting output from Python scripts executed via Cron In-Reply-To: References: Message-ID: 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 at 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 From mysecretrobotfactory at gmail.com Thu May 18 23:56:10 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 18 May 2017 20:56:10 -0700 Subject: [Tutor] deleting elements of a dictionary In-Reply-To: References: Message-ID: 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 <__peter__ at 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From __peter__ at web.de Fri May 19 05:11:29 2017 From: __peter__ at web.de (Peter Otten) Date: Fri, 19 May 2017 11:11:29 +0200 Subject: [Tutor] deleting elements of a dictionary References: Message-ID: 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. From alan.gauld at yahoo.co.uk Fri May 19 05:29:49 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 May 2017 10:29:49 +0100 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: 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 From alan.gauld at yahoo.co.uk Fri May 19 06:03:08 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 May 2017 11:03:08 +0100 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: 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 From __peter__ at web.de Fri May 19 10:15:23 2017 From: __peter__ at web.de (Peter Otten) Date: Fri, 19 May 2017 16:15:23 +0200 Subject: [Tutor] Python Image Library References: Message-ID: 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... From akleider at sonic.net Fri May 19 11:52:21 2017 From: akleider at sonic.net (Alex Kleider) Date: Fri, 19 May 2017 08:52:21 -0700 Subject: [Tutor] Collecting output from Python scripts executed via Cron In-Reply-To: References: Message-ID: <25cc07c9d87d9eb15011966e3a3b50e7@sonic.net> 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From mysecretrobotfactory at gmail.com Fri May 19 10:23:02 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 19 May 2017 07:23:02 -0700 Subject: [Tutor] deleting elements of a dictionary In-Reply-To: References: Message-ID: list(read_dictionary) converts the dictionary into a list right? How can you save the list as a dictionary? Thanks! From alan.gauld at yahoo.co.uk Fri May 19 13:13:51 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 May 2017 18:13:51 +0100 Subject: [Tutor] Python Image Library In-Reply-To: References: Message-ID: 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 From alan.gauld at yahoo.co.uk Fri May 19 13:17:03 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Fri, 19 May 2017 18:17:03 +0100 Subject: [Tutor] deleting elements of a dictionary In-Reply-To: References: Message-ID: 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 From mats at wichmann.us Fri May 19 16:54:14 2017 From: mats at wichmann.us (Mats Wichmann) Date: Fri, 19 May 2017 14:54:14 -0600 Subject: [Tutor] deleting elements of a dictionary In-Reply-To: References: Message-ID: 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. From mysecretrobotfactory at gmail.com Fri May 19 16:21:00 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 19 May 2017 13:21:00 -0700 Subject: [Tutor] always on top Message-ID: 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! From mysecretrobotfactory at gmail.com Fri May 19 16:46:20 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Fri, 19 May 2017 13:46:20 -0700 Subject: [Tutor] always on top Message-ID: nvm, the problem went away on its own :) From cs at zip.com.au Sat May 20 06:04:30 2017 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 20 May 2017 20:04:30 +1000 Subject: [Tutor] Collecting output from Python scripts executed via Cron In-Reply-To: References: Message-ID: <20170520100430.GA49644@cskk.homeip.net> On 19May2017 11: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. > >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. You know that cron jobs _are_ shell commands? Is there anything wrong with either: ..... your-program.py | mail -s subject-line you at example.com Or, if you're using Vixie cron (very very common), set MAILTO in your crontab and let cron do the work: MAILTO=you at example.com ....... your-program.py Obviously "......." is your cron schedule values. Cheers, Cameron Simpson From leo.silver at soholinux.com.au Sat May 20 09:31:22 2017 From: leo.silver at soholinux.com.au (Leo Silver) Date: Sat, 20 May 2017 23:31:22 +1000 Subject: [Tutor] Collecting output from Python scripts executed via Cron In-Reply-To: <20170520100430.GA49644@cskk.homeip.net> References: <20170520100430.GA49644@cskk.homeip.net> Message-ID: Thanks, I'll give this ago and explore the full logging option if I need more control. Regards, Leo. On 20 May 2017 at 20:04, Cameron Simpson wrote: > On 19May2017 11: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. >> >> 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. >> > > You know that cron jobs _are_ shell commands? > > Is there anything wrong with either: > > ..... your-program.py | mail -s subject-line you at example.com > > Or, if you're using Vixie cron (very very common), set MAILTO in your > crontab and let cron do the work: > > MAILTO=you at example.com > ....... your-program.py > > Obviously "......." is your cron schedule values. > > Cheers, > Cameron Simpson > From mats at wichmann.us Sat May 20 09:05:03 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sat, 20 May 2017 07:05:03 -0600 Subject: [Tutor] deleting elements of a dictionary In-Reply-To: References: Message-ID: On 05/19/2017 02:54 PM, Mats Wichmann wrote: > 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. Having now done a quick check, mydict.pop() is no better for this case. Here's a simplistic sample that does work: d = { 100:3, 200:4, 111:5, 222:5, 333:5, 500:6, } print "original: ", d new = {key:value for (key,value) in d.iteritems() if value != 5} print "new: ", new From mysecretrobotfactory at gmail.com Mon May 22 12:11:56 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Mon, 22 May 2017 09:11:56 -0700 Subject: [Tutor] recursion Message-ID: hi all: I have a function to return (x,y) value, but sometimes it would naturally unable to return those 2 values properly. I know what recursion is, and I think all I got to do is to call this function a 2nd time and the problem would go away. How do I do recursion? The function basically look like this def return_xy(): blah blah blah return x,y From alan.gauld at yahoo.co.uk Mon May 22 17:16:40 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 22 May 2017 22:16:40 +0100 Subject: [Tutor] recursion In-Reply-To: References: Message-ID: On 22/05/17 17:11, Michael C wrote: > I have a function to return (x,y) value, but sometimes it would naturally > unable to return those 2 values properly. I know what recursion is, and I > think all I got to do is to call this function a 2nd time and the problem > would go away. Sorry, but that is too vague to help us in any way. Recursion is not about calling the function a second time, it is about calling the function from within the same function. How or whether that would help solve your problem is not at all obvious. You need to ideally show us your code or at least give us more information about what is going wrong. What kind of values are you expecting back? What do you mean by "naturally unable to return those"? Why not? What does it return instead? How are the values being returned(or calculated/obtained)? > How do I do recursion? The function basically look like this > > def return_xy(): > blah blah blah > return x,y That looks like any function that takes no inputs and returns a tuple. It does not help us to help you in any way. The fact that it takes no inputs and does not apparently modify any state variables means recursion would be pointless - in fact it would lead to an infinite loop and lock up your program. We need more detail, and unless you have a good reason not to, you should just show us your code. We are ok with you posting anything up to, say, 100 lines long... -- 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 mysecretrobotfactory at gmail.com Mon May 22 17:21:07 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Mon, 22 May 2017 14:21:07 -0700 Subject: [Tutor] recursion In-Reply-To: References: Message-ID: hi all: oh ya, my function does in fact take no input and doesn't change anything, and all i wanted to was to call itself a 2nd time, yes, so I solved it a few hours back ,and it's good enough for me for now :) Thanks for the response!!! On Mon, May 22, 2017 at 2:16 PM, Alan Gauld via Tutor wrote: > On 22/05/17 17:11, Michael C wrote: > > > I have a function to return (x,y) value, but sometimes it would naturally > > unable to return those 2 values properly. I know what recursion is, and I > > think all I got to do is to call this function a 2nd time and the problem > > would go away. > > Sorry, but that is too vague to help us in any way. > Recursion is not about calling the function a second time, > it is about calling the function from within the same function. > How or whether that would help solve your problem is not at > all obvious. > > You need to ideally show us your code or at least give us more > information about what is going wrong. > What kind of values are you expecting back? > What do you mean by "naturally unable to return those"? > Why not? What does it return instead? > How are the values being returned(or calculated/obtained)? > > > > How do I do recursion? The function basically look like this > > > > def return_xy(): > > blah blah blah > > return x,y > > That looks like any function that takes no inputs and returns > a tuple. It does not help us to help you in any way. > > The fact that it takes no inputs and does not apparently > modify any state variables means recursion would be pointless > - in fact it would lead to an infinite loop and lock up your > program. > > We need more detail, and unless you have a good reason not > to, you should just show us your code. We are ok with you > posting anything up to, say, 100 lines long... > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From __peter__ at web.de Tue May 23 01:18:44 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 23 May 2017 07:18:44 +0200 Subject: [Tutor] recursion References: Message-ID: Michael C wrote: > oh ya, my function does in fact take no input and doesn't change anything, > and all i wanted to was to call itself a 2nd time, yes, so I solved it a > few hours back ,and it's good enough for me for now :) Would you mind showing the code? I'd like to see how you avoid infinite recursion. From mysecretrobotfactory at gmail.com Tue May 23 03:38:05 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Tue, 23 May 2017 00:38:05 -0700 Subject: [Tutor] recursion In-Reply-To: References: Message-ID: no i don't have a way, it just hasn't happened yet LOL On Mon, May 22, 2017 at 10:18 PM, Peter Otten <__peter__ at web.de> wrote: > Michael C wrote: > > > oh ya, my function does in fact take no input and doesn't change > anything, > > and all i wanted to was to call itself a 2nd time, yes, so I solved it a > > few hours back ,and it's good enough for me for now :) > > Would you mind showing the code? I'd like to see how you avoid infinite > recursion. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From alan.gauld at yahoo.co.uk Tue May 23 04:27:45 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 23 May 2017 09:27:45 +0100 Subject: [Tutor] recursion In-Reply-To: References: Message-ID: On 23/05/17 06:18, Peter Otten wrote: > Michael C wrote: > >> oh ya, my function does in fact take no input and doesn't change anything, >> and all i wanted to was to call itself a 2nd time, yes, so I solved it a >> few hours back ,and it's good enough for me for now :) > > Would you mind showing the code? I'd like to see how you avoid infinite > recursion. I suspect he is not using recursion, just calling his function twice. -- 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 __peter__ at web.de Tue May 23 13:37:40 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 23 May 2017 19:37:40 +0200 Subject: [Tutor] recursion References: Message-ID: Alan Gauld via Tutor wrote: > On 23/05/17 06:18, Peter Otten wrote: >> Michael C wrote: >> >>> oh ya, my function does in fact take no input and doesn't change >>> anything, and all i wanted to was to call itself a 2nd time, yes, so I >>> solved it a few hours back ,and it's good enough for me for now :) >> >> Would you mind showing the code? I'd like to see how you avoid infinite >> recursion. > > I suspect he is not using recursion, just calling his function twice. I was hoping for something creative ;) From rafael.knuth at gmail.com Tue May 23 13:38:37 2017 From: rafael.knuth at gmail.com (Rafael Knuth) Date: Tue, 23 May 2017 19:38:37 +0200 Subject: [Tutor] Looping through Dictionaries Message-ID: I wrote a function (shopping list) which calculates the total price of the purchase (quantity * price) as well as the stock change (stock - quantity). I know the latter is imperfect (my function does not take into account if items are out of stock for example, but that's my next challenge. The function does exactly what it's supposed to do: def shopping(quantity, price, stock): total = 0 for key in quantity: total += quantity[key] * price[key] stock_update = stock[key] - quantity[key] print(stock_update) print(total) quantity = { "bapple": 10, "banana": 20 } price = { "bapple": 5, "banana": 3 } stock = { "bapple": 20, "banana": 50 } shopping(quantity, price, stock) Now, I want to print the item next to the stock update, and I am receiving an error message. I couldn't figure out how to fix that: def shopping(quantity, price, stock): total = 0 for key, value in quantity.items(): total += quantity[value] * price[value] stock_update = stock[value] - quantity[value] print(key, stock_update) print(total) quantity = { "bapple": 10, "banana": 20 } price = { "bapple": 5, "banana": 3 } stock = { "bapple": 20, "banana": 30 } shopping(quantity, price, stock) From alan.gauld at yahoo.co.uk Tue May 23 14:45:36 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 23 May 2017 19:45:36 +0100 Subject: [Tutor] Looping through Dictionaries In-Reply-To: References: Message-ID: On 23/05/17 18:38, Rafael Knuth wrote: > Now, I want to print the item next to the stock update, and I am > receiving an error message. I couldn't figure out how to fix that: So show us the error message, all of it. I can't begin to guess what it might be. UI also don't understand what "print the item next to the stock update" means. Do you mean the actual location of the data in the printed string? Or do you mean the value(which?) next to the one in your function? Can you show us some examples? > def shopping(quantity, price, stock): > total = 0 > for key, value in quantity.items(): > total += quantity[value] * price[value] > stock_update = stock[value] - quantity[value] I don't see how this would work. value is the value from the dictionary which is an integer. But you are using it as a key which should be a string? So in your first iteration key,value will be key = 'bapple', value=10 so you are trying to evaluate stock[10]-quantity[10] but thee are no such items. I suspect you mean: stock['bapple'] - quantity['bapple'] or in your function: stock[key] - quantity[key] But in that case you don;t need the value from items() you can just iterate over the keys: for item in quantity: ... stock_update = stock[item] - quantity[item] print(item, stock_update) > print(key, stock_update) > print(total) > > quantity = { > "bapple": 10, > "banana": 20 > } > > price = { > "bapple": 5, > "banana": 3 > } > > stock = { > "bapple": 20, > "banana": 30 > } > > shopping(quantity, price, stock) I'm not clear on what these variables indicate. What is the difference between quantity and stock? Also this might be a good time to look at classes and objects. You could avoid all the multiple stores by putting the various data elements in a class. This would avoid the potential headaches caused by mis-typing the item names in each store, for example. You could then have variables bapple and banana and access, for example, bapple.quantity and bapple.price. And just keep a simple list of items. Just a thought... -- 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 mats at wichmann.us Tue May 23 14:47:25 2017 From: mats at wichmann.us (Mats Wichmann) Date: Tue, 23 May 2017 12:47:25 -0600 Subject: [Tutor] Looping through Dictionaries In-Reply-To: References: Message-ID: On 05/23/2017 11:38 AM, Rafael Knuth wrote: > I wrote a function (shopping list) which calculates the total price of > the purchase (quantity * price) as well as the stock change (stock - > quantity). I know the latter is imperfect (my function does not take > into account if items are out of stock for example, but that's my next > challenge. The function does exactly what it's supposed to do: > > def shopping(quantity, price, stock): > total = 0 > for key in quantity: > total += quantity[key] * price[key] > stock_update = stock[key] - quantity[key] > print(stock_update) > print(total) > > quantity = { > "bapple": 10, > "banana": 20 > } > > price = { > "bapple": 5, > "banana": 3 > } > > stock = { > "bapple": 20, > "banana": 50 > } > > shopping(quantity, price, stock) > > Now, I want to print the item next to the stock update, and I am > receiving an error message. I couldn't figure out how to fix that: in the code below you're trying to use "value" as your key, but it isn't the key. Why did you change that from the code above? it helps if you include the errors, by the way :) > > def shopping(quantity, price, stock): > total = 0 > for key, value in quantity.items(): > total += quantity[value] * price[value] > stock_update = stock[value] - quantity[value] > print(key, stock_update) > print(total) > > quantity = { > "bapple": 10, > "banana": 20 > } > > price = { > "bapple": 5, > "banana": 3 > } > > stock = { > "bapple": 20, > "banana": 30 > } > > shopping(quantity, price, stock) > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mysecretrobotfactory at gmail.com Tue May 23 16:07:50 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Tue, 23 May 2017 13:07:50 -0700 Subject: [Tutor] real time response Message-ID: hi all: I have a code that takes about 20 seconds to complete, but I also need to response to events (the appearance of red dots on a canvas) so I could place a few lines to check for this condition like this def do_stuff: blah blah check() blah blah blah check() blah blah blah check() blah and then either break or return if condition is met? But that just isn't responsive enough. Is there a way to make function check for the event every 0.5 seconds without interfering with the do_stuff, ie a code that has its' own space? then something interrupts and closes down do_stuff and run another function in response? thanks! From alan.gauld at yahoo.co.uk Wed May 24 04:30:20 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Wed, 24 May 2017 09:30:20 +0100 Subject: [Tutor] real time response In-Reply-To: References: Message-ID: On 23/05/17 21:07, Michael C wrote: > def do_stuff: > blah > check() > blah > check() > blah > check() > blah > > and then either break or return if condition is met? > > But that just isn't responsive enough. Is there a way to make function > check for the event every 0.5 seconds without interfering with the > do_stuff, ie a code that has its' own space? Yes. What you have just described is called threading. You can start a separate sub-process running in the background to "do stuff" while in your main program (thread) you can monitor the dots. If your condition is met you can close the sub-thread. Threading is non trivial however and very easy to get wrong. I suggest you google for some tutorials and maybe watch a Youtube video or two to get a feel for it. -- 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 sergio_r at mail.com Wed May 24 14:10:24 2017 From: sergio_r at mail.com (Sergio Rojas) Date: Wed, 24 May 2017 20:10:24 +0200 Subject: [Tutor] deleting elements of a dictionary (Mats Wichmann) In-Reply-To: References: Message-ID: Having now done a quick check, mydict.pop() is no better for this case. Here's a simplistic sample that does work: d = { 100:3, 200:4, 111:5, 222:5, 333:5, 500:6, } print "original: ", d new = {key:value for (key,value) in d.iteritems() if value != 5} print "new: ", new =========================== """ In this case, you ned to specify by hand the entry you want to delete. In case you want to do it without looking at the stuff, here is one way paraphrasing the stackoverflow post https://stackoverflow.com/questions/20672238/find-dictionary-keys-with-duplicate-values """ # d = { 100:3, 200:4, 111:5, 222:5, 333:5, 500:6, 50:7, 60:7, } print ("original: ", d) rev_multidict = {} for key, value in d.items(): rev_multidict.setdefault(value, set()).add(key) print(rev_multidict) v = [values for key, values in rev_multidict.items() if len(values) > 1] print('\t Set of keys with same value ', v) #[print(i) for element in v for i in list(element)] [d.pop(i) for element in v for i in list(element)] print('d with every repeated stuff deleted: ', d) Sergio Enhance your #MachineLearning and #BigData skills via #Python #SciPy 1) https://www.packtpub.com/big-data-and-business-intelligence/numerical-and-scientific-computing-scipy-video 2) https://www.amazon.com/Learning-Numerical-Scientific-Computing-Second/dp/1783987707/ From juan0christian at gmail.com Wed May 24 18:10:23 2017 From: juan0christian at gmail.com (Juan C.) Date: Wed, 24 May 2017 19:10:23 -0300 Subject: [Tutor] How to deploy seamless script updates to your "clients"? Message-ID: I have some Python 3.6.0 scripts that my co-workers use for some small and medium tasks. Whenever I have time I fix some bugs and add some features to said scripts to make their lives (and mine :D) easier, but there's a problem: I need to send a new script via email/chat/whatever and they have to replace it wherever they use it, such a hassle. How would I go to put a "update module" inside my script? I was thinking about using Git, because those scripts are already in personal repositories so that I can keep track of changes. Will I have to setup any special permissions so that the scripts can pull requests from my private repositories? I was thinking of something like "check for update before start", if an update is found the script would replace itself with the newer version and restart, is that possible? For example, 'cool-tool.py' v0.2 starts and find that version v0.3 is out, so it downloads and replace 'cool-tool.py' code with newer code and restart as v0.3. From arj.python at gmail.com Thu May 25 00:01:18 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Thu, 25 May 2017 08:01:18 +0400 Subject: [Tutor] How to deploy seamless script updates to your "clients"? In-Reply-To: References: Message-ID: a basic idea would be to get a webpage and put your code there. This is where you edit your codes Now you make a program which has - an updater - the script to execute in a separate file the updater each times pull the program on the webpage and compare it with the script file if they are the same, it does nothing. if not, it moves the current script to the "previous scripts" folder and create a new script file it then executes the script file (see how to execute python from python) the only thing you'll have to do if you want is some auth if your script is super secret Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 25 May 2017 4:11 am, "Juan C." wrote: > I have some Python 3.6.0 scripts that my co-workers use for some small > and medium tasks. Whenever I have time I fix some bugs and add some > features to said scripts to make their lives (and mine :D) easier, but > there's a problem: I need to send a new script via email/chat/whatever > and they have to replace it wherever they use it, such a hassle. > > How would I go to put a "update module" inside my script? I was > thinking about using Git, because those scripts are already in > personal repositories so that I can keep track of changes. Will I have > to setup any special permissions so that the scripts can pull requests > from my private repositories? > > I was thinking of something like "check for update before start", if > an update is found the script would replace itself with the newer > version and restart, is that possible? For example, 'cool-tool.py' > v0.2 starts and find that version v0.3 is out, so it downloads and > replace 'cool-tool.py' code with newer code and restart as v0.3. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From mats at wichmann.us Wed May 24 20:59:17 2017 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 24 May 2017 18:59:17 -0600 Subject: [Tutor] How to deploy seamless script updates to your "clients"? In-Reply-To: References: Message-ID: <5ab79bd5-a740-e6cf-d9fe-6ff02c690a2b@wichmann.us> On 05/24/2017 04:10 PM, Juan C. wrote: > I have some Python 3.6.0 scripts that my co-workers use for some small > and medium tasks. Whenever I have time I fix some bugs and add some > features to said scripts to make their lives (and mine :D) easier, but > there's a problem: I need to send a new script via email/chat/whatever > and they have to replace it wherever they use it, such a hassle. > > How would I go to put a "update module" inside my script? I was > thinking about using Git, because those scripts are already in > personal repositories so that I can keep track of changes. Will I have > to setup any special permissions so that the scripts can pull requests > from my private repositories? > > I was thinking of something like "check for update before start", if > an update is found the script would replace itself with the newer > version and restart, is that possible? For example, 'cool-tool.py' > v0.2 starts and find that version v0.3 is out, so it downloads and > replace 'cool-tool.py' code with newer code and restart as v0.3. Yes, this is definitely a problem others have built solutions for. It differs a bit depending on whether the script to potentially be updated is a long-running one or one that can afford to "check on startup", but not actually that much. You can use os.execv to restart a script, there are several permutations but they look something like (pick the bits you need, might not need all of it) args = sys.argv[:] args.insert(0, sys.executable) os.chdir(foo) # foo being a saved copy of the directory you start in, in case the program changes directories while running os.execv(sys.executable, args) that's the easy part, though. self-updating software is tricky... much easier if you're just talking about a single file, but then what if updating fails, of if the update itself is broken? Then the user is left with a non-working setup; you'll have to make sure there's a way to get back to working that they can figure out how to operate. For a single file you could put on github, or a local server of course, and on update check download the file (requests module, perhaps), decide if that's a new version, if so replace self and restart. If that's too expensive you can put in a timer - if it's been a week since the last update (for example), do the download-and-check. If neither of those work, you'll have to build an API that can query versions... that's more work for you, but maybe worth it. I think there's an update checker program somewhere on PyPi, but maybe it only works for things located on PyPi? From skgoyal721 at gmail.com Thu May 25 08:15:23 2017 From: skgoyal721 at gmail.com (shubham goyal) Date: Thu, 25 May 2017 17:45:23 +0530 Subject: [Tutor] airflow dag Message-ID: He guys, I want to ask that can we pass the parameters as commandline arguments in airflow when we are triggering the dag and access them inside the dag's python script/file. script: from airflow import DAG from datetime import datetime,timedelta default_args = { 'owner': 'airflow', 'depends_on_past': False, 'start_date': datetime.now(), 'email': ['airflow at airflow.com'], 'email_on_failure': False, 'email_on_retry': False } MAIN_DAG='check_dag' dag = DAG(dag_id=MAIN_DAG, default_args=default_args, schedule_interval=None) with open(file, "r") as f: payload = f.read() # Reading the json data from a file SimpleHttpOperator( # creating cluster using SimpleHttpOperator task_id='cluster_create', method='POST', http_conn_id='qubole_default', # for directing to https://qa.qubole.net/api endpoint='/v2/clusters?auth_token=%s' % (passwd), data=payload, headers={"Content-Type": "application/json"}, params={'auth_token': passwd}, response_check=lambda response: True if response.status_code == 200 else False, dag=dag ) like this here i am trying to create a cluster but i need to pass password as cli arguments when i trigger the dag. can we do that. please help. From alan.gauld at yahoo.co.uk Thu May 25 13:02:24 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Thu, 25 May 2017 18:02:24 +0100 Subject: [Tutor] airflow dag In-Reply-To: References: Message-ID: On 25/05/17 13:15, shubham goyal wrote: > He guys, > > I want to ask that can we pass the parameters as commandline arguments in > airflow when we are triggering the dag and access them inside the dag's > python script/file. I've no idea what a dag is. This list is for people learning Python as a programming language but it has no knowledge(necessarily) of other environments that use Python. So you need to tell us more. In generic terms Python can read command line arguments in the sys.argv list. So you could try adding a line like import sys ... print sys.argv And see if your arguments appear there. Otherwise you'll need to find a dsag forum and try asking there. Or get very lucky and find someone on this list that speaks dag. If they are present in sys.aergv then there are several modules that can parse the arguments looking for option flags etc. argparse is the current official recommendation. > script: > > from airflow import DAG > from datetime import datetime,timedelta > default_args = { > 'owner': 'airflow', > 'depends_on_past': False, > 'start_date': datetime.now(), > 'email': ['airflow at airflow.com'], > 'email_on_failure': False, > 'email_on_retry': False > } > MAIN_DAG='check_dag' > dag = DAG(dag_id=MAIN_DAG, default_args=default_args, > schedule_interval=None) > > with open(file, "r") as f: > payload = f.read() # Reading the json data from a file > SimpleHttpOperator( # creating cluster using SimpleHttpOperator > task_id='cluster_create', > method='POST', > http_conn_id='qubole_default', > # for directing to https://qa.qubole.net/api > endpoint='/v2/clusters?auth_token=%s' % (passwd), > data=payload, > headers={"Content-Type": "application/json"}, > params={'auth_token': passwd}, > response_check=lambda response: True if response.status_code == 200 > else False, > dag=dag I'm not sure what you think the line above does but in normal Python it would have zero effect. > like this here i am trying to create a cluster but i need to pass password > as cli arguments when i trigger the dag. can we do that. please help. You need to explain what you mean bearing in mind we don't know anything about dag, or clusters. -- 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 __peter__ at web.de Thu May 25 13:17:31 2017 From: __peter__ at web.de (Peter Otten) Date: Thu, 25 May 2017 19:17:31 +0200 Subject: [Tutor] airflow dag References: Message-ID: shubham goyal wrote: > He guys, > > I want to ask that can we pass the parameters as commandline arguments in > airflow when we are triggering the dag and access them inside the dag's > python script/file. > script: > like this here i am trying to create a cluster but i need to pass password > as cli arguments when i trigger the dag. can we do that. please help. Have a look at argparse: Provided (1) you are OK with the password being echoed when you type it (2) the password doesn't require escaping to be a valid part of the `endpoint` argument the code might look like this: > from airflow import DAG > from datetime import datetime,timedelta import argparse parser = argparse.ArgumentParser() parser.add_argument("passwd") args = parser.parse_args() > default_args = { > 'owner': 'airflow', > 'depends_on_past': False, > 'start_date': datetime.now(), > 'email': ['airflow at airflow.com'], > 'email_on_failure': False, > 'email_on_retry': False > } > MAIN_DAG='check_dag' > dag = DAG(dag_id=MAIN_DAG, default_args=default_args, > schedule_interval=None) > > with open(file, "r") as f: > payload = f.read() # Reading the json data from a file > SimpleHttpOperator( # creating cluster using SimpleHttpOperator > task_id='cluster_create', > method='POST', > http_conn_id='qubole_default', > # for directing to https://qa.qubole.net/api endpoint='/v2/clusters?auth_token=%s' % args.passwd, > data=payload, > headers={"Content-Type": "application/json"}, params={'auth_token': args.passwd}, > response_check=lambda response: True if response.status_code == > 200 > else False, > dag=dag > ) > To address (1) there is (2) can be solved with From __peter__ at web.de Thu May 25 13:22:26 2017 From: __peter__ at web.de (Peter Otten) Date: Thu, 25 May 2017 19:22:26 +0200 Subject: [Tutor] airflow dag References: Message-ID: Alan Gauld via Tutor wrote: >>dag=dag > > I'm not sure what you think the line above does but > in normal Python it would have zero effect. The context makes it a keyword argument. dag = DAG(...) ... SimpleHttpOperator( ... dag=dag ) From mysecretrobotfactory at gmail.com Thu May 25 14:06:26 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 25 May 2017 11:06:26 -0700 Subject: [Tutor] threading tutorial Message-ID: Hi all: I tried to google for tutorials of threading, but they are all equally confusing. Does someone know of a good page or a book that talk about threading? thanks! From mysecretrobotfactory at gmail.com Thu May 25 14:52:05 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 25 May 2017 11:52:05 -0700 Subject: [Tutor] threading tutorial In-Reply-To: References: Message-ID: Right now all i need is to grab 3 values from 3 variables before killing a thread, like this: def stuff(): do stuff, get values, (x,y,d) # main code startthread(stuff(), blah) # if else need to sleep or kill the thread, and because I'll restart the thread later, I'd like to get the values from the thread, say x,y,d in order to restart the thread. loop. Therefore, how do I get a few values from a few variables from the thread and then close it? Threading is very new to me, so I have to be very diligent. Thanks! On Thu, May 25, 2017 at 11:06 AM Michael C wrote: > Hi all: > > I tried to google for tutorials of threading, but they are all > equally confusing. > Does someone know of a good page or a book that talk about threading? > > > thanks! > From cs at zip.com.au Thu May 25 18:03:59 2017 From: cs at zip.com.au (Cameron Simpson) Date: Fri, 26 May 2017 08:03:59 +1000 Subject: [Tutor] threading tutorial In-Reply-To: References: Message-ID: <20170525220359.GA44584@cskk.homeip.net> On 25May2017 11:52, Michael C wrote: >Right now all i need is to grab 3 values from 3 variables before killing a >thread, like this: > >def stuff(): > do stuff, > get values, (x,y,d) > ># main code >startthread(stuff(), blah) ># if else need to sleep or kill the thread, and because I'll restart the >thread later, I'd like to get the values from the thread, say x,y,d in >order to restart the thread. >loop. > >Therefore, how do I get a few values from a few variables from the thread >and then close it? > >Threading is very new to me, so I have to be very diligent. You always need to be diligent with threads :-) Can you explain why you need to use a thread for this? The first cut of your program looks like you could do it with a ordinary function: def stuff(): ... compute x, y, z ... return x, y, z def main(): x, y, z = stuff() OTOH, your later description suggests that you want to kick off a thread to work on something, and have your main program let it run, or pause it. The implication is that x, y, z represent the thread state allowing you to restart it from scratch at the same point where you paused/stopped things. There are a few different ways to manage that scenario. But first, write yourself a small Thread based program to familiarise yourself with threads. For example (untested): from __future__ import print_function from time import sleep from threading import Thread def thread_main_body(): print("thread started") for n in range(20): print("thread", n) sleep(0.3) print("thread done") def main(): print("main") T = Thread(target=thread_main_body) print("main: Thread created but _not_ started") sleep(1) T.start() print("thread started") for n in range(10): print("main", n) sleep(0.4) print("main program waiting for thread") T.join() print("main program done") You should see the main thread and your subthread outputs interleaved. The sleeps are just to ensure some interleaving and to give good interactive feel. It should run for about 8 seconds overall. Make sure you're happy you understand what is happening, why, and when. There are a few things you need to keep in mind with threads (in Python, and to a degree in other languages): 1: You can't kill/stop a Thread. Instead, the usual approach to to share some state with some kind of "running" flag, a boolean saying that the Thread's function should continue. Then the thread polls that regularly. Eg, if the thread function runs a main loop it might look like this: def fn(): global run_me while run_me: ... do some work ... and then elsewhere you go: global run_me run_me = True ... create and start the Thread ... ... later ... run_me = False T.join() so effectively you ask the Thread to stop, and it obeys when it notices the change to "run_me". Using a global for this is pretty crube, and not the general approach, BTW. 2: Like any other function, the local varaibles to the thread function are not available outside. Thus the "global" hack above. So to share state you would usually make some kind of object with the state, and pass it in to the Thread when you create and start it: def fn(state): while state.run_me: ... do stuff, keep important things like results in "state" ... state.x = 1 state.y = whatever class State(object): pass def main(): state = State() state.run_me = True T = Thread(target=fn, args=(state,)) T.start() for n in range(10): print("main", n, "x =", state.x, "y =", state.y) sleep(0.3) state.run_me = False T.join() As I remarked, there are a few ways to approach your scenario. The above should get you started on one approach. Pausing can be done in a few ways, either by starting and stopping individual threads, one after another, or by starting one thread and using a mutex of some kind to cause it to suspend activity when needed. Yet another approach is corroutines, but I'd recommend getting threading understood first to avoid confusion. Come back woith some functioning code and more questions. Cheers, Cameron Simpson From mysecretrobotfactory at gmail.com Thu May 25 22:47:09 2017 From: mysecretrobotfactory at gmail.com (Michael C) Date: Thu, 25 May 2017 19:47:09 -0700 Subject: [Tutor] threading tutorial In-Reply-To: <20170525220359.GA44584@cskk.homeip.net> References: <20170525220359.GA44584@cskk.homeip.net> Message-ID: message received, i ll take a look tomorrow asap. thanks for replying!!! On Thu, May 25, 2017 at 3:03 PM, Cameron Simpson wrote: > On 25May2017 11:52, Michael C wrote: > >> Right now all i need is to grab 3 values from 3 variables before killing a >> thread, like this: >> >> def stuff(): >> do stuff, >> get values, (x,y,d) >> >> # main code >> startthread(stuff(), blah) >> # if else need to sleep or kill the thread, and because I'll restart the >> thread later, I'd like to get the values from the thread, say x,y,d in >> order to restart the thread. >> loop. >> >> Therefore, how do I get a few values from a few variables from the thread >> and then close it? >> >> Threading is very new to me, so I have to be very diligent. >> > > You always need to be diligent with threads :-) > > Can you explain why you need to use a thread for this? The first cut of > your program looks like you could do it with a ordinary function: > > def stuff(): > ... compute x, y, z ... > return x, y, z > > def main(): > x, y, z = stuff() > > OTOH, your later description suggests that you want to kick off a thread > to work on something, and have your main program let it run, or pause it. > The implication is that x, y, z represent the thread state allowing you to > restart it from scratch at the same point where you paused/stopped things. > > There are a few different ways to manage that scenario. But first, write > yourself a small Thread based program to familiarise yourself with threads. > For example (untested): > > from __future__ import print_function > from time import sleep > from threading import Thread > > def thread_main_body(): > print("thread started") > for n in range(20): > print("thread", n) > sleep(0.3) > print("thread done") > > def main(): > print("main") > T = Thread(target=thread_main_body) > print("main: Thread created but _not_ started") > sleep(1) > T.start() > print("thread started") > for n in range(10): > print("main", n) > sleep(0.4) > print("main program waiting for thread") > T.join() > print("main program done") > > You should see the main thread and your subthread outputs interleaved. The > sleeps are just to ensure some interleaving and to give good interactive > feel. It should run for about 8 seconds overall. Make sure you're happy > you understand what is happening, why, and when. > > There are a few things you need to keep in mind with threads (in Python, > and to a degree in other languages): > > 1: You can't kill/stop a Thread. Instead, the usual approach to to share > some state with some kind of "running" flag, a boolean saying that the > Thread's function should continue. Then the thread polls that regularly. > Eg, if the thread function runs a main loop it might look like this: > > def fn(): > global run_me > while run_me: > ... do some work ... > > and then elsewhere you go: > > global run_me > run_me = True > ... create and start the Thread ... > ... later ... > run_me = False > T.join() > > so effectively you ask the Thread to stop, and it obeys when it notices > the change to "run_me". Using a global for this is pretty crube, and not > the general approach, BTW. > > 2: Like any other function, the local varaibles to the thread function are > not available outside. Thus the "global" hack above. So to share state you > would usually make some kind of object with the state, and pass it in to > the Thread when you create and start it: > > def fn(state): > while state.run_me: > ... do stuff, keep important things like results in "state" ... > state.x = 1 > state.y = whatever > > class State(object): > pass > > def main(): > state = State() > state.run_me = True > T = Thread(target=fn, args=(state,)) > T.start() > for n in range(10): > print("main", n, "x =", state.x, "y =", state.y) > sleep(0.3) > state.run_me = False > T.join() > > As I remarked, there are a few ways to approach your scenario. The above > should get you started on one approach. Pausing can be done in a few ways, > either by starting and stopping individual threads, one after another, or > by starting one thread and using a mutex of some kind to cause it to > suspend activity when needed. Yet another approach is corroutines, but I'd > recommend getting threading understood first to avoid confusion. > > Come back woith some functioning code and more questions. > > Cheers, > Cameron Simpson > From juan0christian at gmail.com Fri May 26 17:56:55 2017 From: juan0christian at gmail.com (Juan C.) Date: Fri, 26 May 2017 18:56:55 -0300 Subject: [Tutor] How to deploy seamless script updates to your "clients"? In-Reply-To: References: Message-ID: On Thu, May 25, 2017 at 1:01 AM, Abdur-Rahmaan Janhangeer wrote: > a basic idea would be to get a webpage and put your code there. This is > where you edit your codes > > Now you make a program which has > - an updater > - the script to execute in a separate file > > the updater each times pull the program on the webpage and compare it with > the script file if they are the same, it does nothing. if not, it moves the > current script to the "previous scripts" folder and create a new script file > > it then executes the script file (see how to execute python from python) > > the only thing you'll have to do if you want is some auth if your script is > super secret What does a webpage have to do with this, I really can't picture that. From alan.gauld at yahoo.co.uk Fri May 26 20:17:24 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sat, 27 May 2017 01:17:24 +0100 Subject: [Tutor] How to deploy seamless script updates to your "clients"? In-Reply-To: References: Message-ID: On 26/05/17 22:56, Juan C. wrote: >> a basic idea would be to get a webpage and put your code there. This is >> where you edit your codes > > What does a webpage have to do with this, I really can't picture that. I think it just means host the files on a web server so you can access them using http. You don't necessarily need a web page with HTML etc. But I'm only guessing at what was intended. -- 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 cs at zip.com.au Sat May 27 00:25:24 2017 From: cs at zip.com.au (Cameron Simpson) Date: Sat, 27 May 2017 14:25:24 +1000 Subject: [Tutor] airflow dag In-Reply-To: References: Message-ID: <20170527042524.GA14729@cskk.homeip.net> On 25May2017 18:02, Alan Gauld wrote: >On 25/05/17 13:15, shubham goyal wrote: >> I want to ask that can we pass the parameters as commandline arguments in >> airflow when we are triggering the dag and access them inside the dag's >> python script/file. > >I've no idea what a dag is. It's a directed acyclic graph, a very standard computer science term. Determining that that is what AirFlow means when it says "DAG" was surprisingly hard, but here we are: http://airflow.apache.org/ (So the hardness comes from a web search not dropping me on their front page:-) Looks like a tool for data driven computation using a graph of data pipelines. By restricting it to a DAG on can be certain the computation will finish and the termination condition is trivially satisfied. No feedback loops. Cheers, Cameron Simpson From mhashmi1979 at gmail.com Sat May 27 07:33:55 2017 From: mhashmi1979 at gmail.com (M Hashmi) Date: Sat, 27 May 2017 04:33:55 -0700 Subject: [Tutor] How to deploy seamless script updates to your "clients"? In-Reply-To: References: Message-ID: That's where Git or other version control systems come in. You can edit or upgrade creating a branch and when a branch tested at your side. You can push it with new tag like "some module changed". I guess this is how it works for everyone. All your team will get replicated code once you merge branch. Its simple and its reliable.....all your resource only may need to work around a dozen shell commands. Hope that help. On Wed, May 24, 2017 at 3:10 PM, Juan C. wrote: > I have some Python 3.6.0 scripts that my co-workers use for some small > and medium tasks. Whenever I have time I fix some bugs and add some > features to said scripts to make their lives (and mine :D) easier, but > there's a problem: I need to send a new script via email/chat/whatever > and they have to replace it wherever they use it, such a hassle. > > How would I go to put a "update module" inside my script? I was > thinking about using Git, because those scripts are already in > personal repositories so that I can keep track of changes. Will I have > to setup any special permissions so that the scripts can pull requests > from my private repositories? > > I was thinking of something like "check for update before start", if > an update is found the script would replace itself with the newer > version and restart, is that possible? For example, 'cool-tool.py' > v0.2 starts and find that version v0.3 is out, so it downloads and > replace 'cool-tool.py' code with newer code and restart as v0.3. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From jalenbarr1 at gmail.com Sat May 27 17:19:08 2017 From: jalenbarr1 at gmail.com (Jalen Barr) Date: Sat, 27 May 2017 16:19:08 -0500 Subject: [Tutor] Problem with if statements and else statements Message-ID: In this code it always changes the PlaceHolder to 0 no matter what Month is set to Month ="September" if Month == "January" or "1": PlaceHolder = 0 else: print("Information Error") print(PlaceHolder) From jalenbarr1 at gmail.com Sat May 27 17:23:12 2017 From: jalenbarr1 at gmail.com (Jalen Barr) Date: Sat, 27 May 2017 16:23:12 -0500 Subject: [Tutor] Problem with if statements and else statements In-Reply-To: References: Message-ID: I am in Python version 3.6.1 On Sat, May 27, 2017 at 4:19 PM, Jalen Barr wrote: > In this code it always changes the PlaceHolder to 0 no matter what Month > is set to > > Month ="September" > > if Month == "January" or "1": > PlaceHolder = 0 > else: > print("Information Error") > print(PlaceHolder) > > From robertvstepp at gmail.com Sat May 27 20:14:59 2017 From: robertvstepp at gmail.com (boB Stepp) Date: Sat, 27 May 2017 19:14:59 -0500 Subject: [Tutor] Problem with if statements and else statements In-Reply-To: References: Message-ID: Hello Jalen! On Sat, May 27, 2017 at 4:19 PM, Jalen Barr wrote: > > In this code it always changes the PlaceHolder to 0 no matter what Month is > set to > > Month ="September" > > if Month == "January" or "1": > PlaceHolder = 0 This must be written as: if Month == "January" or Month == "1": PlaceHolder = 0 The way you wrote it is interpreted as: if (Month == "January") or ("1"): PlaceHolder = 0 Hopefully you will see why you got the result you obtained. -- boB From skgoyal721 at gmail.com Sat May 27 23:37:10 2017 From: skgoyal721 at gmail.com (shubham goyal) Date: Sun, 28 May 2017 09:07:10 +0530 Subject: [Tutor] airflow dag In-Reply-To: <20170527042524.GA14729@cskk.homeip.net> References: <20170527042524.GA14729@cskk.homeip.net> Message-ID: Does anybody have answer? On May 27, 2017 1:53 PM, "Cameron Simpson" wrote: > On 25May2017 18:02, Alan Gauld wrote: > >> On 25/05/17 13:15, shubham goyal wrote: >> >>> I want to ask that can we pass the parameters as commandline arguments in >>> airflow when we are triggering the dag and access them inside the dag's >>> python script/file. >>> >> >> I've no idea what a dag is. >> > > It's a directed acyclic graph, a very standard computer science term. > > Determining that that is what AirFlow means when it says "DAG" was > surprisingly hard, but here we are: > > http://airflow.apache.org/ > > (So the hardness comes from a web search not dropping me on their front > page:-) > > Looks like a tool for data driven computation using a graph of data > pipelines. By restricting it to a DAG on can be certain the computation > will finish and the termination condition is trivially satisfied. No > feedback loops. > > Cheers, > Cameron Simpson > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From steve at pearwood.info Sun May 28 06:23:58 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 28 May 2017 20:23:58 +1000 Subject: [Tutor] airflow dag In-Reply-To: References: <20170527042524.GA14729@cskk.homeip.net> Message-ID: <20170528102358.GD23443@ando.pearwood.info> On Sun, May 28, 2017 at 09:07:10AM +0530, shubham goyal wrote: > Does anybody have answer? Answer to what question? Peter has already answered your question about passing parameters as command line arguments. Do you have another question? -- Steve From steve at pearwood.info Sun May 28 06:21:32 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Sun, 28 May 2017 20:21:32 +1000 Subject: [Tutor] airflow dag In-Reply-To: <20170527042524.GA14729@cskk.homeip.net> References: <20170527042524.GA14729@cskk.homeip.net> Message-ID: <20170528102132.GC23443@ando.pearwood.info> On Sat, May 27, 2017 at 02:25:24PM +1000, Cameron Simpson wrote: > On 25May2017 18:02, Alan Gauld wrote: > >On 25/05/17 13:15, shubham goyal wrote: > >>I want to ask that can we pass the parameters as commandline arguments in > >>airflow when we are triggering the dag and access them inside the dag's > >>python script/file. > > > >I've no idea what a dag is. > > It's a directed acyclic graph, a very standard computer science term. When spelled as "DAG". When spelled as "dag" in an email about airflow, it looks like a typo for "drag". Or possibly: a lock of matted or dung-coated wool; a person with a good sense of humour (Australian and New Zealand slang); a flap along the edge of a garment (used in medieval clothing). -- Steve From alan.gauld at yahoo.co.uk Sun May 28 09:46:39 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Sun, 28 May 2017 14:46:39 +0100 Subject: [Tutor] airflow dag In-Reply-To: References: <20170527042524.GA14729@cskk.homeip.net> Message-ID: On 28/05/17 04:37, shubham goyal wrote: > Does anybody have answer? You received two answers, both of which asked you to try something and get back to us for more information. Did you try printing sys.argv? What was the result? And did you try Peter's argparse code? You still haven't explained what your dag is? What library are you using? It may treat args in a non standard way, but we can't tell if you don't give us the information we need to help you. -- 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 francois.dion at gmail.com Sun May 28 10:41:34 2017 From: francois.dion at gmail.com (Francois Dion) Date: Sun, 28 May 2017 10:41:34 -0400 Subject: [Tutor] airflow dag In-Reply-To: References: <20170527042524.GA14729@cskk.homeip.net> Message-ID: My mailbox if full of similar stories: companies dumping airflow on their ETL (or similar) group. Those who knew Python succeeded, those who didn't failed, and some even moved to other companies because they couldn't cope with all this complexity dumped on them all at once. Moral of the story, it is best to learn python first, become good at it, then get into specific tools. And, of course, this is not specific to a relatively straightforward module / application like airflow. I've seen the same with scikit-learn. Same also in other languages. And to make matters worse, the curricula (and books) always starts with a crash course in a specific language, then go on and on about the application, perpetuating the myth that learning the programming language well is totally unimportant. Francois On Sun, May 28, 2017 at 9:46 AM, Alan Gauld via Tutor wrote: > On 28/05/17 04:37, shubham goyal wrote: > > Does anybody have answer? > > You received two answers, both of which asked > you to try something and get back to us for more > information. Did you try printing sys.argv? > What was the result? > > And did you try Peter's argparse code? > > You still haven't explained what your dag is? > What library are you using? It may treat args > in a non standard way, but we can't tell if > you don't give us the information we need to > help you. > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > -- raspberry-python.blogspot.com - www.pyptug.org - www.3DFutureTech.info - @f_dion From mats at wichmann.us Sun May 28 15:58:52 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 28 May 2017 13:58:52 -0600 Subject: [Tutor] How to deploy seamless script updates to your "clients"? In-Reply-To: References: Message-ID: On 05/27/2017 05:33 AM, M Hashmi wrote: > That's where Git or other version control systems come in. You can edit or > upgrade creating a branch and when a branch tested at your side. You can > push it with new tag like "some module changed". I guess this is how it > works for everyone. All your team will get replicated code once you merge > branch. Its simple and its reliable.....all your resource only may need to > work around a dozen shell commands. Letting git (or mercurial, or some other version control system) handle this is a nice approach. It doesn't have to be a public server, it can of course be an inside-company git server. Endorsed. === I did end up curious how hard it would be to write the check code, so after a little hunting around I put together a simple one (might become the source for a blog entry someday, or not). The way this one works is an update file in yaml format is in a Known Location (I used github), it's fetched, and saved in a local cache file. The update file describes whatever different products are covered, with a version, checksum, release date, download url, whatever else you need to describe. In the simpleminded version it just returns true/false as to whether an update is needed (probably would want to return the url to download in the "true" case). Each script could import this and call it with its name as the argument to see if it needs to update itself. It's not huge, but too big for an email; lives here if anyone is curious: https://github.com/mwichmann/PyBlog/tree/master/updater.d no warranties, etc. From mats at wichmann.us Sun May 28 16:13:16 2017 From: mats at wichmann.us (Mats Wichmann) Date: Sun, 28 May 2017 14:13:16 -0600 Subject: [Tutor] Problem with if statements and else statements In-Reply-To: References: Message-ID: On 05/27/2017 06:14 PM, boB Stepp wrote: > Hello Jalen! > > On Sat, May 27, 2017 at 4:19 PM, Jalen Barr wrote: >> >> In this code it always changes the PlaceHolder to 0 no matter what Month is >> set to >> >> Month ="September" >> >> if Month == "January" or "1": >> PlaceHolder = 0 > > This must be written as: > > if Month == "January" or Month == "1": > PlaceHolder = 0 > > The way you wrote it is interpreted as: > > if (Month == "January") or ("1"): > PlaceHolder = 0 and since "1" is always True, PlaceHolder is always set to 0. FWIW, if checking for multiples, you could also write: if Month in ['January', '1']: From tmrsg11 at gmail.com Sun May 28 13:58:22 2017 From: tmrsg11 at gmail.com (C W) Date: Sun, 28 May 2017 13:58:22 -0400 Subject: [Tutor] Counting a string backwards Message-ID: Dear Python list, I am having trouble understanding the following. If I do case 1, great = "Machine learning is awesome!" > print(great[-1]) > ! Now if I do case 2, > print(great[-3:-1]) > me Where did the exclamation mark go in case 2? I was told the count begins at zero, that's true going forward, but not backwards. What is wrong? Thank you! Mike From akleider at sonic.net Sun May 28 19:12:16 2017 From: akleider at sonic.net (Alex Kleider) Date: Sun, 28 May 2017 16:12:16 -0700 Subject: [Tutor] Problem with if statements and else statements In-Reply-To: References: Message-ID: <37957fb23b6f08d16c8531954b3e543b@sonic.net> On 2017-05-28 13:13, Mats Wichmann wrote: > FWIW, if checking for multiples, you could also write: > > if Month in ['January', '1']: Would >>> if Month in {'January', '1'}: be even better? (regarding efficiency perhaps? Trivial point, I know, but just wondering.) From alan.gauld at yahoo.co.uk Sun May 28 19:19:40 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 29 May 2017 00:19:40 +0100 Subject: [Tutor] Counting a string backwards In-Reply-To: References: Message-ID: On 28/05/17 18:58, C W wrote: > Now if I do case 2, >> print(great[-3:-1]) >> me > > Where did the exclamation mark go in case 2? > > I was told the count begins at zero, that's true going forward, but not > backwards. Its not about where the count starts its about where it finishes. It finishes 1 item before the second index. so in this case you get the items at -3 and -2. If you specify a third parameter you can change the direction of the count thus: >>> great[-1:-3:-1] '!e' So now you get the characters at -1 and -2 (ie in reverse order) If you want them in original order starting at -3 just omit the -1: >>> great[-3:] 'me!' HTH, -- 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 Sun May 28 20:17:34 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Mon, 29 May 2017 01:17:34 +0100 Subject: [Tutor] Problem with if statements and else statements In-Reply-To: <37957fb23b6f08d16c8531954b3e543b@sonic.net> References: <37957fb23b6f08d16c8531954b3e543b@sonic.net> Message-ID: On 29/05/17 00:12, Alex Kleider wrote: > On 2017-05-28 13:13, Mats Wichmann wrote: > >> FWIW, if checking for multiples, you could also write: >> >> if Month in ['January', '1']: > > Would > >>>> if Month in {'January', '1'}: > > be even better? (regarding efficiency perhaps? Trivial point, I know, > but just wondering.) If in doubt try it out and profile/time it. But I don't think it will make much difference since ultimately it still has to test each value (although a hashing algorithm may be involved that works on partial matches...) But if in doubt... -- 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 steve at pearwood.info Sun May 28 22:00:26 2017 From: steve at pearwood.info (Steven D'Aprano) Date: Mon, 29 May 2017 12:00:26 +1000 Subject: [Tutor] Counting a string backwards In-Reply-To: References: Message-ID: <20170529020026.GG23443@ando.pearwood.info> On Sun, May 28, 2017 at 01:58:22PM -0400, C W wrote: > Dear Python list, > > I am having trouble understanding the following. [...] The way to think about string indexing and slicing is that the index positions mark *between* the characters. Take your string: Machine learning is awesome! For brevity, I'll just use the first word: Machine Imagine slicing it between the characters. I'll mark the cuts with a vertical bar: |M|a|c|h|i|n|e| and add indexes. The indexes will only line correctly if you use a monspaced or fixed width font like Courier, otherwise things may not line up correctly. |M|a|c|h|i|n|e| 0 1 2 3 4 5 6 7 Here they are again starting from the right, I've spread things out a bit to fit in the minus signs: |M |a |c |h |i |n |e | -7 -6 -5 -4 -3 -2 -1 0 Notice that 0 gets used twice. Of course, that's impossible, because it would be ambiguous. If you give 0 as an index, how does Python know whether you mean 0 at the start or 0 or the end? So the simple rule Python uses is that 0 *always* means the start. When you give a single index, Python always uses the character immediately to the right of the cut: s = "Machine" s[0] => returns "M" s[-1] => returns "e" s[7] => raises an exception, because there is no character to the right When you give two indexes, using slice notation, Python returns the characters BETWEEN those cuts: s[0:7] => returns "Machine" s[1:-1] => returns "achin" Because 0 always means the start of the string, how do you slice to the end? You can use the length of the string (in this case, 7) or you can leave the ending position blank, and it defaults to the length of the string: s[1:] # means the same as [1:len(s)] You can leave the starting position blank too, it defaults to 0: s[:] # means the same as [0:len(s)] So remember that slices always cut *between* the index positions. Things get complicated when you include a step (or stride), especially when the step is negative. For step sizes other than 1, it is probably best to think of looping over the string: py> s = "Nobody expects the Spanish Inquisition!" py> s[-1:1:-2] '!otsun snp h tex db' is somewhat like: for i in range(len(s)-1, 1, -2): print s[i] -- Steve From __peter__ at web.de Mon May 29 01:36:11 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 29 May 2017 07:36:11 +0200 Subject: [Tutor] Problem with if statements and else statements References: <37957fb23b6f08d16c8531954b3e543b@sonic.net> Message-ID: Alex Kleider wrote: > On 2017-05-28 13:13, Mats Wichmann wrote: > >> FWIW, if checking for multiples, you could also write: >> >> if Month in ['January', '1']: > > Would > >>>> if Month in {'January', '1'}: > > be even better? (regarding efficiency perhaps? Trivial point, I know, > but just wondering.) Yes, according to timeit there is already a significant difference: $ python3 -m timeit -s 'month = "September"' 'month == "January" or month == "1"' 10000000 loops, best of 3: 0.126 usec per loop $ python3 -m timeit -s 'month = "September"' 'month in ["January", "1"]' 10000000 loops, best of 3: 0.108 usec per loop $ python3 -m timeit -s 'month = "September"' 'month in {"January", "1"}' 10000000 loops, best of 3: 0.0684 usec per loop As the size of the list/set grows you'll see the effects of O(N) versus O(1): $ python3 -m timeit -s 'N = 10**6; numbers = list(range(N))' 'N in numbers' 10 loops, best of 3: 32.5 msec per loop $ python3 -m timeit -s 'N = 10**6; numbers = set(range(N))' 'N in numbers' 10000000 loops, best of 3: 0.0921 usec per loop From __peter__ at web.de Mon May 29 01:54:10 2017 From: __peter__ at web.de (Peter Otten) Date: Mon, 29 May 2017 07:54:10 +0200 Subject: [Tutor] Counting a string backwards References: <20170529020026.GG23443@ando.pearwood.info> Message-ID: Steven D'Aprano wrote: > Because 0 always means the start of the string, how do you slice to the > end? You can use the length of the string (in this case, 7) or you can > leave the ending position blank, and it defaults to the length of the > string: For completeness, there's a third option, None, which is equivalent to a missing value: >>> "Machine!"[None:None] 'Machine!' Sometimes this is even moderately useful: >>> def clip(s, n): ... return s[:-n or None] ... >>> for i in reversed(range(5)): ... print(i, clip("Machine!", i)) ... 4 Mach 3 Machi 2 Machin 1 Machine 0 Machine! From skgoyal721 at gmail.com Mon May 29 02:55:46 2017 From: skgoyal721 at gmail.com (shubham goyal) Date: Mon, 29 May 2017 12:25:46 +0530 Subject: [Tutor] airflow dag In-Reply-To: References: <20170527042524.GA14729@cskk.homeip.net> Message-ID: Hello, See this is the code: from airflow import DAG from datetime import datetime,timedelta default_args = { 'owner': 'airflow', 'depends_on_past': False, 'start_date': datetime.now(), 'email': ['airflow at airflow.com'], 'email_on_failure': False, 'email_on_retry': False } MAIN_DAG='check_dag' dag = DAG(dag_id=MAIN_DAG, default_args=default_args, schedule_interval=None) with open(file, "r") as f: payload = f.read() # Reading the json data from a file SimpleHttpOperator( # creating cluster using SimpleHttpOperator task_id='cluster_create', method='POST', http_conn_id='qubole_default', # for directing to https://qa.qubole.net/api endpoint='/v2/clusters?auth_token=%s' % (passwd), data=payload, headers={"Content-Type": "application/json"}, params={'auth_token': passwd}, response_check=lambda response: True if response.status_code == 200 else False, dag=dag ) So this code use simplehttpopeator( https://airflow.incubator.apache.org/code.html) which is used to create the cluster or post the information on this url(qa.qubole.net/api) this is same as requests.post() in python. As you can see i am using passwd here > endpoint='/v2/clusters?auth_token=%s' % (passwd), and i am making many dags like that which are connected and they all use the password(auth_token) to send the data so i want to pass passwd as command line argument. Argument parser is not working here i tried argument parser and also sys.argv but it doesn't work. from airflow import DAG from airflow.operators import SimpleHttpOperator from datetime import datetime,timedelta import argparse import os parser = argparse.ArgumentParser() parser.add_argument("passwd") args = parser.parse_args() default_args = { 'owner': 'airflow', 'depends_on_past': False, 'start_date': datetime.now(), 'email': ['airflow at airflow.com'], 'email_on_failure': False, 'email_on_retry': False } print args.passwd MAIN_DAG='check_dag' dag = DAG(dag_id=MAIN_DAG, default_args=default_args, schedule_interval=None) file=os.path.join(os.path.dirname(os.path.abspath(__file__)),"check.json") with open(file, "r") as f: payload = f.read() # Reading the json data from a file SimpleHttpOperator( # creating cluster using SimpleHttpOperator task_id='cluster_create', method='POST', http_conn_id='qubole_default', # for directing to https://qa.qubole.net/api endpoint='/v2/clusters?auth_token=%s' % (args.passwd), data=payload, headers={"Content-Type": "application/json"}, params={'auth_token': args.passwd}, response_check=lambda response: True if response.status_code == 200 else False, dag=dag ) if i use this its not able to trigger the dag. for triggering the dag we use airflow trigger_dag dagid -r runid argumentparser work in python python script.py --passwd passwd #this work but this is not a python script airflow trigger_dag dagid -r runid --passwd passwd sorry for my english. On Sun, May 28, 2017 at 9:17 PM, shubham goyal wrote: > Hello all, > > > You are not understanding my point I understand command line argument > ,sys.argv[],argparser to pass arguments via command line but I want to do > these things when I trigger the dag > And how I trigger the dag > airflow trigger_dag dagname -s runid > > This triggering of dag doesn't support to pass command line argument but I > want to pass authentication token as command line argument when I trigger > the dag(you can see in airflow docs) so I am asking is there a way to do it. > > I tried argument parser but when I use that it's not able to trigger the > dag. > > I want something like this (in cli) > > airflow trigger_dag dagname -r runid --passwd auth_token > > And want to access this authtoken in Python script. > > > On May 28, 2017 8:18 PM, "Francois Dion" wrote: > >> My mailbox if full of similar stories: companies dumping airflow on their >> ETL (or similar) group. Those who knew Python succeeded, those who didn't >> failed, and some even moved to other companies because they couldn't cope >> with all this complexity dumped on them all at once. >> >> Moral of the story, it is best to learn python first, become good at it, >> then get into specific tools. >> >> And, of course, this is not specific to a relatively straightforward >> module >> / application like airflow. I've seen the same with scikit-learn. Same >> also >> in other languages. And to make matters worse, the curricula (and books) >> always starts with a crash course in a specific language, then go on and >> on >> about the application, perpetuating the myth that learning the programming >> language well is totally unimportant. >> >> Francois >> >> On Sun, May 28, 2017 at 9:46 AM, Alan Gauld via Tutor >> wrote: >> >> > On 28/05/17 04:37, shubham goyal wrote: >> > > Does anybody have answer? >> > >> > You received two answers, both of which asked >> > you to try something and get back to us for more >> > information. Did you try printing sys.argv? >> > What was the result? >> > >> > And did you try Peter's argparse code? >> > >> > You still haven't explained what your dag is? >> > What library are you using? It may treat args >> > in a non standard way, but we can't tell if >> > you don't give us the information we need to >> > help you. >> > >> > -- >> > 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 at python.org >> > To unsubscribe or change subscription options: >> > https://mail.python.org/mailman/listinfo/tutor >> > >> >> >> >> -- >> raspberry-python.blogspot.com - www.pyptug.org - www.3DFutureTech.info - >> @f_dion >> _______________________________________________ >> Tutor maillist - Tutor at python.org >> To unsubscribe or change subscription options: >> https://mail.python.org/mailman/listinfo/tutor >> > From tmrsg11 at gmail.com Sun May 28 20:11:35 2017 From: tmrsg11 at gmail.com (C W) Date: Sun, 28 May 2017 20:11:35 -0400 Subject: [Tutor] Counting a string backwards In-Reply-To: References: Message-ID: Hi Alan Thank you very much, I got it. So in this case, there is no need to specify where it ends. In fact, even if I wanted to specify the ending, I can't! Thank you! On Sun, May 28, 2017 at 7:19 PM, Alan Gauld via Tutor wrote: > On 28/05/17 18:58, C W wrote: > > > Now if I do case 2, > >> print(great[-3:-1]) > >> me > > > > Where did the exclamation mark go in case 2? > > > > I was told the count begins at zero, that's true going forward, but not > > backwards. > > Its not about where the count starts its about where it finishes. > It finishes 1 item before the second index. so in this case > you get the items at -3 and -2. > > If you specify a third parameter you can change the > direction of the count thus: > > >>> great[-1:-3:-1] > '!e' > > So now you get the characters at -1 and -2 (ie in reverse order) > > If you want them in original order starting at -3 just omit > the -1: > > >>> great[-3:] > 'me!' > > HTH, > > -- > 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 at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From tmrsg11 at gmail.com Sun May 28 22:34:36 2017 From: tmrsg11 at gmail.com (C W) Date: Sun, 28 May 2017 22:34:36 -0400 Subject: [Tutor] Counting a string backwards In-Reply-To: <20170529020026.GG23443@ando.pearwood.info> References: <20170529020026.GG23443@ando.pearwood.info> Message-ID: Wow, that's the best explanation I've seen so far, now it's gonna stick with me! Thank you! On Sun, May 28, 2017 at 10:00 PM, Steven D'Aprano wrote: > On Sun, May 28, 2017 at 01:58:22PM -0400, C W wrote: > > Dear Python list, > > > > I am having trouble understanding the following. > [...] > > > The way to think about string indexing and slicing is that the index > positions mark *between* the characters. Take your string: > > Machine learning is awesome! > > For brevity, I'll just use the first word: > > Machine > > Imagine slicing it between the characters. I'll mark the cuts with a > vertical bar: > > |M|a|c|h|i|n|e| > > and add indexes. The indexes will only line correctly if you use a > monspaced or fixed width font like Courier, otherwise things may not > line up correctly. > > |M|a|c|h|i|n|e| > 0 1 2 3 4 5 6 7 > > Here they are again starting from the right, I've spread things out a > bit to fit in the minus signs: > > |M |a |c |h |i |n |e | > -7 -6 -5 -4 -3 -2 -1 0 > > Notice that 0 gets used twice. Of course, that's impossible, because it > would be ambiguous. If you give 0 as an index, how does Python know > whether you mean 0 at the start or 0 or the end? So the simple rule > Python uses is that 0 *always* means the start. > > When you give a single index, Python always uses the character > immediately to the right of the cut: > > s = "Machine" > s[0] > => returns "M" > > s[-1] > => returns "e" > > s[7] > => raises an exception, because there is no character to the right > > When you give two indexes, using slice notation, Python returns the > characters BETWEEN those cuts: > > s[0:7] > => returns "Machine" > > s[1:-1] > => returns "achin" > > Because 0 always means the start of the string, how do you slice to the > end? You can use the length of the string (in this case, 7) or you can > leave the ending position blank, and it defaults to the length of the > string: > > s[1:] # means the same as [1:len(s)] > > You can leave the starting position blank too, it defaults to 0: > > s[:] # means the same as [0:len(s)] > > So remember that slices always cut *between* the index positions. > > > Things get complicated when you include a step (or stride), especially > when the step is negative. For step sizes other than 1, it is > probably best to think of looping over the string: > > py> s = "Nobody expects the Spanish Inquisition!" > py> s[-1:1:-2] > '!otsun snp h tex db' > > is somewhat like: > > for i in range(len(s)-1, 1, -2): > print s[i] > > > > -- > Steve > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From arj.python at gmail.com Mon May 29 11:47:21 2017 From: arj.python at gmail.com (Abdur-Rahmaan Janhangeer) Date: Mon, 29 May 2017 19:47:21 +0400 Subject: [Tutor] How to deploy seamless script updates to your "clients"? In-Reply-To: References: Message-ID: What does a webpage has to do with it? (topic : web scraping) well i meant you put your script in a webpage no need to put any html tags if you wish. then your program has two files. an updater and the script the updater on startup scrape your web page and see if the text is same as the script file if yes : run script file if not : override the script file with the text of the web page Abdur-Rahmaan Janhangeer, Mauritius abdurrahmaanjanhangeer.wordpress.com On 25 May 2017 04:11, "Juan C." wrote: > I have some Python 3.6.0 scripts that my co-workers use for some small > and medium tasks. Whenever I have time I fix some bugs and add some > features to said scripts to make their lives (and mine :D) easier, but > there's a problem: I need to send a new script via email/chat/whatever > and they have to replace it wherever they use it, such a hassle. > > How would I go to put a "update module" inside my script? I was > thinking about using Git, because those scripts are already in > personal repositories so that I can keep track of changes. Will I have > to setup any special permissions so that the scripts can pull requests > from my private repositories? > > I was thinking of something like "check for update before start", if > an update is found the script would replace itself with the newer > version and restart, is that possible? For example, 'cool-tool.py' > v0.2 starts and find that version v0.3 is out, so it downloads and > replace 'cool-tool.py' code with newer code and restart as v0.3. > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > From jack.lambert at gmail.com Mon May 29 16:40:19 2017 From: jack.lambert at gmail.com (Jack Lambert) Date: Mon, 29 May 2017 13:40:19 -0700 Subject: [Tutor] 3D plotting Message-ID: Hello! I am very new to python and am using it for some astronomy data analysis. What I am trying to is plot a 3D scatter plot of three variables that are being read from a file. I've tried using code from other examples of 3d plots but, having little idea how the code functions, I have had very little luck. Here is the relevant code I am hoping to augment. > import numpy as np import scipy as sp import math from astropy.io import fits import matplotlib.pyplot as plot from pylab import * import matplotlib as mpl from mpl_toolkits.mplot3d import Axes3D def sread(infile): hdulist = fits.open(infile) tdata = hdulist[1].data ra = tdata['ra'] # degrees dec = tdata['dec'] # degree p = tdata['parallax'] # mas return ra,dec,p ra,dec,p = sread('TgasSource_000-000-001.fits') for i in range(len(ra)): if ra[i] > 164 and ra[i] < 166 and dec[i] > 49 and dec[i] < 51 : plot(ra[i],dec[i],'bo') show() > so I'm hoping to plot ra and dec on the x and y axes with p on the z axis. Any way you can give me an idea how to implement this? Thanks! From alan.gauld at yahoo.co.uk Mon May 29 19:17:02 2017 From: alan.gauld at yahoo.co.uk (Alan Gauld) Date: Tue, 30 May 2017 00:17:02 +0100 Subject: [Tutor] 3D plotting In-Reply-To: References: Message-ID: On 29/05/17 21:40, Jack Lambert wrote: > I am very new to python and am using it for some astronomy data analysis. > What I am trying to is plot a 3D scatter plot of three variables that are > being read from a file. I've tried using code from other examples of 3d > plots but, having little idea how the code functions, I have had very > little luck. I'm not sure where you got the examples but the official site contains a wealth of samples. http://matplotlib.org/users/screenshots.html If you find one that's close to what you want, grab the source code and try to map your data to that code. That may be what you've already done, in which case you are going to have to spend a bit of time learning the tools. There is a specific matplotlib support forum however who may be better placed than this list to help: https://mail.python.org/mailman/listinfo/matplotlib-users -- 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 cs at zip.com.au Mon May 29 19:08:25 2017 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 30 May 2017 09:08:25 +1000 Subject: [Tutor] Problem with if statements and else statements In-Reply-To: References: Message-ID: <20170529230825.GA24623@cskk.homeip.net> On 29May2017 01:17, Alan Gauld wrote: >On 29/05/17 00:12, Alex Kleider wrote: >> Would >>>>> if Month in {'January', '1'}: >> >> be even better? (regarding efficiency perhaps? Trivial point, I know, >> but just wondering.) > >If in doubt try it out and profile/time it. > >But I don't think it will make much difference since ultimately >it still has to test each value (although a hashing algorithm >may be involved that works on partial matches...) But if in >doubt... Hi Alex, As written it should be a bit slower: to construct a set each member get tested for presence. The cost is in making the set, not in searching it. _However_, supposing your program were doing this a lot. You might well have a global (or, better, long lived shared object) containing a set that has already been constructed. Then: if Month in the_set: is very fast; constant time. Whereas as you would expect, checking a list is linear with the size of the list. So, using a list: seen = [] for item in some_item_generator(): if item in seen: continue seen.append(item) ... do stuff with item, which is new ... The cost of the "if" goes up linearly as you add more items. Using a set: seen = {} for item in some_item_generator(): if item in seen: continue seen.add(item) ... do stuff with item, which is new ... The second version will be much more effiient as the "seen" set grows; the lookup time on the set is essentially O(1) (constant time). But for an ad hoc 2 element list as in your original example the difference will be pretty small; making the 2 element set _should_ be slightly more expensive, and isn't the common idiom (==> less readable). Personally I use: if value in ('a', 'b', 'c'): BTW, in Python we tend to use named like "Fred" for classes (or factories), and "fred" for regular variables. And "FRED" for things that would be constants in other languages. Eg: MAX_THINGS = 16 class Foo: .... def FooBah(x): return Foo(x, style="bah") for fred in ....: Cheers, Cameron Simpson From akleider at sonic.net Mon May 29 19:44:32 2017 From: akleider at sonic.net (Alex Kleider) Date: Mon, 29 May 2017 16:44:32 -0700 Subject: [Tutor] Problem with if statements and else statements In-Reply-To: <20170529230825.GA24623@cskk.homeip.net> References: <20170529230825.GA24623@cskk.homeip.net> Message-ID: On 2017-05-29 16:08, Cameron Simpson wrote: snip > BTW, in Python we tend to use named like "Fred" for classes (or > factories), and "fred" for regular variables. And "FRED" for things > that would be constants in other languages. Eg: > > MAX_THINGS = 16 > > class Foo: > .... > > def FooBah(x): > return Foo(x, style="bah") Yes, I've been aware of these conventions except for the last- I haven't knowingly used 'factories.' I assume from the context that factories return class instances so it makes sense to use the same conventions that apply to class names. ...and thanks again for explaining. Alex From ben+python at benfinney.id.au Mon May 29 21:09:08 2017 From: ben+python at benfinney.id.au (Ben Finney) Date: Tue, 30 May 2017 11:09:08 +1000 Subject: [Tutor] Using venv References: Message-ID: <85o9ubywuj.fsf@benfinney.id.au> Jim writes: > It has been suggested to me that I should use a virtual environment > and venv would be a good choice. I've read through PEP405 and this > link [1]. One possible confusion is the terminology. You have ?a virtual environment? when you create one. The ?venv? module is not itself a virtual environment; it is what you use to create one :-) > Though some of it seems a little confusing to me, I'm sure I can get > it up and running. This question seems a little dumb and maybe I am > being a little dense, but then what? * In each shell where you want to be working in that virtual Python environment, activate the virtualenv (by running the commands from its corresponding ?activate? script; e.g. ?source $VENV/bin/activate?). * Do things requiring Python. > Your program/script is done how do you run it? Do you always have to > activate your venv and run it from there? To get the benefits of that particular virtualenv, yes. > I like to run Tkinter programs from a launcher. Would that be possible > and what would the command look like? How are the programs installed? Can you customise how they're launched? > Lately I have been writing some Libreoffice calc macros and evaluating > pyspread for it's macro capability. Would modules installed in my venv > be available to the spreadsheet programs? The trick is that the environment variables for a process need to be set either when the program starts, or within the program; it can't be done externally. That isn't special to Python, it is how processes work. The virtualenv is activated by setting particular shell environment variables to specific values. If you can do that, the answer is yes. The ?source $VENV/bin/activate? command just runs shell commands to set those environment variables. It's not the only way to set those environment variables. Other Python-bundled programs, like LibreOffice or Blender, will likely have their own ways of activating a virtualenv; or at least you'll probably find people in those communities discussing how to do it. -- \ ?I distrust those people who know so well what God wants them | `\ to do to their fellows, because it always coincides with their | _o__) own desires.? ?Susan Brownell Anthony, 1896 | Ben Finney From jf_byrnes at comcast.net Mon May 29 21:47:12 2017 From: jf_byrnes at comcast.net (Jim) Date: Mon, 29 May 2017 20:47:12 -0500 Subject: [Tutor] Using venv In-Reply-To: <85o9ubywuj.fsf@benfinney.id.au> References: <85o9ubywuj.fsf@benfinney.id.au> Message-ID: On 05/29/2017 08:09 PM, Ben Finney wrote: You should probably disregard this message as I have since solved the problem I was asking about. I originally wrote this message on 01/27/17, how it make it back to the list I don't know. Regards, Jim > Jim writes: > >> It has been suggested to me that I should use a virtual environment >> and venv would be a good choice. I've read through PEP405 and this >> link [1]. > > One possible confusion is the terminology. > > You have ?a virtual environment? when you create one. The ?venv? module > is not itself a virtual environment; it is what you use to create one :-) > >> Though some of it seems a little confusing to me, I'm sure I can get >> it up and running. This question seems a little dumb and maybe I am >> being a little dense, but then what? > > * In each shell where you want to be working in that virtual Python > environment, activate the virtualenv (by running the commands from its > corresponding ?activate? script; e.g. ?source $VENV/bin/activate?). > > * Do things requiring Python. > >> Your program/script is done how do you run it? Do you always have to >> activate your venv and run it from there? > > To get the benefits of that particular virtualenv, yes. > >> I like to run Tkinter programs from a launcher. Would that be possible >> and what would the command look like? > > How are the programs installed? Can you customise how they're launched? > >> Lately I have been writing some Libreoffice calc macros and evaluating >> pyspread for it's macro capability. Would modules installed in my venv >> be available to the spreadsheet programs? > > The trick is that the environment variables for a process need to be set > either when the program starts, or within the program; it can't be done > externally. That isn't special to Python, it is how processes work. > > The virtualenv is activated by setting particular shell environment > variables to specific values. If you can do that, the answer is yes. > > The ?source $VENV/bin/activate? command just runs shell commands to set > those environment variables. It's not the only way to set those > environment variables. > > Other Python-bundled programs, like LibreOffice or Blender, will likely > have their own ways of activating a virtualenv; or at least you'll > probably find people in those communities discussing how to do it. > From __peter__ at web.de Tue May 30 06:06:47 2017 From: __peter__ at web.de (Peter Otten) Date: Tue, 30 May 2017 12:06:47 +0200 Subject: [Tutor] Problem with if statements and else statements References: <20170529230825.GA24623@cskk.homeip.net> Message-ID: Cameron Simpson wrote: > On 29May2017 01:17, Alan Gauld wrote: >>On 29/05/17 00:12, Alex Kleider wrote: >>> Would >>>>>> if Month in {'January', '1'}: >>> >>> be even better? (regarding efficiency perhaps? Trivial point, I know, >>> but just wondering.) >> >>If in doubt try it out and profile/time it. >> >>But I don't think it will make much difference since ultimately >>it still has to test each value (although a hashing algorithm >>may be involved that works on partial matches...) But if in >>doubt... > > Hi Alex, > > As written it should be a bit slower: to construct a set each member get > tested for presence. The cost is in making the set, not in searching it. No, CPython is a bit smarter than that: >>> dis.dis('if m in {"1", "January"}: pass') 1 0 LOAD_NAME 0 (m) 2 LOAD_CONST 3 (frozenset({'1', 'January'})) 4 COMPARE_OP 6 (in) 6 POP_JUMP_IF_FALSE 8 >> 8 LOAD_CONST 2 (None) 10 RETURN_VALUE However, there seems to be limit: >>> def check(n): ... f = io.StringIO() ... dis.dis("if m in {%s}: pass" % ",".join(map(str, range(n))), file=f) ... return f.getvalue().count("LOAD_CONST") ... >>> check(123) 2 >>> check(124) 125 This is python 3.6; for 3.5 and 3.4 I found a maximum length of 80 using the same method. Further tests indicate that you cannot have two sets of length 80. > _However_, supposing your program were doing this a lot. You might well > have a global (or, better, long lived shared object) containing a set that > has already been constructed. Then: > > if Month in the_set: > > is very fast; constant time. Overall, using globals may still be a good idea. Even if there's often no direct performance reward (looking up a global is even a tad slower) you gain predictability. > Whereas as you would expect, checking a list > is linear with the size of the list. > > So, using a list: > > seen = [] > for item in some_item_generator(): > if item in seen: > continue > seen.append(item) > ... do stuff with item, which is new ... > > The cost of the "if" goes up linearly as you add more items. > > Using a set: > > seen = {} > for item in some_item_generator(): > if item in seen: > continue > seen.add(item) > ... do stuff with item, which is new ... > > The second version will be much more effiient as the "seen" set grows; the > lookup time on the set is essentially O(1) (constant time). > > But for an ad hoc 2 element list as in your original example the > difference will be pretty small; making the 2 element set _should_ be > slightly more expensive, and isn't the common idiom (==> less readable). > Personally I use: > > if value in ('a', 'b', 'c'): > > BTW, in Python we tend to use named like "Fred" for classes (or > factories), and "fred" for regular variables. And "FRED" for things that > would be constants in other languages. Eg: > > MAX_THINGS = 16 > > class Foo: > .... > > def FooBah(x): > return Foo(x, style="bah") > > for fred in ....: > > Cheers, > Cameron Simpson > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From cs at zip.com.au Tue May 30 08:49:16 2017 From: cs at zip.com.au (Cameron Simpson) Date: Tue, 30 May 2017 22:49:16 +1000 Subject: [Tutor] Problem with if statements and else statements In-Reply-To: References: Message-ID: <20170530124916.GA47127@cskk.homeip.net> On 30May2017 12:06, Peter Otten <__peter__ at web.de> wrote: >Cameron Simpson wrote: >> As written it should be a bit slower: to construct a set each member get >> tested for presence. The cost is in making the set, not in searching it. > >No, CPython is a bit smarter than that: > >>>> dis.dis('if m in {"1", "January"}: pass') > 1 0 LOAD_NAME 0 (m) > 2 LOAD_CONST 3 (frozenset({'1', 'January'})) Ah, I was wonderig about things like that during today... [ More cool info snipped... ] Thanks, Cameron Simpson From johnf at jfcomputer.com Wed May 31 13:22:31 2017 From: johnf at jfcomputer.com (john) Date: Wed, 31 May 2017 10:22:31 -0700 Subject: [Tutor] sub-modules and python3 Message-ID: <9d6e77d9-1965-6d02-f0a9-2ed5e9951167@jfcomputer.com> Hi folks, In the past I used a simple "import filename" for sub-modules in python 2. With python 3 I have run into errors reported (file not found) using python 2 import statements. But I'm not asking how to correct the import as I am able to change the way I write the import as a work around - but I'm importing all the files at once. What I want to know is what is the best practice for my situation. Is there a simple way using python 3 to emulate python 2 imports? Is there a standard python 3 import tool for sub-modules (files)? Is it acceptable to add/change the os path to allow my app to find the modules/files? Any help or thoughts on the matter is welcome. Johnf From mats at wichmann.us Wed May 31 13:45:19 2017 From: mats at wichmann.us (Mats Wichmann) Date: Wed, 31 May 2017 11:45:19 -0600 Subject: [Tutor] sub-modules and python3 In-Reply-To: <9d6e77d9-1965-6d02-f0a9-2ed5e9951167@jfcomputer.com> References: <9d6e77d9-1965-6d02-f0a9-2ed5e9951167@jfcomputer.com> Message-ID: <5146312f-bfa0-e9dc-3de4-c02d1e419374@wichmann.us> On 05/31/2017 11:22 AM, john wrote: > Hi folks, > > In the past I used a simple "import filename" for sub-modules in python > 2. With python 3 I have run into errors reported (file not found) using > python 2 import statements. But I'm not asking how to correct the > import as I am able to change the way I write the import as a work > around - but I'm importing all the files at once. What I want to know > is what is the best practice for my situation. > > Is there a simple way using python 3 to emulate python 2 imports? > > Is there a standard python 3 import tool for sub-modules (files)? > > Is it acceptable to add/change the os path to allow my app to find the > modules/files? > > Any help or thoughts on the matter is welcome. try running the 2to3 tool and see what it suggests - I know it's supposed to know what changes should be made to imports. You don't have to accept its' suggestions, of course. From __peter__ at web.de Wed May 31 15:03:42 2017 From: __peter__ at web.de (Peter Otten) Date: Wed, 31 May 2017 21:03:42 +0200 Subject: [Tutor] sub-modules and python3 References: <9d6e77d9-1965-6d02-f0a9-2ed5e9951167@jfcomputer.com> Message-ID: john wrote: > Hi folks, > > In the past I used a simple "import filename" for sub-modules in python > 2. With python 3 I have run into errors reported (file not found) using > python 2 import statements. But I'm not asking how to correct the > import as I am able to change the way I write the import as a work > around - but I'm importing all the files at once. What I want to know > is what is the best practice for my situation. > > Is there a simple way using python 3 to emulate python 2 imports? > > Is there a standard python 3 import tool for sub-modules (files)? > Any help or thoughts on the matter is welcome. Are you talking about intra-package imports? Rather than having Python 3 emulate Python 2 you could switch on absolute imports in py2: from __future__ import absolute_import # must be at the beginning # of the module, then import alpha # import toplevel alpha.py from . import alpha # import sibling in current package > Is it acceptable to add/change the os path to allow my app to find the > modules/files? That is likely to create a big mess; you may end up with two versions of the same module, with puzzling results. From szattila88 at gmail.com Wed May 31 14:44:48 2017 From: szattila88 at gmail.com (=?UTF-8?Q?Attila_Szab=C3=B3?=) Date: Wed, 31 May 2017 20:44:48 +0200 Subject: [Tutor] Issue with wsgi_ref.simple_server - sometimes very slow! Message-ID: Hi All, I'm facing a really strange behavior with python's wsgi_ref.simple_server module. I have the following setup: - Raspberry Pi 2 - Ubuntu Mate 16.04 - Python3.5 - I have the following simple source: #!/usr/bin/env python3import wsgiref.simple_server def my_func(env, start_response): start_response('200 OK', []) return [''.encode()] server = wsgiref.simple_server.make_server( '0.0.0.0', 19891, my_func,) server.serve_forever() After several requests (around every ~5 requests) the response time is getting really-really slow (1-60sec) compared to the average 0.1s response, the server serves the request and then quick again for couple of new requests and then again....getting to be slow... I'm trying to reach the node via my router, and also from outside internet and the latency is there. I have tried the same code also on Windows10 with same python version, from behind the same router and the issue was not present. Also I have removed the router and connected internet directly to Raspberry PI and I faced the same issue. So I think I can say that that is not because of the router for sure. Also, always when I interrupt the running server I'm getting the following exception: Exception happened during processing of request from ('192.168.1.100', 3540)Traceback (most recent call last): File "/usr/lib/python3.5/socketserver.py", line 313, in _handle_request_noblock self.process_request(request, client_address) File "/usr/lib/python3.5/socketserver.py", line 341, in process_request self.finish_request(request, client_address) File "/usr/lib/python3.5/socketserver.py", line 354, in finish_request self.RequestHandlerClass(request, client_address, self) File "/usr/lib/python3.5/socketserver.py", line 681, in __init__ self.handle() File "/usr/lib/python3.5/wsgiref/simple_server.py", line 119, in handle self.raw_requestline = self.rfile.readline(65537) File "/usr/lib/python3.5/socket.py", line 575, in readinto return self._sock.recv_into(b)KeyboardInterrupt That's why I think there is some issue with that recv_into function, but I cannot figure it out how to resolve this... :/ Does anybody faced this same issue? Or anybody has an idea what should I try or what should I modify in python source to solve this issue? Thanks, Attila From johnf at jfcomputer.com Wed May 31 21:25:46 2017 From: johnf at jfcomputer.com (johnf) Date: Wed, 31 May 2017 18:25:46 -0700 Subject: [Tutor] sub-modules and python3 In-Reply-To: References: <9d6e77d9-1965-6d02-f0a9-2ed5e9951167@jfcomputer.com> Message-ID: I'm not trying to support python 2 and python 3. I just want to use python 3. The issue of using absolute imports seems like a hack. What happens if a user or I need to change the path. Should I have to change the all the imports? I believe I am going to use absolute paths in the end (all I need is a good editor to make the changes) but I was hoping to find something that would work in a more universal way. Johnf On 05/31/2017 12:03 PM, Peter Otten wrote: > john wrote: > >> Hi folks, >> >> In the past I used a simple "import filename" for sub-modules in python >> 2. With python 3 I have run into errors reported (file not found) using >> python 2 import statements. But I'm not asking how to correct the >> import as I am able to change the way I write the import as a work >> around - but I'm importing all the files at once. What I want to know >> is what is the best practice for my situation. >> >> Is there a simple way using python 3 to emulate python 2 imports? >> >> Is there a standard python 3 import tool for sub-modules (files)? >> Any help or thoughts on the matter is welcome. > Are you talking about intra-package imports? Rather than having Python 3 > emulate Python 2 you could switch on absolute imports in py2: > > from __future__ import absolute_import # must be at the beginning > # of the module, then > > import alpha # import toplevel alpha.py > from . import alpha # import sibling in current package > >> Is it acceptable to add/change the os path to allow my app to find the >> modules/files? > That is likely to create a big mess; you may end up with two versions of the > same module, with puzzling results. > > _______________________________________________ > Tutor maillist - Tutor at python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor From ben+python at benfinney.id.au Wed May 31 21:46:54 2017 From: ben+python at benfinney.id.au (Ben Finney) Date: Thu, 01 Jun 2017 11:46:54 +1000 Subject: [Tutor] sub-modules and python3 References: <9d6e77d9-1965-6d02-f0a9-2ed5e9951167@jfcomputer.com> Message-ID: <85vaogxywh.fsf@benfinney.id.au> johnf writes: > I just want to use python 3. The issue of using absolute imports seems > like a hack. Absolute import is what Python 3 does when you ask to import a module without leading dots. Relative import is what Python 3 does when you ask to import a module with leading dots. If you want to use Python 3 ? and I am glad you do! ? that's the import behaviour you'll get. > What happens if a user or I need to change the path. Should I have to > change the all the imports? You are asking for relative imports, and yes relative import is what you should use to avoid having to care where the code is installed. In Python 3, to import from a relative path, use leading dots (because a path without leading dots is absolute). This explicitly tells Python that the path is relative to the module where the import statement is. You would have learned this when you worked through the Python 3 tutorial https://docs.python.org/3/tutorial/modules.html>. Maybe it's time to work through again? -- \ ?About four years ago, I was ? no, it was yesterday.? ?Steven | `\ Wright | _o__) | Ben Finney