Re: [Tutor] filtering listed directories
On 22/08/15 23:32, Alan Gauld wrote: On 22/08/15 11:43, Laura Creighton wrote: How can I filter out these hidden directories? Help(tkFileDialog) doesn't help me as it just shows **options, but doesn't show what these options might be. tix (tkinter extensions) https://wiki.python.org/moin/Tix have some more file dialogs, so maybe there is joy there. There is a FileSelectDialog in Tix that has a dircmd option according to the Tix documentation. However, I've played about with it and can't figure out how to make it work! There is also allegedly a 'hidden' check-box subwidget that controls whether hidden files are shown. Again I couldn't find how to access this. But maybe some questions on a Tix (or Tk) forum might get more help? Once you know how to do it in native Tcl/Tk/Tix you can usually figure out how to do it in Python. Thanks for the Tcl tk hint, so I searched for info for tcl tk. Unfortunately the options appear to be the same as offered by tkinter. I had hoped that the cause of my problem would be that I'm still to learn that bit ;) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] filtering listed directories
On 23/08/15 00:42, Laura Creighton wrote: In a message of Sat, 22 Aug 2015 14:32:56 +0100, Alan Gauld writes: But maybe some questions on a Tix (or Tk) forum might get more help? Once you know how to do it in native Tcl/Tk/Tix you can usually figure out how to do it in Python. -- Alan G I asked the question on tkinter-discuss, but the question hasn't shown up yet. In the meantime, I have found this: http://www.ccs.neu.edu/research/demeter/course/projects/demdraw/www/tickle/u3/tk3_dialogs.html which looks like, if we converted it to tkinter, would do the job, since all it wants is a list of files. I have guests coming over for dinner, so it will be much later before I can work on this. (And I will be slow -- so if you are a wizard at converting tk to tkinter, by all means feel free to step in here. :) ) Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor Thanks Laura, unfortunately I know next to nothing of tk, so I'll have wait. No worries, This in not an urgent thing, and is mostly a learning exercise, as I have a cli tool to do what what I'm aiming to write using Tkinter. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Problem using lxml
Anthony Papillion schrieb am 23.08.2015 um 01:16: > from lxml import html > import requests > > page = requests.get("http://joplin.craigslist.org/search/w4m";) > tree = html.fromstring(page.text) While requests has its merits, this can be simplified to tree = html.parse("http://joplin.craigslist.org/search/w4m";) > titles = tree.xpath('//a[@class="hdrlnk"]/text()') > try: > for title in titles: > print title This only works as long as the link tags only contain plain text, no other tags, because "text()" selects individual text nodes in XPath. Also, using @class="hdrlnk" will not match link tags that use class=" hdrlnk " or class="abc hdrlnk other". If you want to be on the safe side, I'd use cssselect instead and then serialise the complete text content of the link tag to a string, i.e. from lxml.etree import tostring for link_element in tree.cssselect("a.hdrlnk"): title = tostring( link_element, method="text", encoding="unicode", with_tail=False) print(title.strip()) Note that the "cssselect()" feature requires the external "cssselect" package to be installed. "pip install cssselect" should handle that. > except: > pass Oh, and bare "except:" clauses are generally frowned upon because they can easily hide bugs by also catching unexpected exceptions. Better be explicit about the exception type(s) you want to catch. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] filtering listed directories
In a message of Sun, 23 Aug 2015 13:09:41 +1000, Chris Roy-Smith writes: >On 22/08/15 23:32, Alan Gauld wrote: >> On 22/08/15 11:43, Laura Creighton wrote: >> How can I filter out these hidden directories? Help(tkFileDialog) doesn't help me as it just shows **options, but doesn't show what these options might be. >>> >>> >>> tix (tkinter extensions) https://wiki.python.org/moin/Tix >>> have some more file dialogs, so maybe there is joy there. >>> >> >> There is a FileSelectDialog in Tix that has a dircmd option >> according to the Tix documentation. >> >> However, I've played about with it and can't figure out how >> to make it work! >> >> There is also allegedly a 'hidden' check-box subwidget >> that controls whether hidden files are shown. Again I >> couldn't find how to access this. >> >> But maybe some questions on a Tix (or Tk) forum might >> get more help? Once you know how to do it in native >> Tcl/Tk/Tix you can usually figure out how to do it >> in Python. >> >Thanks for the Tcl tk hint, so I searched for info for tcl tk. >Unfortunately the options appear to be the same as offered by tkinter. I >had hoped that the cause of my problem would be that I'm still to learn >that bit ;) No, the problem is that the tk widget is badly designed. Not only is there only minimal pattern support matching, but there is no way to subclass the thing and feed it your own list of file and directory names. It really is a case of "Do not open. No user-servicable parts to be found within." which is frustrating. Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] filtering listed directories
wxPython has a SHOWHIDDEN checkbox for turning exactly this on and off, but if you wanted to exclude all .pyc files while leaving the rest alone, I don't see a way to do that, either, right now. QT has a ton of options including showing hidden files. The common problem we seem to be up against is that the gui packages really want to use the native OS file dialogs whenever possible, and the people making such dialogs -- at least in some OSes, never dreamed that somebody would ever want to exclude only certain files (as opposed to only match certain files). Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] filtering listed directories
On 23/08/15 18:13, Laura Creighton wrote: In a message of Sun, 23 Aug 2015 13:09:41 +1000, Chris Roy-Smith writes: On 22/08/15 23:32, Alan Gauld wrote: On 22/08/15 11:43, Laura Creighton wrote: How can I filter out these hidden directories? Help(tkFileDialog) doesn't help me as it just shows **options, but doesn't show what these options might be. tix (tkinter extensions) https://wiki.python.org/moin/Tix have some more file dialogs, so maybe there is joy there. There is a FileSelectDialog in Tix that has a dircmd option according to the Tix documentation. However, I've played about with it and can't figure out how to make it work! There is also allegedly a 'hidden' check-box subwidget that controls whether hidden files are shown. Again I couldn't find how to access this. But maybe some questions on a Tix (or Tk) forum might get more help? Once you know how to do it in native Tcl/Tk/Tix you can usually figure out how to do it in Python. Thanks for the Tcl tk hint, so I searched for info for tcl tk. Unfortunately the options appear to be the same as offered by tkinter. I had hoped that the cause of my problem would be that I'm still to learn that bit ;) No, the problem is that the tk widget is badly designed. Not only is there only minimal pattern support matching, but there is no way to subclass the thing and feed it your own list of file and directory names. It really is a case of "Do not open. No user-servicable parts to be found within." which is frustrating. Laura Thanks Laura, I don't use idle but wondered how it handled hidden directories, and found that it also shows them. I guess I'll have to be inconsistent using the title bar to warn users and trap inappropriate selections sending the user back to try again. I can only assume that windows users don't get this problem. Do any of the other graphical packages avoid this problem? I guess I should try the appropriate news groups. Chris ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing back to same CSV in the next column
Hello, Here is my final script. It is doing what I wanted it to. I wanted to just share it as a final product and thank you all for your feedback on the various previous revisions. import socket ListOfIPAddresses = [] with open('top500ips.csv', 'r') as f: for line in f: line = line.strip() ListOfIPAddresses.append(line) newFile = open('top500ips.csv', 'w') for address in ListOfIPAddresses: try: ResolvedAddresses = socket.gethostbyaddr(address)[0] newFile.write(ResolvedAddresses + "\n") # print(ResolvedAddresses) except socket.herror as e: newFile.write("No resolution available for %s" % (address) + "\n") If you have any suggestions, please do share. Thank you. On Monday, August 17, 2015 4:35 AM, Alan Gauld wrote: On 17/08/15 02:51, Nym City via Tutor wrote: > the output of the gethostbyaddr module includes three item > (hostname, aliaslist, ipaddrlist). However, in my output > I just what the hostname field. So I created a list but > I am not able to pull out just the [0] item from this > and instead I get the following error: > TypeError: 'int' object is not subscriptable. > for line in in_file: > try: > name = socket.gethostbyaddr(line.strip()) > ListOfIPAddresses.append(name) > out_file.write(str(ListOfIPAddresses))[0] Look at that last line and break it down. Where is the index operation? It's right at the end so it applies to the output of the write() operation. You need it against the list, before you convert to string and before you write to file. > Also, could you please give some explanation of '\t'. Its the tab character. it inserts a tab, just like hitting the tab key on the keyboard. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Tkinter-discuss] tkinter file dialog pattern matching (fwd)
oooh. Seems that there is an undocumented feature we can use! Laura --- Forwarded Message Return-Path: Date: Sun, 23 Aug 2015 12:40:02 +0200 From: Michael Lange To: tkinter-disc...@python.org Message-Id: <20150823124002.7391f37e21f9b5cfaa917...@web.de> In-Reply-To: <20150822210424.321b826f@lenny> References: <201508221103.t7mb3kdx010...@fido.openend.se> Hi, On Sat, 22 Aug 2015 21:04:24 +0100 Pawel Mosakowski wrote: > Hi, > > I've found this little gem in the Tk docs > https://www.tcl.tk/man/tcl8.4/TkCmd/getOpenFile.htm#M13 > From what I see "file patterns" in the file dialog are not "regex > patterns" and do not support special characters. Only things that work > are: > 1) * - any extension > 2) "" - files without extension > 3) literal extension without wildcard chars > Unfortunately it looks like there is no simple way to filter out hidden > files. actually the unix tk file dialog has an an (however undocumented) feature to hide hidden elements and display even a button that allows to toggle between hidden elements on/off, however we need to do a little tcl to get to this. Since the feature is not documented anywhere it might also be a good idea to wrap this into a try...except. See this little code snippet: # from Tkinter import * import tkFileDialog as tkfd root = Tk() try: # call a dummy dialog with an impossible option to initialize the file # dialog without really getting a dialog window; this will throw a # TclError, so we need a try...except : try: root.tk.call('tk_getOpenFile', '-foobarbaz') except TclError: pass # now set the magic variables accordingly root.tk.call('set', '::tk::dialog::file::showHiddenBtn', '1') root.tk.call('set', '::tk::dialog::file::showHiddenVar', '0') except: pass # a simple callback for testing: def openfile(event): fname = tkfd.askopenfilename() print(fname) root.bind('', openfile) root.mainloop() # Best regards Michael ___ Tkinter-discuss mailing list tkinter-disc...@python.org https://mail.python.org/mailman/listinfo/tkinter-discuss --- End of Forwarded Message ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Tkinter-discuss] tkinter file dialog pattern matching (fwd)
More goodies from Michael Lange --- Forwarded Message From: Michael Lange To: tkinter-disc...@python.org if you only need to hide hidden files, please see my other post. If you need a more versatile file dialog widget, you might want to consider wrapping an existing tcl widget for tkinter, this is usually rather easy once you understand how it is done (all you need is mostly the man page and a little time) and might save you a lot of headaches. One example that might be interisting is here: http://chiselapp.com/user/schelte/repository/fsdialog/index You might also want to try the Tix.ExFileSelectDialog, it is rather dated, but actually has a switch to toggle hidden files on/off and wildcard filtering capabilities. Unfortunately here with debian jessie I can only test it with Tcl, with python for some reason I get a segfault :-( Best regards Michael --- End of Forwarded Message ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] filtering listed directories
On 23/08/15 09:31, Chris Roy-Smith wrote: There is a FileSelectDialog in Tix that has a dircmd option according to the Tix documentation. There is also allegedly a 'hidden' check-box subwidget that controls whether hidden files are shown. Again I couldn't find how to access this. This combo looks like the best solution if I can find how to make it work. Its the logical solution and I'm astounded that none of the common GUIs seem to offer it. (Although I've been building GUIS for over 20 years and never needed it until now! So maybe not so surprising). I'm hoping to buy out some time over the next couple of days to seriously explore the Tix dialogs. there only minimal pattern support matching, but there is no way to subclass the thing and feed it your own list of file and directory This is true and it is because Tkinter has used the underlying GUI toolkit rather than building open/save from scratch. (Native Tk offers no standard dialogs for file operations!) However there is another option in the shape of the Dialog module which is designed to allow you to easily build a custom dialog of your own design. This could be done using the Tix tree view widget for example and you could then build in a file filter operation. (A search may even reveal that somebody has done this already... I think that's what the Tk code Laura posted was doing)) I can only assume that windows users don't get this problem. Its got nothing to do with the OS, the same limitations apply in all cases. They have taken the minimum subset of options when they built the common dialogs. But the standard Windows dialog doesn't offer exclusion options either, so even if they had offered platform specific hooks, it would not have helped on Windows. wxPython does the same, so has the same limitations. Do any of the other graphical packages avoid this problem? The GTk dialogs on my Linux box have slightly more sophisticated options so I'd guess that PyGTK at least will offer more in the API (But even they don't appear to have filter out capability). PyQt/SIDE probably offers more too but I don't know very much about Qt. One possibility on Windows is that the list of folders shown is con trolled by the users registry settings in the file explorer. You can choose to hide "system files" in the Explorer view and that sets a registry flag. I haven't checked but its possible that the Windows open/save dialogs respect that setting. But that's just a guess! -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Writing back to same CSV in the next column
On 23/08/15 14:16, Nym City wrote: Hello, Here is my final script. It is doing what I wanted it to. I wanted to just share it as a final product and thank you all for your feedback on the various previous revisions. import socket ListOfIPAddresses = [] with open('top500ips.csv', 'r') as f: for line in f: line = line.strip() ListOfIPAddresses.append(line) The initial assignment plus this for loop could all be replaced by readlines(): ListOfIPAddresses = f.readlines() newFile = open('top500ips.csv', 'w') You should use the 'with' style that you used above. Otherwise you should definitely close the file after the loop below. Do one or the other, but not both... for address in ListOfIPAddresses: try: ResolvedAddresses = socket.gethostbyaddr(address)[0] Being picky this variable should probably be named ResolvedAddress since its only one address being resolved at a time. newFile.write(ResolvedAddresses + "\n") # print(ResolvedAddresses) except socket.herror as e: newFile.write("No resolution available for %s" % (address) + "\n") Otherwise it looks fine to me. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Tkinter-discuss] tkinter file dialog pattern matching (fwd)
On 23/08/15 15:00, Laura Creighton wrote: You might also want to try the Tix.ExFileSelectDialog, it is rather dated, but actually has a switch to toggle hidden files on/off and wildcard filtering capabilities. Unfortunately here with debian jessie I can only test it with Tcl, with python for some reason I get a segfault :-( Aha! Glad it's not just me. That segfault was the obstacle I hit. I assumed I was just using it wrong but if Michael gets it too then I don't feel so bad! :-) But I still think it looks the best option with its dircmd hook so I'm going to spend some time making it work (if I can!) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Tkinter-discuss] tkinter file dialog pattern matching (fwd)
In a message of Sun, 23 Aug 2015 15:39:07 +0100, Alan Gauld writes: >On 23/08/15 15:00, Laura Creighton wrote: > >> You might also want to try the Tix.ExFileSelectDialog, it is rather >> dated, but actually has a switch to toggle hidden files on/off and >> wildcard filtering capabilities. Unfortunately here with debian jessie I >> can only test it with Tcl, with python for some reason I get a >> segfault :-( > >Aha! Glad it's not just me. That segfault was the obstacle I hit. >I assumed I was just using it wrong but if Michael gets it >too then I don't feel so bad! :-) > >But I still think it looks the best option with its dircmd >hook so I'm going to spend some time making it work >(if I can!) segfaults debian sid, too. Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Tkinter-discuss] tkinter file dialog pattern matching (fwd)
But, aha, it works for Python3.5 I wrote this minimal program, to see what needed doing to solve the OP's problem, and, surprise, it solves it right out of the box. # -*- coding: utf-8 -*- from tkinter import * import tkinter.tix as tix root = tix.Tk() def print_selected(args): print('selected dir:', args) def pathSelect(): d = tix.ExFileSelectDialog(master=root, command=print_selected) d.popup() button = Button(root, text="dialog", command=pathSelect) button.pack() root.mainloop() --- shows my .files or not depending on whether I check the checkbox. Now , of course I have gotten greedy and want a way to say 'show me everything but the images' and 'show me everythingbut the .pyc files' Laura ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do not understand why test is running.
On Sat, Aug 22, 2015 at 3:18 AM, Peter Otten <__pete...@web.de> wrote: > boB Stepp wrote: > >> In the cold light of morning, I see that in this invocation, the path >> is wrong. But even if I correct it, I get the same results: >> >> e:\Projects\mcm>py -m unittest ./test/db/test_manager.py > [...] >> ValueError: Empty module name > > Make sure that there are files > > ./test/__init__.py > ./test/db/__init__.py I had done this. > and then try > > py -m unittest test.db.test_manager I *thought* that I had done this, but perhaps... >> e:\Projects\mcm>py ./test/db/test_manager.py >> Traceback (most recent call last): >> File "./test/db/test_manager.py", line 16, in >> import mcm.db.manager >> ImportError: No module named 'mcm' > > Make sure the parent directory of the mcm package (I believe this is > E:\Projects\mcm) is in your PYTHONPATH, then try again. ... I was not in the directory, E:\Projects\mcm. It is my understanding that if I start in a particular directory when I invoke Python, then it looks in that location first in PYTHONPATH. Anyway, typing py -m unittest test.db.test_manager from this location works (And now makes sense.). But I still have remaining questions to clear up: Peter said earlier in this thread: If you want to trigger the if __name__ == "__main__": ... you have to invoke the test script with py ./mcm/test/db/test_manager.py the same way you would invoke any other script. - If I try this or begin in E:\Projects\mcm and type py ./test/db/test_manager.py I get E:\Projects\mcm>py ./test/db/test_manager.py Traceback (most recent call last): File "./test/db/test_manager.py", line 16, in import mcm.db.manager ImportError: No module named 'mcm' I don't understand why this is the case. I did various experiments with different file structures for the project, when I put the test_manager.py file in the same directory as the module file being tested, manager.py, then (after adjusting the import statement in test_manager.py appropriately) typing py test_manager.py runs the unit tests. So, why does the above not work? Why does it return "No module named 'mcm'"? Especially in the context that now test discovery has no problem and runs correctly and test.db.test_manager runs correctly. And am I misreading the docs at https://docs.python.org/3/library/unittest.html#test-discovery: --- 26.3.2. Command-Line Interface [...] Test modules can be specified by file path as well: python -m unittest tests/test_something.py This allows you to use the shell filename completion to specify the test module. The file specified must still be importable as a module. The path is converted to a module name by removing the ‘.py’ and converting path separators into ‘.’ According to this, from E:\Projects\mcm, I should be able to type py -m unittest ./test/db/test_manager.py but this continues to result in: E:\Projects\mcm>py -m unittest ./test/db/test_manager.py Traceback (most recent call last): File "C:\Python34\lib\runpy.py", line 170, in _run_module_as_main "__main__", mod_spec) File "C:\Python34\lib\runpy.py", line 85, in _run_code exec(code, run_globals) File "C:\Python34\lib\unittest\__main__.py", line 18, in main(module=None) File "C:\Python34\lib\unittest\main.py", line 92, in __init__ self.parseArgs(argv) File "C:\Python34\lib\unittest\main.py", line 139, in parseArgs self.createTests() File "C:\Python34\lib\unittest\main.py", line 146, in createTests self.module) File "C:\Python34\lib\unittest\loader.py", line 146, in loadTestsFromNames suites = [self.loadTestsFromName(name, module) for name in names] File "C:\Python34\lib\unittest\loader.py", line 146, in suites = [self.loadTestsFromName(name, module) for name in names] File "C:\Python34\lib\unittest\loader.py", line 105, in loadTestsFromName module = __import__('.'.join(parts_copy)) ValueError: Empty module name -- I believe that I understand Peter's point earlier, but if I am reading the docs correctly, I should be able to do this. And in fact while I was experimenting with different directory structures, I found some that would indeed allow me to invoke unit tests by designating the file path. So, I can proceed with my project, but I still don't understand these two issues. boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Tkinter-discuss] tkinter file dialog pattern matching (fwd)
In a message of Sun, 23 Aug 2015 16:45:11 +0200, Laura Creighton writes: >segfaults debian sid, too. > >Laura Updating to Python 2.7.10 (default, Jul 1 2015, 10:54:53) and installing the tix-dev debian package, instead of just tix ... and I am not sure which of these fixed the problem, because serious I just did this to help me _find_ what I assumed would still be there made the problem go away. So this minimal program with Python2 syntax looks like what Chris Roy-Smith was looking for in the first place. Laura # -*- coding: utf-8 -*- from Tkinter import * from Tkconstants import * import Tix from Tkconstants import * root = Tix.Tk() def print_selected(args): print('selected dir:', args) def pathSelect(): d =Tix.ExFileSelectDialog(master=root, command=print_selected) d.popup() button = Button(root, text="dialog", command=pathSelect) button.pack() root.mainloop() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] [Tkinter-discuss] tkinter file dialog pattern matching (fwd)
On 23/08/15 16:11, Laura Creighton wrote: But, aha, it works for Python3.5 I only trued it on 2.7. I'll have a go on 3.4... I wrote this minimal program, to see what needed doing to solve the OP's problem, and, surprise, it solves it right out of the box. from tkinter import * import tkinter.tix as tix I believe tix is a complete superset of tkinter so you only need the tix import. root = tix.Tk() def print_selected(args): print('selected dir:', args) def pathSelect(): d = tix.ExFileSelectDialog(master=root, command=print_selected) d.popup() The dircmd option should work too. Although you might need to pull out the subwidget first sub = d.subwidget('fsbox') sub['dircmd'] = lambda d,f,h: glob.glob(pattern) -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do not understand why test is running.
On Sun, Aug 23, 2015 at 10:42:25AM -0500, boB Stepp wrote: [...] > If I try this or begin in E:\Projects\mcm and type py > ./test/db/test_manager.py I get > > E:\Projects\mcm>py ./test/db/test_manager.py > Traceback (most recent call last): > File "./test/db/test_manager.py", line 16, in > import mcm.db.manager > ImportError: No module named 'mcm' > > > I don't understand why this is the case. Without seeing the complete structure of your project, I cannot be sure, but I can think of at least two problems: 1) Does the mcm directory contain a file called __init__.py? If not, then Python will not recognise it as a module and `import mcm` will fail. 2) Even if mcm contains a __init__.py file, the directory itself must be located in your PYTHONPATH. If you run this: py -c "import sys; print(sys.path)" it will show you the directories in the PYTHONPATH that are searched for a module called mcm. Given that your directory is E:\Projects\mcm, that implies that E:\Projects\ must be in the search path in order to locate mcm as a module. Think of it this way... simplified (very simplified!) Python does this when you try to import a module: # `import spam` pseudo-code for directory in sys.path: filenames = os.listdir(directory) for name in filenames: if os.path.isfile(name) and name == "spam.py": load(directory\spam.py) elif os.path.isdir(name) and name == "spam": if os.path.exists(directory\spam\__init__.py): load(directory\spam\__init__.py) There's more, of course -- the real import code is much more complex, it handles cached modules in sys.modules, compiled .pyc and .pyo files, zip files, custom importers, and much more. And I daresay it is more efficient than the pseudo-code I show above. But the important factor is that none of the directories in the PYTHONPATH are themselves considered modules, only their contents can be considered modules. So, if the current directory is E:\Projects\mcm then that directory will be added to the PYTHONPATH, sure enough. But there is nothing inside that directory called mcm, you need either: E:\Projects\mcm\mcm.py or E:\Projects\mcm\mcm\__init__.py in order to import it. Or you can add E:\Projects to the PYTHONPATH. Or move up a level, to E:\Projects, and run your code from there. To put it another way, Python will look *inside* the current directory for modules to import, but it won't look *at* the current directory as a module to import. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do not understand why test is running.
boB Stepp wrote: > On Sat, Aug 22, 2015 at 3:18 AM, Peter Otten <__pete...@web.de> wrote: >> boB Stepp wrote: >> >>> In the cold light of morning, I see that in this invocation, the path >>> is wrong. But even if I correct it, I get the same results: >>> >>> e:\Projects\mcm>py -m unittest ./test/db/test_manager.py >> [...] >>> ValueError: Empty module name >> >> Make sure that there are files >> >> ./test/__init__.py >> ./test/db/__init__.py > > I had done this. > >> and then try >> >> py -m unittest test.db.test_manager > > I *thought* that I had done this, but perhaps... > >>> e:\Projects\mcm>py ./test/db/test_manager.py >>> Traceback (most recent call last): >>> File "./test/db/test_manager.py", line 16, in >>> import mcm.db.manager >>> ImportError: No module named 'mcm' >> >> Make sure the parent directory of the mcm package (I believe this is >> E:\Projects\mcm) is in your PYTHONPATH, then try again. > > ... I was not in the directory, E:\Projects\mcm. It is my > understanding that if I start in a particular directory when I invoke > Python, then it looks in that location first in PYTHONPATH. No, it looks in the location of the script, not in the working directory: $ mkdir sub $ echo 'print("hello")' > foo.py $ echo 'import foo' > main.py $ python3 main.py hello That's the normal situation as the main script you're working on is typically in the working directory. Let's move it: $ mv main.py sub $ python3 sub/main.py Traceback (most recent call last): File "sub/main.py", line 1, in import foo ImportError: No module named 'foo' You have to add the working directory explicitly, e. g.: $ PYTHONPATH=. python3 sub/main.py hello > Anyway, > typing py -m unittest test.db.test_manager from this location works > (And now makes sense.). > > But I still have remaining questions to clear up: > > Peter said earlier in this thread: > > > If you want to trigger the > > if __name__ == "__main__": ... > > you have to invoke the test script with > > py ./mcm/test/db/test_manager.py > > the same way you would invoke any other script. > - > > If I try this or begin in E:\Projects\mcm and type py > ./test/db/test_manager.py I get > > E:\Projects\mcm>py ./test/db/test_manager.py > Traceback (most recent call last): > File "./test/db/test_manager.py", line 16, in > import mcm.db.manager > ImportError: No module named 'mcm' > > > I don't understand why this is the case. I did various experiments > with different file structures for the project, when I put the > test_manager.py file in the same directory as the module file being > tested, manager.py, then (after adjusting the import statement in > test_manager.py appropriately) typing py test_manager.py runs the unit > tests. So, why does the above not work? Why does it return "No > module named 'mcm'"? Especially in the context that now test > discovery has no problem and runs correctly and test.db.test_manager > runs correctly. > > And am I misreading the docs at > https://docs.python.org/3/library/unittest.html#test-discovery: > > --- > 26.3.2. Command-Line Interface > > [...] > > Test modules can be specified by file path as well: > > python -m unittest tests/test_something.py > > This allows you to use the shell filename completion to specify the > test module. The file specified must still be importable as a module. > The path is converted to a module name by removing the ‘.py’ and > converting path separators into ‘.’ I didn't know this. > According to this, from E:\Projects\mcm, I should be able to type > > py -m unittest ./test/db/test_manager.py Let's process the argument you provide following the above recipe: (1) Remove .py "./test/db/test_manager" (2) Replace / with . "..test.db.test_manager" Do you see now why > this continues to result in: [...] > File "C:\Python34\lib\unittest\loader.py", line 105, in > loadTestsFromName > module = __import__('.'.join(parts_copy)) > ValueError: Empty module name ? Without the leading ./ it will probably work. > I believe that I understand Peter's point earlier, but if I am reading > the docs correctly, I should be able to do this. While I don't think it's a bug you might still file a feature request on bugs.python.org. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do not understand why test is running.
On Sun, Aug 23, 2015 at 06:47:49PM +0200, Peter Otten wrote: > > ... I was not in the directory, E:\Projects\mcm. It is my > > understanding that if I start in a particular directory when I invoke > > Python, then it looks in that location first in PYTHONPATH. > > No, it looks in the location of the script, not in the working directory: I'm gobsmacked. It appears you are correct, but I could have sworn that "." (the current working directory) was added to the path. I could have sworn that I have seen "." at the front of sys.path, but apparently I must have dreamed it. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do not understand why test is running.
On Sun, Aug 23, 2015 at 11:47 AM, Peter Otten <__pete...@web.de> wrote: > boB Stepp wrote: >> And am I misreading the docs at >> https://docs.python.org/3/library/unittest.html#test-discovery: >> >> > --- >> 26.3.2. Command-Line Interface >> >> [...] >> >> Test modules can be specified by file path as well: >> >> python -m unittest tests/test_something.py >> >> This allows you to use the shell filename completion to specify the >> test module. The file specified must still be importable as a module. >> The path is converted to a module name by removing the ‘.py’ and >> converting path separators into ‘.’ > > I didn't know this. > >> According to this, from E:\Projects\mcm, I should be able to type >> >> py -m unittest ./test/db/test_manager.py > > Let's process the argument you provide following the above recipe: > > (1) Remove .py > > "./test/db/test_manager" > > (2) Replace / with . > > "..test.db.test_manager" > > Do you see now why > >> this continues to result in: > > [...] > >> File "C:\Python34\lib\unittest\loader.py", line 105, in >> loadTestsFromName >> module = __import__('.'.join(parts_copy)) >> ValueError: Empty module name > > ? This gets into the same issue you pointed out earlier in the thread. Thanks for demonstrating how I *should* have applied the docs to my current question. > Without the leading ./ it will probably work. Unfortunately, no: E:\Projects\mcm>E:\Projects\mcm>py -m unittest test/db/test_manager.py 'E:\Projects\mcm' is not recognized as an internal or external command, operable program or batch file. I also tried (Just to try...) E:\Projects\mcm>E:\Projects\mcm>py -m unittest /test/db/test_manager.py 'E:\Projects\mcm' is not recognized as an internal or external command, operable program or batch file. However, if I use the FULL path: E:\Projects\mcm>py -m unittest e:\Projects\mcm\test\db\test_manager.py . -- Ran 1 test in 0.000s OK all is fine. >> I believe that I understand Peter's point earlier, but if I am reading >> the docs correctly, I should be able to do this. With your help, I have (finally) demonstrated this. > While I don't think it's a bug you might still file a feature request on > bugs.python.org. In retrospect, the only change I would suggest to the cited docs, is to say "... full file path ..." or "... fully qualified file path ..." instead of just "... file path ...". -- boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Do not understand why test is running.
On Sun, Aug 23, 2015 at 11:47 AM, Peter Otten <__pete...@web.de> wrote: > boB Stepp wrote: > >> On Sat, Aug 22, 2015 at 3:18 AM, Peter Otten <__pete...@web.de> wrote: >>> boB Stepp wrote: >> ... I was not in the directory, E:\Projects\mcm. It is my >> understanding that if I start in a particular directory when I invoke >> Python, then it looks in that location first in PYTHONPATH. > > No, it looks in the location of the script, not in the working directory: [...] > You have to add the working directory explicitly, e. g.: > > $ PYTHONPATH=. python3 sub/main.py > hello Applying your wisdom to my remaining question, before I was doing this unsuccessfully: E:\Projects\mcm>py e:\Projects\mcm\test\db\test_manager.py Traceback (most recent call last): File "e:\Projects\mcm\test\db\test_manager.py", line 16, in import mcm.db.manager ImportError: No module named 'mcm' But if I set the PYTHONPATH to e:/Projects/mcm as you seem to be suggesting, gives: E:\Projects\mcm>set PYTHONPATH=e:/Projects/mcm; E:\Projects\mcm>py e:\Projects\mcm\test\db\test_manager.py . -- Ran 1 test in 0.000s OK Success! Thanks, Peter! You really clarified a LOT of points that I just was spinning my wheels uselessly on. >> Peter said earlier in this thread: >> >> >> If you want to trigger the >> >> if __name__ == "__main__": ... >> >> you have to invoke the test script with >> >> py ./mcm/test/db/test_manager.py >> >> the same way you would invoke any other script. >> - >> >> If I try this or begin in E:\Projects\mcm and type py >> ./test/db/test_manager.py I get >> >> E:\Projects\mcm>py ./test/db/test_manager.py >> Traceback (most recent call last): >> File "./test/db/test_manager.py", line 16, in >> import mcm.db.manager >> ImportError: No module named 'mcm' This final question is resolved by setting the PYTHONPATH environment variable as above. I just want to note that this is *not* an instance where a FULL path needs to be used to run test_manager.py as I had earlier tried just above. Now after setting PYTHONPATH I get: E:\Projects\mcm>py ./test/db/test_manager.py . -- Ran 1 test in 0.000s OK I think this wraps up everything I was struggling with. Have I missed anything? boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Using lambda
I am beginner in pythonI see the use of lambda has been for really simple ones as in the numerous examples over the net.Why cant we use lambda in another one like g = lambda x: (lambda y: y + 1) + 1when I am able to do that in two lines h = lambda x: x + 1>>> h(12)13y = lambda x: h(x) + 1>>> y(1)3 thanksrakesh ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor