Re: [Tutor] Using lambda
On 24Aug2015 12:08, rakesh sharma wrote: 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) + 1 when I am able to do that in two lines >>> h = lambda x: x + 1 >>> h(12) 13 >>> y = lambda x: h(x) + 1 >>> y(1) 3 Hi, First, please include more whitespace in your posts to make them easier to read. If you are includining line breaks etc, I think something is eating them. Regarding your question, you can do what you ask. You suggestion was: g = lambda x: (lambda y: y + 1) + 1 The thing you've missed is that a lambda is a function; you need to call it. Your example only defines a lambda but doesn't call it. Try this: g = lambda x: (lambda y: y + 1)(x) + 1 which passes x through to the inner lambda: >>> g(12) 14 So yes, you can do it. But as you can see it doesn't make for very readable code. Lambdas are ok for small expressions. When things get more complex it is worth breaking them up. Cheers, Cameron Simpson ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Using lambda
On 24/08/15 07:38, rakesh sharma wrote: 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 Your email has arrived with no formatting so its hard to work out your examples. You can use lambda in multiple lines but it is limited to a single expression. >>> ml = lambda n: ( ... 5*n + ( ... n*n - ( ... 25))) >>> ml(7) 59 >>> And you can use lambda within a lambda: >>> adder = lambda x: lambda n: n+x >>> adder(3) at 0x7fe3dca0ccf8> >>> add3 = adder(3) >>> add3(5) 8 >>> But for longer functions you are better using def to define them. In Python lambda is not the underlying mechanism for creating functions it is a convenience feature. Python is not Lisp. -- 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 23:52, Laura Creighton wrote: 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 Thanks Laura, That does exactly what I wanted to do. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python help
[Tutor] Python help IDN3 iradn3777 at gmail.com Thu Aug 13 03:01:12 CEST 2015 Previous message (by thread): [Tutor] revisiting a puzzle about -3**2 vs (-3)**2 Next message (by thread): [Tutor] Python help Messages sorted by: [ date ] [ thread ] [ subject ] [ author ] To whom it may concern, I am having a problem solving the below question. I have used every resource that I could find to help me, but I'm seeing nothing. Can someone out there please help me understand the below question and learn how to accomplish this task in Python? I would really appreciate any help that someone could afford. *Problem 1:* Write a program that will calculate the problem and stop after the condition has been met. a=number of loops (start with zero) b=a+1 c=a+b Condition: If c is less than 5, then the loop will continue; else, it will end. 3. *Problem 2:*Print a string variable that states the number of loops required to meet the condition for Problem 1. My attempt below. I used a while loop even though the question is saying IF/THEN/ELSE. To my knowledge loops in Python have to be while/for. Also, it seems like the question is missing some direction, but I could be wrong. Thank you for your help. a = 0 b = a + 1 c = a + b while (c < 5): print(c) c = c + 1 Yes, I agree the wording of the question is confusing. But I think the following solution makes sense for it. My solution is close to yours, with a few differences: . 1) the variable a will count the number of times the loop goes around. 2) variable a has to be incremented inside the while loop 3) both a and c have to be initialized to zero before the loop starts. 4) after the loop ends, print a string with the variable to tell how many times the loop went a = 0 #increments with each loop c = 0 while (c<5): b = a+1 c = a+b a += 1 print(c) print("Number of loops until c >= 5:", a) I think that is all that is being asked here. HTH! -William ___ 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 17:20, Laura Creighton wrote: Updating to Python 2.7.10 (default, Jul 1 2015, 10:54:53) and installing the tix-dev debian package, ... and I am not sure which of these fixed the problem, Presumably the 2.7.10 because adding tix-dev does nothing to fix it on 2.7.6... But 2.7.10 isn't available on Mint 17 yet so i can't confirm positively. -- 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 17:20, Laura Creighton wrote: Updating to Python 2.7.10 (default, Jul 1 2015, 10:54:53) and installing the tix-dev debian package, The seg fault is still there on Python 3.4 and 2.7.6 with tix-dev. It also happens regardless of the type of dialog I try (ExFileSelectDialog, FileSelectDialog, DirSelectDialog) Because its a seg fault rather than a Python error I'm guessing its at the C level in either the Python interpreter or in the calling mechanism into the native tix libraries. I further assume the native tix code is OK since michael lange got it working using native Tcl/tix. And since its working in both Python 3.5 and 2.7.10 I'm guessing somebody has fixed it as a known issue or fixed it accidentally in the code base and the fix has been ported to both the latest builds. One other option is tix.py itself. I've looked at the latest tix.py on my system and there are some "FixMe" lines so its conceivable that the latest Py releases have an updated tix.py. Can you check that Laura since I don't have either 3.5 or 2.7.10? But the FixMe's don't look like they would cause a core dump. This is beginning to bug me...Although my experience of tix on Python suggests its not unusual. There are several other useful widgets that I failed to get working. The documented widgets work fine but the undocumented ones not so much. :-( -- 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] filtering listed directories
On 22/08/15 15:42, Laura Creighton wrote: 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 just had a go at this but unfortunately it uses a C++ library to do the actual dialog creation. Various lines like: #this is where the open & save stuff happens if {$boxflag == "open"} { set filename $entry # A file is being opened, so we call the c++ function to open a file. # We call function cppOpenCD in the iGraph object, passing it the file set retVal [$iGraph cppOpenCD: $entry] ... So the cppOpenCD call in the iGraph library is the key to whatever is going on here. Busted again... :-( -- 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)
Maybe a tkinter bug? https://bugs.python.org/issue11077 There was a mismatch between the tkinter tests and the tkinter shipped with python 3.4, which I reported here: https://bugs.python.org/issue24858 -- this is a debian packaging problem and maybe more things are wrong with it. lac@smartwheels:/usr/lib/python3.5/tkinter$ grep FIXME tix.py # FIXME: It should inherit -superclass tixShell # FIXME: It should inherit -superclass tixLabelWidget # FIXME: It should inherit -superclass tixLabelWidget # FIXME: It should inherit -superclass tixScrolledHList # FIXME: It should inherit -superclass tixScrolledHList # FIXME: It should inherit -superclass tixDialogShell # FIXME: It should inherit -superclass tixDialogShell # FIXME: It should inherit -superclass tixStdDialogShell # FIXME: It should inherit -superclass tixLabelWidget # FIXME: return python object # FIXME: This is dangerous to expose to be called on its own. # FIXME: It should inherit -superclass tixShell # FIXME: It should inherit -superclass tixScrolledWidget # FIXME: It should inherit -superclass tixScrolledWidget # FIXME: It should inherit -superclass tixScrolledWidget # FIXME: It should inherit -superclass tixScrolledWidget # FIXME: It should inherit -superclass tixScrolledWidget # FIXME: It should inherit -superclass tixLabelWidget # FIXME: It should inherit from Shell # FIXME: It should inherit -superclass tixScrolledWidget # FIXME: It should inherit -superclass tixTree # FIXME: It should inherit -superclass tixScrolledWidget # FIXME: It should inherit -superclass tixScrolledWidget Those are the ones I have in 3.5. See if you have more. 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)
On 24/08/15 18:45, Laura Creighton wrote: # FIXME: It should inherit -superclass tixDialogShell # FIXME: It should inherit -superclass tixDialogShell # FIXME: It should inherit -superclass tixStdDialogShell Nope, these are the same FixMes that I saw. Its (still) a mystery... -- 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)
One other thing -- if you read bugs.python.org about tix issues you will get the strong idea that as far as python-dev is concerned, tix is on its way out, and ttk ttk is the way to go, so if you want your better widget to be part of the new python distribution, making it a ttk widget seems a good idea. 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)
On 24/08/15 19:23, Laura Creighton wrote: One other thing -- if you read bugs.python.org about tix issues you will get the strong idea that as far as python-dev is concerned, tix is on its way out, and ttk ttk is the way to go, so if you want your better widget to be part of the new python distribution, making it a ttk widget seems a good idea. They are very different beasts. ttk is about native styling. It doesn't include any new widgets that I'm aware of. Tix by contrast has the potential tyo offer a very powerful set of widgets as well as a framework for expansion. But tix does currently seem a fairly poor offering, PMW would IMHO have been a better option for an enhanced widget set. Its certainly more pythonic in nature. Although I'm not sure if its being maintained whereas Tix is at least under semi-active development in the Tcl world. But since I don't even participate much on the main list, let alone python-dev I can't really comment! :-) -- 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] Tkinter
First off, i dunno if im doing this right. Anyway, in tkinter, im making a program to store statistics, attributes, ect, and whenever you press a button labled "Add Attribute" it created a new text box, you have an unlimited number of text boxes to use. The problem is; they all have the same name, so the text you type in one of them is copied to all of the others. My question: How do i make a value go up by one when a button is clicked, and how do i treat somethings name(The something being the new attribute you add) as a property, so that when a new one is made, its name is "Att" and then whatever the value equals. Kinda like if you say - HiDerr = "Hai" Hue = "Hue" print(HiDerr + Hue) But with tkinter and GUI parts, or whatever you'd like to call it. Thanks. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tkinter
On 24/08/15 21:31, The Dildoge Gamer wrote: unlimited number of text boxes to use. The problem is; they all have the same name, so the text you type in one of them is copied to all of the others. My question: How do i make a value go up by one when a button is clicked, and how do i treat somethings name(The something being the new attribute you add) as a property, so that when a new one is made, its name is "Att" and then whatever the value equals. Kinda like if you say - You really shouldn't do that. It gets awfully complicated very quickly. Instead, use a list to store these text box references. You can then access them using an index into the list. Something like textBoxes = [] ... newTB = TextBox(...) textBoxes.append(newTB) ... textBoxes[3].insert(END,'Fourth text box') ... print(textboxes[1].get(...)) ... Alternatively you could use a dictionary and give each text box a string or numeric key. But in this case I suspect a list is a better fit for your situation. 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@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor