1# (c) 2016, Ansible by Red Hat <info@ansible.com> 2# 3# This file is part of Ansible 4# 5# Ansible is free software: you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation, either version 3 of the License, or 8# (at your option) any later version. 9# 10# Ansible is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with Ansible. If not, see <http://www.gnu.org/licenses/>. 17 18# Make coding more python3-ish 19from __future__ import (absolute_import, division, print_function) 20__metaclass__ = type 21 22from ansible.module_utils.six import string_types 23 24 25def pct_to_int(value, num_items, min_value=1): 26 ''' 27 Converts a given value to a percentage if specified as "x%", 28 otherwise converts the given value to an integer. 29 ''' 30 if isinstance(value, string_types) and value.endswith('%'): 31 value_pct = int(value.replace("%", "")) 32 return int((value_pct / 100.0) * num_items) or min_value 33 else: 34 return int(value) 35 36 37def object_to_dict(obj, exclude=None): 38 """ 39 Converts an object into a dict making the properties into keys, allows excluding certain keys 40 """ 41 if exclude is None or not isinstance(exclude, list): 42 exclude = [] 43 return dict((key, getattr(obj, key)) for key in dir(obj) if not (key.startswith('_') or key in exclude)) 44 45 46def deduplicate_list(original_list): 47 """ 48 Creates a deduplicated list with the order in which each item is first found. 49 """ 50 seen = set() 51 return [x for x in original_list if x not in seen and not seen.add(x)] 52