Re: [Tutor] specifying precision with scientific notation
Hi Wayne, Yes - that helps. I missed the correct combination of parentheses. Now I am trying to improve my aesthetics, as Evert suggested earlier > Personally, I would print this as 7.63e-03 +- 0.83e-03, which shows the > precision a bit better. But that's just a matter of aesthetics, and would > make things even more complicated (then again, since you are printing numbers > and formatting them, you are concerned about aesthetics). I agree with him here, and would like to match this style. I have been trying to figure out if you can specify the "mantissa" and exponent in scientific notation, by reading in the string.format section of python, but have been unsuccessful. Is there an easy way to do this? To be precise, I now have > a = 0.0762921383941; ea = 0.000830132912068 > p = int(("%.1e" % (a / ea)).split('e')[-1]) > print(('%.' + str(int(2+p)) +'e +- %.1e') % (a, ea)) 7.629e-02 +- 8.3e-04 and instead I would like this to print 7.629e-02 +- 0.083e-02 I could imagine writing a little function that does all this, but am hoping there is a quick (and dirty) way to just force the scientific notation to into this format - I guess by forcing the power of the exponent. Thanks, Andre On Oct 5, 2010, at 6:39 PM, Wayne Werner wrote: > On Tue, Oct 5, 2010 at 7:51 PM, Andre' Walker-Loud > wrote: > Hi Alan, > > The point I can not get to work is > > > fmt = "%.%de + %.1e" % n else: > > when I try this, I get (python 2.6.5, OS X 10.6) > > > n = 3; fmt = "%.%de + %.1e" % n; fmt > '%de + 3.0e+00' > > But something like the "%.%de " %n is exactly what I am looking for - if I > could get it to work. > > > a = 0.00762921383941 > ea = 0.000830132912068 > > > p = int(("%.1e" % (a / ea)).split('e')[-1]) # grab the power in the > scientific n > otation (is there a better way?) > if p >= 0: > print(('%.' + str(int(2+p)) +'e +- %.1e') % (a, ea)) > else: > print('%.2e +- %.1e' % (a, ea)) > #(desired output): 7.63e-03 +- 8.3e-04 > > > This works for me - I added some extra parenthesis because the original was > giving me this error: > > Traceback (most recent call last): > File "exponent.py", line 7, in > print('%.' + str(int(2+p)) +'e +- %.1e' % (a, ea)) > TypeError: not all arguments converted during string formatting > > HTH, > Wayne > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] specifying precision with scientific notation
Hi Alan, The point I can not get to work is > fmt = "%.%de + %.1e" % n else: when I try this, I get (python 2.6.5, OS X 10.6) > n = 3; fmt = "%.%de + %.1e" % n; fmt '%de + 3.0e+00' But something like the "%.%de " %n is exactly what I am looking for - if I could get it to work. Thanks, Andre On Oct 5, 2010, at 3:43 PM, Alan Gauld wrote: > "Andre' Walker-Loud" wrote > >>> a = 0.00762921383941 >>> ea = 0.000830132912068 >>> a / ea >> 9.190352205653852 >> >> By default, I will print the uncertainty ("ea") with two significant digits. >> In this example, the central value is about 10 times larger than the >> uncertainty, so I want to print it with 3 significant figures. > > I don't understand why the difference but if the deciding factor is related > to the ratio why bother with all the string stuff? Just use the ratio > directly... > >>> p = int(("%.1e" % (a / ea)).split('e')[-1]) # grab the power in the >>> scientific notation (is there a better way?) >>> if p >= 0: > > Why not just > > limit = 10 > n = 3 if a/ea <= limit else n = 2 # or whatever expression is needed to > calculate n > fmt = "%.%de + %.1e" % n else: > > print fmt % (a, ea) > > > But I suspect I'm missing something in your reasoning about what size of n > you want. > > -- > Alan Gauld > Author of the Learn to Program web site > http://www.alan-g.me.uk/ > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] perl or python
> Is it better to learn Perl or Python since i can manage only writing > simple bash shell scripts. > Please suggest/guide. Do you already have both installed (seeing bash, I bet you're on some version of Linux, so it's likely you do). You could try writing a very simple "guess my number" game or some other very simple program in both to see which you like best. Also, this might help you compare them: http://www.99-bottles-of-beer.net/p.html Alan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a class
"T MURPHY" wrote how do i go about creating a class in python. class C: pass is the simplest way. But it's not very useful, being empty. But most tutorials discuss OOP, which one are you using? -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] specifying precision with scientific notation
"Andre' Walker-Loud" wrote a = 0.00762921383941 ea = 0.000830132912068 a / ea 9.190352205653852 By default, I will print the uncertainty ("ea") with two significant digits. In this example, the central value is about 10 times larger than the uncertainty, so I want to print it with 3 significant figures. I don't understand why the difference but if the deciding factor is related to the ratio why bother with all the string stuff? Just use the ratio directly... p = int(("%.1e" % (a / ea)).split('e')[-1]) # grab the power in the scientific notation (is there a better way?) if p >= 0: Why not just limit = 10 n = 3 if a/ea <= limit else n = 2 # or whatever expression is needed to calculate n fmt = "%.%de + %.1e" % n else: print fmt % (a, ea) But I suspect I'm missing something in your reasoning about what size of n you want. -- Alan Gauld Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] creating a class
On Wed, Oct 6, 2010 at 4:16 AM, T MURPHY wrote: > how do i go about creating a class in python. By using the "class" keyword. Example: class Fruit(object): def __init__(self, name) self.name = name class Apple(Fruit): def __init__(self): super(Apple, self).__init__("apple") apple = Apple() print apple.name For more information, I suggest you start reading the python tutorial (1) cheers James 1. http://docs.python.org/tutorial/ -- -- James Mills -- -- "Problems are solved by method" ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] creating a class
how do i go about creating a class in python. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] specifying precision with scientific notation
> I want to print scientific numbers with a specified number of decimal places. > However, I want the number printed to be dynamically determined by the data. > Example: > >> a = 0.00762921383941 >> ea = 0.000830132912068 >> a / ea > 9.190352205653852 > > By default, I will print the uncertainty ("ea") with two significant digits. > In this example, the central value is about 10 times larger than the > uncertainty, so I want to print it with 3 significant figures. Note that 'specified number of decimal places' != 'number of significant digits'. But it's fairly obvious you mean significant digits here. But that aside, perhaps Python's decimal module can help you to ease things: http://docs.python.org/library/decimal.html > So I want to do something like > >> p = int(("%.1e" % (a / ea)).split('e')[-1]) # grab the power in the >> scientific notation (is there a better way?) >> if p >= 0: >> print('%.' + str(int(2+p)) +'e +- %.1e' % (a, ea)) >> else: > print('%.2e +- %.1e' % (a, ea)) > > (desired output): 7.63e-03 +- 8.3e-04 Personally, I would print this as 7.63e-03 +- 0.83e-03, which shows the precision a bit better. But that's just a matter of aesthetics, and would make things even more complicated (then again, since you are printing numbers and formatting them, you are concerned about aesthetics). But if the decimal module can't make it easier for you, I don't really think there's an easy way of doing this. Though you may want to make things slightly more convenient by creating your own Float class (inheriting from float), or even a Value class that has a number and an error. Then override the the __str__ method and you should be done for any number you print, while all the other operations can work as float (for the first class at least), or similar to float (second class). Cheers, Evert > but this fails. And I haven't figured out how to get this to work. Seems > like it should be simple. > > Any help? > > > Thanks, > > Andre ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] specifying precision with scientific notation
Hi All, I want to print scientific numbers with a specified number of decimal places. However, I want the number printed to be dynamically determined by the data. Example: > a = 0.00762921383941 > ea = 0.000830132912068 > a / ea 9.190352205653852 By default, I will print the uncertainty ("ea") with two significant digits. In this example, the central value is about 10 times larger than the uncertainty, so I want to print it with 3 significant figures. So I want to do something like > p = int(("%.1e" % (a / ea)).split('e')[-1]) # grab the power in the > scientific notation (is there a better way?) > if p >= 0: > print('%.' + str(int(2+p)) +'e +- %.1e' % (a, ea)) > else: print('%.2e +- %.1e' % (a, ea)) (desired output): 7.63e-03 +- 8.3e-04 but this fails. And I haven't figured out how to get this to work. Seems like it should be simple. Any help? Thanks, Andre ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] EXECUTING PYTHON AND SQL STAMENTS
"Susana Iraiis Delgado Rodriguez" wrote it throws me that "C:/Archivos" is not recognized as an executable external or internal command, programm or file. You can only use / in paths used by Python. You are passing this to the CMD processor via os.system so CMD complains. It expects / to indicate a command option (/? for example) You need to use \ in paths passed to CMD. HTH, Alan G. PS I just noticed you switched to subprocess so this is now somewhat academic! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] EXECUTING PYTHON AND SQL STAMENTS
Thank you for taking the time to answer. I already changed my os.system() for your code. I got an error, when I executed this: > os.system(" 'C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe ' "+arg1 > +" -where "+arg2 +" " +arg3) > it throws me that "C:/Archivos" is not recognized as an executable > external or internal command, programm or file. > If you really have other opton to fix my problem I'll be thankful because I > don't have any idea to make this code useful. > > The error message suggests the OS is seeing "C:/Archivos" as the command, as opposed to the entire path to ogr2ogr.exe, which implies some quoting issue/quotes being stripped off/lost somewhere along the line. Walter ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] EXECUTING PYTHON AND SQL STAMENTS
Hello, I already solved the problem, I change all the code, instead of using os.system I changed to subprocess.Popen() and it worked fine: import shlex, subprocess def process(): print "Ingresa en el siguiente orden:" print "Nombre del nuevo mapa.shp Nombre de la capa Nombre del mapa original" command_line = raw_input() args = shlex.split(command_line) p = subprocess.Popen(['C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr', args[0], '-where', args[1], args[2]]) if p: print "Mapa generado" process() Now the user has to enter 3 arguments an finally it worked. I have a question, how can I tell the user if p execute ok? because even thouhg I entered wrong parameters, it prints "Mapa generado". This line should only appears if the arguments are acceptable. 2010/10/5 Susana Iraiis Delgado Rodriguez > Hello Norman: > > Thank you for taking the time to answer. I already changed my os.system() > for your code. I got an error, when I executed this: > os.system(" 'C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe ' > "+arg1 +" -where "+arg2 +" " +arg3) > it throws me that "C:/Archivos" is not recognized as an executable > external or internal command, programm or file. > If you really have other opton to fix my problem I'll be thankful because I > don't have any idea to make this code useful. > Thank you > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] EXECUTING PYTHON AND SQL STAMENTS
Hello Norman: Thank you for taking the time to answer. I already changed my os.system() for your code. I got an error, when I executed this: os.system(" 'C:/Archivos de programa/FWTools2.4.7/bin/ogr2ogr.exe ' "+arg1 +" -where "+arg2 +" " +arg3) it throws me that "C:/Archivos" is not recognized as an executable external or internal command, programm or file. If you really have other opton to fix my problem I'll be thankful because I don't have any idea to make this code useful. Thank you ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] perl or python
On Tue, 5 Oct 2010 09:54:42 pm Kaushal Shriyan wrote: > Hi > > Is it better to learn Perl or Python since i can manage only writing > simple bash shell scripts. > Please suggest/guide. This is a mailing list for Python, filled with people who like and use Python. What do you think we're going to recommend? These resources try to be even-handed. They might help: http://www.python.org/doc/essays/comparisons.html http://danvk.org/josephus.html http://wiki.python.org/moin/LanguageComparisons And here is an article by the Perl hacker and programming guru Eric Raymond: http://www.linuxjournal.com/article/3882 -- Steven D'Aprano ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] perl or python
Hi Is it better to learn Perl or Python since i can manage only writing simple bash shell scripts. Please suggest/guide. Thanks and Regards Kaushal ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor