This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def is_iterable(_obj): | |
''' Returns True when _obj is iterable and not a string ''' | |
import types | |
if not isinstance(_obj, types.StringTypes): | |
try: | |
iterable = iter(_obj) | |
except: | |
iterable = False | |
else: | |
iterable = False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def print_nested(_obj, _iterator=0): | |
''' iterate through an iterable object | |
- print_nested if parameter is another iterable object | |
- print value if parameter is not iterable | |
DEPENDENCY: is_iterable.py: https://gist.github.com/schbrongx/ad66fdd50d72b0042cd26c2689b0c10f | |
''' | |
for param in _obj: | |
if is_iterable(_obj[param]): | |
log.debug(' ' * _iterator + param + ': ') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def replace_in_nested_dict(dict_in, str_old, str_new): | |
''' loop over ALL levels of a dictionary, replacing str_old with str_new | |
i am working with collections.OrderedDict(), which may be replaced with | |
an ordinary dict. i have tested it with both. | |
''' | |
if not isinstance(dict_in, dict): | |
raise TypeError('First argument has to be of type dict. Is type: ' + type(dict_in)) | |
if not isinstance(str_old, str): | |
raise TypeError('Second argument has to be of type str. Is type: ' + type(str_old)) | |
if not isinstance(str_new, str): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def setup_yaml(): | |
""" add a yaml representer which preservers the order when dumping an OrderedDict """ | |
representer_ordered_dict = lambda self, content: self.represent_mapping('tag:yaml.org,2002:map', content.items()) | |
yaml.add_representer(collections.OrderedDict, representer_ordered_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (Get-Module -ListAvailable -Name VMware.PowerCLI) { | |
Import-Module VMWare.PowerCLI | |
} | |
else { | |
try { | |
Install-Module -Name VMware.PowerCLI | |
} | |
catch { | |
exit 1 | |
} |