Re: [Tutor] Advise with coding
On 11/10/16 10:16, tracey jones-Francis wrote: > Is it possible someone on here can advise me where I am going wrong. Yesm, thats what this list is for but... We need to see code - we can't guess what you did. We need to know what input you used, what output you got and why its not what you expected. Also provide the full text of any error messages you get. Finally it ioften helpsa to know which OS and Python version you are using and, if applicable, what IDE you are using to write/run the code. If that seems a lot, it all helps to provide specific answers. The more specific your information is, the more specific our response can be,. -- 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] Advise with coding
Hi I have just started Com Sci in Uni and I am new to coding. I feel pretty inadequate as I am really struggling with the latest tasks. Most of the work is self-directed learning so we need to figure it out ourselves but I am running out of time. Is it possible someone on here can advise me where I am going wrong. Many thanks in anticipation T ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] advise on this loop
On Thu, Jun 4, 2009 at 3:48 PM, W W wrote: > On Thu, Jun 4, 2009 at 8:29 AM, Norman Khine wrote: >> >> Hello, >> I have the following code, which searches through a directory structure >> such as: >> >> $ tree -L 2 companies >> companies >> |-- aberdeen-airport-ltd >> | |-- aberdeen-airport-dyce-grampian >> | `-- aberdeen-airport-dyce-grampian.metadata >> |-- aberdeen-airport-ltd.metadata >> |-- aberdeen-motor-company-ltd >> | |-- mugiemoss-road-grampian >> | `-- mugiemoss-road-grampian.metadata >> >> >> the code can be seen at: >> >> http://paste.lisp.org/display/81348 >> >> I basically have a form, where the user makes a search for 'Company' >> name, than the code searches for all the items that do not contain >> 'hotel' in the metadata >> >> This works for the first level, i.e. when searching in the companies >> directory. >> >> Now I want to extend this to search for the '/companies/company/ but >> to only search the addresses that are in the found = [] list from the >> previous search. > > I haven't looked at your code, but I presume your "search" is a function? I > certainly hope so anyway! Also I presume you return the found list? If so, > it should be trivial. If not, you should probably adjust your code to do > such things. Hi, thanks for the reply. It was simpler than I thought. > HTH, > Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] advise on this loop
On Thu, Jun 4, 2009 at 8:29 AM, Norman Khine wrote: > Hello, > I have the following code, which searches through a directory structure > such as: > > $ tree -L 2 companies > companies > |-- aberdeen-airport-ltd > | |-- aberdeen-airport-dyce-grampian > | `-- aberdeen-airport-dyce-grampian.metadata > |-- aberdeen-airport-ltd.metadata > |-- aberdeen-motor-company-ltd > | |-- mugiemoss-road-grampian > | `-- mugiemoss-road-grampian.metadata > > > the code can be seen at: > > http://paste.lisp.org/display/81348 > > I basically have a form, where the user makes a search for 'Company' > name, than the code searches for all the items that do not contain > 'hotel' in the metadata > > This works for the first level, i.e. when searching in the companies > directory. > > Now I want to extend this to search for the '/companies/company/ but > to only search the addresses that are in the found = [] list from the > previous search. I haven't looked at your code, but I presume your "search" is a function? I certainly hope so anyway! Also I presume you return the found list? If so, it should be trivial. If not, you should probably adjust your code to do such things. HTH, Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] advise on this loop
Hello, I have the following code, which searches through a directory structure such as: $ tree -L 2 companies companies |-- aberdeen-airport-ltd | |-- aberdeen-airport-dyce-grampian | `-- aberdeen-airport-dyce-grampian.metadata |-- aberdeen-airport-ltd.metadata |-- aberdeen-motor-company-ltd | |-- mugiemoss-road-grampian | `-- mugiemoss-road-grampian.metadata the code can be seen at: http://paste.lisp.org/display/81348 I basically have a form, where the user makes a search for 'Company' name, than the code searches for all the items that do not contain 'hotel' in the metadata This works for the first level, i.e. when searching in the companies directory. Now I want to extend this to search for the '/companies/company/ but to only search the addresses that are in the found = [] list from the previous search. Thanks Norman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] advise on using re
On Sun, Feb 15, 2009 at 5:53 AM, Norman Khine wrote: MXlocate = re.compile('^.*preference = (\d*).*exchanger = (.*)$', > On the shell, the command > > $ nslookup -querytype=MX abakuc.com > Server: 193.252.19.3 > Address:193.252.19.3#53 > > Non-authoritative answer: > abakuc.com mail exchanger = 30 ASPMX3.GOOGLEMAIL.com. > abakuc.com mail exchanger = 30 ASPMX4.GOOGLEMAIL.com. > abakuc.com mail exchanger = 30 ASPMX5.GOOGLEMAIL.com. > abakuc.com mail exchanger = 60 mail.abakuc.com. > abakuc.com mail exchanger = 10 ASPMX.L.GOOGLE.com. > abakuc.com mail exchanger = 20 ALT1.ASPMX.L.GOOGLE.com. > abakuc.com mail exchanger = 20 ALT2.ASPMX.L.GOOGLE.com. > abakuc.com mail exchanger = 30 ASPMX2.GOOGLEMAIL.com. > > > I would like to extract the MX servers in to a list. None of your output lines contain the text 'preference = ' so your re does not match any of them. A standalone re tester can help with figuring out re's, for example Tools/scripts/redemo.py which comes with Python. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] advise on using re
Hello, What am I doing wrong here. >>> import smtplib, socket, re, os, operator, sys >>> domain = 'abakuc.com' >>> validMXservers = [] >>> MXlocate = re.compile('^.*preference = (\d*).*exchanger = (.*)$', re.IGNORECASE) >>> MXservers = os.popen('nslookup -querytype=MX %s' %domain, 'r') >>> for line in MXservers: ... if 'mail exchanger' in line: ... MXcheck = MXlocate.match(line) ... if MXcheck: ... MXserver = MXcheck.group(1), MXcheck.group(2) ... validMXservers.append(MXserver) ... >>> validMXservers [] >>> print MXservers >>> print line >>> print MXcheck None >>> On the shell, the command $ nslookup -querytype=MX abakuc.com Server: 193.252.19.3 Address:193.252.19.3#53 Non-authoritative answer: abakuc.com mail exchanger = 30 ASPMX3.GOOGLEMAIL.com. abakuc.com mail exchanger = 30 ASPMX4.GOOGLEMAIL.com. abakuc.com mail exchanger = 30 ASPMX5.GOOGLEMAIL.com. abakuc.com mail exchanger = 60 mail.abakuc.com. abakuc.com mail exchanger = 10 ASPMX.L.GOOGLE.com. abakuc.com mail exchanger = 20 ALT1.ASPMX.L.GOOGLE.com. abakuc.com mail exchanger = 20 ALT2.ASPMX.L.GOOGLE.com. abakuc.com mail exchanger = 30 ASPMX2.GOOGLEMAIL.com. I would like to extract the MX servers in to a list. Cheers Norman ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
Jacob, Apart from all the other comments you received, here are my thoughts. I think you could do one more thing to speed up your calculations and that is to use a more efficient method. The Reimann sum is not a very efficient. One simple method that is rahter popular is Simpson's rule. The calculations are not much more complicated than what you already have You then you just need to make sure that the number of intervals are even. Johan Jacob S. wrote: Hi all. Long time no see. (About five days, right?) Anyway, I know the first thing that some of you are going to say is using eval(). I don't want a whole guilt trip on security risks and all that. I do not want to share the code with anyone else while it's on my computer, and I sure don't have anyone near me that knows python. I would be surprised if more than 50 people in Portland, IN knew anything about python. So security is not a problem. I guess what I'm looking for is someone who knows the Reimann Sum better than I do and can tell me whether I can do something to make it more efficient. It's horribly slow with ten thousand steps-- I don't know the notation very well, but it loops the loop O(step*(maximum-minimum)) times, which is horribly sucky. In case anyone doesn't know, Reimann's sum is the computer's version of the definite integral, the area under a curve in a particular domain. Basic input and output. If a curve is a straight line, say y = x, the area under the line on an interval can be found by geometric means. However, if you use a parabola, or any other function, say y = 3*x**2, What is the function? 3*x*x What is the minimum? 2 What is the maximum? 5 117.000435 Which, considering that it is supposed to be exactly 117, It's darn good. Unfortunately, it also takes about 10 seconds to do all that. Any suggestions? Any advice? TIA Jacob Schmidt from __future__ import division import psyco psyco.full() fofx = raw_input("What is the function? ") minimum = raw_input("What is the minimum? ") maximum = raw_input("What is the maximum? ") minimum = float(minimum) maximum = float(maximum) total = 0 step = 10 x = minimum while minimum <= x <= maximum: area = eval(fofx)*1/step total = total+area x = x+1/step print total # ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor <>___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
Thanks everyone! Kent started the suggestion of making a code object, but everyone else seems to have worked their way to it. Beautiful! I only have to call exec once, and it cuts down time considerably. Here is the new code. Jacob Schmidt Please remind me if I've forgotten anything. ### Start ### from __future__ import division from math import * import psyco psyco.full() def reimannsum(fofx,x,max1): total = 0 step = 1e-5 exec "def f(x): return %s" % fofx while x <= max1: total = total+f(x) x = x+step return abs(total*step) fofx = raw_input("What is the function? ") minimum = raw_input("What is the minimum? ") maximum = raw_input("What is the maximum? ") minimum = float(minimum) maximum = float(maximum) print reimannsum(fofx,minimum,maximum) End ## If the user must be able to enter in the function, then it would be better to evaluate this once and turn it into some sort of function that you can call inside the loop (it's the eval that is so expensive). How to do that depends a lot on how complex the possible functions can be (if they'll only include 'x*+/-' and numbers, for example, it's not so tricky). exp = raw_input('Type expression') func = eval('lambda x: " + exp) print func(42) etc... Or if you really can't grokm lambda: exec('def func(x): return " + exp) should do the same... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
> If the user must be able to enter in the function, then it would be better > to evaluate this once and turn it into some sort of function that you can > call inside the loop (it's the eval that is so expensive). How to do that > depends a lot on how complex the possible functions can be (if they'll only > include 'x*+/-' and numbers, for example, it's not so tricky). exp = raw_input('Type expression') func = eval('lambda x: " + exp) print func(42) etc... Or if you really can't grokm lambda: exec('def func(x): return " + exp) should do the same... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
> > There has been alot of talk on this list about using list comprehensions > > lately, and this could be one of those useful places. While I don't > > have time to experiment with real code, I would suggest changing your > > function to look like: > > > > steps = [ min_x + i*delta_x for i in range(steps) ] > > totalarea = sum([ eval(func_x)*delta_x for x in steps ]) > > Calling eval() there in the inner loop might be costly, because Python > needs to do extra work to tokenize and parse the string, every time > through the iteration. We want to reduce the work done in tight inner > loops like that. > > We can do some of that work up front by compiling the code. Here's some > hacky code to do the compilation up front: > > ### > >>> def makeFunction(expressionString): > ... compiledExp = compile(expressionString, 'makeFunction', 'eval') > ... def f(x): > ... return eval(compiledExp, {}, {'x' : x}) > ... return f > ... > ### Oh! There's a slightly simpler way to write that makeFunction(): ### >>> def makeFunction(s): ... return eval("lambda x: " + s) ... ### It even has a little less overhead than the previous code. ### >>> timeIt(myFunctionOriginal) 0.035856008529663086 >>> >>> timeIt(makeFunction("3*x*x")) 0.00087714195251464844 ### Best of wishes! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
On Wed, 26 Jan 2005, Bill Kranec wrote: > There has been alot of talk on this list about using list comprehensions > lately, and this could be one of those useful places. While I don't > have time to experiment with real code, I would suggest changing your > function to look like: > > steps = [ min_x + i*delta_x for i in range(steps) ] > totalarea = sum([ eval(func_x)*delta_x for x in steps ]) > > Since list comprehensions are significantly faster than while loops, > this could be a big speed boost. > > There may be a mistake or two in the above code, but hopefully the idea > will be helpful. Calling eval() there in the inner loop might be costly, because Python needs to do extra work to tokenize and parse the string, every time through the iteration. We want to reduce the work done in tight inner loops like that. We can do some of that work up front by compiling the code. Here's some hacky code to do the compilation up front: ### >>> def makeFunction(expressionString): ... compiledExp = compile(expressionString, 'makeFunction', 'eval') ... def f(x): ... return eval(compiledExp, {}, {'x' : x}) ... return f ... ### Some of the stuff there is a bit obscure, but the idea is that we get Python to parse and compile the expression down once. Later on, we can evaluation the compiled code, and that should be faster than evaluating a string. Once we have this, we can use it like this: ### >>> myFunction = makeFunction("3*x*x") >>> myFunction(0) 0 >>> myFunction(1) 3 >>> myFunction(2) 12 >>> myFunction(3) 27 ### So although makeFunction()'s internals are weird, it shouldn't be too hard to treat it as a black box. *grin* Let's see how this performs against that 3x^2 expression we saw before. The original approach that calls eval() on the string takes time: ### >>> def timeIt(f, n=1000): ... start = time.time() ... for i in xrange(n): ... f(i) ... end = time.time() ... return end - start ... >>> def myFunctionOriginal(x): ... return eval("3*x*x") ... >>> timeIt(myFunctionOriginal) 0.036462068557739258 ### The precompiled expression can work more quickly: ### >>> timeIt(myFunction) 0.0050611495971679688 ### And we should still get the same results: ### >>> for i in range(2000): ... assert myFunction(i) == myFunctionOriginal(i) ... >>> ### I hope this helps! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
There has been alot of talk on this list about using list comprehensions lately, and this could be one of those useful places. While I don't have time to experiment with real code, I would suggest changing your function to look like: steps = [ min_x + i*delta_x for i in range(steps) ] totalarea = sum([ eval(func_x)*delta_x for x in steps ]) Since list comprehensions are significantly faster than while loops, this could be a big speed boost. There may be a mistake or two in the above code, but hopefully the idea will be helpful. Bill TJ wrote: What is the function? 3*x*x What is the minimum? 2 What is the maximum? 5 117.000435 Which, considering that it is supposed to be exactly 117, It's darn good. Unfortunately, it also takes about 10 seconds to do all that. Any suggestions? Any advice? TIA Jacob Schmidt Jacob, You can get better accuracy with orders of magnitude fewer steps by evaluating the function at the midpoint of each step rather than the low value. This has the added benefit of yielding the same result when stepping x up (2 to 5) or down (5 to 2). Here's some modified code (I don't have psyco): from __future__ import division import time def reimannSum(func_x, min_x, max_x, steps): start = time.time() totalArea = 0 #precalculate step size delta_x = 1 / steps #set x to midpoint of first step x = min_x + delta_x / 2 while min_x <= x <= max_x: totalArea += eval(func_x) * delta_x x += delta_x return totalArea, steps, time.time() - start stepsList = [10, 1, 1000, 500, 200, 100] fmt = 'Area: %f Steps: %d Time: %f' for steps in stepsList: print fmt % reimannSum('3 * x * x', 2, 5, steps) The results on my machine are: Area: 117.00 Steps: 10 Time: 44.727405 Area: 117.00 Steps: 1 Time: 4.472391 Area: 116.99 Steps: 1000 Time: 0.454841 Area: 116.97 Steps: 500 Time: 0.223208 Area: 116.81 Steps: 200 Time: 0.089651 Area: 116.25 Steps: 100 Time: 0.044431 TJ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
There was a discussion about this same question (in the context of graphing a user-supplied function) just a few weeks ago. My suggestion was to use exec to create a real function object that you can call directly; you can read it here: http://mail.python.org/pipermail/tutor/2005-January/034696.html Read the whole thread for other ideas. Kent Jacob S. wrote: Hi all. Long time no see. (About five days, right?) Anyway, I know the first thing that some of you are going to say is using eval(). I don't want a whole guilt trip on security risks and all that. I do not want to share the code with anyone else while it's on my computer, and I sure don't have anyone near me that knows python. I would be surprised if more than 50 people in Portland, IN knew anything about python. So security is not a problem. I guess what I'm looking for is someone who knows the Reimann Sum better than I do and can tell me whether I can do something to make it more efficient. It's horribly slow with ten thousand steps-- I don't know the notation very well, but it loops the loop O(step*(maximum-minimum)) times, which is horribly sucky. In case anyone doesn't know, Reimann's sum is the computer's version of the definite integral, the area under a curve in a particular domain. Basic input and output. If a curve is a straight line, say y = x, the area under the line on an interval can be found by geometric means. However, if you use a parabola, or any other function, say y = 3*x**2, What is the function? 3*x*x What is the minimum? 2 What is the maximum? 5 117.000435 Which, considering that it is supposed to be exactly 117, It's darn good. Unfortunately, it also takes about 10 seconds to do all that. Any suggestions? Any advice? TIA Jacob Schmidt from __future__ import division import psyco psyco.full() fofx = raw_input("What is the function? ") minimum = raw_input("What is the minimum? ") maximum = raw_input("What is the maximum? ") minimum = float(minimum) maximum = float(maximum) total = 0 step = 10 x = minimum while minimum <= x <= maximum: area = eval(fofx)*1/step total = total+area x = x+1/step print total # ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Advise...
What is the function? 3*x*x What is the minimum? 2 What is the maximum? 5 117.000435 Which, considering that it is supposed to be exactly 117, It's darn good. Unfortunately, it also takes about 10 seconds to do all that. Any suggestions? Any advice? TIA Jacob Schmidt Jacob, You can get better accuracy with orders of magnitude fewer steps by evaluating the function at the midpoint of each step rather than the low value. This has the added benefit of yielding the same result when stepping x up (2 to 5) or down (5 to 2). Here's some modified code (I don't have psyco): from __future__ import division import time def reimannSum(func_x, min_x, max_x, steps): start = time.time() totalArea = 0 #precalculate step size delta_x = 1 / steps #set x to midpoint of first step x = min_x + delta_x / 2 while min_x <= x <= max_x: totalArea += eval(func_x) * delta_x x += delta_x return totalArea, steps, time.time() - start stepsList = [10, 1, 1000, 500, 200, 100] fmt = 'Area: %f Steps: %d Time: %f' for steps in stepsList: print fmt % reimannSum('3 * x * x', 2, 5, steps) The results on my machine are: Area: 117.00 Steps: 10 Time: 44.727405 Area: 117.00 Steps: 1 Time: 4.472391 Area: 116.99 Steps: 1000 Time: 0.454841 Area: 116.97 Steps: 500 Time: 0.223208 Area: 116.81 Steps: 200 Time: 0.089651 Area: 116.25 Steps: 100 Time: 0.044431 TJ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Advise...
[Tony Meyer] > If the user must be able to enter in the function, then it > would be better to evaluate this once and turn it into some > sort of function that you can call inside the loop (it's the > eval that is so expensive). I should have included some code that does this: """ from __future__ import division import psyco psyco.full() fofx = raw_input("What is the function? ") minimum = raw_input("What is the minimum? ") maximum = raw_input("What is the maximum? ") start = time.time() minimum = float(minimum) maximum = float(maximum) total = 0 step = 10 x = minimum a = compile(fofx, '', 'eval') while minimum <= x <= maximum: area = eval(a)/step total = total+area x = x+1/step print total """ This takes ~1.7 seconds here, so about 8 times faster than without the compile() call, but about 2.5 times slower than directly with the 3*x*x. =Tony.Meyer ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Advise...
> Anyway, I know the first thing that some of you are going to > say is using eval(). [...] > Which, considering that it is supposed to be exactly 117, > It's darn good. > Unfortunately, it also takes about 10 seconds to do all that. > Any suggestions? Any advice? On my machine your version takes about 14 seconds. If you replace 'eval(fofx)' with '3*x*x' it takes about 0.7 seconds. Am I allowed to say "don't use eval()" if I'm saying it because it's extremely slow, rather than for security reasons? ;) If the user must be able to enter in the function, then it would be better to evaluate this once and turn it into some sort of function that you can call inside the loop (it's the eval that is so expensive). How to do that depends a lot on how complex the possible functions can be (if they'll only include 'x*+/-' and numbers, for example, it's not so tricky). =Tony.Meyer ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Advise...
Hi all. Long time no see. (About five days, right?) Anyway, I know the first thing that some of you are going to say is using eval(). I don't want a whole guilt trip on security risks and all that. I do not want to share the code with anyone else while it's on my computer, and I sure don't have anyone near me that knows python. I would be surprised if more than 50 people in Portland, IN knew anything about python. So security is not a problem. I guess what I'm looking for is someone who knows the Reimann Sum better than I do and can tell me whether I can do something to make it more efficient. It's horribly slow with ten thousand steps-- I don't know the notation very well, but it loops the loop O(step*(maximum-minimum)) times, which is horribly sucky. In case anyone doesn't know, Reimann's sum is the computer's version of the definite integral, the area under a curve in a particular domain. Basic input and output. If a curve is a straight line, say y = x, the area under the line on an interval can be found by geometric means. However, if you use a parabola, or any other function, say y = 3*x**2, What is the function? 3*x*x What is the minimum? 2 What is the maximum? 5 117.000435 Which, considering that it is supposed to be exactly 117, It's darn good. Unfortunately, it also takes about 10 seconds to do all that. Any suggestions? Any advice? TIA Jacob Schmidt from __future__ import division import psyco psyco.full() fofx = raw_input("What is the function? ") minimum = raw_input("What is the minimum? ") maximum = raw_input("What is the maximum? ") minimum = float(minimum) maximum = float(maximum) total = 0 step = 10 x = minimum while minimum <= x <= maximum: area = eval(fofx)*1/step total = total+area x = x+1/step print total # ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor