1# Copyright 2008-2015 Nokia Networks 2# Copyright 2016- Robot Framework Foundation 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16from collections import Mapping, UserString 17from io import IOBase 18 19from .platform import RERAISED_EXCEPTIONS 20 21 22def is_integer(item): 23 return isinstance(item, int) 24 25 26def is_number(item): 27 return isinstance(item, (int, float)) 28 29 30def is_bytes(item): 31 return isinstance(item, (bytes, bytearray)) 32 33 34def is_string(item): 35 return isinstance(item, str) 36 37 38def is_unicode(item): 39 return isinstance(item, str) 40 41 42def is_list_like(item): 43 if isinstance(item, (str, bytes, bytearray, UserString, IOBase)): 44 return False 45 try: 46 iter(item) 47 except RERAISED_EXCEPTIONS: 48 raise 49 except: 50 return False 51 else: 52 return True 53 54 55def is_dict_like(item): 56 return isinstance(item, Mapping) 57 58 59def type_name(item): 60 if isinstance(item, IOBase): 61 return 'file' 62 cls = item.__class__ if hasattr(item, '__class__') else type(item) 63 named_types = {str: 'string', bool: 'boolean', int: 'integer', 64 type(None): 'None', dict: 'dictionary', type: 'class'} 65 return named_types.get(cls, cls.__name__) 66