python and getopt and spaces in option
Hello, I have been trying to find an example of how to deal with options that have spaces in them. I am using jython, which is the same I think as python 2.2.3. I feebly tried to use optparse and argparse with no success (got gettext, locale, and optparse). The code is as follows: try: (opts, args) = getopt.getopt(sys.argv[1:], "hs:p:n:j:d:l:u:c:t:w:q:v", ["help", "server=", "port=", "dsName=","jndiName=","driverName=","driverURL=","user=","passWD=","targetServer=","whereProp=","testTableName=","version"]) except getopt.GetoptError: usage() for opt in opts: (key, value) = opt if (key in ("-v", "--version")): print "Version: " + version sys.exit(1) if (key in ("-h", "--help")): usage() if (key in ("-s", "--server")): server = value if (key in ("-p", "--port")): port = value if (key in ("-n", "--dsName")): dsName = value if (key in ("-j", "--jndiName")): jndiName = value if (key in ("-d", "--driverName")): driverName = value if (key in ("-l", "--driverURL")): driverURL = value if (key in ("-u", "--user")): user = value if (key in ("-c", "--passWD")): passWD = value if (key in ("-t", "--targetServer")): targetServer = value if (key in ("-q", "--testTableName")): testTableName = value if (key in ("-w", "--whereProp")): whereProp = value print "server: " + server print "port: " + port print "dsName: " + dsName print "jndiName: " + jndiName print "driverName: " + driverName print "driverURL: " + driverURL print "user: " + user print "passWD: " + passWD print "testtable: " + testTableName print "targetServer: " + targetServer print "whereProp: " + whereProp The one that gives me trouble is with the -q option, because it can look like: -q "SQL 1 TABLE". It returns back just SQL. How do I get it to return the whole thing that is in double quotes? David -- http://mail.python.org/mailman/listinfo/python-list
getop or optparse with option with spaces?
Hello, I have been trying to find an example of how to deal with options that have spaces in them. I am using jython, which is the same I think as python 2.2.3. I feebly tried to use optparse and argparse with no success (got gettext, locale, and optparse). The code is as follows: try: (opts, args) = getopt.getopt(sys.argv[1:], "hs:p:n:j:d:l:u:c:t:w:q:v", ["help", "server=", "port=", "dsName=","jndiName=","driverName=","driverURL=","user=","passWD=","targetServer=","whereProp=","testTableName=","version"]) except getopt.GetoptError: usage() for opt in opts: (key, value) = opt if (key in ("-v", "--version")): print "Version: " + version sys.exit(1) if (key in ("-h", "--help")): usage() if (key in ("-s", "--server")): server = value if (key in ("-p", "--port")): port = value if (key in ("-n", "--dsName")): dsName = value if (key in ("-j", "--jndiName")): jndiName = value if (key in ("-d", "--driverName")): driverName = value if (key in ("-l", "--driverURL")): driverURL = value if (key in ("-u", "--user")): user = value if (key in ("-c", "--passWD")): passWD = value if (key in ("-t", "--targetServer")): targetServer = value if (key in ("-q", "--testTableName")): testTableName = value if (key in ("-w", "--whereProp")): whereProp = value print "server: " + server print "port: " + port print "dsName: " + dsName print "jndiName: " + jndiName print "driverName: " + driverName print "driverURL: " + driverURL print "user: " + user print "passWD: " + passWD print "testtable: " + testTableName print "targetServer: " + targetServer print "whereProp: " + whereProp The one that gives me trouble is with the -q option, because it can look like: -q "SQL 1 TABLE". It returns back just SQL. How do I get it to return the whole thing that is in double quotes? Another problem is that whereProp value is just not seen. Is there a limit to the size for argv? If I use optparse instead of getopt, I see that SQL 1 TABLE goes into args instead of values by the way. A temporary workaround is to use " ".join(args) and assign that to testTableName, but I worry about what will happen if testTableName is blank or has something with no spaces. Also, it just seem weird I have to do a work around like that. I could have swore using double quotes should have fixed this issue, but they do not seem to work. David -- http://mail.python.org/mailman/listinfo/python-list
RE: getop or optparse with option with spaces?
Unfortunately, I had no luck installing argparse, which is really confusing because I would need to use some old version pre-optik to use I think. The Jython I use is like python 2.2.3. I spent all of yesterday trying to get either getopt, argparse, or optparse to work. Even with optparse I had to modify a module. For example, in textwrap.py, they have @ line 124 in the module: if self.replace_whitespace: #if isinstance(text,str): # text = text.translate(self.whitespace_trans) #elif isinstances(text,Unicode): text = text.translate(self.unicode_whitespace_trans) return text I had to comment out the if isinstance(text,str) and elif. Is the double quotes supposed to work? -Original Message- From: Javier Collado [mailto:javier.coll...@gmail.com] Sent: Wednesday, June 10, 2009 9:38 AM To: David Shapiro Cc: python-list@python.org Subject: Re: getop or optparse with option with spaces? Hello, It's strange behaviour. Have you tried argparse (http://code.google.com/p/argparse/)? I've been using it for long time without any problem like that? Best regards, Javier 2009/6/10 David Shapiro : > Hello, > > I have been trying to find an example of how to deal with options that have > spaces in them. I am using jython, which is the same I think as python > 2.2.3. I feebly tried to use optparse and argparse with no success (got > gettext, locale, and optparse). The code is as follows: > > try: > (opts, args) = getopt.getopt(sys.argv[1:], "hs:p:n:j:d:l:u:c:t:w:q:v", > ["help", "server=", "port=", > "dsName=","jndiName=","driverName=","driverURL=","user=","passWD=","targetServer=","whereProp=","testTableName=","version"]) > except getopt.GetoptError: > usage() > > for opt in opts: > (key, value) = opt > if (key in ("-v", "--version")): > print "Version: " + version > sys.exit(1) > if (key in ("-h", "--help")): > usage() > if (key in ("-s", "--server")): > server = value > if (key in ("-p", "--port")): > port = value > if (key in ("-n", "--dsName")): > dsName = value > if (key in ("-j", "--jndiName")): > jndiName = value > if (key in ("-d", "--driverName")): > driverName = value > if (key in ("-l", "--driverURL")): > driverURL = value > if (key in ("-u", "--user")): > user = value > if (key in ("-c", "--passWD")): > passWD = value > if (key in ("-t", "--targetServer")): > targetServer = value > if (key in ("-q", "--testTableName")): > testTableName = value > if (key in ("-w", "--whereProp")): > whereProp = value > > > print "server: " + server > print "port: " + port > print "dsName: " + dsName > print "jndiName: " + jndiName > print "driverName: " + driverName > print "driverURL: " + driverURL > print "user: " + user > print "passWD: " + passWD > print "testtable: " + testTableName > print "targetServer: " + targetServer > print "whereProp: " + whereProp > > The one that gives me trouble is with the -q option, because it can look > like: -q "SQL 1 TABLE". It returns back just SQL. How do I get it to return > the whole thing that is in double quotes? Another problem is that whereProp > value is just not seen. Is there a limit to the size for argv? > > If I use optparse instead of getopt, I see that SQL 1 TABLE goes into args > instead of values by the way. A temporary workaround is to use " > ".join(args) and assign that to testTableName, but I worry about what will > happen if testTableName is blank or has something with no spaces. Also, it > just seem weird I have to do a work around like that. I could have swore > using double quotes should have fixed this issue, but they do not seem to > work. > > David > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list
RE: xml application advice
How about using web server (tomcat jsp) and then java for the xml part, which would allow you to build a nice gui for you. You can use python for backend work. If you can combine some of the levels of your xml it will be easier to traverse. I am not sure this will work for you, but I put as an example: - - - - - -Original Message- From: python-list-bounces+david.shapiro=sas@python.org [mailto:python-list-bounces+david.shapiro=sas@python.org] On Behalf Of William Purcell Sent: Wednesday, June 10, 2009 9:58 AM To: python-list@python.org Subject: xml application advice I am writing a application to calculate pressure drop for a piping network. Namely a building sprinkler system. This will be a command line program at first with the system described in xml (at least that is how I think I want to do it). An important part of this calculation is finding the 'hydraulically most remote' sprinkler. This is something that I could specify with an attribute for now and later think about how to automate it. I need to walk through the dom tree until I find a node of type "sprinkler" that has an attribute of hydraulically_most_remote with a value of True. After I find this I need to break the itterator/for loop and then start walking backwards keeping a running total of the pressure drop until I reach a node that has multiple pipesections and then walk to the end of each branch and calculate the pressure drop, and then add them to the branch that contained the hydraulically most remote sprinkler, and then move on, repeating this until I walk all the way back to the inflow node. I am having trouble finding a decent python/xml resource on the web. I have ordered Python & XML by Jones and Drake, but I am anxious to get something started. The only decent online resource that I can seem to find is http://pyxml.sourceforge.net/topics/howto/xml-howto.html which doesn't seem to be a very comprehensive how-to. Do demonstrate just about everything I know about xml and python I attached t.py and ex.xml. Another thing that is confusing is dir(walker) does not show walker having an attribute currentNode and dir(walker.currentNode) does not show walker.currentNode having an attribute tagName. Bill -- http://mail.python.org/mailman/listinfo/python-list
Re: Connection tester
Not al pages suppost GET. If a page pings and returns does not mean it can be logged into and work (maybe database down). Have you seen soapui? - Original Message - From: python-list-bounces+david.shapiro=sas@python.org To: python-list@python.org Sent: Wed Jun 10 10:26:22 2009 Subject: Connection tester Hey! I am developing a small application that tests multiple websites and compares their "response time". Some of these sites do not respond to a ping and, for the measurement to be standardized, all sites must have the same action preformed upon them. Another problem is that not all of the sites have the same page size and I am not interested in how long it takes to load a page but instead just how long it takes for the website to respond. Finally, I am looking to keep this script platform independent, if at all possible. Here is the code: try: # Get the starting time origTime = time.time() # Create the socket connection and then close s = socket.socket(AF_INET, SOCK_STREAM) s.connect((targetIP, port)) s.send("GET / HTTP/1.0\r\n\r\n") result = s.recv(1024) s.shutdown(SHUT_RDWR) except: result = "" # Check for problems and report back the time if result == "": return Result((time.time() - origTime) * 1000, True) else: return Result((time.time() - origTime) * 1000, False) Result is just an object that holds the time it took for the method to finish and if there were any errors. What I am worried about is that the socket is potentially closed before the website can finish sending in all the data. Does anyone have any suggestions or is the script fine as it is? -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
RE: How to escape # hash character in regex match strings
Maybe a using a Unicode equiv of # would do the trick. -Original Message- From: python-list-bounces+david.shapiro=sas@python.org [mailto:python-list-bounces+david.shapiro=sas@python.org] On Behalf Of Peter Otten Sent: Wednesday, June 10, 2009 11:32 AM To: python-list@python.org Subject: Re: How to escape # hash character in regex match strings 504cr...@gmail.com wrote: > I've encountered a problem with my RegEx learning curve -- how to > escape hash characters # in strings being matched, e.g.: > string = re.escape('123#abc456') match = re.match('\d+', string) print match > > <_sre.SRE_Match object at 0x00A6A800> print match.group() > > 123 > > The correct result should be: > > 123456 >>> "".join(re.findall("\d+", "123#abc456")) '123456' > I've tried to escape the hash symbol in the match string without > result. > > Any ideas? Is the answer something I overlooked in my lurching Python > schooling? re.escape() is used to build the regex from a string that may contain characters that have a special meaning in regular expressions but that you want to treat as literals. You can for example search for r"C:\dir" with >>> re.compile(re.escape(r"C:\dir")).findall(r"C:\dir C:7ir") ['C:\\dir'] Without escaping you'd get >>> re.compile(r"C:\dir").findall(r"C:\dir C:7ir") ['C:7ir'] Peter -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list