Re: [Tutor] a question about symbol
On 5/28/06, Bob Gailer <[EMAIL PROTECTED]> wrote: > > linda.s wrote: > When I test the following code, > I got something like (use 80 as argument): > 80?F=27?C > Why '?' appear? > > # code > import string, sys > > # If no arguments were given, print a helpful message > if len(sys.argv)==1: > print 'Usage: celsius temp1 temp2 ...' > sys.exit(0) > > # Loop over the arguments > for i in sys.argv[1:]: > try: > fahrenheit=float(string.atoi(i)) > except string.atoi_error: > print repr(i), "not a numeric value" > else: > celsius=(fahrenheit-32)*5.0/9.0 > print '%i\260F = %i\260C' % (int(fahrenheit), int(celsius+.5)) > > On my computer I get the desired result. I paste it here 80°F = 27°C and I > see degree symbols. > > What operating system / terminal hardware are you using? > -- > Bob Gailer > 510-978-4454 mac and terminal. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] a question about symbol
linda.s wrote: > On 5/28/06, Bob Gailer <[EMAIL PROTECTED]> wrote: >> linda.s wrote: >> When I test the following code, >> I got something like (use 80 as argument): >> 80?F=27?C >> Why '?' appear? >> >> # code >> print '%i\260F = %i\260C' % (int(fahrenheit), int(celsius+.5)) >> >> On my computer I get the desired result. I paste it here 80°F = 27°C and I >> see degree symbols. >> >> What operating system / terminal hardware are you using? >> -- >> Bob Gailer >> 510-978-4454 > > mac and terminal. \260 represesents the character with octal value 260, hex B0. In Latin-1 and Unicode this is a degree sign. My guess is that your terminal is set to display UTF-8 characters rather than latin-1; in UTF-8 B0 by itself is an error. It's also possible it is set to MacRoman but in that case I think you would see an infinity sign instead of a question mark. From the Python interpreter prompt, type import sys sys.stdout.encoding to see what encoding your terminal is set to. If it is UTF-8, change the \260 to \xc2\xb0 which is the correct sequence for a degree sign in UTF-8. If it is MacRoman, change the \260 to \xa1 Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] implementing config files
Tracy R Reed wrote: > I need to implement a config file which will provide hostnames, the > names of checks to run on those hosts, and the options to those checks. > These things are sets which are nested inside each other which we can > think of like nested objects. I could make a dictionary containing the > host names each of which is a dictionary containing the services to be > checked etc. > > But rather than just make dictionaries of dictionaries (which could get > confusing) I was wondering if I could make it more robust by somehow > defining objects nested inside of other objects in the config file with > certain attributes. For example I would like to be able to have a > check_tcp object which would have two attributes: hostname and port. If > you try to assign it anything else you get an error. If port isn't a > number between 0 and 2^16 you get an error. One possibility is to make the config file be an actual Python module. Then it can instantiate objects directly. The class constructors can do as much checking as you like. Your config file might look like this: import testdefs host1 = testdefs.Host('192.168.0.53', '80') host2 = testdefs.Host('192.168.0.55', '27') tests = [ testdefs.check_tcp(host1), testdefs.check_http(host1), testdefs.check_tcp(host2), testdefs.check_ftp(host2), ] testdefs.Host is a class which validates and stores the hostname and port, raising an exception if there is a problem. testdefs.check_tcp() etc are classes that validate and remember their init parameters and have an execute() method to actually do the test. So just by importing the above module you validate all the parameters. To run the tests, use something like for test in tests: test.execute() > Basically I don't want > errors in the config file to propagate all the way to the client machine > on which the code is going to be executed and have wrong arguments > passed to the program which we os.popen() and read the results from. You obviously have to import the file to get the error checking, but you could do that on a test machine before you distribute it. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] implementing config files
Carlos: Where does one find the objxml module? I have looked on python.org and Google and can't find it. Thanks in advance, Barry [EMAIL PROTECTED] 541-302-1107 We who cut mere stones must always be envisioning cathedrals. -Quarry worker's creed > -Original Message- > Date: Thu, 1 Jun 2006 23:36:27 -0700 > From: "Carlos Daniel Ruvalcaba Valenzuela" <[EMAIL PROTECTED]> > Subject: Re: [Tutor] implementing config files > To: "Tracy R Reed" <[EMAIL PROTECTED]> > Cc: tutor@python.org > Message-ID: > <[EMAIL PROTECTED]> > Content-Type: text/plain; charset="iso-8859-1" > > Your could try to use XML files to store configuration files, I > already coded something like that, using expat parser and loading the > XML contents to objects and attributes, this is a sample code of how > works my module: > > Lets supouse we have this file config.xml with the following contents: > > > > MySQL > localhost > 21 > username > > > > > and in our code: > > from objxml import * > > fd = file('config.xml', 'r') > p = XMLParser(fd) > > root = p.Root > port = str(root.connection.port) > user = str(root.connection.username) > > All nodes are objects, converting them to strings gets you the > content, you can also access the atributes as normal object > attributes. > > This code can be useful for what you want, take a look. > > Regards > Carlos Daniel Ruvalcaba Valenzuela > > > On 6/1/06, Tracy R Reed <[EMAIL PROTECTED]> wrote: > > Hello all! > > > > I am writing some code to implement a bunch of passive checks for the > > nagios network monitoring system. <> > > > > -- > > Tracy R Reed http://ultraviolet.org > > A: Because we read from top to bottom, left to right > > Q: Why should I start my reply below the quoted text ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Trouble with os.path.isfile
Greetings: One of the functions of my test system web server program is to allow the user to view the contents of the image file directories and select files to load onto the hardware. Obviously, a central part of this function is to identify and display the names of directories and files in a specified directory. I haven’t been able to find any canned code or cookbook entries that do this, so I’m making one from scratch. Here is the helper function that gets and returns the directory and file names: >> def lstdirsnfiles(tdir): flst = os.listdir(tdir) flst.sort() fnames = [] dnames = ['.', '..'] for name in flst: absfn = os.path.join(tdir, name) if os.path.isdir(absfn): dnames.append(name) elif os.path.isfile(absfn): fnames.append(name) return dnames, fnames >> The root of the image file directory tree is “/home/iip/images”. When lstdirsnfiles is run with tdir set appropriately, the lists returned contain no additional information: dnames = ['.', '..'] and fnames = []. I added some tracing statements to see what was happening inside the function: >> def lstdirsnfiles(tdir): global DEBUG, dbgfname DEBUG = True flst = os.listdir(tdir) flst.sort() fnames = [] dnames = ['.', '..'] if DEBUG: dbgf = open(dbgfname,mode="a") dbgf.write("\n"+str (time.localtime())+"\n") dbgf.write("Entering lstdirsnfiles\n") dbgf.write("The directory = %s\n" % tdir) dbgf.write("The file list = %s\n" % flst) for name in flst: absfn = os.path.join(tdir, name) if DEBUG: dbgf.write("File path = %s, isfile = %s\n" % (absfn, os.path.isfile(absfn))) if os.path.isdir(absfn): dnames.append(name) elif os.path.isfile(absfn): fnames.append(name) if DEBUG: dbgf.write("dnames = %s\n" % dnames) dbgf.write("fnames = %s\n" % fnames) dbgf.close() DEBUG = False return dnames, fnames >> The log vile received the following: >> (2006, 6, 2, 15, 23, 4, 4, 153, 1) Entering lstdirsnfiles The directory = /home/iip/images The file list = ['test1.bmp', 'test2.bmp', 'test3.bmp', 'test4.bmp', 'test5.bmp', 'test6.bmp'] File path = /home/iip/images/test1.bmp, isfile = False File path = /home/iip/images/test2.bmp, isfile = False File path = /home/iip/images/test3.bmp, isfile = False File path = /home/iip/images/test4.bmp, isfile = False File path = /home/iip/images/test5.bmp, isfile = False File path = /home/iip/images/test6.bmp, isfile = False dnames = ['.', '..'] fnames = [] >> So there are entries in the directory, but the function doesn’t recognize them as files. Finally, I opened the interpreter and tried doing this, as nearly as possible, interactively. I got this: >> >>> import os >>> tdir = '/home/iip/images' >>> os.listdir(tdir) ['test4.bmp', 'test3.bmp', 'test2.bmp', 'test1.bmp', 'test5.bmp', 'test6.bmp'] >>> flst = os.listdir(tdir) >>> flst.sort() >>> flst ['test1.bmp', 'test2.bmp', 'test3.bmp', 'test4.bmp', 'test5.bmp', 'test6.bmp'] >>> for name in flst: ... absfn = os.path.join(tdir,name) ... print absfn, "is", ... if not os.path.isfile(absfn): ... print "NOT", ... print "a file" ... /home/iip/images/test1.bmp is a file /home/iip/images/test2.bmp is a file /home/iip/images/test3.bmp is a file /home/iip/images/test4.bmp is a file /home/iip/images/test5.bmp is a file /home/iip/images/test6.bmp is a file > As you can see, the interpreter correctly identifies the strings in the list as names of files, but my function does not. I can’t see anything wrong with the logic in the function. Can any or you see the problem I’m missing? As always, thanks in advance for your help. Barry [EMAIL PROTECTED] 541-302-1107 We who cut mere stones must always be envisioning cathedrals. —Quarry worker's creed ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Trouble with os.path.isfile
From: Carlos Daniel Ruvalcaba Valenzuela <[EMAIL PROTECTED]> Date: Jun 2, 2006 11:10 PM Subject: Re: [Tutor] Trouble with os.path.isfile To: "Carroll, Barry" <[EMAIL PROTECTED]> Here is the problem IMO: I did some modifications to your code (very minimal on ifs) and it worked: import os def lstdirsnfiles(tdir): flst = os.listdir(tdir) flst.sort() fnames = [] dnames = ['.', '..'] for name in flst: absfn = os.path.join(tdir, name) if os.path.isdir(absfn): dnames.append(name) if os.path.isfile(absfn): fnames.append(name) return (dnames, fnames) (dirs, files) = lstdirsnfiles('/home/clsdaniel') It returns correctly all directories and files. Regards Carlos Daniel Ruvalcaba Valenzuela On 6/2/06, Carroll, Barry <[EMAIL PROTECTED]> wrote: > > > > > Greetings: > > > > One of the functions of my test system web server program is to allow the > user to view the contents of the image file directories and select files to > load onto the hardware. Obviously, a central part of this function is to > identify and display the names of directories and files in a specified > directory. I haven't been able to find any canned code or cookbook entries > that do this, so I'm making one from scratch. Here is the helper function > that gets and returns the directory and file names: > > > > >> > > def lstdirsnfiles(tdir): > > flst = os.listdir(tdir) > > flst.sort() > > fnames = [] > > dnames = ['.', '..'] > > for name in flst: > > absfn = os.path.join(tdir, name) > > if os.path.isdir(absfn): > > dnames.append(name) > > elif os.path.isfile(absfn): > > fnames.append(name) > > return dnames, fnames > > >> > > > > The root of the image file directory tree is "/home/iip/images". When > lstdirsnfiles is run with tdir set appropriately, the lists returned > contain no additional information: dnames = ['.', '..'] and fnames = []. I > added some tracing statements to see what was happening inside the > function: > > > > >> > > def lstdirsnfiles(tdir): > > global DEBUG, dbgfname > > DEBUG = True > > > > flst = os.listdir(tdir) > > flst.sort() > > fnames = [] > > dnames = ['.', '..'] > > > > if DEBUG: > > dbgf = open(dbgfname,mode="a") > > dbgf.write("\n"+str (time.localtime())+"\n") > > dbgf.write("Entering lstdirsnfiles\n") > > dbgf.write("The directory = %s\n" % tdir) > > dbgf.write("The file list = %s\n" % flst) > > > > for name in flst: > > absfn = os.path.join(tdir, name) > > > > if DEBUG: > > dbgf.write("File path = %s, isfile = %s\n" % > >(absfn, os.path.isfile(absfn))) > > > > if os.path.isdir(absfn): > > dnames.append(name) > > elif os.path.isfile(absfn): > > fnames.append(name) > > > > if DEBUG: > > dbgf.write("dnames = %s\n" % dnames) > > dbgf.write("fnames = %s\n" % fnames) > > dbgf.close() > > DEBUG = False > > > > return dnames, fnames > > >> > > > > The log vile received the following: > > >> > > (2006, 6, 2, 15, 23, 4, 4, 153, 1) > > Entering lstdirsnfiles > > The directory = /home/iip/images > > The file list = ['test1.bmp', 'test2.bmp', 'test3.bmp', 'test4.bmp', > 'test5.bmp', 'test6.bmp'] > > File path = /home/iip/images/test1.bmp, isfile = False > > File path = /home/iip/images/test2.bmp, isfile = False > > File path = /home/iip/images/test3.bmp, isfile = False > > File path = /home/iip/images/test4.bmp, isfile = False > > File path = /home/iip/images/test5.bmp, isfile = False > > File path = /home/iip/images/test6.bmp, isfile = False > > dnames = ['.', '..'] > > fnames = [] > > >> > > > > So there are entries in the directory, but the function doesn't recognize > them as files. > > > > Finally, I opened the interpreter and tried doing this, as nearly as > possible, interactively. I got this: > > > > >> > > >>> import os > > >>> tdir = '/home/iip/images' > > >>> os.listdir(tdir) > > ['test4.bmp', 'test3.bmp', 'test2.bmp', 'test1.bmp', 'test5.bmp', > 'test6.bmp'] > > >>> flst = os.listdir(tdir) > > >>> flst.sort() > > >>> flst > > ['test1.bmp', 'test2.bmp', 'test3.bmp', 'test4.bmp', 'test5.bmp', > 'test6.bmp'] > > >>> for name in flst: > > ... absfn = os.path.join(tdir,name) > > ... print absfn, "is", > > ... if not os.path.isfile(absfn): > > ... print "NOT", > > ... print "a file" > > ... > > /home/iip/images/test1.bmp is a file > > /home/iip/images/test2.bmp is a file > > /home/iip/images/test3.bmp is a file > > /home/iip/images/test4.bmp is a file > > /home/iip/images/test5.bmp is a file > > /home/iip/images/test6.bmp is a file > > > > > > >