Robot Framework Integrated Development Environment (RIDE)
robottypes2.py
Go to the documentation of this file.
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 
16 from collections import Mapping
17 from UserDict import UserDict
18 from UserString import UserString
19 from types import ClassType, NoneType
20 
21 try:
22  from java.lang import String
23 except ImportError:
24  String = ()
25 
26 from .platform import RERAISED_EXCEPTIONS
27 
28 
29 def is_integer(item):
30  return isinstance(item, (int, long))
31 
32 
33 def is_number(item):
34  return isinstance(item, (int, long, float))
35 
36 
37 def is_bytes(item):
38  return isinstance(item, (bytes, bytearray))
39 
40 
41 def is_string(item):
42  # Returns False with `b'bytes'` on IronPython on purpose. Results of
43  # `isinstance(item, basestring)` would depend on IronPython 2.7.x version.
44  return isinstance(item, (str, unicode))
45 
46 
47 def is_unicode(item):
48  return isinstance(item, unicode)
49 
50 
51 def is_tuple(item):
52  return isinstance(item, tuple)
53 
54 
55 def is_list_like(item):
56  if isinstance(item, (str, unicode, bytes, bytearray, UserString, String,
57  file)):
58  return False
59  try:
60  iter(item)
61  except RERAISED_EXCEPTIONS:
62  raise
63  except:
64  return False
65  else:
66  return True
67 
68 
69 def is_dict_like(item):
70  return isinstance(item, (Mapping, UserDict))
71 
72 
73 def type_name(item):
74  cls = item.__class__ if hasattr(item, '__class__') else type(item)
75  named_types = {str: 'string', unicode: 'string', bool: 'boolean',
76  int: 'integer', long: 'integer', NoneType: 'None',
77  dict: 'dictionary', type: 'class', ClassType: 'class'}
78  return named_types.get(cls, cls.__name__)