TuxOtaku
TuxOtaku

Reputation: 51

Strip characters from list in Python

so I'm trying to print out a list of VMWare templates that are sitting in our lab. I want the output to look like this:

vagrant-ubuntu12.04-small
vagrant-centos6.6-small
vagrant-ubuntu12.04
vagrant-centos6.6

Whereas the current output looks more like this:

['[datastore2] vagrant-ubuntu12.04-small']
['[datastore2] vagrant-centos6.6-small']
['[datastore1] vagrant-centos6.6']
['[datastore1] vagrant-ubuntu12.04']

Here's my code:

from pysphere import VIServer
from pprint import pprint

VSPHERE = VIServer()

VSPHERE.connect('helike.labs.sdelements.com',
                'xxxxxxxxx',
                'xxxxxxxxx')

VMLIST = VSPHERE.get_registered_vms()


def is_template(string):
    """ Is it a template? """
    if string.find(".vmtx") == -1:
        return False
    else:
        return True


def is_vagrant_template(string):
    """ Is it a Vagrant Template? """
    if string.find("vagrant") == -1:
        return False
    else:
        return True


def is_proper_template(string):
    """ filter out extraneous templates """
    if string.find("sde") == -1:
        return True
    else:
        return False

temp1 = filter(is_template, VMLIST)
temp2 = filter(is_vagrant_template, temp1)
temp3 = filter(is_proper_template, temp2)

for item in temp3:
    relist = item.split('/')[:1]
    pprint(relist)

I know this is probably really amateurish code but I'm not really a python guy. Is there some kind of regex or something I could use to clean this up a bit?

Upvotes: 2

Views: 113

Answers (3)

gbrener
gbrener

Reputation: 5835

The function you're looking for is map: https://docs.python.org/2/library/functions.html#map

What you'd want to do is call map after filter, like so:

def is_proper_vagrant_template(string):
    """ Is it a proper Vagrant template? """
    return ".vmtx" in string and "vagrant" in string and "sde" not in string

def clean_template(string):
    """ Return the second half of the string, assuming whitespace as a separator """
    return string.split()[1]

temp1 = filter(is_proper_vagrant_template, VMLIST)
clean = map(clean_template, temp1)

In the snippet above, filter works the same way as what you had before, only I rewrote the call to combine your three functions into one. The map function takes the filtered list and calls clean_template on each element, returning the results as a list.

clean_template returns the second half of the string (the part that you're interested in), assuming there is no whitespace in the string other than what you identified.

Upvotes: 0

Padraic Cunningham
Padraic Cunningham

Reputation: 180391

If it is always the same format just split once on whitespace and extract the second element:

data = [['[datastore2] vagrant-ubuntu12.04-small'],
['[datastore2] vagrant-centos6.6-small'],
['[datastore1] vagrant-centos6.6'],
['[datastore1] vagrant-ubuntu12.04']]


for sub in data:
    print(sub[0].split(None,1)[1])

vagrant-ubuntu12.04-small
vagrant-centos6.6-small
vagrant-centos6.6
vagrant-ubuntu12.04

You can probably also do the split before you put the data in a list but without seeing the actual input it is impossible to say for sure.

Upvotes: 6

user557597
user557597

Reputation:

A simple regex can do it, gives some flexibility.
Can either just grab capture group 1 into an array,
or just global find and replace with capture group 1.
If you don't know all possible characters, just replace
[a-z\d.-]+ with \S+

(?mi)^\['\[[^\]]*\]\h+([a-z\d.-]+)\h*'\]

 (?mi)                         # Modes: Multi-line, No-Case
 ^                             # BOL
 \[' \[ [^\]]* \]
 \h+ 
 ( [a-z\d.-]+ )                # (1)
 \h* 
 '\]

Upvotes: 1

Related Questions