Re: [Tutor] How to use function from specific module and then switch to other module
Classification: UNCLASSIFIED Caveat (s): FOUO Kent, Thanks for the lead. I eventually did something like the Strategy Pattern you sent. It was so much simpler when I just inherited the functions. I think I need to redesign the code but for now it works and my boss will be happy. Thanks again. John Ertl Meteorologist FNMOC 7 Grace Hopper Ave. Monterey, CA 93943 (831) 656-5704 [EMAIL PROTECTED] Classification: UNCLASSIFIED Caveat (s): FOUO From: Kent Johnson Sent: Thu 11/6/2008 7:36 PM To: Ertl, John C CIV 63134 Cc: tutor@python.org Subject: Re: [Tutor] How to use function from specific module and then switch to other module On Thu, Nov 6, 2008 at 5:54 PM, Ertl, John C CIV 63134 <[EMAIL PROTECTED]> wrote: > Classification: UNCLASSIFIED > Caveat (s): FOUO > > I have a program that collects weather data from weather models. I > originally had a module that contained a bunch of function that I used. So > I just added it to the init of the class I was using and inherited the > functions. That worked great but now I have two different models that I can > get weather from. Each Module A and B have the exact same function names > in them but they both do slightly different things. It might help to see a working example of what you did for one model. > > The idea is as I step through a list I want to use a different function > (same name but from a different module) for each element in the list. How > do I have a generic way to do this. > > for example for point 1 I want to use the rain function from Module A and > then for point 2 I want to use the rain function from Module B. At first > though I would just init the class from either A or B for each point but I > need the function from A or B to be able to use function from my main > program...that is why the inheritance thing worked great for just one > module. Don't try to make the model A and B into base classes of the forecast. Just pass the forecast object to the model. So your rain() method will look more like this: def rain(self, fc): fc.calTime() # this function is in the main forecast class fc.rain = do stuff for model A where fc will be the forecast instance. You might also be able to use simple functions rather than classes: def rain(fc): fc.calTime() # this function is in the main forecast class fc.rain = do stuff for model A then just call moduleA.rain(self) or moduleB.rain(self). You might want to read about the Strategy pattern, that is what you are doing. Here is a Python example of a class-based Strategy. http://mail.python.org/pipermail/python-list/2006-April/379188.html This example passes the strategy to the constructor but you can set it in your pointInfo() method if you like. I would just make one function to handle each point though: for each in x.pointList: x.handlePoint(each) where def handlePoint(self, point): pointStrategy = ... pointStrategy.rain(self, point) Kent Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to use function from specific module and then switch to other module
Classification: UNCLASSIFIED Caveat (s): FOUO I have a program that collects weather data from weather models. I originally had a module that contained a bunch of function that I used. So I just added it to the init of the class I was using and inherited the functions. That worked great but now I have two different models that I can get weather from. Each Module A and B have the exact same function names in them but they both do slightly different things. The idea is as I step through a list I want to use a different function (same name but from a different module) for each element in the list. How do I have a generic way to do this. for example for point 1 I want to use the rain function from Module A and then for point 2 I want to use the rain function from Module B. At first though I would just init the class from either A or B for each point but I need the function from A or B to be able to use function from my main program...that is why the inheritance thing worked great for just one module. I have tried to make a very simple and not syntactically correct mockup of the code to help illustrate my problem. class forecast: def __init__(self) def calcTime(self): #calculate some time and date stuff. x.day = time stuff x.time = time stuff def cloths2Wear(self): self.rain() # I wan to make this simple call but somewhere along the line have set which module def getPoint(self): self.pointList = 1,2,3,4,5,6 def pointInfo(self): # get info about the point say which model to use x.model = A or B x = forecast() x.getPoint() for each in x.pointList: x.pointInfo(each) x.rain() # I want to call the correct function based on what was set in pointInfo. module A class A: def __init__(self): self.model=A def rain(self): self.calTime() # this function is in the main forecast class self. rain = do stuff for model A module B class B: def __init__(self): self.model=B def rain(self): self.calTime() # this function is in the main forecast class self. rain = do stuff for model B Thanks for any ideas you might have. John Ertl Meteorologist FNMOC 7 Grace Hopper Ave. Monterey, CA 93943 (831) 656-5704 [EMAIL PROTECTED] Classification: UNCLASSIFIED Caveat (s): FOUO ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] threading not working how about fork?
Classification: UNCLASSIFIED Caveat (s): FOUO Thanks for the help and I am looking into the pyprocessing but threading is causing too many headaches (I may have to rewrite things). Lets say I have something as simple as below: def takeTime(a): print "Started %s" % a time.sleep(10) print "Ended %s" % a for each in [1,2,3,4,5]: pid = os.fork() takeTime(each) Each time the function is called it waits 10 seconds. And if I run it as above it does the first and waits 10 seconds then the second and waits ten seconds...etc. Wow could I get it to run through all 5 "forks" without waiting for the previous one to complete? That was all five would be done in about 10 seconds. This is simpler than my real problem but I can not even figure this one out. Thanks for the help. John Ertl Meteorologist FNMOC 7 Grace Hopper Ave. Monterey, CA 93943 (831) 656-5704 [EMAIL PROTECTED] Classification: UNCLASSIFIED Caveat (s): FOUO ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python 2.4 threading
Classification: UNCLASSIFIED Caveat (s): FOUO All, I have a program that basically loops through two loops and I wanted to speedup the stuff inside the second loop so I thought threading would be a good idea but I am having a hard time getting it to work. All of the examples I have seen that use a Queu have had the threaded stuff inside its own class. I have tired that but I am not getting it. Could anyone give me some hints on how I might set this up. Below is an outline of my current program. What do I need to do to thread the inside loop. Many of the functions depicted below inherit function from other modules. class weather(a bunch of stuff) def 1 def 2 def3 defetc weax = weather() .stuff. for each in outsideloop: def1 defn for each in insideloop: def1 def2 defn I have tried to take the inside loop and make it a class but there are so many functions , some inherited that are used on the inside and outside loop that I am getting errors all over the place. Is it possible to thread without it being its own class? Thanks for the ideas and help. John Ertl Meteorologist FNMOC 7 Grace Hopper Ave. Monterey, CA 93943 (831) 656-5704 [EMAIL PROTECTED] Classification: UNCLASSIFIED Caveat (s): FOUO ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to return an error from a CGI
Classification: UNCLASSIFIED Caveat (s): FOUO All, I have been writing simple cgi scripts for a long time but have never worried about the error codes. Now I have been asked to return a specific error and I have no idea how to do this. I do not even know if I should be returning an HTTP or URL error. If I understand correctly, I am supposed to return something similar to say a 500 error. I think this is a HTTP error and I tried a few things but no luck. Thanks for the ideas. John Ertl Meteorologist FNMOC 7 Grace Hopper Ave. Monterey, CA 93943 (831) 656-5704 [EMAIL PROTECTED] Classification: UNCLASSIFIED Caveat (s): FOUO ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Best way to POST XML to CGI
Alan, Thanks for the input. I am trying to make something that is capable of being a bit more B2B than a standard HTML form...but be able to have a friendly interface for a person. I have not liked SOAP in the past...way to much extra stuff. I was trying to think REST but I have to admit I am missing some pieces. Maybe XML/RPC. Thanks again for the advice...I will rethink how I am doing this. John Ertl Meteorologist FNMOC 7 Grace Hopper Ave. Monterey, CA 93943 (831) 656-5704 [EMAIL PROTECTED] From: [EMAIL PROTECTED] on behalf of Alan Gauld Sent: Thu 6/7/2007 9:30 AM To: tutor@python.org Subject: Re: [Tutor] Best way to POST XML to CGI "Ertl, John C CIV 63134" <[EMAIL PROTECTED]> wrote > I have a Python program that makes images of data > and I want to be able to make these via a web interface. Fair enough. > I want to keep it simple so I thought I would send > an XML file to a cgi program But thats not simple. CGI is not really designed to deal with XML, they are almost complementary technologies. CGI sends name/value pairs either in GET or POST format. XML encodes the values into a text file. Different things. XML tends to be used for RPC or B2B type applications, CGI is typically used for basic form submissions. Sure you can do it, but I'm not sure there is much point! I'd probably look to use CGI to capture the details then create the XML file at the server end. Its a lot more efficient in terms of network bandwidth too. XML is about the most inefficient network "protocol" ever invented. > I have an HTML form that I use to collect input on what > kind of image. I then take that form input and make it > into an XML string. I then take that XML string and POST > it to a CGI script. As I say, a pretty complex process compared to a simple CGI submit action. > I bet Python has a simple way to receive a XML post > so I do not have to look for a specific form name? Python offers many ways to deal with XML file transmissions, including XML/RPC and SOAP. But CGI is probably not the best approach for XML IMHO. It can be done in the same way as posting a normal text file but its pretty much turning an easy job into a hard one for no benefit. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Best way to POST XML to CGI
All, I have a Python program that makes images of data and I want to be able to make these via a web interface. I want to keep it simple so I thought I would send an XML file to a cgi program (XML is what I am supposed to use). I have the parsing of the XML all figured out but the way I am sending the XML and receiving it does not look like they are the best ways. I have an HTML form that I use to collect input on what kind of image. I then take that form input and make it into an XML string. I then take that XML string and POST it to a CGI script. I am using cgi to retrieve the XML string. Once I have the string I get the needed info and make an image. The Part that just does not look right to me is using form = cgi.FieldStorage() Of someone else wanted to just post an XML string they would have to have the same form name "XMLhttp" that is not very user friendly. My guess is I am missing something about how cgi can work. I bet Python has a simple way to receive a XML post so I do not have to look for a specific form name? Any help would be appreciated. Thanks, John Web page code to post XML in a text area http:///cgi-bin/parsXML2.py"; target=text/xml method=post> Code that receives the XML POST form = cgi.FieldStorage() if not (form.has_key("XMLhttp")): print "content-type: text/html\n\n" print "Error" else: xmlString = form["XMLhttp"].value print "content-type: text/html\n\n" req = ParseXML() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] problem with mmap
All, I am trying to work an example that I found at http://bitworking.org/news/132/REST-Tips-URI-space-is-infinite When I try the code below I get an error and I am not able to figure it out. Thanks for any and all help. I have two png and a text file (sortzip.txt) in the same dir as this code but I do not even get to that part...the mmap call is bad. I have never used mmap before so this is new to me. I am running python 2.4 John # code from mmap import mmap import os from bisect import bisect_left import sys class Zipcodes(object): """Use mmap to treat the sorted file of zipcodes as an array""" def __init__(self): self.f = open("sortzips.txt", "r+") self.size = os.path.getsize("sortzips.txt") self.m = mmap(self.f.fileno(), self.size) def __getitem__(self, i): self.m.seek(6*i) return self.m.read(5) def __del__(self): self.m.close() self.f.close() def __len__(self): return self.size / 6 zipcodes = Zipcodes() target = os.environ.get('PATH_INFO', '/')[1:] found = ( zipcodes[bisect_left(zipcodes, target)] == target ) print "Status: " + ( found and "200 Ok" or "404 Not Found" ) print "Cache-control: max-age=172800" print "Content-type: image/png" print "" file = open(found and "good.png" or "bad.png", "r") png = file.read() file.close() sys.stdout.write(png) ## error message when I try to run this. Traceback (most recent call last): File "./zipcode.cgi", line 23, in ? zipcodes = Zipcodes() File "./zipcode.cgi", line 14, in __init__ self.m = mmap(self.f.fileno(), self.size) EnvironmentError: [Errno 22] Invalid argument Exception exceptions.AttributeError: "'Zipcodes' object has no attribute 'm'" in > ignored ### John Ertl Meteorologist FNMOC 7 Grace Hopper Ave. Monterey, CA 93943 (831) 656-5704 [EMAIL PROTECTED] <>___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with re
Kent, Thanks for the nock on the head, that has bitten me before. Taking out the spaces worked great. Thanks again, John Ertl -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kent Johnson Sent: Monday, May 08, 2006 10:53 AM Cc: tutor@python.org Subject:Re: [Tutor] trouble with re Ertl, John wrote: > I have a file with 10,000 + lines and it has a coma delimited string on each > line. > > The file should look like: > > DFRE,ship name,1234567 > FGDE,ship 2, > ,sdfsf > > The ,sdfsf line is bad data > > p = re.compile('\d{7}$ | [,]$') # this is the line that I can not get > correct I an trying to find lines that end in a comma or 7 digits Spaces are significant in regular expressions unless you compile them with the re.VERBOSE flag. Also you don't need to make a group for a single character. Try p = re.compile('\d{7}$|,$') or maybe p = re.compile('(\d{7}|,)$') Actually since the seven digits are preceded by the comma you could just make the digits optional: p = re.compile(',(\d{7})?$') Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] trouble with re
I have a file with 10,000 + lines and it has a coma delimited string on each line. The file should look like: DFRE,ship name,1234567 FGDE,ship 2, ,sdfsf The ,sdfsf line is bad data Some of the lines are messed up...I want to find all lines that do not end in a comma or seven digits and do some work on them. I can do the search for just the last seven digits but I can not do the seven digits or the comma at the end in the same search. Any ideas import re import sys import os p = re.compile('\d{7}$ | [,]$') # this is the line that I can not get correct I an trying to find lines that end in a comma or 7 digits newFile = open("newFile.txt",'w') oldFile = open("shipData.txt",'r') for line in oldFile: if p.search(line): newFile.write(line) else: newFile.write("*BAD DATA " + line) newFile.close() oldFile.close() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Halting execution
For this case, just wanting to stop the code for testing, using a short statement as the arg is not only OK it is an example in the doc. exit( [arg]) Exit from Python. .. In particular, sys.exit("some error message") is a quick way to exit a program when an error occurs. I agree I should be a bit more careful about giving examples that are quick fixes as opposed to best practice. John -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Hugo González Monteverde Sent: Wednesday, May 03, 2006 12:05 PM To: tutor@python.org Subject:Re: [Tutor] Halting execution It would be similar to Perl's die() commmand. But no, in Python the argument is the error status of your script. You'd have to do something like def exitnow(arg): print arg #maybe sys has not been imported import sys sys.exit(1) Hugo Ertl, John wrote: > Matthew, > > Not sure if ipython is different but have you tried sys.exit("stoping now") > ___ 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] Halting execution
Matthew, Not sure if ipython is different but have you tried sys.exit("stoping now") You could also put in a flag to turn the printing on and off so it wont print it out when you are testing. John -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Matthew Webber Sent: Wednesday, May 03, 2006 9:35 AM To: tutor@python.org Subject:[Tutor] Halting execution This has got to be trivial, but I can't find the answer ... I want to stop execution of my main script half way through. This is just for debugging - the code in the bottom half of the script generates a whole lot of output that I don't want. Inserting "break" or "return" doesn't work, but something like that is what I had in mind. I'm running under ipython on windows, FWIW. Thanks ___ 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] question about run time
f the month you want to check") parser.add_option("-m", "--month",default="None" ,help="The month of the year you want to check") parser.add_option("-y", "--year",default="None", help="The year you want to check") (options, args) = parser.parse_args() myUse = memoryUsage(options.userName,options.gpfs,options.email,options.memLimit, options.inodeLimit,options.month,options.day,options.year) myUse.getGPFSList() for each in myUse.gpfsList: myUse.makeFilePath(each) (memAmount,inodeAmount) = myUse.extractUserData() myUse.add(memAmount,inodeAmount) print "Your memory usage is %s KB and your inode usage is %s" % (myUse.memTotal,myUse.inodeTotal) print "Your memory limit is %s KB and your inode limit is %s" % (myUse.memLimit, myUse.inodeLimit) if myUse.memLimit < myUse.memTotal or myUse.inodeLimit < myUse.inodeTotal: print "You have excedded your limit" myUse.sendEmail("%s memory/inode limit reached on gpfs " % myUse.userName) -Original Message- From: Danny Yoo [mailto:[EMAIL PROTECTED] Sent: Tuesday, May 02, 2006 1:32 PM To: Ertl, John Cc: tutor@python.org Subject:Re: [Tutor] question about run time > I have been using python for sometime...and occasionally I noticed > significant delay before the code would run but unitl now I have been > able to write it off to other things. Now I have a short script that I > wrote to check some files and print out a few lines. > > I have noticed that usually the first time I fire it up in the morning > or after a long time of not running it, it takes 10-15 seconds to run > and the output to the screen is very slow...maybe 1 second per line. > If I run it soon after that it runs and the output is on the screen in > less then a second. I would think this has to do with compiling but I > am not sure. Any ideas how to speed this up? > > I am running python 2.4 on a RHE3.0 cluster. ^^ Hi John, One thing to check is to see if the program is spending the majority of its time doing input and output (I/O Bound), or if it's really doing heavy computations (CPU bound). Knowing this might provide clues as to why you're seeing this kind of jerky performance. Also, you may want to check with your cluster folks on the possible effects the cluster's architecture may have on program startup. You're running on a slightly specialized platform, so I wouldn't be surprised if the cluster architecture is contributing something special. Finally, if you want to share that script for people to comment on, that might help. Good luck! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] question about run time
Kent, I will check with the systems guys...and the Perl guys down the hall to see if they have the same problem. Thanks for the help. John Ertl -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Kent Johnson Sent: Tuesday, May 02, 2006 12:27 PM Cc: tutor@python.org Subject:Re: [Tutor] question about run time Ertl, John wrote: > Kent, > > The files are very small (a few hundred lines). Maybe it is a network > issue? But then why is it always slow the first time in the morning? I > don't know network stuff but that seams a bit strange. Maybe the network access is slow and the files are cached locally after the first access? I think Windows does this... Some things you might want to try: - Open one of the files in a text editor. Close it and open it again. Is it faster the second time? - Write a simple python program to open one of the files and read it. Is it faster the second time you run it? HTH, I'm guessing here. I have definitely seen scripts that run faster the second time and attribute it to file caching somewhere...though I haven't seen such a significant difference as you. Kent > > Thanks, > > John Ertl > > -Original Message- > From: Kent Johnson [mailto:[EMAIL PROTECTED] > Sent: Tuesday, May 02, 2006 12:06 PM > To: Ertl, John > Cc: tutor@python.org > Subject: Re: [Tutor] question about run time > > Ertl, John wrote: >> I have been using python for sometime...and occasionally I noticed >> significant delay before the code would run but unitl now I have been able >> to write it off to other things. Now I have a short script that I wrote > to >> check some files and print out a few lines. >> >> I have noticed that usually the first time I fire it up in the morning or >> after a long time of not running it, it takes 10-15 seconds to run and the >> output to the screen is very slow...maybe 1 second per line. If I run it >> soon after that it runs and the output is on the screen in less then a >> second. I would think this has to do with compiling but I am not sure. > Any >> ideas how to speed this up? > > Compiling is not that slow. Are you files huge? Possibly they are in the > disk cache after the first run. > > Kent > > > ___ 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] question about run time
Kent, The files are very small (a few hundred lines). Maybe it is a network issue? But then why is it always slow the first time in the morning? I don't know network stuff but that seams a bit strange. Thanks, John Ertl -Original Message- From: Kent Johnson [mailto:[EMAIL PROTECTED] Sent: Tuesday, May 02, 2006 12:06 PM To: Ertl, John Cc: tutor@python.org Subject:Re: [Tutor] question about run time Ertl, John wrote: > I have been using python for sometime...and occasionally I noticed > significant delay before the code would run but unitl now I have been able > to write it off to other things. Now I have a short script that I wrote to > check some files and print out a few lines. > > I have noticed that usually the first time I fire it up in the morning or > after a long time of not running it, it takes 10-15 seconds to run and the > output to the screen is very slow...maybe 1 second per line. If I run it > soon after that it runs and the output is on the screen in less then a > second. I would think this has to do with compiling but I am not sure. Any > ideas how to speed this up? Compiling is not that slow. Are you files huge? Possibly they are in the disk cache after the first run. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] question about run time
I have been using python for sometime...and occasionally I noticed significant delay before the code would run but unitl now I have been able to write it off to other things. Now I have a short script that I wrote to check some files and print out a few lines. I have noticed that usually the first time I fire it up in the morning or after a long time of not running it, it takes 10-15 seconds to run and the output to the screen is very slow...maybe 1 second per line. If I run it soon after that it runs and the output is on the screen in less then a second. I would think this has to do with compiling but I am not sure. Any ideas how to speed this up? I am running python 2.4 on a RHE3.0 cluster. Thanks, John Ertl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to extract number of files from directory
Marc, You can do most system type stuff in Python and it makes it much easier. import glob jpgList = glob.glob("*.jpg") # the glob allows you to use wild cards in the search jpgCount = len(jpgList) This gives you a list of all files that end in .jpg. You can then do a len(jpgList) to get the number of files John -Original Message- From: Marc Buehler [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 12, 2005 11:10 AM To: tutor@python.org Subject:[Tutor] how to extract number of files from directory hi. i'm new to Python ... i would like to extract the number of JPG files from the current directory and use that number as a parameter in my python script. i tried: a = os.system('ls *JPG | wc -l') when i do: print a i get '0'. what am i missing? marc --- The apocalyptic vision of a criminally insane charismatic cult leader http://www.marcbuehler.net __ Yahoo! Music Unlimited Access over 1 million songs. Try it free. http://music.yahoo.com/unlimited/ ___ 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] launching and monitor from make
Alan, Thanks, just as you and Ewald said it works great. Almost too easy...I have this feeling something will turn around a bite me once it goes to ops. Thanks again John Ertl -Original Message- From: Alan Gauld [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 12, 2005 10:01 AM To: Ertl, John; tutor@python.org Subject:Re: [Tutor] launching and monitor from make > The CM team here would like to have the script run each week as part of > the > automated rebuilds. The suggestion was to make the execution of the > scritp > part of the bigger programs make file. How would I handel errors when the > program is run from a makefile? I hate make so I have not done a lot of > playing with what make can do in this respect but can python get an error > message to make? You can write the errors to stderr instead of stdout. You can also exit with an error code using sys.exit(). make should detect the non standard error code because it uses standard shell processing.. Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] launching and monitor from make
Ewald, That easy...If you know. Thanks for the example and the help. Now lets see if I can modify the make file without upsetting the make god. Thanks again. John Ertl -Original Message- From: Ewald Ertl [mailto:[EMAIL PROTECTED] Sent: Wednesday, October 12, 2005 7:40 AM To: Ertl, John Subject:Re: [Tutor] launching and monitor from make Hi John Ertl, John wrote: > All, > > I have a very simple python program that reads one file and overwrites > anouther text file. This workes great from the command line and it has > error checking but just outputs messages and the like to the screen. > > The CM team here would like to have the script run each week as part of the > automated rebuilds. The suggestion was to make the execution of the scritp > part of the bigger programs make file. How would I handel errors when the > program is run from a makefile? I hate make so I have not done a lot of > playing with what make can do in this respect but can python get an error > message to make? I think your Python-Script is an executeable "Shell"-Script with "#!/usr/bin/env python ... " So you can just insert your script in the makefile When the exit-Code of the script is 0, than make assumes that everything is ok, otherwise an error occured. Here's a short example with "ls" Here the mkfile want's to list the file "hugo" which does not exist mkfile: all: ls -l hugo -- #gmake -f mkfile ls -l hugo hugo: Datei oder Verzeichnis nicht gefunden gmake: *** [all] Error 2 Here the mkfile itself is listed with ls mkfile: all: ls -l mkfile -- #gmake -f mkfile ls -l mkfile -rw-rw-r-- 1 ewer entw 19 Okt 12 16:35 mkfile HTH Ewald Ertl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] launching and monitor from make
All, I have a very simple python program that reads one file and overwrites anouther text file. This workes great from the command line and it has error checking but just outputs messages and the like to the screen. The CM team here would like to have the script run each week as part of the automated rebuilds. The suggestion was to make the execution of the scritp part of the bigger programs make file. How would I handel errors when the program is run from a makefile? I hate make so I have not done a lot of playing with what make can do in this respect but can python get an error message to make? Thanks, John Ertl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Finding difference in two .gif
All, Well I found the answer...getbox() gave me the bounding box and then I could just plot that portion of the two images. Python 2.4, PIL 1.1.5 The simple code I have tried: import Image import ImageChops file1 = Image.open("/home/PT04_RH.2005072300.gif") file2 = Image.open("/home/PT04_RH.2005093000.gif") diffbox = ImageChops.difference(file1,file2).getbbox() diffImage = file1.crop(diffbox) diffImage.save("diffOut.gif") ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Finding difference in two .gif
All, I have two gif images that should be exactly the same...one static and the other recreated from the same data. For months this has been working great but yesterday the "new" image that I made was slightly different. The image size is 4 bytes different. When I look at the image side by side I can not see the difference. I have very limited image manipulation software and I was trying to isolate the image difference using PIL but all I get are black images. I know nothing about how the imaging works so it could be as easy as reseting the scale but I tried with no luck. Does anyone know how to isolate (hopefully small) image differences using PIL? Thank you, John Ertl Python 2.4, PIL 1.1.5 The simple code I have tried: import Image import ImageChops file1 = Image.open("/home/PT04_RH.2005072300.gif") file2 = Image.open("/home/PT04_RH.2005093000.gif") #diffImage = ImageChops.difference(file1,file2) diffImage = ImageChops.subtract(file1,file2) diffImage.save("diffOut.gif") ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Generate 8 digit random number
Sorry for that you will have to change the %09 to a %08 to get 8 digits. I got a bit to fixated on the John Ertl -Original Message- From: Ertl, John Sent: Friday, August 26, 2005 2:23 PM To: Alberto Troiano; tutor@python.org Subject:RE: [Tutor] Generate 8 digit random number Alberto If you don't mind having leading 0 then you could just do the random like you did then format it to 9 digits. You could give this a try num = random.randrange(,) num8 = "%09i" % num John Ertl -Original Message- From: Byron [mailto:[EMAIL PROTECTED] Sent: Friday, August 26, 2005 1:50 PM To: Alberto Troiano; tutor@python.org Subject:Re: [Tutor] Generate 8 digit random number Hi Alberto, Here's how to do it: --- import random def generateKey(): nums = "0123456789" strNumber = "" count = 0 while (count < 8): strNumber += nums[random.randrange(len(nums))] count += 1 print strNumber # A quick test... count = 0 while (count < 1): generateKey() count += 1 --- Byron :-) --- Alberto Troiano wrote: > Hi everyone > > I need to generate a password..It has to be an 8 digit number and it has to > be random > > The code I've been trying is the following: > > > import random > random.randrange(,) > > The code works but sometimes it picks a number with 7 digits. Is there any > way that I can tell him to select always a random number with 8 digits? > > Thanks in advanced > > Alberto > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ 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] Generate 8 digit random number
Alberto If you don't mind having leading 0 then you could just do the random like you did then format it to 9 digits. You could give this a try num = random.randrange(,) num8 = "%09i" % num John Ertl -Original Message- From: Byron [mailto:[EMAIL PROTECTED] Sent: Friday, August 26, 2005 1:50 PM To: Alberto Troiano; tutor@python.org Subject:Re: [Tutor] Generate 8 digit random number Hi Alberto, Here's how to do it: --- import random def generateKey(): nums = "0123456789" strNumber = "" count = 0 while (count < 8): strNumber += nums[random.randrange(len(nums))] count += 1 print strNumber # A quick test... count = 0 while (count < 1): generateKey() count += 1 --- Byron :-) --- Alberto Troiano wrote: > Hi everyone > > I need to generate a password..It has to be an 8 digit number and it has to > be random > > The code I've been trying is the following: > > > import random > random.randrange(,) > > The code works but sometimes it picks a number with 7 digits. Is there any > way that I can tell him to select always a random number with 8 digits? > > Thanks in advanced > > Alberto > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] would pickle or cpickle help?
All, I have a text file that contains several thousand lines of space delimited text that contain a ship ID and a ship name. This file is updated every few months. I have another file that contains the ship ID and some other info but not the ship name. I have to append the ship name to the end of the line. Easy enough. I currently make a dictionary of the shipID(key) and ship name(value) and use this to append the ship name to the end of the line that contains the ship ID. Could I use something like cpickle to store the dictionary once it is made so I would not have to make it each time? I have never tried to use pickle so I am bit fuzzy on what it can store and what it can't. Also would it really buy me anything...it only takes a second or two to make the dictionary? There is a chance the file that I use to make the dictionary will eventually grow to be 10,000 lines or more. Thanks for the ideas. John Ertl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Just a Python formatting question
Kristiano, It is sometimes hard to tell the indentions in an email but it looks like your last line print "Just right" has a space in front of it. Python does not know what logical block the indented part belongs to. John Ertl -Original Message- From: Kristiano Ang [mailto:[EMAIL PROTECTED] Sent: Wednesday, June 08, 2005 09:49 To: tutor@python.org Subject: [Tutor] Just a Python formatting question Hey guys, I'm pretty new to Python (using Mac Python 2.4.1) and have a question with formatting that I hope you can help me with. Sometimes, when I write code with Python (copied off tuts.), I get error messages and highlights of the word "else". Take for example: #plays the guessing game higher or lower #originally written by Josh Cogliati, improved by Quique and copied by Kristiano Ang number=78 guess=0 while guess != number: guess=input ("Guess a number:") if guess > number: print "Too High" elif guess < number: print "Too low" print "Just right" I just can't get it to run and I get some indentation error. I'm pretty new to this so I'm sorry if this question(s) sound(s) amateurish. Do help. Thanks. -Kristiano Ang ___ 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] read() question
John, The 'r' is the mode and is used to indicate you are reading the file. You could also use 'w' for only writing. See the attached link for more. http://www.python.org/doc/current/tut/node9.html#SECTION0092 0 John -Original Message- From: John Carmona [mailto:[EMAIL PROTECTED] Sent: Wednesday, May 11, 2005 09:51 To: tutor@python.org Subject: [Tutor] read() question MyText = open('The_text.txt','r').read() In the above line could someone tell me what the 'r' stand for. Many thanks JC ___ 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] using TK to view an image and then close the window
Michael, Thanks for the understanding and help, It works kind of. I was getting this error with one of my iterations of the code as well. Do you know what might be causing this? Since your code also produced this I figured Oh-Well and I just put in a try and this at least keeps the error from printing to the screen. I will keep trying...a guick google gave me an idea but not sure. Thanks again for the help Traceback (most recent call last): File "./eraseLauncher.py", line 19, in ? TK.TKview(newIm,mainTitle="image") File "/gpfs3/home/ertlj/BATCH/meteogram/new/test/TKviewTest.py", line 22, in TKview canvas.create_image(0,0,anchor='nw',image=p) File "/home/ertlj/ertljVersion/lib/python2.4/lib-tk/Tkinter.py", line 2086, in create_image return self._create('image', args, kw) File "/home/ertlj/ertljVersion/lib/python2.4/lib-tk/Tkinter.py", line 2075, in _create return getint(self.tk.call( _tkinter.TclError: image "pyimage2" doesn't exist -Original Message- From: Michael Lange [mailto:[EMAIL PROTECTED] Sent: Tuesday, April 26, 2005 01:58 To: Ertl, John Subject: Re: [Tutor] using TK to view an image and then close the window On Mon, 25 Apr 2005 14:09:17 -0700 "Ertl, John" <[EMAIL PROTECTED]> wrote: > Michael, > > I got the TK code from an old post...I really do not know much about how it > is supposed to work. I just thought I seamed so close that it should be > simple to fix. I was trying to use self and what not but I still could not > get the window to close down. I guess I will just have to learn a bit more > about Tkinter. > > Thanks, > > John Ertl > John, you are right, it was quite close. It just could be done a little simpler, like this: def TKview(img,mainTitle="image"): top = Tkinter.Tk() top.protocol("WM_DELETE_WINDOW", top.quit)# not really necessary if you don't want to do any cleanup on exit top.bind("",lambda event : top.quit)# use lambda here to catch the event that gets passed by bind() canvas = Tkinter.Canvas(top) canvas.pack() p = ImageTk.PhotoImage(img) canvas['width'] = img.size[0] canvas['height'] = img.size[1] canvas.create_image(0,0,anchor='nw',image=p) top.mainloop() I didn't test this, but I think it should do pretty much the same as you expected from the code you posted. Calling top.destroy() is not really necessary, because python should do this for you when you quit the mainloop. However it seems to be "cleaner" to call destroy() explicitely with a construction like this (pseudo code): top = Tkinter.Tk() top.protocol("WM_DELETE_WINDOW", top.quit) top.mainloop() top.destroy() Using the protocol() method is necessary here, because otherwise clicking the "Close" button in the window's title bar would destroy the window, so calling top.destroy() would raise an error. The last line of this code is only reached, after the mainloop was interrupted by calling quit() . What's nice about this construction is, that you can put some cleanup-on-exit stuff before top.destroy() that's performed automatically when the window is closed; in a more complex application with several modules it's also nice that you can use any (child) Tk widget's quit() method to stop the mainloop, so it's not necessary to have a reference to the main Tk() instance in every module. Best regards Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] RE: using TK to view an image and then close the window
I forgot to mention that I am running on a Linux system if that makes any difference for the TK part. Thanks, John Ertl -Original Message- From: Ertl, John Sent: Monday, April 25, 2005 10:24 To: tutor@python.org Subject: using TK to view an image and then close the window All, I have been struggling with being able to view an image and most things have not worked for one reason or another so I am trying to fall back on a way I used to do it in PERL. Use TK to show the image. I do not know TKInter but I have two scripts one that gets a list of images and anouther that displays them using Tkinter. This is basically how I would like to use it...I am going to be using PIL to manipulate the image (and show() does not work) so I just want a quick view to see how it looks. I can get the image to show up and minimize and maximize but I can not get the window to close and then display the next image. I have tired several things but no luck. Any help on getting the TK window to shutdown cleanly would be appreciated. Thanks John Ertl # this is supposed to display an image in a TK window but the close (X) does not work. import Image import Tkinter,ImageTk def TKview(img,mainTitle="image"): app = Tkinter.Tk() app.withdraw() top = Tkinter.Toplevel(app,visual="truecolor",colormap="new") top.title(mainTitle) top.protocol("WM_DELETE_WINDOW", quit) top.bind("",quit) top.bind("",quit) canvas = Tkinter.Canvas(top) canvas.pack() p = ImageTk.PhotoImage(img) canvas['width'] = img.size[0] canvas['height'] = img.size[1] canvas.create_image(0,0,anchor='nw',image=p) top.mainloop() def quit(event=None): top.destroy() top.quit() # this code gets the images opens them and calls the TK code above to display them import glob import thread import Image import TKviewTest # the module to view the images gifList = glob.glob("./*.gif") print gifList for image in gifList: image = image[2:] # glob leaves ./ in file name newIm= Image.open(image) TK = TKviewTest thread.start_new_thread(TK.TKview(newIm,mainTitle="image")) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] using TK to view an image and then close the window
All, I have been struggling with being able to view an image and most things have not worked for one reason or another so I am trying to fall back on a way I used to do it in PERL. Use TK to show the image. I do not know TKInter but I have two scripts one that gets a list of images and anouther that displays them using Tkinter. This is basically how I would like to use it...I am going to be using PIL to manipulate the image (and show() does not work) so I just want a quick view to see how it looks. I can get the image to show up and minimize and maximize but I can not get the window to close and then display the next image. I have tired several things but no luck. Any help on getting the TK window to shutdown cleanly would be appreciated. Thanks John Ertl # this is supposed to display an image in a TK window but the close (X) does not work. import Image import Tkinter,ImageTk def TKview(img,mainTitle="image"): app = Tkinter.Tk() app.withdraw() top = Tkinter.Toplevel(app,visual="truecolor",colormap="new") top.title(mainTitle) top.protocol("WM_DELETE_WINDOW", quit) top.bind("",quit) top.bind("",quit) canvas = Tkinter.Canvas(top) canvas.pack() p = ImageTk.PhotoImage(img) canvas['width'] = img.size[0] canvas['height'] = img.size[1] canvas.create_image(0,0,anchor='nw',image=p) top.mainloop() def quit(event=None): top.destroy() top.quit() # this code gets the images opens them and calls the TK code above to display them import glob import thread import Image import TKviewTest # the module to view the images gifList = glob.glob("./*.gif") print gifList for image in gifList: image = image[2:] # glob leaves ./ in file name newIm= Image.open(image) TK = TKviewTest thread.start_new_thread(TK.TKview(newIm,mainTitle="image")) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] trouble setting the environment
Kent, Like you allude ...a bit too much "what the heck is that" going on. I will give a few other things a try...I may just have to have the program run and get the info then stop and have the user source the correct .ksh then run another py program. (basically the same thing but manually). Thanks, John Ertl -Original Message- From: Kent Johnson [mailto:[EMAIL PROTECTED] Sent: Thursday, April 21, 2005 13:49 Cc: tutor@python.org Subject: Re: [Tutor] trouble setting the environment Ertl, John wrote: > Kent, > > Good idea except that the environment that needs to be set depends on the > answers to some of the input that I get in the Python program. Nothing is > ever easy here. Maybe you could write a Python program that asks the questions, then spawns a shell task which sets the correct environment and runs another Python program that does the rest of the work? Or, a ksh wrapper that sources the right program then outputs its environment to a .py file that you can import to get the config? Just don't give me credit for the idea, I don't want to have anything to do with it :-) Kent > -Original Message- > From: Kent Johnson [mailto:[EMAIL PROTECTED] > Sent: Thursday, April 21, 2005 13:20 > Cc: tutor@python.org > Subject: Re: [Tutor] trouble setting the environment > > Ertl, John wrote: > >>All, >> >>I have program and init I want to "source" a .ksh file to set some >>environment variables and then use those variables in my program. >> >>Is this possible? I vaguely remember something about the system env and > > the > >>interpreters env being separate after the interpreter starts up. > > > What about making a shell file that sources your ksh file, then starts > python? > > Kent > ___ 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] trouble setting the environment
Kent, Good idea except that the environment that needs to be set depends on the answers to some of the input that I get in the Python program. Nothing is ever easy here. Thanks for the ideas. John Ertl -Original Message- From: Kent Johnson [mailto:[EMAIL PROTECTED] Sent: Thursday, April 21, 2005 13:20 Cc: tutor@python.org Subject: Re: [Tutor] trouble setting the environment Ertl, John wrote: > All, > > I have program and init I want to "source" a .ksh file to set some > environment variables and then use those variables in my program. > > Is this possible? I vaguely remember something about the system env and the > interpreters env being separate after the interpreter starts up. What about making a shell file that sources your ksh file, then starts python? Kent > > For instance if I have a .ksh file called envSet.ksh: > > #!/bin/ksh > > unset OPSBIN > > export OPSBIN=/u/ops/bin > > ---end -- > > Then > > >>>>os.system(". envSet.ksh") > > 0 > >>>>os.getenv("OPSBIN") >>>> > > > What is the 0. I know that I can set the env using Python but all of the > correct env are in the .ksh files maintained by others. I would hate to > have to take the .ksh and tread each line and if it is an export turn that > into a python os.environ statement. > > Any ideas. > > Thanks > > John > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] trouble setting the environment
All, I have program and init I want to "source" a .ksh file to set some environment variables and then use those variables in my program. Is this possible? I vaguely remember something about the system env and the interpreters env being separate after the interpreter starts up. For instance if I have a .ksh file called envSet.ksh: #!/bin/ksh unset OPSBIN export OPSBIN=/u/ops/bin ---end -- Then >>> os.system(". envSet.ksh") 0 >>> os.getenv("OPSBIN") >>> What is the 0. I know that I can set the env using Python but all of the correct env are in the .ksh files maintained by others. I would hate to have to take the .ksh and tread each line and if it is an export turn that into a python os.environ statement. Any ideas. Thanks John ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: FW: [Tutor] Trying to d0 HTTP GET
Kent and Danny, Thanks for the help. The trick was to put the .read() at the end of the urllib2 line. It makes some sense now but man I hate it when it is that simple and I just can't see it. Thanks again for the great help. John Ertl -Original Message- From: Kent Johnson [mailto:[EMAIL PROTECTED] Sent: Tuesday, April 19, 2005 10:33 Cc: tutor@python.org Subject: Re: FW: [Tutor] Trying to d0 HTTP GET Ertl, John wrote: > All, > > I have figured out a bit more. I can get the binary values from the service > but I think they come back as a single string. How do I read that into an > array? The code below will read the first number into the array and print > it out but how would I read the whole thing into an array...I would like to > skip the step of putting the raw binary numbers into a variable and instead > read it directly into the binvalues array. > > I have tried things like binvalues.read(rawData.read(4,size of array)) and > a few other things but none of them work. I was hoping for a fromstream but > no luck no that either. Unless the data is huge you should probably just read it all into a string, then pass the string to binvalue like this: binvalues = array.array('f') rawData = urllib2.urlopen(...).read() binvalues.fromstring(rawData) binvalues.byteswap() This is likely to be the fastest approach as all the looping happens internally to urllib2 and array. The only limitation is that both representations have to fit in memory at once. Alternately you could wrap the rawData in a generator function which returns floats. Then pass the generator to binvalues.extend(). Something like this (untested): import array, struct, urllib2 def generateFloats(rawDataStream): while True: s = rawData.read(4) if len(s) < 4: return f = struct.unpack('f', s) # prefix the 'f' with the correct byte-order character... yield f binvalues = array.array('f') rawDataStream = urllib2.urlopen(...) binvalues.extend(generateFloats(rawDataStream)) Kent > Thanks for any help. > > binvalues = array.array('f') > > rawData = > urllib2.urlopen("http://dsd1u:7003/GRID:U:NOGAPS:2005041800:global_360x181:a > ir_temp:ht_sfc:0002::fcst_ops:0240") > > > binvalues.fromstring(rawData.read(4)) # 4 byte float > > binvalues.byteswap() > > print binvalues > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
FW: [Tutor] Trying to d0 HTTP GET
All, I have figured out a bit more. I can get the binary values from the service but I think they come back as a single string. How do I read that into an array? The code below will read the first number into the array and print it out but how would I read the whole thing into an array...I would like to skip the step of putting the raw binary numbers into a variable and instead read it directly into the binvalues array. I have tried things like binvalues.read(rawData.read(4,size of array)) and a few other things but none of them work. I was hoping for a fromstream but no luck no that either. Thanks for any help. binvalues = array.array('f') rawData = urllib2.urlopen("http://dsd1u:7003/GRID:U:NOGAPS:2005041800:global_360x181:a ir_temp:ht_sfc:0002::fcst_ops:0240") binvalues.fromstring(rawData.read(4)) # 4 byte float binvalues.byteswap() print binvalues -Original Message- From: Kent Johnson Cc: tutor@python.org Sent: 4/18/05 1:10 PM Subject: Re: [Tutor] Trying to d0 HTTP GET Ertl, John wrote: > All, > > I am trying to get some binary data from a web service. None of the tech > guys are around so I am hoping you might be able to shed some light on what > might be happening. I would think that f = urllib.urlopen(...) data = f.read() would work. You could try urllib2.urlopen() and see if it is any better. How big is the data you are expecting? Kent > > Here is part of the email that explained what I needed to do. > > - clip --- > > If you can do an http "get" from Python, you'll be set. > > http://dsd1u:7003/GRID:U:WW3_GLOBAL:2005041512:global_360x181:max_wav_ht :sur > face:::fcst_ops:0480 > > It returns an http header like the following (if the grid exists), > followed by the grid data in big-endian, IEEE format. > > "HTTP/1.1 200 OK\r\n" > "Server: ISIS/4.0\r\n" > "Content-type: application/x-grid\r\n" > "Content-length: 261234\r\n\r\n" > > - end- > > The grid data is in Binary. How would I get to this? I would imagine that > since f (the object) exists the call to the web service worked. Now I need > to read the grid...eventually I need to put it into a Numeric array but not > sure how to get just the grid from "f". > > As a simple starting point I tried. > > >>>>import urllib >>>>f = > > urllib.urlopen("http://dsd1u:7003/GRID:U:WW3_GLOBAL:2005041800:global_36 0x18 > 1:max_wav_ht:surface:::fcst_ops:0240") > >>>>f.info() > > > >>>>f.readlines() > > > I tried read(), readLines() and some other stuff using scipy and Numeric. > > The prompt has moved to the next line but nothing else has happened for 30 > min or so (I have tried several times). When I try to close IDLE it says > the program is still running. How should I be getting this data is it > trying to read the binary and that is why it is stalled? > > Thanks, > > John Ertl > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ 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] Trying to d0 HTTP GET
This data set is 65160 bytes. I am having a bit more success with urllib2 but still not there yet...byte swapping and such. But now I think the server is having problems. Thanks for your help. -Original Message- From: Kent Johnson [mailto:[EMAIL PROTECTED] Sent: Monday, April 18, 2005 13:10 Cc: tutor@python.org Subject: Re: [Tutor] Trying to d0 HTTP GET Ertl, John wrote: > All, > > I am trying to get some binary data from a web service. None of the tech > guys are around so I am hoping you might be able to shed some light on what > might be happening. I would think that f = urllib.urlopen(...) data = f.read() would work. You could try urllib2.urlopen() and see if it is any better. How big is the data you are expecting? Kent > > Here is part of the email that explained what I needed to do. > > - clip --- > > If you can do an http "get" from Python, you'll be set. > > http://dsd1u:7003/GRID:U:WW3_GLOBAL:2005041512:global_360x181:max_wav_ht:sur > face:::fcst_ops:0480 > > It returns an http header like the following (if the grid exists), > followed by the grid data in big-endian, IEEE format. > > "HTTP/1.1 200 OK\r\n" > "Server: ISIS/4.0\r\n" > "Content-type: application/x-grid\r\n" > "Content-length: 261234\r\n\r\n" > > - end- > > The grid data is in Binary. How would I get to this? I would imagine that > since f (the object) exists the call to the web service worked. Now I need > to read the grid...eventually I need to put it into a Numeric array but not > sure how to get just the grid from "f". > > As a simple starting point I tried. > > >>>>import urllib >>>>f = > > urllib.urlopen("http://dsd1u:7003/GRID:U:WW3_GLOBAL:2005041800:global_360x18 > 1:max_wav_ht:surface:::fcst_ops:0240") > >>>>f.info() > > > >>>>f.readlines() > > > I tried read(), readLines() and some other stuff using scipy and Numeric. > > The prompt has moved to the next line but nothing else has happened for 30 > min or so (I have tried several times). When I try to close IDLE it says > the program is still running. How should I be getting this data is it > trying to read the binary and that is why it is stalled? > > Thanks, > > John Ertl > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Trying to d0 HTTP GET
All, I am trying to get some binary data from a web service. None of the tech guys are around so I am hoping you might be able to shed some light on what might be happening. Here is part of the email that explained what I needed to do. - clip --- If you can do an http "get" from Python, you'll be set. http://dsd1u:7003/GRID:U:WW3_GLOBAL:2005041512:global_360x181:max_wav_ht:sur face:::fcst_ops:0480 It returns an http header like the following (if the grid exists), followed by the grid data in big-endian, IEEE format. "HTTP/1.1 200 OK\r\n" "Server: ISIS/4.0\r\n" "Content-type: application/x-grid\r\n" "Content-length: 261234\r\n\r\n" - end- The grid data is in Binary. How would I get to this? I would imagine that since f (the object) exists the call to the web service worked. Now I need to read the grid...eventually I need to put it into a Numeric array but not sure how to get just the grid from "f". As a simple starting point I tried. >>> import urllib >>> f = urllib.urlopen("http://dsd1u:7003/GRID:U:WW3_GLOBAL:2005041800:global_360x18 1:max_wav_ht:surface:::fcst_ops:0240") >>> f.info() >>> f.readlines() I tried read(), readLines() and some other stuff using scipy and Numeric. The prompt has moved to the next line but nothing else has happened for 30 min or so (I have tried several times). When I try to close IDLE it says the program is still running. How should I be getting this data is it trying to read the binary and that is why it is stalled? Thanks, John Ertl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] how to display an image using python
Danny, Pygame.org...I would not have thought to look there. In my google it did not pop up. I will definitely take a look and thanks for the example. John Ertl -Original Message- From: Danny Yoo [mailto:[EMAIL PROTECTED] Sent: Thursday, April 14, 2005 12:25 To: Ertl, John Cc: Tutor Subject: Re: [Tutor] how to display an image using python > xv on the machines and PIL uses xv to display. I have looked at > PythonMagick but I could not even get past installing it. It does not have > a setup.py and uses boost. I am hoping for a more straightforward Python > way. Hi John, You may want to try PyGame: http://www.pygame.org/ Although it's mainly for game development, it provides a simple graphics API that we can use to display images. If you're running Linux, it's likely that you have the Simple DirectMedia Layer (SDL) library installed. I'm not too familiar with the API, but I was able to get some kind of working example. We can first construct an image surface: http://www.pygame.org/docs/ref/pygame_image.html by loading one from our file: ## >>> import pygame.image >>> picture = pygame.image.load("na-cat.gif") >>> >>> picture.get_size() (256, 48) ## At this point, 'picture' contains a "surface": http://www.pygame.org/docs/ref/Surface.html We can copy ('blit') this surface onto our main screen: ## >>> import pygame.display >>> pygame.display.set_mode(picture.get_size()) >>> main_surface = pygame.display.get_surface() >>> main_surface.blit(picture, (0, 0)) >>> pygame.display.update() ## I hope this helps! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] how to display an image using python
All, I have asked this question before, but one more time most have commented about manipulation but displaying the image has become the big issue. I want to display png and gif images on a Linux machine using python. I am using PyNGL to make the images and PIL to manipulate them but I cannot load xv on the machines and PIL uses xv to display. I have looked at PythonMagick but I could not even get past installing it. It does not have a setup.py and uses boost. I am hoping for a more straightforward Python way. Thanks, John Ertl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] viewing gif png ?
I need to manipulate and view gif and png images. I have PIL installed so I can do the manipulation but PIL does not let me view the images, because I do not have xv. What is the recommended python module for viewing gif and png? Thanks for the recommendation. John Ertl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] re question
All, Thanks. I love this list...great freindly advice. I had taken a slightly longer approach to Kent's "re.findall(r'[\d\.]+', s)" but the simplicity is just too good to pass up. Jacob I too got the warning about encoding and saved with the line added. It still would not strip out the charecter...I may try a bit harder to make it work just becouse it bugs me that you made it work. Thanks again John Ertl -Original Message- From: Kent Johnson Cc: tutor@python.org Sent: 3/27/05 2:31 PM Subject: Re: [Tutor] re question Jacob S. wrote: > Kent -- when pulling out just the numbers, why go to the trouble of > splitting by "," first? Good question. It made sense at the time :-) Here is another way using re.findall(): >>> import re >>> s='Std Lvl: 850hPa, 1503m, 16.8C, 15.7C, 205 @ 11kts' >>> re.findall(r'[\d\.]+', s) ['850', '1503', '16.8', '15.7', '205', '11'] Kent > > import re > pat = re.compile(r"[^\d.]*") > > t = """SigWind: 857hPa, , 21.0C, 20.1C, 210 @ 9kts > SigWind: 850hPa±, , , , 205 @ 11kts > Std Lvl: 850hPa, 1503m, 16.8C, 15.7C, 205 @ 11kts""" > > result = pat.split(t) > print result > > yields > > ['', '857', '21.0', '20.1', '210', '9', '850', '205', '11', '850', > '1503', '16.8', '15.7', '205', '11', ''] > > IDLE pops up with a dialog that says Non-ASCII found, yet no encoding > declared. Add a line like > # -*- coding: cp1252 -*- > to your file > Choose OK to save this file as cp1252 > Edit your general options to silence this warning > > It has buttons: Ok, Edit my file > Edit my file adds the commented line above to the top of the script. > > Could this possibly be causing his problem? > > HTH, > Jacob > >> I don't know why this isn't working for you but this worked for me at >> a DOS console: >> >>> s='850hPa±' >> >>> s >> '850hPa\xf1' >> >>> import re >> >>> re.sub('\xf1', '*', s) >> '850hPa*' >> >>> import sys >> >>> sys.stdout.encoding >> 'cp437' >> >> and also in IDLE with a different encoding: >> >>> s='850hPa±' >> >>> s >> '850hPa\xb1' >> >>> import re >> >>> re.sub('\xb1', '*', s) >> '850hPa*' >> >>> import sys >> >>> sys.stdout.encoding >> 'cp1252' >> >> So one guess is that the data is in a different encoding than what you >> expect? When you print the string and get '\xb1', is that in the same >> program that is doing the regex? >> >> Another approach would be to just pull out the numbers and ignore >> everything else: >> >>> s='Std Lvl: 850hPa, 1503m, 16.8C, 15.7C, 205 @ 11kts' >> >>> l=s.split(',') >> >>> l >> ['Std Lvl: 850hPa', ' 1503m', ' 16.8C', ' 15.7C', ' 205 @ 11kts'] >> >>> [ re.search(r'[\d\.]+', i).group() for i in l] >> ['850', '1503', '16.8', '15.7', '205'] >> >> Kent >> >> Ertl, John wrote: >> >>> All >>> >>> I have a string that has a bunch of numbers with the units attached >>> to them. >>> I want to strip off the units. I am using a regular expression and >>> sub to >>> do this. This works great for almost all of the cases. These are >>> the type of lines: >>> >>> SigWind: 857hPa, , 21.0C, 20.1C, 210 @ 9kts >>> SigWind: 850hPa±, , , , 205 @ 11kts >>> Std Lvl: 850hPa, 1503m, 16.8C, 15.7C, 205 @ 11kts >>> >>> I am using the following cleanstring = re.compile( >>> '(hPa|hPa\xb1|m|C|kts)' >>> ). And then the cleanstring.sub("",line). I have tried using >>> numerous \ to >>> escape the \xb1. >>> >>> I also tried replacing all non numeric characters that are part of a >>> number-character string but I could not make that work. The idea was >>> replace >>> all non-number characters in a "word" that is made up of numbers >>> followed by >>> numbers. >>> >>> I then split the line at the commas so in the current thinking I need >>> the >>> commas for the split. How do I deal with the hPa±? When I print it >>> out it >>> looks like it is a hexadecimal escape character (\xb1) but I am note >>> sure >>> how to deal with this. >>> >>> Any ideas? >>> >>> Thanks >>> ___ >>> Tutor maillist - Tutor@python.org >>> http://mail.python.org/mailman/listinfo/tutor >>> >> >> ___ >> Tutor maillist - Tutor@python.org >> http://mail.python.org/mailman/listinfo/tutor >> > > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] re question
All I have a string that has a bunch of numbers with the units attached to them. I want to strip off the units. I am using a regular expression and sub to do this. This works great for almost all of the cases. These are the type of lines: SigWind: 857hPa, , 21.0C, 20.1C, 210 @ 9kts SigWind: 850hPa±, , , , 205 @ 11kts Std Lvl: 850hPa, 1503m, 16.8C, 15.7C, 205 @ 11kts I am using the following cleanstring = re.compile( '(hPa|hPa\xb1|m|C|kts)' ). And then the cleanstring.sub("",line). I have tried using numerous \ to escape the \xb1. I also tried replacing all non numeric characters that are part of a number-character string but I could not make that work. The idea was replace all non-number characters in a "word" that is made up of numbers followed by numbers. I then split the line at the commas so in the current thinking I need the commas for the split. How do I deal with the hPa±? When I print it out it looks like it is a hexadecimal escape character (\xb1) but I am note sure how to deal with this. Any ideas? Thanks ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Image manipulation
All, I have an image with a bunch of white space that I need to crop. I would like to automate the process using python and I was wondering what is the best module for image manipulation? I have seen PIL and Python magic what is recommended? John Ertl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Python and a web image map
Liam, Thanks for the code chunk and the advice. Java script here I come. John Ertl -Original Message- From: Liam Clarke [mailto:[EMAIL PROTECTED] Sent: Monday, February 28, 2005 13:27 To: Tutor Tutor Subject: Re: [Tutor] Python and a web image map I would say it's best done as a Javascript thing. function goFunc(e){ x = e.clientX y = e.clientY alert("X=" + x + " Y=" + y) } window.onload = function(e){document.onclick = goFunc;}; Javascript or Python? Save the above as an HTM and click, it should give you the x,y co-ords for the browser window excluding scrolbars etc. Shouldn't be too hard to create a window that matches the size of your map, and convert window co-ords into image co-ords. Good luck with that, though, Javascript is a funny beast, but there's some good tutorials out there. Regards, Liam Clarke On Mon, 28 Feb 2005 12:00:09 -0800, Ertl, John <[EMAIL PROTECTED]> wrote: > > > All, > > I have been doing Python for a bit now but I am trying to make a clickable > map of the world on a web page that gives me the latitude and longitude of a > location selected. I have done little with HTML beyond forms and have done > no Java script. Is this a problem Python can solve or is this a HTML / Java > script issue. > > Thanks, > > John Ertl > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python and a web image map
All, I have been doing Python for a bit now but I am trying to make a clickable map of the world on a web page that gives me the latitude and longitude of a location selected. I have done little with HTML beyond forms and have done no Java script. Is this a problem Python can solve or is this a HTML / Java script issue. Thanks, John Ertl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] What is in the traceback object
Danny, That is great...every time I have a problem someone has already solved it...the other problem is finding that solution...Thanks again. John Ertl -Original Message- From: Danny Yoo [mailto:[EMAIL PROTECTED] Sent: Tuesday, February 08, 2005 10:39 To: Ertl, John Cc: 'tutor@python.org' Subject: Re: [Tutor] What is in the traceback object On Tue, 8 Feb 2005, Ertl, John wrote: > I have a bit of code that uses a module and I am trying to get more info > on the error. > > I am using this bit of code: > > try: > rhfill= Ngl.contour(wks,rhisobar,rh_res) > except: > execType,value,tracebak = sys.exc_info()[:3] > print execType > print value > print tracebak > > In the log file I get this: > > exceptions.SystemError > error return without exception set > > > How do I get the actual traceback so I can read it? Hi John, You can use the 'traceback' module: http://www.python.org/doc/lib/module-traceback.html Use the traceback.print_exc() function within the except block: it'll automatically pull information from sys.exc_info() for you: ## try: rhfill= Ngl.contour(wks,rhisobar,rh_res) except: traceback.print_exc() ## Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] What is in the traceback object
I have a bit of code that uses a module and I am trying to get more info on the error. I am using this bit of code: try: rhfill = Ngl.contour(wks,rhisobar,rh_res) except: execType,value,tracebak = sys.exc_info()[:3] print execType print value print tracebak In the log file I get this: exceptions.SystemError error return without exception set How do I get the actual traceback so I can read it? Thanks, John Ertl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Diffing two files.
Thanks, So simple...DAA just do an equivalency on the list...no need to do sets or step through each line. John -Original Message- From: Kent Johnson [mailto:[EMAIL PROTECTED] Sent: Saturday, January 29, 2005 06:05 Cc: Tutor@python.org Subject: Re: [Tutor] Diffing two files. OK, that is clear. diffutils is probably overkill. A simple loop to accumulate the lines of interest should work. Here is some untested (!) code that may do what you want :-) def getCommonPart(filePath): ''' Get a list containing all the lines of a file that fall between the start and end lines. The returned list does not include the actual start and end lines. If start is not found, returns None. If end is not found, returns the lines after start. ''' start = '-Beginning flag\n' end = '-Ending flag\n' common = None # This will be the list of lines, also a flag of whether start has been seen for line in open(filePath): if common is None and line == start: common = [] elif line == end: break else: common.append(line) return common # Now it's easy to compare the two files: lines1 = getCommonPart(file1) lines2 = getCommonPart(file2) if lines1 != lines2: # Do what you need to do in case of a mismatch... # If you want details of the differences then you might want to use difflib here Kent Ertl, John wrote: > Kent > > What I need to do is find what should be common and see if it really is. I > have two output files...The output files will have a bunch of systems stuff > then the text of interest and then a bunch more systems stuff. The systems > stuff may be different for each file but the text of interest will always > have a fixed line in front of it and behind it. > > The idea is to get the text of interest (using the known beginning and > ending flags in the text) from each file and then check to make sure the > text of interest is the same in both files. > > I have not done much text stuff so this is new territory for me. I will > take a look at difflib. > > Thanks again > > John Ertl > > Simplified example of a text files. > > Sldfsdf > Sdfsdfsf > Sdfsdfsdfwefs > Sdcfasdsgerg > Vsadgfasgdbgdfgsdf > -Beginning flag > This > Text > Should be > The > Same in the other file. > -Ending flag > Sdfsdfsdfsd > Sdfsdfsdfasd > Sdfsadfsdf > Sdfsadfasdf > Sdfsdfasd > Sdfasdf > s > > > -Original Message- > From: Kent Johnson [mailto:[EMAIL PROTECTED] > Sent: Friday, January 28, 2005 15:23 > Cc: Tutor@python.org > Subject: Re: [Tutor] Diffing two files. > > You don't really say what you are trying to accomplish. Do you want to > identify the common text, or > find the pieces that differ? > > If the common text is always the same and you know it ahead of time, you can > just search the lines > of each file to find it. > > If you need to identify the common part, difflib might be useful. There is > an example on this page > of finding matching blocks of two sequences: > http://docs.python.org/lib/sequencematcher-examples.html > > In your case the sequences will be lists of lines rather than strings (which > are sequences of > characters) > > Kent > > Ertl, John wrote: > >>All, >> >>I have two text files that should contain a section of text that is the >>same. Luckily the section of text has a defined beginning and end. It >>looks like the most straightforward thing would be to read the targeted > > text > >>from each file (only 50 lines or so) into lists and then compare the > > lists. > >>I would think I could use sets to find a unique list (hopefully there > > would > >>not be anything)...or I could do line by line comparison. Any advise on >>what is the better method. Should I avoid the list comparison > > approach...is > >>there a built in way of comparing entire files instead of dealing > > explicitly > >>with the lines? >> >>Thanks, >> >>John Ertl >>___ >>Tutor maillist - Tutor@python.org >>http://mail.python.org/mailman/listinfo/tutor >> > > > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ 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] Diffing two files.
Kent What I need to do is find what should be common and see if it really is. I have two output files...The output files will have a bunch of systems stuff then the text of interest and then a bunch more systems stuff. The systems stuff may be different for each file but the text of interest will always have a fixed line in front of it and behind it. The idea is to get the text of interest (using the known beginning and ending flags in the text) from each file and then check to make sure the text of interest is the same in both files. I have not done much text stuff so this is new territory for me. I will take a look at difflib. Thanks again John Ertl Simplified example of a text files. Sldfsdf Sdfsdfsf Sdfsdfsdfwefs Sdcfasdsgerg Vsadgfasgdbgdfgsdf -Beginning flag This Text Should be The Same in the other file. -Ending flag Sdfsdfsdfsd Sdfsdfsdfasd Sdfsadfsdf Sdfsadfasdf Sdfsdfasd Sdfasdf s -Original Message- From: Kent Johnson [mailto:[EMAIL PROTECTED] Sent: Friday, January 28, 2005 15:23 Cc: Tutor@python.org Subject: Re: [Tutor] Diffing two files. You don't really say what you are trying to accomplish. Do you want to identify the common text, or find the pieces that differ? If the common text is always the same and you know it ahead of time, you can just search the lines of each file to find it. If you need to identify the common part, difflib might be useful. There is an example on this page of finding matching blocks of two sequences: http://docs.python.org/lib/sequencematcher-examples.html In your case the sequences will be lists of lines rather than strings (which are sequences of characters) Kent Ertl, John wrote: > All, > > I have two text files that should contain a section of text that is the > same. Luckily the section of text has a defined beginning and end. It > looks like the most straightforward thing would be to read the targeted text > from each file (only 50 lines or so) into lists and then compare the lists. > I would think I could use sets to find a unique list (hopefully there would > not be anything)...or I could do line by line comparison. Any advise on > what is the better method. Should I avoid the list comparison approach...is > there a built in way of comparing entire files instead of dealing explicitly > with the lines? > > Thanks, > > John Ertl > ___ > Tutor maillist - Tutor@python.org > http://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Diffing two files.
All, I have two text files that should contain a section of text that is the same. Luckily the section of text has a defined beginning and end. It looks like the most straightforward thing would be to read the targeted text from each file (only 50 lines or so) into lists and then compare the lists. I would think I could use sets to find a unique list (hopefully there would not be anything)...or I could do line by line comparison. Any advise on what is the better method. Should I avoid the list comparison approach...is there a built in way of comparing entire files instead of dealing explicitly with the lines? Thanks, John Ertl ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Popen and sending output to a file
That is too easy. I was looking at the examples of how to replace the old popen and I just did not get it but the page you sent is great. John -Original Message- From: Danny Yoo [mailto:[EMAIL PROTECTED] Sent: Wednesday, January 19, 2005 10:34 To: Ertl, John Cc: tutor@python.org Subject: Re: [Tutor] Popen and sending output to a file On Wed, 19 Jan 2005, Ertl, John wrote: > I am using the subprocess.Popen from 2.4. I can launch a job from > python and the output from the job goes to the screen but now I would > like to have the output go to a file. I could do the crude > > subprocess.Popen("dtg | cat > job.out", shell=True) > > But I would think there is a better way built into Popen but I could not > figure out the documentation. Hi John, According to: http://www.python.org/doc/lib/node227.html we can redirect standard input, output, and error by calling Popen with the 'stdin', 'stdout', or 'stderr' keyword arguments. We should be able to do something like: ### job_output = open("job.out", "w") subprocess.Popen("dtg", shell=True, stdout=job_output) ### Best of wishes to you! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Popen and sending output to a file
I am using the subprocess.Popen from 2.4. I can launch a job from python and the output from the job goes to the screen but now I would like to have the output go to a file. I could do the crude subprocess.Popen("dtg | cat > job.out", shell=True) But I would think there is a better way built into Popen but I could not figure out the documentation. Any help would be appreciated. John ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Popen? or something else
Hugo, That looks like it will work great. Thanks, John -Original Message- From: Hugo González Monteverde [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 22, 2004 12:41 To: [EMAIL PROTECTED] Cc: Ertl, John Subject: Re: [Tutor] Popen? or something else You may use the 'commands' module, if your subprocess should return right away, then you can use: ## import commands mystring = commands.getoutput("dtg") ## then mystring should have "2004122212" (and possibly '\n', but you'll have to check that out, not sure about your command)in it... Hope it helps, it sure is quicker than popen (I only use popen if I need to keep reading the output, for a one-time capture, I personally prefer the "commands" module...) Hugo Ertl, John wrote: > Roger, > > I have been doing it the Pythonic way (that is why I have no idea about how > Popen works) but I need to make sure (the systems guys called me on it) I > use the same dtg as everyone else...it is possible (has not happened yet in > 20 years) it could be set to something else. > ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
[Tutor] Is there a "better" way to do this?
I am trying to do the usual thing of asking for an input and then checking it to see if it is valid. If the entry is not valid then ask again until you get the correct answer. I have come up with this class. I am trying to make a transition from procedural programming to object oriented. Is this a good approach for such a check? It seems to me this is more work then needed. (I can put in a counter also to break out if you try too many times). Please comment if you have the time. class greating: def __init__(self): self.OK = False self.lowValue = 1 self.highValue = 6 def opening(self): print """ Please choose from the following options. 1) - Normal Unit test with static data. 2) - Normal Unit test with missing data. 3) - Integration test with current DTG. 4) - Integration test with missing data. 5) - Clean directory 6) - Exit """ self.choice = raw_input("Choice(1-6) ") def check(self): try: self.choice = int(self.choice) except ValueError: print "Please enter a number from ",self.lowValue," to ",self.highValue pass if self.choice > self.highValue or self.choice < self.lowValue: print "You have entered an invalid entry. Please try again" else: self.OK = True a = greating() while a.OK != True: a.opening() a.check() print a.choice ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] Popen? or something else
Roger, I have been doing it the Pythonic way (that is why I have no idea about how Popen works) but I need to make sure (the systems guys called me on it) I use the same dtg as everyone else...it is possible (has not happened yet in 20 years) it could be set to something else. Is the example you gave using the new 2.4 Popen? It looks like the older popen. I can get the older popen to work but not Popen. Thanks again. John Ertl -Original Message- From: Roger Merchberger [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 22, 2004 10:25 To: [EMAIL PROTECTED] Subject: Re: [Tutor] Popen? or something else Rumor has it that Ertl, John may have mentioned these words: >All, > >I hate to ask this but I have just installed 2.4 and I need to get some info >from a subprocess (I think that is correct term). > >At the Linux command line if I input dtg I get back a string representing a >date time group. How do I do this in Python? I would think Popen but I >just don't see it. It could, but there's also a better (IMHO), 'pythonic' way, something like this: def gettoday(): import time today = time.strftime('%Y%m%d%H',time.localtime(time.time())) return (today) >$ dtg >2004122212 If you wanted to use popen, it would look rather like this: import os dtg_s = os.popen("/path/to/dtg").readlines()[0] But this may use more system resources (spawning child shells & whatnot) than doing everything internally with the time module in Python. HTH, Roger "Merch" Merchberger -- Roger "Merch" Merchberger | A new truth in advertising slogan SysAdmin, Iceberg Computers | for MicroSoft: "We're not the oxy... [EMAIL PROTECTED] | ...in oxymoron!" ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
[Tutor] Popen? or something else
All, I hate to ask this but I have just installed 2.4 and I need to get some info from a subprocess (I think that is correct term). At the Linux command line if I input dtg I get back a string representing a date time group. How do I do this in Python? I would think Popen but I just don't see it. $ dtg 2004122212 Thanks, John Ertl ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] removedirs ?
Jason, I could...That is the exact feature I am trying to replicate, but I would just like to do it in Python if I can (in a simple way). I am writing this code in Python to avoid some funny scripting that I would need to do. To go back to combing shell and Python again would be a bit deflating...but the straight forward path might be the best. Thanks, John Ertl -Original Message- From: Jason Child [mailto:[EMAIL PROTECTED] Sent: Thursday, December 16, 2004 12:36 Cc: [EMAIL PROTECTED] Subject: Re: [Tutor] removedirs ? Ertl, John wrote: >I am trying to remove a directory that has other directories and files in >it. I thought removedirs was supposed to do a recursive remove of files and >directories. > >When I try it I get > > > >>>>os.removedirs("DAF") >>>> >>>> > >Traceback (most recent call last): > File "", line 1, in -toplevel- >os.removedirs("DAF") > File "/home/ertlj/ertljVersion/lib/python2.3/os.py", line 167, in >removedirs >rmdir(name) >OSError: [Errno 17] File exists: 'DAF' > >Thanks, > >John Ertl > > >___ >Tutor maillist - [EMAIL PROTECTED] >http://mail.python.org/mailman/listinfo/tutor > > > it seems to me that if its on a *nix box you could use the shell command rm -rf ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
[Tutor] removedirs ?
I am trying to remove a directory that has other directories and files in it. I thought removedirs was supposed to do a recursive remove of files and directories. When I try it I get >>> os.removedirs("DAF") Traceback (most recent call last): File "", line 1, in -toplevel- os.removedirs("DAF") File "/home/ertlj/ertljVersion/lib/python2.3/os.py", line 167, in removedirs rmdir(name) OSError: [Errno 17] File exists: 'DAF' Thanks, John Ertl ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] turning a number into a formated string
Very elegant, It makes me feel like "I should have thought of that". Thanks for the help and the reminder to think of the simplistic approach. John -Original Message- From: Tim Peters [mailto:[EMAIL PROTECTED] Sent: Wednesday, December 15, 2004 12:15 To: Ertl, John Cc: tutor-list (Python) Subject: Re: [Tutor] turning a number into a formated string [Ertl, John] > I need to take a number and turn it into a formatted string. > The final output needs to look like when the X is the > integer part padded on the left and Y is the decimal part padded > on the right. > I figured I could split the number at "." and then use zfill or > something like this (LEVEL1 = "%04d" % LEVEL1) for the > part but I am not sure how to right pad the decimal part of the > number. > Example. > 1 and 1.0 needs to look like 0001 ( I figured I would have to > check the length of the list made from the split to see if a decimal > portion existed) > 1.1 needs to look like 00011000 > 22.33 needs to look like 00223330 Really? The input has two digits 3, but the output has three digits 3. I'll assume you meant 00223300 instead. > .22 needs to look like 2200 > Any ideas on the right padding the decimal side using "0" I expect that a "%09.4f" format does everything you asked for, except that it contains a period. So let's try to repair that: >>> def johnpad(n): ... return ("%09.4f" % n).replace('.', '') Then: >>> johnpad(1) '0001' >>> johnpad(1.0) '0001' >>> johnpad(1.1) '00011000' >>> johnpad(22.33) '00223300' >>> johnpad(.22) '2200' >>> ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
[Tutor] turning a number into a formated string
I need to take a number and turn it into a formatted string. The final output needs to look like when the X is the integer part padded on the left and Y is the decimal part padded on the right. I figured I could split the number at "." and then use zfill or something like this (LEVEL1 = "%04d" % LEVEL1) for the part but I am not sure how to right pad the decimal part of the number. Example. 1 and 1.0 needs to look like 0001 ( I figured I would have to check the length of the list made from the split to see if a decimal portion existed) 1.1 needs to look like 00011000 22.33 needs to look like 00223330 .22 needs to look like 2200 Any ideas on the right padding the decimal side using "0" Thanks, John ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
RE: [Tutor] maximum value in a Numeric array
All, Thanks for the help...I am using the older Numeric 23.4. I have some stuff that cannot use Numarray yet. Numeric does not seam to have the same functionality. Happy Holidays. John Ertl -Original Message- From: Danny Yoo [mailto:[EMAIL PROTECTED] Sent: Friday, December 10, 2004 11:26 To: Ertl, John Cc: [EMAIL PROTECTED] Subject: Re: [Tutor] maximum value in a Numeric array On Fri, 10 Dec 2004, Ertl, John wrote: > I am trying to get the maximum value in a 2-D array. I can use max but > it returns the 1-D array that the max value is in and I then I need to > do max again on that array to get the single max value. > > There has to be a more straightforward way...I have just not found it. > > >>> b = array([[1,2],[3,4]]) > >>> max(b) > array([3, 4]) > >>> c = max(b) > >>> max(c) > 4 Hi John, According to: http://stsdas.stsci.edu/numarray/numarray-1.1.html/node35.html#l2h-108 you can use the 'max()' method of an array: ### >>> import numarray >>> b = numarray.array([[1,2],[3,4]]) >>> b.max() 4 ### Hope this helps! ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor
[Tutor] maximum value in a Numeric array
All, I am trying to get the maximum value in a 2-D array. I can use max but it returns the 1-D array that the max value is in and I then I need to do max again on that array to get the single max value. There has to be a more straightforward way...I have just not found it. >>> b = array([[1,2],[3,4]]) >>> max(b) array([3, 4]) >>> c = max(b) >>> max(c) 4 >>> I could also flatten the array to 1 D first then do max but the array I am going to be working with is fairly large. Thanks ___ Tutor maillist - [EMAIL PROTECTED] http://mail.python.org/mailman/listinfo/tutor