Parsing XML: SAX, DOM, Expat, or Something Else?
Hello All, I've been charged with developing an XML configuration file format, for one of the applications that my company develops. (Yes, I know it would have been easier to just use the configuration file format as described in RFC 822) While I am finally comfortable with the XML description that we've agreed upon; I am still trying to determine the best XML parser API to use. I would love to use XPATH, however I face the following restriction: The Python scripts that perform the parsing of the XML configuration file must be compatible with Python Versions 2.2-present. This means that any functionality that I use needs to be compatible with Python 2.2. I started with the DOM API in xml.dom.minidom and I thought that it might be the best way to go, but then I ran across the Recipes of Wai Yip Tung http://code.activestate.com/recipes/534109/ and John Bair, Christoph Dietze from the second edition of the Python cookbook. Wai's implementation uses thes SAX parser and John and Christoph's implementation uses Expat API. In the end what I really want is to transform the XML into an object that looks like config.component.setting or a map config[component][setting]. Another restriction is that I don't want to have to ship additional modules. Does anyone have any advice, comments, or HELP??? Aquil H. Abdullah Developer -- http://mail.python.org/mailman/listinfo/python-list
Logging in Python
Hello All, I have an application where logging may need to be configured in multiple places. I've used the Python Logging Framework for sometime, but I'm still not sure how to test if logging has configured. For example, I have modules A, B, and C. Below is some pseudo code... moduleA class A(object): def __init__(self): ... startLogging(config): # Configure logging # global logger ... moduleB import moduleA from myconfig import MyConfig class B(object): def __init__(self): # self.config = MyConfig() # if logging has started [HOW DO YOU DO THIS?] # self.logger = logging.getLogger("moduleB") # else # self.logger = moduleA.startLogging(self.config) # moduleA.startLogging ... Where I need help is determining if a logger has already been configured. Any advice? Aquil -- http://mail.python.org/mailman/listinfo/python-list
Re: Logging in Python
Thanks for your suggestions. I've also figured that I can test if logging.RootLogger.manager.loggerDict has any items in it. Or if it has a logger for the module that I wish to start. I like basicLogger idea though as it seems like the cleanest implementation. On Feb 10, 3:21 pm, Vinay Sajip wrote: > On Feb 10, 5:50 pm, aha wrote: > > > > > Hello All, > > > I have an application whereloggingmay need to be configured in > > multiple places. I've used the PythonLoggingFramework for sometime, > > but I'm still not sure how to test iflogginghas configured. For > > example, I have modules A, B, and C. > > > Below is some pseudo code... > > moduleA > > > class A(object): > > def __init__(self): > > ... > > > startLogging(config): > > # Configurelogging > > # global logger > > ... > > > moduleB > > import moduleA > > from myconfig import MyConfig > > class B(object): > > def __init__(self): > > # self.config = MyConfig() > > # iflogginghas started [HOW DO YOU DO THIS?] > > # self.logger =logging.getLogger("moduleB") > > # else > > # self.logger = moduleA.startLogging(self.config) > > # moduleA.startLogging > > ... > > > Where I need help is determining if a logger has already been > > configured. Any advice? > > > Aquil > > It depends upon how complicated your logging requirements are. For > example, each module can have the following code in it: > > import logging > > logging.basicConfig(level=logging.DEBUG, filename="/tmp/myapp.log", > filemode="w") # An example > > logger = logging.getLogger(__name__) > > ... your code, involving logger.debug(...) statements > > basicConfig() attaches a FileLogger to the root logger, so all logging > output would be routed to the file "/tmp/myapp.log" in the example. > However, basicConfig() does nothing if the root logger already has > handlers, so calling it in each module shouldn't cause problems. It's > also nice to use the module name (__name__) as the logger name. > > Another pattern is to configure logging in your main module, if there > is one, and then the other modules just assume logging is configured > and log away. If there isn't a main module, have all the modules > import a common module which, when imported, configures logging how > you want it. Under normal circumstances, the import code will only run > once, so your logging only gets configured the first time the module > gets imported by any of the others. > > Regards, > > Vinay Sajip -- http://mail.python.org/mailman/listinfo/python-list
A tale of two execs
Hello All, I am working on a project where I need to support versions of Python as old as 2.3. Previously, we distributed Python with our product, but this seemed a bit silly so we are no longer doing this. The problem that I am faced with is that we have Python scripts that use the subprocess module, and subprocess is not available in Python 2.3. Below is the strategy I am thinking about using, however if, you have better ideas please let me know. def runner(cmd, stdin, stdout, ...): try: import subprocess sbm = 1 except: sbm = 0 # Now do something if sbm: process = subporcess(...) else: import popen2 process = popen2.Popen4(...) Has anyone else run into a situation similar to this one? -- http://mail.python.org/mailman/listinfo/python-list
Re: A tale of two execs
Hello All, So below is my naive attempt at the wrapper, it is only half baked since I am no master at Interprocess communication... I know that it is lacking a lot of things comment greatly appreciated: #!/usr/bin/env python import os, sys, re, exceptions try: import subprocess except ImportError: import popen2 subprocess = None PIPE = 10 SOUT = 11 class Runner: def __init__(self): self.stdout = None self.stdin = None self.stderr = None self.pid = None self.process = None def runCmdLine(self, args, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False,cwd=None, env=None): """ args -- a string, or a sequence of program arguments. The argument to execute is normally the first item in the args sequence or the string if a string is given, but can be explicitly set by using the executable argument. executable -- string to be executed stdin, stdout, stderr -- value of standard input, standard output, and standard error file handles. None implies no redirection. Valid values are PIPE, an existing file descriptor (a positive integer), an existing file object, and None. shell -- specifies whether command should be executed through shell cwd -- specifies whether, childs current directory will be changed to cwd env -- A map that specifies the environment variables in child process that will be overwritten NOTE: Depending on the modules available, some arguments may not be used. """ if subprocess: # Use subprocess to exec functions if stdin == PIPE: sin = subprocess.PIPE else: # NOTE: User may input invalid values that will cause exceptions sin = stdin if stdout == PIPE: sout = subprocess.PIPE else: sout = stdout if stderr == SOUT: serr = subprocess.STDOUT else: serr = stderr self.process = subprocess.Popen(args, stdin=sin, stdout=sout, stderr=serr, preexec_fn=preexec_fn,close_fds=close_fds, shell=shell, env=env, cwd=cwd) self.pid = self.process.pid # Add attributes stdin, stdout, and stderr self.stdin = self.process.stdin self.stdout = self.process.stdout self.stderr = self.process.stderr else: # Use popen2 to exec functions # Determine which form of popen2 to use if stderr == SOUT or stderr == None: self.process = popen2.Popen4(args) self.stdin = self.process.tochild self.stdout = self.process.fromchild self.stderr = None else: self.process = popen2.Popen3(args) self.stdin = self.process.tochild self.stdout = self.process.fromchild self.stderr = self.process.childerr self.pid = self.process.pid def wait(self,): """ Waits for and returns the status code of the child process. """ return self.process.wait() def poll(self,): """ Returns -1 if the child process hasn't completed yet, or it's return code otherwise """ return self.process.poll() Aquil On Feb 23, 2:10 pm, Christian Heimes wrote: > aha wrote > > > def runner(cmd, stdin, stdout, ...): > > try: > > import subprocess > > sbm = 1 > > except: > > sbm = 0 > > > # Now do something > > if sbm: > > process = subporcess(...) > > else: > > import popen2 > > process = popen2.Popen4(...) > > > Has anyone else run into a situation similar to this one? > > The canonical way for your try/except clause is: > > try: > import subprocess > except ImportError: > subprocess = None > > ... > > if subprocess is not None: > ... > else: > ... > > Christian -- http://mail.python.org/mailman/listinfo/python-list
Re: A tale of two execs
I've decided to change the runCmdLine method to Popen. On Feb 23, 4:54 pm, aha wrote: > Hello All, > So below is my naive attempt at the wrapper, it is only half baked > since I am no master at Interprocess communication... I know that it > is lacking a lot of things comment greatly appreciated: > > #!/usr/bin/env python > > import os, sys, re, exceptions > try: > import subprocess > except ImportError: > import popen2 > subprocess = None > > PIPE = 10 > SOUT = 11 > > class Runner: > def __init__(self): > self.stdout = None > self.stdin = None > self.stderr = None > self.pid = None > self.process = None > > def runCmdLine(self, args, executable=None, stdin=None, > stdout=None, stderr=None, preexec_fn=None, > close_fds=False, shell=False,cwd=None, env=None): > """ > args -- a string, or a sequence of program arguments. The > argument to execute is > normally the first item in the args sequence or the string if > a string is given, > but can be explicitly set by using the executable argument. > > executable -- string to be executed > > stdin, stdout, stderr -- value of standard input, standard > output, and standard > error file handles. None implies no redirection. Valid values > are PIPE, an existing > file descriptor (a positive integer), an existing file object, > and None. > > shell -- specifies whether command should be executed through > shell > > cwd -- specifies whether, childs current directory will be > changed to cwd > > env -- A map that specifies the environment variables in child > process > that will be overwritten > > NOTE: Depending on the modules available, some arguments may > not be used. > """ > if subprocess: > # Use subprocess to exec functions > if stdin == PIPE: > sin = subprocess.PIPE > else: > # NOTE: User may input invalid values that will cause > exceptions > sin = stdin > if stdout == PIPE: > sout = subprocess.PIPE > else: > sout = stdout > if stderr == SOUT: > serr = subprocess.STDOUT > else: > serr = stderr > > self.process = subprocess.Popen(args, stdin=sin, > stdout=sout, stderr=serr, > > preexec_fn=preexec_fn,close_fds=close_fds, shell=shell, > env=env, cwd=cwd) > self.pid = self.process.pid > # Add attributes stdin, stdout, and stderr > self.stdin = self.process.stdin > self.stdout = self.process.stdout > self.stderr = self.process.stderr > else: > # Use popen2 to exec functions > # Determine which form of popen2 to use > if stderr == SOUT or stderr == None: > self.process = popen2.Popen4(args) > self.stdin = self.process.tochild > self.stdout = self.process.fromchild > self.stderr = None > else: > self.process = popen2.Popen3(args) > self.stdin = self.process.tochild > self.stdout = self.process.fromchild > self.stderr = self.process.childerr > > self.pid = self.process.pid > > def wait(self,): > """ > Waits for and returns the status code of the child process. > """ > return self.process.wait() > > def poll(self,): > """ > Returns -1 if the child process hasn't completed yet, or it's > return code > otherwise > """ > return self.process.poll() > > Aquil > > On Feb 23, 2:10 pm, Christian Heimes wrote: > > > aha wrote > > > > def runner(cmd, stdin, stdout, ...): > > > try: > > > import subprocess > > > sbm = 1 > > > except: > > > sbm = 0 > > > > # Now do something > > > if sbm: > > > process = subporcess(...) > > > else: > > > import popen2 > > > process = popen2.Popen4(...) > > > > Has anyone else run into a situation similar to this one? > > > The canonical way for your try/except clause is: > > > try: > > import subprocess > > except ImportError: > > subprocess = None > > > ... > > > if subprocess is not None: > > ... > > else: > > ... > > > Christian > > -- http://mail.python.org/mailman/listinfo/python-list
Re: A tale of two execs
Hello All, It occurred to me that I might just want to try copying the subprocess.py installed in a /usr/lib/python24 installation to the directory where I place the scripts that I need to launch my application...I know this was mentioned earlier. Anyway, my application worked under python 2.3 and later python versions as well. Here is what I did: I renamed subprocess.py to subprocess23.py and then used the following code: try: import subprocess except ImportError: import subprocess23 as subprocess I chose this route because I took a deeper look at the code in the subprocess module and I also realized that I would not be able to use the popen2, because I would not be able to do things like os.setpgrp for preexec_fn, with popen2. In the end, if I wanted that type of functionality I would have to use os.dup, fork, exec, which meant reinventing the wheel. I overcame the issue of dealing with msvcrt and _subprocess under windows by requiring python24 or greater under windows. Regards, Aquil -- http://mail.python.org/mailman/listinfo/python-list
Using Python To Launch Python
Hello All, I have a situation where I can count on a Python installation being available on a system, but I can't count on it being a version of Python needed by my application. Since my application has it's own version of Python installed with it how should I use the system Python to launch the version of Python that launches my Application. Yes, this is a convoluted process, but not all Pythons are built the same :) Right now I am leaning towards using exec to start a new process, but I thought I would check to see if anyone else has had the need to perform a task similar to this one. AHA -- http://mail.python.org/mailman/listinfo/python-list
Determining Processor Vender
Dose anyone know of a cross-platform method for determining the vendor of a processor? Under linux I can check /proc/cpuinfo. What I'd like to be able to do is determine if a processor is AMD or Intel, so that I can use the appropriate numerical libraries for my application. Regards, Aquil -- http://mail.python.org/mailman/listinfo/python-list
Re: Determining Processor Vender
On Sep 5, 3:00 pm, aha <[EMAIL PROTECTED]> wrote: > Dose anyone know of a cross-platform method for determining the vendor > of a processor? Under linux I can check /proc/cpuinfo. What I'd like > to be able to do is determine if a processor is AMD or Intel, so that > I can use the appropriate numerical libraries for my application. > > Regards, > > Aquil Additionally, I've tried the platform module...platform.processor() on my 64-bit AMD system, under Linux, returns x86_64. And under windows retruns an empty string '' -- http://mail.python.org/mailman/listinfo/python-list
Re: Need formatting suggestion for long strings
On Sep 5, 3:29 pm, Jean-Paul Calderone <[EMAIL PROTECTED]> wrote: > On Fri, 5 Sep 2008 14:24:16 -0500, Robert Dailey <[EMAIL PROTECTED]> wrote: > >Hi, > > >I find quite often that I'm writing things like this: > > >raise FatalExcept( "Insufficient number of arguments specified. Exactly {0} > >arguments are required. See stage.bat for documentation on accepted > >parameters.".format( num_arguments ) ) > > >On my display (Vertical monitor), this exceeds the width of my display, and > >to view the entire length of the string I am forced to pan my view left and > >right. Is there a special way I can format this string so that it fits > >nicely on the screen? Keep in mind that one important factor is that > >whitespace is very sensitive, and I do not want line breaks in my script > >file to become part of the string itself. I like how C++ handles strings, > >like this: > > >char const* mystring = > > "This is a very long string that " > > "spans multiple lines and does " > > "not include line breaks or tabs " > > "from the source file between " > > "the strings partitions." > > >What do you guys prefer? Thanks for reading. > > mystring = ( > "This is a very long string that " > "spans multiple lines and does " > "not include line breaks or tabs " > "from the source file between " > "the strings partitions.") > > Jean-Paul Can you be more specific? What is the formatting criteria? Are you talking about formatting the string for display or are you talking about the source? -- http://mail.python.org/mailman/listinfo/python-list
Re: Unable to start a process with subprocess Popen()
On Sep 8, 7:23 am, [EMAIL PROTECTED] wrote: > On Mon, Sep 8, 2008 at 11:50 AM, <[EMAIL PROTECTED]> wrote: > > Hi, > > > I'm using the subprocess module's Popen() to start a batch file. This > > batch file basically calls an exe which also gets started. > > Unfortunately, this does not produce any results. I looked into the > > Task bar that this exe has started but it does not consume and cpu so > > I believet that this exe is not working. > > > I used the following command to start the batch fiile: > > > testing = subprocess.Popen([batchFilePath], \ > > shell = True, \ > > stdout = subprocess.PIPE, \ > > stderr = subprocess.PIPE).communicate()[0] > > > batchFilePath is the path of the batch file. > > > -- > > Regrads, > > Rajat > > Ok, I re-phrase my question: > > there is a batch file that executes a exe file. The batch just works > if run from command prompt and produces output to standard output and > the file. > > Now, I try to call the same batch file from subprocess.Pope() call. > The batch file gets called and that internally also calls the exe > file. > > But, the exe just runs forever i.e. hangs and does not produces output > , atleast not to the file. > > Please suggest is there is something wrong with the above code. > > Regards, > Rajat Hello Rajat, I would take a look at the thread below, it might help it might not: http://groups.google.com/group/comp.lang.python/browse_thread/thread/4505613f014fdec7/3ee15c9c88a5efdc?hl=en#3ee15c9c88a5efdc Also, if you post a larger section of the code I might be able to give you a hand. Once you've run the testing = subprocess.Popen() make sure you use a testing.wait() -- http://mail.python.org/mailman/listinfo/python-list