lundi 16 juillet 2018

Converting String With Patterns To List

I'm currently working with Ansible inventory hosts and my script needs to parse them in order to complete some tasks.

The issue here is converting the string patterns to list. I have accomplished it by splitting them with a couple of iterations, but it feels overcomplicated for the code and hard to read.

Basically I'm looking for a way to convert a simple string with pattern to a list:

>>> x = 'mymachine[1:2]cluster[1:2]'
>>> y = magic_expression(x)
>>> y
['mymachine1cluster1', 'mymachine1cluster2', 'mymachine2cluster1', 'mymachine2cluster2']

Is there a better way ?

Aucun commentaire:

Enregistrer un commentaire