Re: [Tutor] Dynamic Function Calls
All three methods are defined below the snippet I provided. def func(): code... def func0(): do stuff def func1(): do stuff def func2(): do stuff Megan Land FVT Blade EMET Test Engineer ml...@us.ibm.com From: Kent Johnson To: Megan Land/Raleigh/Contr/i...@ibmus Cc: tutor@python.org Date: 08/13/2009 05:18 PM Subject:Re: [Tutor] Dynamic Function Calls Sent by:kent3...@gmail.com On Thu, Aug 13, 2009 at 3:30 PM, Megan Land wrote: > Hi, > > I'm trying to call a function from a dictionary. I did some googling and > from what I can tell my code should work, but doesn't. Here's an example: > > def myFunc(self, inputList): > dict={0: func0, 1: func1, 2:func2} > for element in inputList: > dict[element]() > > When I go to run this I get an error saying func0 is not defined. Does > anyone have any ideas as to why this won't work? I'm using Python 2.6 if > that makes any difference. You don't show any definition for func0 in the above snippet. Where is it defined? Kent <><>___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] rationale for nested classes?
Hi everyone, I was wondering if there's anyone who can offer a use case/rationale for nested class? For, instance, in the following example (taken from here: http://www.brpreiss.com/books/opus7/html/page598.html): class A(object): def __init__(self): self.y = 0 class B(object): def __init__(self): self.x = 0 def f(self): pass My initial thought was that perhaps they inherit the behavior of parent classes, but that apparently is not the case: Are there specific situations when nested classes come in handy (perhaps for grouping conceptually related classes that don't share attributes?). Or is it typically better to keep all classes on the same level of a heirarchy, and then just use inheritance to establish parent-child relationships where appropriate? Thanks! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rationale for nested classes?
2009/8/14 Geneviève DIAGORN : > Bonjour, > Je suis absente jusqu'au 02/09 inclus. > Good to know. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamic Function Calls
Megan Land wrote: All three methods are defined below the snippet I provided. In Python names must be defined before they are referenced. Put these defs above the snippet. def func(): code... def func0(): do stuff def func1(): do stuff def func2(): do stuff Megan Land FVT Blade EMET Test Engineer ml...@us.ibm.com Inactive hide details for Kent Johnson ---08/13/2009 05:18:10 PM---On Thu, Aug 13, 2009 at 3:30 PM, Megan Land---08/13/2009 05:18:10 PM---On Thu, Aug 13, 2009 at 3:30 PM, Megan Land wrote: > Hi, From: Kent Johnson To: Megan Land/Raleigh/Contr/i...@ibmus Cc: tutor@python.org Date: 08/13/2009 05:18 PM Subject: Re: [Tutor] Dynamic Function Calls Sent by: kent3...@gmail.com On Thu, Aug 13, 2009 at 3:30 PM, Megan Land wrote: > Hi, > > I'm trying to call a function from a dictionary. I did some googling and > from what I can tell my code should work, but doesn't. Here's an example: > > def myFunc(self, inputList): > dict={0: func0, 1: func1, 2:func2} > for element in inputList: > dict[element]() > > When I go to run this I get an error saying func0 is not defined. Does > anyone have any ideas as to why this won't work? I'm using Python 2.6 if > that makes any difference. You don't show any definition for func0 in the above snippet. Where is it defined? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] awk, test
Hi Todd, Did you get your searches running? Have you had a chance to look at my exam? Are you planning to go next door for beer some day next week? If so, do you know when. I'd like to stop by and chat. Best, Vincent -- Assistant Professor of Biology Farmingdale State College (State University of New York) PhD Candidate Committee on the Conceptual and Historical Studies of Science University of ChicagoEnding soon! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rationale for nested classes?
Serdar Tumgoren wrote: Hi everyone, I was wondering if there's anyone who can offer a use case/rationale for nested class? In my Python Pipelines program I have: class Count: ... class Counter: ... When an instance of Count is created, one or more instances of Counter are created, belonging to that instance. There is no need for the Counter class to be visible anywhere outside the Count class. Nesting the definition makes that clear and puts the definition where it is easy to find. Also some day (down the road) there might be some other class in which I'd like to create instances of another class named Counter. Nesting the Counter classes makes that possible. For, instance, in the following example (taken from here: http://www.brpreiss.com/books/opus7/html/page598.html): class A(object): def __init__(self): self.y = 0 class B(object): def __init__(self): self.x = 0 def f(self): pass My initial thought was that perhaps they inherit the behavior of parent classes, but that apparently is not the case: Are there specific situations when nested classes come in handy (perhaps for grouping conceptually related classes that don't share attributes?). Or is it typically better to keep all classes on the same level of a heirarchy, and then just use inheritance to establish parent-child relationships where appropriate? -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rationale for nested classes?
"Serdar Tumgoren" wrote I was wondering if there's anyone who can offer a use case/rationale for nested class? It provides another level of name hiding but personally I don;t think there is much benefit. If you keep the related classses in the same module that should be good enough IMHO. But the fact that you can have nested classes isn't a bad thing, it keeps Python consistent, but its not too userful in my view. class A(object): class B(object): My initial thought was that perhaps they inherit the behavior of parent classes, but that apparently is not the case: Nope, inheritance is done by inheritance! :-) Are there specific situations when nested classes come in handy (perhaps for grouping conceptually related classes that don't share attributes?). Really only used if you have a special kind of thing that is only ever going to be used inside your outer class. But its only an indicator because you can still use A.B to access the class from outside! So its a mild deterrant not a real barrier. Or is it typically better to keep all classes on the same level of a heirarchy, and then just use inheritance to establish parent-child relationships where appropriate? Yes, but nesting really has nothing to do with inheritance. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamic Function Calls
bob gailer wrote: Megan Land wrote: All three methods are defined below the snippet I provided. In Python names must be defined before they are referenced. Put these defs above the snippet. def func(): code... def func0(): do stuff def func1(): do stuff def func2(): do stuff Megan Land FVT Blade EMET Test Engineer ml...@us.ibm.com Inactive hide details for Kent Johnson ---08/13/2009 05:18:10 PM---On Thu, Aug 13, 2009 at 3:30 PM, Megan Land---08/13/2009 05:18:10 PM---On Thu, Aug 13, 2009 at 3:30 PM, Megan Land wrote: > Hi, From: Kent Johnson To: Megan Land/Raleigh/Contr/i...@ibmus Cc: tutor@python.org Date: 08/13/2009 05:18 PM Subject: Re: [Tutor] Dynamic Function Calls Sent by: kent3...@gmail.com On Thu, Aug 13, 2009 at 3:30 PM, Megan Land wrote: > Hi, > > I'm trying to call a function from a dictionary. I did some googling and > from what I can tell my code should work, but doesn't. Here's an example: > > def myFunc(self, inputList): > dict={0: func0, 1: func1, 2:func2} > for element in inputList: > dict[element]() > > When I go to run this I get an error saying func0 is not defined. Does > anyone have any ideas as to why this won't work? I'm using Python 2.6 if > that makes any difference. You don't show any definition for func0 in the above snippet. Where is it defined? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor You can put these defs in any order. But when you invoke the function from your outerlevel code, all of them need to have been defined. I'm guessing you had these in this order: def myFunc(self, inputList): dictionary={0: func0, 1: func1, 2:func2} for element in inputList: dictionary[element]()... myFunc(3, 1, 2, 1) #this is too early in the file, because the following defs have not been defined yet def func(): code... def func0(): do stuff def func1(): do stuff def func2(): do stuff #move the call to myFunc() here Move the outerlevel code to the end, and you're usually better off. You also might want to put it inside an if __name__ == "__main__": clause. Note, I also changed the variable 'dict' to 'dictionary,' since dict already has a meaning in Python. Not a big deal in this particular case, but if you ever wanted to convert a list to a dict, and called dict(), you'd wonder what went wrong. Better to kill the habit early. DaveA ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamic Function Calls
From: Dave Angel To: bob gailer Cc: Megan Land/Raleigh/Contr/i...@ibmus, tutor@python.org Date: 08/14/2009 01:53 PM Subject:Re: Re: [Tutor] Dynamic Function Calls bob gailer wrote: > Megan > Land wrote: >> >> All three methods are defined below the snippet I provided. >> > > In Python names must be defined before they are referenced. Put these > defs above the snippet. > >> >> def func(): >> code... >> def func0(): >> do stuff >> def func1(): >> do stuff >> def func2(): >> do stuff >> >> Megan Land >> FVT Blade EMET Test Engineer >> ml...@us.ibm.com >> >> Inactive hide details for Kent Johnson ---08/13/2009 05:18:10 PM---On >> Thu, Aug 13, 2009 at 3:30 PM, Megan Land> ---08/13/2009 05:18:10 PM---On Thu, Aug 13, 2009 at 3:30 PM, Megan >> Land wrote: > Hi, >> >> >> From: >> Kent Johnson >> >> To: >> Megan Land/Raleigh/Contr/i...@ibmus >> >> Cc: >> tutor@python.org >> >> Date: >> 08/13/2009 05:18 PM >> >> Subject: >> Re: [Tutor] Dynamic Function Calls >> >> Sent by: >> kent3...@gmail.com >> >> >> >> >> >> On Thu, Aug 13, 2009 at 3:30 PM, Megan Land wrote: >> > Hi, >> > >> > I'm trying to call a function from a dictionary. I did some >> googling and >> > from what I can tell my code should work, but doesn't. Here's an >> example: >> > >> > def myFunc(self, inputList): >> > dict={0: func0, 1: func1, 2:func2} >> > for element in inputList: >> > dict[element]() >> > >> > When I go to run this I get an error saying func0 is not defined. Does >> > anyone have any ideas as to why this won't work? I'm using Python >> 2.6 if >> > that makes any difference. >> >> You don't show any definition for func0 in the above snippet. Where is >> it defined? >> >> Kent >> >> >> >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> > You can put these defs in any order. But when you invoke the function from your outerlevel code, all of them need to have been defined. I'm guessing you had these in this order: def myFunc(self, inputList): dictionary={0: func0, 1: func1, 2:func2} for element in inputList: dictionary[element]()... myFunc(3, 1, 2, 1) #this is too early in the file, because the following defs have not been defined yet def func(): code... def func0(): do stuff def func1(): do stuff def func2(): do stuff #move the call to myFunc() here Move the outerlevel code to the end, and you're usually better off. You also might want to put it inside an if __name__ == "__main__": clause. Note, I also changed the variable 'dict' to 'dictionary,' since dict already has a meaning in Python. Not a big deal in this particular case, but if you ever wanted to convert a list to a dict, and called dict(), you'd wonder what went wrong. Better to kill the habit early. DaveA I have the method inside a main method at the end of my program. The weird thing is that I typed up my small example and ran it and it worked fine. But when I run my big program, I still get the error that func0, as I call it in my example, is not defined. Do you think there could be something else in my program that is making this go wrong? Megan Land FVT Blade EMET Test Engineer ml...@us.ibm.com<><>___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] rationale for nested classes?
Okay, those explanations definitely help. I thought I had run into a situation where nested classes might be called for, but I think plain-old inheritance is really what I'm after. Many thanks! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] calculate values of raster from vector
Is there a way I could read in a raster image, read in a shapefile image and then calculate the mean and standard deviation of the raster values within the shapefile? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] [PyQt4] Parent problems with QThread and QProgressDialog
Hi All, Benefiting from this excellent ticket: http://snippets.prendreuncafe.com/snippets/tagged/pyqt/order_by/date ... I can implement the behavior that I want and with QThread QProgressDialog (knowing that the progress bar updates whenever a new file is processed) in my application. Nevertheless I can not get rid of the parent window (the QProgressDialog comes with its parent window, which is really ugly). So I would like to get rid of this window relative to QProgressDialog that can display properly during treatment. How ?. In addition I would like to display the file being processed in the progress bar, but I can not (for now all files loaded are displayed in the bar). You can see the code here: http://pastebin.com/d71e7bd6b ... and the screenshot showing the parent window that cause me problems here: http://irruption.net/bla/ekd/ekd_developp/14_08_09_problem_avec_parent_dans_le_QThread_001.jpg Can you help me solve these problems ?. Excuse me for my bad English (it's a google translation of my message in french). I Hope you can help me. a+ -- http://ekd.tuxfamily.org http://lprod.org/wiki/doku.php/video:encodage:avchd_converter ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamic Function Calls
Megan Land wrote: From: Dave Angel To: bob gailer Cc: Megan Land/Raleigh/Contr/i...@ibmus, tutor@python.org Date: 08/14/2009 01:53 PM Subject:Re: Re: [Tutor] Dynamic Function Calls bob gailer wrote: Megan Land wrote: All three methods are defined below the snippet I provided. In Python names must be defined before they are referenced. Put these defs above the snippet. def func(): code... def func0(): do stuff def func1(): do stuff def func2(): do stuff Megan Land FVT Blade EMET Test Engineer ml...@us.ibm.com Inactive hide details for Kent Johnson ---08/13/2009 05:18:10 PM---On Thu, Aug 13, 2009 at 3:30 PM, Megan Land wrote: > Hi, From: Kent Johnson To: Megan Land/Raleigh/Contr/i...@ibmus Cc: tutor@python.org Date: 08/13/2009 05:18 PM Subject: Re: [Tutor] Dynamic Function Calls Sent by: kent3...@gmail.com On Thu, Aug 13, 2009 at 3:30 PM, Megan Land wrote: Hi, I'm trying to call a function from a dictionary. I did some googling and from what I can tell my code should work, but doesn't. Here's an example: def myFunc(self, inputList): dict={0: func0, 1: func1, 2:func2} for element in inputList: dict[element]() When I go to run this I get an error saying func0 is not defined. Does anyone have any ideas as to why this won't work? I'm using Python 2.6 if that makes any difference. You don't show any definition for func0 in the above snippet. Where is it defined? Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor You can put these defs in any order. But when you invoke the function from your outerlevel code, all of them need to have been defined. I'm guessing you had these in this order: def myFunc(self, inputList): dictionary={0: func0, 1: func1, 2:func2} for element in inputList: dictionary[element]()... myFunc(3, 1, 2, 1) #this is too early in the file, because the following defs have not been defined yet def func(): code... def func0(): do stuff def func1(): do stuff def func2(): do stuff #move the call to myFunc() here Move the outerlevel code to the end, and you're usually better off. You also might want to put it inside an if __name__ == "__main__": clause. Note, I also changed the variable 'dict' to 'dictionary,' since dict already has a meaning in Python. Not a big deal in this particular case, but if you ever wanted to convert a list to a dict, and called dict(), you'd wonder what went wrong. Better to kill the habit early. DaveA I have the method inside a main method at the end of my program. The weird thing is that I typed up my small example and ran it and it worked fine. But when I run my big program, I still get the error that func0, as I call it in my example, is not defined. Do you think there could be something else in my program that is making this go wrong? Megan Land FVT Blade EMET Test Engineer ml...@us.ibm.com Something's wrong with your email program's quoting logic. So your last response looks like it's part of my last email. ::: Do you think there could be something else Yes, certainly. Question is what. You use the word method a couple of times above, yet none of your code shows classes. Was that a typo? First thing is you should stop paraphrasing and describing, and include actual code, actual full error messages, and so on. If you had actually run a small sample, you would have found out then that it worked, and that your description doesn't match the problem you're having with the full code. Once you have a real code example that fails, use copy/paste so we see it exactly as it is. Next, you should realize that an error like : Traceback (most recent call last): File "", line 1, in NameError: name 'xyzzy' is not defined means that
Re: [Tutor] write program to extract data
Michael Miesner wrote: Hi- I work in a research lab and part of the lab I'm not usually associated with uses a program that outputs data in a .txt file for each participant that is run. The participant # is the title of the text document (ie E00343456.txt) style and I'd like to be able to take this and other data in the file and draw it into a spreadsheet. The first 3/4 of the output is the scenario. I've bolded the areas that I really want to be able to draw out. In case some people cant see the bold, they are the sections called "driver mistakes" and individual mistakes. Preferably, what I'd really like to do is make the script so that I execute it, and in doing so, tell it what folder to look in, and it takes all the .txt's out of that folder, and adds them to the spreadsheet. I'd like the title of the .txt to be the first column, and the data held in the spreadsheet to be the be the next columns. Below is output of 1 data file. Date: August 13, 2009 Time: 12:16:18:151 PM ID: Scenario file: C:\Documents and Settings\APL02\Desktop\Driving Sim Files\Sim 10-21-08.txt Configuration file: 7 0 0 0 204 130.55 0 999.00 8 0 0 0 9 0 0 0 10 0 0 0 *Driver mistakes: Total number of off road accidents = 1 Total number of collisions = 3 Total number of pedestrians hit = 3 Total number of speed exceedances = 11 Total number of speeding tickets =0 Total number of traffic light tickets = 1 Total number of stop signs missed = 0 Total number of centerline crossings =5 Total number of road edge excursions =4 Total number of stops at traffic lights = 2 Total number of correct DA responses =0 Total number of incorrect DA responses = 0 Total number of DAs with no response =0 Total run length (Drive T, X, Total T) = 761.92 34000 761.92 Total number of illegal turns = 0 Total number of low speed warnings = 0 Total number of high speed warnings = 0 Over speed limit (% Time, % Distance) = 27.1547.77 Out of lane (% Time, % Distance) =4.22 3.74 Individual mistakes (Time, Distance, Elapsed distance or object number, Elapsed time, Maximum value): Centerline crossing 68.16 3311.11 110.692.60 -3.46 Centerline crossing 94.26 4598.99 252.834.45 -3.19 Centerline crossing 127.85 6758.39 109.701.92 -2.78 Centerline crossing 162.14 9082.09 273.043.27 -9.72 Speed exceedance 162.80 9135.05 4596.82 48.87 120.00 Road edge excursion 166.34 9438.89 77.080.83 13.45 Hit pedestrian 204.4613731.87 1 Speed exceedance 221.5113829.40 912.17 22.75 67.13 Hit pedestrian 237.0514741.43 5 Speed exceedance 259.9315171.25 746.18 13.35 60.35 Centerline crossing 317.6916126.40 14.768.15 -4.37 Vehicle collision (F)318.6316141.15 66 Vehicle collision (F)341.9116166.52 91 Red light ticket 452.0416548.74 2 Speed exceedance 458.2416802.82 1484.23 18.15 95.58 Speed exceedance 516.1420362.78 685.87 10.13 68.67 Speed exceedance 551.2421996.05 2111.93 24.31 91.76 Vehicle collision (F)580.5824459.77 163 Speed exceedance 606.4925376.76 3929.19 42.84 120.00 Road edge excursion 622.0026916.68 86.740.77 13.30 Road edge excursion 623.6227100.59 251.812.17 14.42 Road edge excursion 641.3129212.44 93.528.02 19.52 Off road accident642.1129305.93 Speed exceedance 664.9930081.20 777.80 10.76 82.64 Hit pedestrian 695.2931013.94 30 Speed exceedance 719.3031513.55 915.64 14.16 71.88 Speed exceedance 742.4632837.36 81.721.57 52.90 Speed exceedance 749.7633199.52 801.52 12.16 76.04 * Did you really have to quote all 1100 lines, when what you really wanted to say was there's a bunch of junk at the beginning of the file, then the following stuff ? Anyway, the most important question is how rigid is this file format? Will there always be exactly one line starting "* Driver mistakes"? Will the lines between that and the final "*" always be the same ones, and in the same order? Or is "Road edge excursion" somehow an optional line. or whatever? Are the numbers identified by the column they start in, or by spaces between them? Is a zero stored sometimes as an empty field? It already appears that some lines are appearing multiple times, and not in order. Assuming that's deliberate, is the
Re: [Tutor] write program to extract data
Whoops, I forgot the reply-all. On Fri, Aug 14, 2009 at 3:03 PM, Michael Miesner wrote: > That is one distinct advantage, It'll always be just like that. However, I > dont know where to even begin; I dont know what command pulls text out > ($string?) or how to import it into a spreadsheet. > Alan Gauld has a spiffy python tutorial here: http://www.freenetpages.co.uk/hp/alan.gauld/ I presume you have at least a little programming experience. Python is really simple to pick up if you've done any other language. Check out "Handling Text" and "Handling Files" for some specific pointers. Are you familiar with comma separated value (.csv) files? It's fairly trivial to import those into a spreadsheet program, and if you're the one writing the .csv it's not too difficult to get them written the right way. HTH, Wayne > > > On Fri, Aug 14, 2009 at 3:54 PM, Wayne wrote: > >> On Fri, Aug 14, 2009 at 12:08 PM, Michael Miesner < >> michael.mies...@gmail.com> wrote: >> >>> Hi- >>> I work in a research lab and part of the lab I'm not usually associated >>> with uses a program that outputs data in a .txt file for each participant >>> that is run. >>> The participant # is the title of the text document (ie E00343456.txt) >>> style and I'd like to be able to take this and other data in the file and >>> draw it into a spreadsheet. >>> The first 3/4 of the output is the scenario. I've bolded the areas that I >>> really want to be able to draw out. In case some people cant see the bold, >>> they are the sections called "driver mistakes" and individual mistakes. >>> >>> >>> Preferably, what I'd really like to do is make the script so that I >>> execute it, and in doing so, tell it what folder to look in, and it takes >>> all the .txt's out of that folder, and adds them to the spreadsheet. I'd >>> like the title of the .txt to be the first column, and the data held in the >>> spreadsheet to be the be the next columns. >> >> >>> >>> >>> >>> *Driver mistakes: >>> >>> Total number of off road accidents = 1 >>> Total number of collisions = 3 >>> Total number of pedestrians hit = 3 >>> Total number of speed exceedances = 11 >>> Total number of speeding tickets =0 >>> Total number of traffic light tickets = 1 >>> Total number of stop signs missed = 0 >>> Total number of centerline crossings =5 >>> Total number of road edge excursions =4 >>> Total number of stops at traffic lights = 2 >>> Total number of correct DA responses =0 >>> Total number of incorrect DA responses = 0 >>> Total number of DAs with no response =0 >>> Total run length (Drive T, X, Total T) = 761.92 34000 >>> 761.92 >>> Total number of illegal turns = 0 >>> Total number of low speed warnings = 0 >>> Total number of high speed warnings = 0 >>> Over speed limit (% Time, % Distance) = 27.1547.77 >>> Out of lane (% Time, % Distance) =4.22 3.74 >>> >>> Individual mistakes (Time, Distance, Elapsed distance or object number, >>> Elapsed time, Maximum value): >>> >>> Centerline crossing 68.16 3311.11 110.692.60 >>> -3.46 >>> Centerline crossing 94.26 4598.99 252.834.45 >>> -3.19 >>> Centerline crossing 127.85 6758.39 109.701.92 >>> -2.78 >>> Centerline crossing 162.14 9082.09 273.043.27 >>> -9.72 >>> Speed exceedance 162.80 9135.05 4596.82 48.87 >>> 120.00 >>> Road edge excursion 166.34 9438.89 77.080.83 >>> 13.45 >>> Hit pedestrian 204.4613731.87 1 >>> Speed exceedance 221.5113829.40 912.17 22.75 >>> 67.13 >>> Hit pedestrian 237.0514741.43 5 >>> Speed exceedance 259.9315171.25 746.18 13.35 >>> 60.35 >>> Centerline crossing 317.6916126.40 14.768.15 >>> -4.37 >>> Vehicle collision (F)318.6316141.15 66 >>> Vehicle collision (F)341.9116166.52 91 >>> Red light ticket 452.0416548.74 2 >>> Speed exceedance 458.2416802.82 1484.23 18.15 >>> 95.58 >>> Speed exceedance 516.1420362.78 685.87 10.13 >>> 68.67 >>> Speed exceedance 551.2421996.05 2111.93 24.31 >>> 91.76 >>> Vehicle collision (F)580.5824459.77 163 >>> Speed exceedance 606.4925376.76 3929.19 42.84 >>> 120.00 >>> Road edge excursion 622.0026916.68 86.740.77 >>> 13.30 >>> Road edge excursion 623.6227100.59 251.812.17 >>> 14.42 >>> Road edge excursion 641.3129212.44 93.528.02 >>> 19.52 >>> Off road accident642.1129305.93 >>> Speed exceedance 664.9930081.20 777.80 10.76 >>> 82.64 >>> Hit pedestrian 695.2931013.94 30 >>> Speed excee
Re: [Tutor] Dynamic Function Calls
Dave Angel wrote on 08/14/2009 03:53:30 PM: > From: > > Dave Angel > > To: > > Megan Land/Raleigh/Contr/i...@ibmus > > Cc: > > bob gailer , tutor@python.org > > Date: > > 08/14/2009 03:53 PM > > Subject: > > Re: [Tutor] Dynamic Function Calls > > Megan Land wrote: > > > > From: Dave Angel > > > > To: bob gailer > > > > Cc: Megan Land/Raleigh/Contr/i...@ibmus, tutor@python.org > > > > Date: 08/14/2009 01:53 PM > > > > Subject:Re: Re: [Tutor] Dynamic Function Calls > > > > > > > > > > > > > > > > > > > > bob gailer wrote: > > > >> Megan > >> Land wrote: > >> > >>> All three methods are defined below the snippet I provided. > >>> > >>> > >> In Python names must be defined before they are referenced. Put these > >> defs above the snippet. > >> > >> > >>> def func(): > >>> code... > >>> def func0(): > >>> do stuff > >>> def func1(): > >>> do stuff > >>> def func2(): > >>> do stuff > >>> > >>> Megan Land > >>> FVT Blade EMET Test Engineer > >>> ml...@us.ibm.com > >>> > >>> Inactive hide details for Kent Johnson ---08/13/2009 05:18:10 PM---On > >>> Thu, Aug 13, 2009 at 3:30 PM, Megan Land >>> ---08/13/2009 05:18:10 PM---On Thu, Aug 13, 2009 at 3:30 PM, Megan > >>> Land wrote: > Hi, > >>> > >>> > >>> From: > >>> Kent Johnson > >>> > >>> To: > >>> Megan Land/Raleigh/Contr/i...@ibmus > >>> > >>> Cc: > >>> tutor@python.org > >>> > >>> Date: > >>> 08/13/2009 05:18 PM > >>> > >>> Subject: > >>> Re: [Tutor] Dynamic Function Calls > >>> > >>> Sent by: > >>> kent3...@gmail.com > >>> > >>> > >>> > >>> > >>> > >>> On Thu, Aug 13, 2009 at 3:30 PM, Megan Land wrote: > >>> > Hi, > > I'm trying to call a function from a dictionary. I did some > > >>> googling and > >>> > from what I can tell my code should work, but doesn't. Here's an > > >>> example: > >>> > def myFunc(self, inputList): > dict={0: func0, 1: func1, 2:func2} > for element in inputList: > dict[element]() > > When I go to run this I get an error saying func0 is not defined. Does > anyone have any ideas as to why this won't work? I'm using Python > > >>> 2.6 if > >>> > that makes any difference. > > >>> You don't show any definition for func0 in the above snippet. Where is > >>> it defined? > >>> > >>> Kent > >>> > >>> > >>> > >>> ___ > >>> Tutor maillist - Tutor@python.org > >>> http://mail.python.org/mailman/listinfo/tutor > >>> > >>> > > You can put these defs in any order. But when you invoke the function > > from your outerlevel code, all of them need to have been defined. I'm > > guessing you had these in this order: > > > > > > def myFunc(self, inputList): > > dictionary={0: func0, 1: func1, 2:func2} > > for element in inputList: > > dictionary[element]()... > > > > myFunc(3, 1, 2, 1) #this is too early in the file, > > because the following defs have not been defined yet > > > > def func(): > > code... > > def func0(): > > do stuff > > def func1(): > > do stuff > > def func2(): > > do stuff > > > > #move the call to myFunc() here > > > > > > > > Move the outerlevel code to the end, and you're usually better off. You > > also might want to put it inside an > > if __name__ == "__main__": > > > > clause. > > > > > > Note, I also changed the variable 'dict' to 'dictionary,' since dict > > already has a meaning in Python. Not a big deal in this particular > > case, but if you ever wanted to convert a list to a dict, and called > > dict(), you'd wonder what went wrong. Better to kill the habit early. > > > > DaveA > > > > > > I have the method inside a main method at the end of my program. The weird > > thing is that I typed up my small example and ran it and it worked fine. > > But when I run my big program, I still get the error that func0, as I call > > it in my example, is not defined. Do you think there could be something > > else in my program that is making this go wrong? > > > > > > Megan Land > > FVT Blade EMET Test Engineer > > ml...@us.ibm.com > > > Something's wrong with your email program's quoting logic. So your last > response looks like it's part of my last email. > > ::: Do you think there could be something else > > Yes, certainly. Question is what. You use the word method a couple of > times above, yet none of your code shows classes. Was that a typo? > > First thing is you should stop paraphrasing and describing, and include > actual code, actual full error messages, and so on. If you had actually > run a small sample, you would have found out then that it worked, and > that your description doesn't match the problem you're having with the > full code. Once you have a real code example that fails, use copy/paste > so we see it exactly as it is
Re: [Tutor] Tutor Digest, Vol 66, Issue 38
Howdy all, Hope this email finds you all well - roll on the weekend. I've been tasked with replicating an Excel spreadsheet. The spreadsheet contains cells that use Excels' NORMDIST() function. I have all 3 variables required - value, mean and standard deviation - but does anyone already have equivalent python code for this function? Many thanks! Cheers, Garry PS: Excel Help: NORMDIST Returns the normal distribution for the specified mean and standard deviation. This function has a very wide range of applications in statistics, including hypothesis testing. Syntax NORMDIST(x,mean,standard_dev,cumulative) X is the value for which you want the distribution. Mean is the arithmetic mean of the distribution. Standard_dev is the standard deviation of the distribution. Cumulative is a logical value that determines the form of the function. If cumulative is TRUE, NORMDIST returns the cumulative distribution function; if FALSE, it returns the probability mass function. Remarks If mean or standard_dev is nonnumeric, NORMDIST returns the #VALUE! error value. If standard_dev ≤ 0, NORMDIST returns the #NUM! error value. If mean = 0, standard_dev = 1, and cumulative = TRUE, NORMDIST returns the standard normal distribution, NORMSDIST. When cumulative = TRUE, the formula is the integral from negative infinity to x of the given formula. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Normal Distribution
Sorry! Now with an appropriate subject line... G. Howdy all, Hope this email finds you all well - roll on the weekend. I've been tasked with replicating an Excel spreadsheet. The spreadsheet contains cells that use Excels' NORMDIST() function. I have all 3 variables required - value, mean and standard deviation - but does anyone already have equivalent python code for this function? Many thanks! Cheers, Garry PS: Excel Help: NORMDIST Returns the normal distribution for the specified mean and standard deviation. This function has a very wide range of applications in statistics, including hypothesis testing. Syntax NORMDIST(x,mean,standard_dev,cumulative) X is the value for which you want the distribution. Mean is the arithmetic mean of the distribution. Standard_dev is the standard deviation of the distribution. Cumulative is a logical value that determines the form of the function. If cumulative is TRUE, NORMDIST returns the cumulative distribution function; if FALSE, it returns the probability mass function. Remarks If mean or standard_dev is nonnumeric, NORMDIST returns the #VALUE! error value. If standard_dev ≤ 0, NORMDIST returns the #NUM! error value. If mean = 0, standard_dev = 1, and cumulative = TRUE, NORMDIST returns the standard normal distribution, NORMSDIST. When cumulative = TRUE, the formula is the integral from negative infinity to x of the given formula. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dynamic Function Calls
Megan Land wrote: I tried what you said. I can call the function by itself (not within the dictionary). I also printed the help(__name__). All of my functions were listed. Here's a more representative example of my code (I doing this for work so I don't want to give my entire program away): import os class Test(): def func(self): funcDict={".txt":text, ".html":html} exList=os.listdir("/home/megan/test") for i in exList: (filePath, fileName)=os.path.split(i) (name, extension)=os.path.splitext(fileName) self.funcDict[extension]() def text(self): print "This is a text file" def html(self): print "This is an html file" def main(): newTest=Test() newTest.func() if __name__=="__main__": main() Traceback (most recent call last): File "test.py", line 23, in main() File "test.py", line 19, in main newTest.func() File "test.py", line 4, in func funcDict={".txt":text, ".html":html} NameError: global name 'text' is not defined It worked fine until I put it inside of a class, but I'm not sure what that would have to do with it. I don't need to have use all that, but now that everything is setup like that I want to keep it that way. Any help you can provide will be greatly appreciated. Megan Land As you said, it worked fine till you put those two function definitions inside a class. As I guessed earlier, your use of the word "method" was a tipoff. Anyway, within the method func(), an unqualified name will be searched for locally (an attribute of func()), then globally (within the module, at global scope). It does not search for other methods. Still, the first question I have to ask is "why are text() and html() instance methods?" You're not using self anywhere in those methods, but maybe you are in your actual code. I have to assume something, so I'll assume you need self for all three methods. In this case, simply change the line to: funcDict={".txt":self.text, ".html":self.html} and it should work fine. You're creating bound methods, which already know their self-value when they're placed in the funcDict. If I assume they don't need self, I'd just move them to global scope, and forget about it. But if you just can't do that, then how about nested functions? (I had already added the parameter "name" for testing purposes, so please forgive that change below.) def func(self): def text(name): print "This", name, "is also a text file, nested" def html(name): print "This", name, "is also an html file, nested" funcDict={".txt":self.text, ".html":self.html} exList=os.listdir(".") for i in exList: (filePath, fileName)=os.path.split(i) (name, extension)=os.path.splitext(fileName) if extension in funcDict: funcDict[extension](name) I believe you could also do something with the @static decorator, but I haven't played with it, and it doesn't look like you need it anyway. DaveA ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor