On Tue, May 17, 2011 at 12:10 PM, Alan Gauld <alan.ga...@btinternet.com>wrote:
> > Given that you want more than one method I assume > this is a homwework assignment rather than a practical > problem? If so we would prefer that you showed us what > you tried and we can then help you fix it rather than > just offer you solutions from which you will learn little. > It's really more that I'm a newbie to python. Trying to translate from perl to python in my head... How did you think it should be done? > Maybe if we start from there? > This is where I started (not elegant at all, but learning): def get_vms(): import subprocess p = subprocess.Popen(['/usr/sbin/ldm', 'ls', '-p'], stdout=subprocess.PIPE).communicate()[0] p = p.split('\n') [p.remove(item) for item in p if not item.startswith('DOMAIN')] p = [item.replace('DOMAIN|', '') for item in p] p = [i.split('|') for i in p] print p The results are as follows: Python 2.4.6 (#1, Dec 13 2009, 23:45:11) [C] on sunos5 Type "help", "copyright", "credits" or "license" for more information. >>> import subprocess >>> p = subprocess.Popen(['/usr/sbin/ldm', 'ls', '-p'], stdout=subprocess.PIPE).communicate()[0] >>> p 'VERSION 1.5\nDOMAIN|name=primary|state=active|flags=-n-cv-|cons=UART|ncpu=8|mem=8589934592|util=1.0|uptime=5790996\nDOMAIN|name=ilhsf002v001|state=active|flags=-n----|cons=5000|ncpu=32|mem=34359738368|util=0.0|uptime=5423393\nDOMAIN|name=ilhsf002v002|state=inactive|flags=------|cons=|ncpu=8|mem=8589934592|util=|uptime=\n' >>> p = p.split('\n') >>> p ['VERSION 1.5', 'DOMAIN|name=primary|state=active|flags=-n-cv-|cons=UART|ncpu=8|mem=8589934592|util=1.0|uptime=5790996', 'DOMAIN|name=ilhsf002v001|state=active|flags=-n----|cons=5000|ncpu=32|mem=34359738368|util=0.0|uptime=5423393', 'DOMAIN|name=ilhsf002v002|state=inactive|flags=------|cons=|ncpu=8|mem=8589934592|util=|uptime=', ''] >>> [p.remove(item) for item in p if not item.startswith('DOMAIN')] [None, None] >>> p ['DOMAIN|name=primary|state=active|flags=-n-cv-|cons=UART|ncpu=8|mem=8589934592|util=1.0|uptime=5790996', 'DOMAIN|name=ilhsf002v001|state=active|flags=-n----|cons=5000|ncpu=32|mem=34359738368|util=0.0|uptime=5423393', 'DOMAIN|name=ilhsf002v002|state=inactive|flags=------|cons=|ncpu=8|mem=8589934592|util=|uptime='] >>> p = [item.replace('DOMAIN|', '') for item in p] >>> p ['name=primary|state=active|flags=-n-cv-|cons=UART|ncpu=8|mem=8589934592|util=1.0|uptime=5790996', 'name=ilhsf002v001|state=active|flags=-n----|cons=5000|ncpu=32|mem=34359738368|util=0.0|uptime=5423393', 'name=ilhsf002v002|state=inactive|flags=------|cons=|ncpu=8|mem=8589934592|util=|uptime='] >>> p = [i.split('|') for i in p] >>> >>> p [['name=primary', 'state=active', 'flags=-n-cv-', 'cons=UART', 'ncpu=8', 'mem=8589934592', 'util=1.0', 'uptime=5790996'], ['name=ilhsf002v001', 'state=active', 'flags=-n----', 'cons=5000', 'ncpu=32', 'mem=34359738368', 'util=0.0', 'uptime=5423393'], ['name=ilhsf002v002', 'state=inactive', 'flags=------', 'cons=', 'ncpu=8', 'mem=8589934592', 'util=', 'uptime=']] >>> Goal: To take the results from a process, parse the data into a dictionary object that can be returned from the function for use in a script. The dictionary object needs to be able to access sub-data (e.g. state, memory, cpus, etc.) from the key (server names).
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor