Re: [Tutor] How to perform variable assignment and
On Fri, Oct 2, 2009 at 11:14 PM, Oxymoron wrote: > Hello, > > On Sat, Oct 3, 2009 at 3:56 PM, Didar Hossain > wrote: >> >> homedir = os.environ.get('HOME') >> >> if homedir: >> print "My home directory is %s" % homedir >> >> >> I do this in Perl - >> >> my $home; >> >> if ($home = $ENV{'HOME'}) { print "My home directory is $home\n"; } >> >> Can I do a similar shortcut statement like the above in Python? > > There are probably various syntactic tricks to achieve the same, however, > the main reason you can't do it is that assignment in Python is a statement > rather than an expression, i.e. it does not return a value that the if > statement can evaluate. kamal is correct. you cannot do it in Python because assignments are not expressions, and when they are, it leads to problems, i.e., code readability, bugs, etc. Python fights hard to prevent those from "interrupting your problem-solving," and there's a cost to it -- hopefully the benefits outweigh the minor costs. as far as your solution goes, it is one of the cleanest solution you can come up with. however, there is a tiny bug: if the $HOME environment variable is *not* set, you will get a KeyError exception. one solution is to add a default value to your get() method call so that it returns an object with a Boolean False value: import os homedir = os.environ.get('HOME', '') # or False or None if homedir: print "My home directory is %s" % homedir hope this helps! -- wesley - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "Core Python Programming", Prentice Hall, (c)2007,2001 "Python Fundamentals", Prentice Hall, (c)2009 http://corepython.com wesley.j.chun :: wescpy-at-gmail.com python training and technical consulting cyberweb.consulting : silicon valley, ca http://cyberwebconsulting.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to perform variable assignment and
Hello, On Sat, Oct 3, 2009 at 3:56 PM, Didar Hossain wrote: > homedir = os.environ.get('HOME') > > if homedir: >print "My home directory is %s" % homedir > > > I do this in Perl - > > my $home; > > if ($home = $ENV{'HOME'}) { print "My home directory is $home\n"; } > > Can I do a similar shortcut statement like the above in Python? > > There are probably various syntactic tricks to achieve the same, however, the main reason you can't do it is that assignment in Python is a statement rather than an expression, i.e. it does not return a value that the if statement can evaluate. While having an assignment as an expression could be convenient in places (I have had my moments where I wish it was!), that particular usage is also a source of common bugs. Often in conditional statements people want to do a comparison using the == operator rather than an assignment (=) + eval. Marking assignment as a statement rather than an expression prevents this bug, since it is now a syntax error in this case - an if statement cannot work on something that doesn't return anything. My Perl is rusty at best! Not sure if there's an == operator, if there isn't then the decision to allow this is probably more justified since comparison and assignment are sufficiently different syntax-wise that the intent is clear. -- Kamal -- There is more to life than increasing its speed. -- Mahatma Gandhi ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to perform variable assignment and
Hi, I am currently learning Python and I was wondering is there a shorter way to do the following - import os homedir = os.environ.get('HOME') if homedir: print "My home directory is %s" % homedir I do this in Perl - my $home; if ($home = $ENV{'HOME'}) { print "My home directory is $home\n"; } Can I do a similar shortcut statement like the above in Python? Regards, Didar ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to python: some advises for image processing tool
I remember reading some Python tutorials that where written specifically for Astronomers. Did a search on Google. This is not the tutorial I originally read but maybe you will find it to be useful. http://www.stsci.edu/hst/training/events/Python/readManDispImages_WU.pdf Regards Rudiger On Fri, 02 Oct 2009 11:47 +0200, "Nicola De Quattro" wrote: > Hi, > I'm a 26 years old Italian engineer. Because to the fact that I'm an > astrophile and astronomical tool (expecially under Linux) are not so > common, I would like to develop some simple tool for some purposes. > Well, I'm not knew to software development but I've never used python. > So I want to list you my needed for the first tool, so you can tell me > if Python could be a good choice and how to begin the develop. > First of all, I need a software running both under Windows and Linux > at least (it would be better if running either under MacOS). This is a > first reason to choose Python. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to python: some advises for image processing tool
Hello Nicola! For scientific computing there are the Numpy and Scipy libraries: http://www.scipy.org/ For making graphs there is Matplotlib: http://matplotlib.sourceforge.net/ You should join the mailing lists of these projects. For the GUI I would use QT4: http://doc.trolltech.com/4.5/index.html The Python bindings are called PyQt4: http://www.riverbankcomputing.co.uk/news You should also look at Enthought's Traits UI, which is specially dedicated to scientist, but I don't know if it can be installed on Linux. http://code.enthought.com/projects/traits/ Maybe Python's built in GUI might be good enough for you. http://wiki.python.org/moin/TkInter However you should definitely write a command line program first, and start with the GUI when the program more or less works. GUI programming is a lot of work for a relatively small gain in usability. Good luck with your project, Eike. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] small program
Andrius wrote: Hi! There are interesting online course http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-00Fall-2008/LectureVideos/index.htm where I'm trying to learn Python. Looks quite interesting and I would like to create a program which should repeat a simply string several times with list number before. Like "1. Wanna more. 2. Wanna more. ..." Suppose to a loop here repeating, say x times. Should it look like that: y="Wanna more. " x=1 x=x+d: d=<100 print d+y How to create a program for such kind of task? Pardon my skepticism - I'm guessing you are taking the course and this is homework. We prefer not to do your homework for you. If it is not homework - we prefer not to write programs for you. Unfortunately Benno gave you a solution. If you want to learn Python read on: In regard to the above code - did you try to run it? That is step one. Try to run it. See if you can figure out why it fails. Hint there are 2 syntax errors. At python.org there are links to several tutorials which will show you how to repeat operations. To repeat things in Python we can use a loop. What Python statements are used for loops? -- Bob Gailer Chapel Hill NC 919-636-4239 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Images, and other things.
On Tue, Sep 29, 2009 at 7:00 PM, Corey Richardson wrote: > I haven't looked into this, but could you make a real time image using > python? I think it would be most hard > Anyway, I am having trouble with int(). I am trying to int(raw_input("some > number")), but it returns > Traceback (most recent call last): > File "C:/Users/Quick-Start/Documents/Python Doc's/Game_File", line 94, in > > fatness = int(raw_input("How many candies do you want to eat? :")) > TypeError: 'int' object is not callable You probably have a line like int = 3 in your program which rebinds the built-in int() function to a (non-callable) int object. Showing the whole program would help. Kent ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to python: some advises for image processing tool
2009/10/2 Stefan Behnel : > Without looking further into your problem, there are two (main) general > purpose image manipulation libraries for Python (that I know of) that you > may want to look at: PIL and ImageMagick. At least ImageMagick supports FITS. There is a python module for fits files. Never used it so the usual caveats apply here ;-) http://www.stsci.edu/resources/software_hardware/pyfits Greets Sander ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to run php scripts in pylons framework?
On Fri, Oct 2, 2009 at 1:24 AM, ggi ggi wrote: > > Dear All, > > I have some php scripts which I don't want to rewrite in python. How can I > run php Scripts in python? > > Thanks in advance > Googi G > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > Although rewriting is a better option, you can use the subprocess module to make system calls to the php interpreter import subprocess #simple caller, disguard output subprocess.call("php /path/to/my/old/script.php") # if you want output proc = subprocess.Popen("php /path/to/my/script.php", shell=True, stdout=subprocess.PIPE) script_response = proc.stdout.read() Hope that helps, Vince ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to python: some advises for image processing tool
On Fri, Oct 2, 2009 at 8:44 AM, Stefan Behnel wrote: > Nicola De Quattro wrote: > > So I've to open an image (various formats, first I could need only > > .fits), to process the image in order to select the interesting strip > > containing the star and the spectrum (first image of the link posted, > > but I hope I can select the strip not only in the horizontal line but > > I will rotate the image), to obtain a graph like the second and third > > image (this may be simple, it plots the value of luminosity of each > > point), calibrate the image using periodical table of elements as > > shown in 6th image in order to color the graph. > > Without looking further into your problem, there are two (main) general > purpose image manipulation libraries for Python (that I know of) that you > may want to look at: PIL and ImageMagick. At least ImageMagick supports > FITS. This is definitely a project you could do with python. It might take a fair amount of programming, but it should certainly take less with python than most other languages. Especially with the implementation of a GUI. I'd suggest PyGTK+ simply because of the rather extensive documentation. I've used both Tkinter and PyGTK and the latter is much easier to deal with when using images, though YMMV. The most difficult task would be analyzing the image and possibly some of the graph generation. HTH, Wayne ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New to python: some advises for image processing tool
Nicola De Quattro wrote: > So I've to open an image (various formats, first I could need only > .fits), to process the image in order to select the interesting strip > containing the star and the spectrum (first image of the link posted, > but I hope I can select the strip not only in the horizontal line but > I will rotate the image), to obtain a graph like the second and third > image (this may be simple, it plots the value of luminosity of each > point), calibrate the image using periodical table of elements as > shown in 6th image in order to color the graph. Without looking further into your problem, there are two (main) general purpose image manipulation libraries for Python (that I know of) that you may want to look at: PIL and ImageMagick. At least ImageMagick supports FITS. Stefan ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to run php scripts in pylons framework?
Dear All, I have some php scripts which I don't want to rewrite in python. How can I run php Scripts in python? Thanks in advance Googi G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] help with alternate execution
wrobl...@cmich.edu wrote: Thank you for the reply.. I tried putting the print repr(n) before I defined 'n' with raw_input. My script looks like this-- def divisible(n): if n%3 == 0: print n, "is divisible by 3" else: print n, "is not divisible by 3" n= raw_input("enter a number= ") print repr(n) print divisible(n) I don't understand what the problem is The problem is raw_input gives you a string. So in your example n is a string and when it comes to n%3 python tries to format your string, but since you haven't any formating symbols in it, it fails. To fix your program, convert the n to int: divisible(int(n)) - Patrick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] New to python: some advises for image processing tool
Hi, I'm a 26 years old Italian engineer. Because to the fact that I'm an astrophile and astronomical tool (expecially under Linux) are not so common, I would like to develop some simple tool for some purposes. Well, I'm not knew to software development but I've never used python. So I want to list you my needed for the first tool, so you can tell me if Python could be a good choice and how to begin the develop. First of all, I need a software running both under Windows and Linux at least (it would be better if running either under MacOS). This is a first reason to choose Python. Secondly, I want a OpenSource tool The software shall be a tool for Star Analyzer (spectroscopic analyzer) images. A similar software already exists, it can be seen at this link (I'm sorry, it is in Italian, but you can understand from the images what is the input and the output) http://www.astropix.it/software/astrospectrum.html So I've to open an image (various formats, first I could need only .fits), to process the image in order to select the interesting strip containing the star and the spectrum (first image of the link posted, but I hope I can select the strip not only in the horizontal line but I will rotate the image), to obtain a graph like the second and third image (this may be simple, it plots the value of luminosity of each point), calibrate the image using periodical table of elements as shown in 6th image in order to color the graph. Well, I know that many people doesn't understand the problem :) I hope you can be able, with the elements reported above, how I have to proceed, remembering that I've no problem programming in Matlab and C but I've never worked with graphical interfaces and with image processing (only under Matlab). I've learned most of python commands. I think I've to start with some library? Thank you very much and sorry for the poem (and for my english)! PS: If the question is "Why don't you use Astrospectrum?" the answer is: it is only for Linux and is not opensource :( -- Nicola De Quattro Mobile: (+39)3292964937 Web: http://nikde4.altervista.org Skype: lead_expression MSNetwork: lead_express...@hotmail.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] UnboundLocalError and Break
If your code's more than 10 lines long or so, put it on pastebin.com and send us the link rather than inlining the whole thing. You could also send it as an attachment.Your formatting is all screwed up and I can't read the code at all, but I have an idea about your error. On Wed, Sep 30, 2009 at 8:53 PM, Corey Richardson wrote: > Here is my code that is being used/affected by the block that is erroring. > The rest is superfluous, and does not affect it: > > intel = 10 #Define the abilities. > strn = 10 > con = 10 > dex = 10 > wis = 10 > exp = 0 > cha = 10 > playHp = 20 > melWep = 1 # A sword > rngWep = 1 #A bow > attack = dex+strn/2 #If they hit! > melDmg = strn+1+melWep/2 #Melee damage > deff = (con/3)+(dex/2)+6 #Defense > rngDmg = dex+1+rngWep/2 #Ranged damage > print "You are hiking through the woods one day, and you are jumped > by a goblin!" #The beginning of the adventure > print "Do you want to 1. Attack w/sword, 2. Attack w/bow, 3. Flee, > or 4. Nothing" > gob1Hp = 10 #First monsters hitpoints > def monsAttk (damage, attack,playHp): #A monsters attack if > attack > deff: #Monster hit playHp -= damage #Player takes > damage (I think...I will have > to work that out) > print "You have sustained", damage, "damage, with", > damage-playHp, "remaining" #Inform player of their status > def gmAttack(monHp, monDef, monAgil) : #A player attack > op1 = raw_input("Your choice?") > while op1 != "5": #Just a loop that you can break out of > (possibly...working that out) if op1 == '1': #Option 1 > if attack + 10 > monDef : #If the player hits > monHp-= melDmg #Monster takes damage > print "you did", melDmg, "damage! It now has", > monHp, "health left!" #Inform player how much health it has. > monsAttk(4, 15) #Monster attacks elif op1 == > '2': #Option 2 if attack + 10 >monDef: #If you hit the > monster > monHp -= rngDmg #Monster takes damage > print "you did", rngDmg, "damage! It now has", > monHp, "health left!" #Inform player how much health it has > monsAttk(4, 15) #Monster attacks > elif op1 == '3' : #Option 3 > if attack + dex > monAgil : #If they can escape > print "Thou hast fled!" #They have fled! > break #Stop the loop (I think. Can someone please inform > me as of how this works? I have looked online...it makes no sense to me) > elif op1 == '4' : #Option 4...the dumb one > monsAttk(4, 15) #Monster attacks > monsAttk(4,15) #Monster attacks again, cause they are dumb > print "You should attack..." #Inform them of their stupidity > else : #Well, what the hell did they pick? > print "Unknown option, try again!" #Inform them they > didn't pick a valid option > while gob1Hp >= 0: #Well it is still alive > gmAttack(gob1Hp,13,15) #player attacks > if gob1Hp <= 0.5 : #if it dies > print "Gratz! You have gotten 5 exp! You only have", 100-exp, > "exp left till lv 1!" #print their experience > exp += 5 #Give them exp > > > If you catch an error besides what is wrong, please don't point it out, as > I would like to work it out myself, unless marked in the comments. But this > one is evading me...I have playHp defined at the beginning, and I am getting > this error : > > Traceback (most recent call last): > File "C:\Users\Quick-Start\Documents\Python Doc's\Game_File.py", > line 183, in > gmAttack(gob1Hp,13,15) > File "C:\Users\Quick-Start\Documents\Python Doc's\Game_File.py", > line 167, in gmAttack > monsAttk(4, 15) > File "C:\Users\Quick-Start\Documents\Python Doc's\Game_File.py", > line 157, in monsAttk > playHp -= damage > UnboundLocalError: local variable 'playHp' referenced before assignment > > If you could please help me, that would be great. I'm just trying to get > the game engine working, as of now, it is 30% done, with Inventory, > Equipment, and a more sophisticated and simpler code to come. It's a bit of > a mess now... > > > > ___ > 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] small program
On Thu, Oct 1, 2009 at 8:31 PM, Andrius wrote: > Hi! > > There are interesting online course > http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-00Fall-2008/LectureVideos/index.htm > where I'm trying to learn Python. > Looks quite interesting and I would like to create a program which > should repeat a simply string several times with list number before. > Like "1. Wanna more. 2. Wanna more. ..." > Suppose to a loop here repeating, say x times. Should it look like that: > > y="Wanna more. " > x=1 > x=x+d: > d=<100 > print d+y > > How to create a program for such kind of task? [Forgot to reply-all the first time:] Something like this should do the trick, if I get what you're trying to do: x = 10 for i in range (1, x + 1): print i, "whee" HTH, benno ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] small program
Hi! There are interesting online course http://ocw.mit.edu/OcwWeb/Electrical-Engineering-and-Computer-Science/6-00Fall-2008/LectureVideos/index.htm where I'm trying to learn Python. Looks quite interesting and I would like to create a program which should repeat a simply string several times with list number before. Like "1. Wanna more. 2. Wanna more. ..." Suppose to a loop here repeating, say x times. Should it look like that: y="Wanna more. " x=1 x=x+d: d=<100 print d+y How to create a program for such kind of task? Regards, Andrius ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor