1 # -*- coding: utf-8 -*-
3 # Copyright (C) 2008 John Paulett (john -at- 7oars.com)
6 # This software is licensed as described in the file COPYING, which
7 # you should have received as part of this distribution.
9 """Helper functions for pickling and unpickling. Most functions assist in
10 determining the type of an object.
16 COLLECTIONS
= set, list, tuple
17 PRIMITIVES
= str, unicode, int, float, bool, long
18 NEEDS_REPR
= (datetime
.datetime
, datetime
.time
, datetime
.date
,
22 """Returns True is obj is a reference to a type.
34 return type(obj
) is types
.TypeType
or repr(obj
).startswith('<class')
37 """Returns True is obj is a reference to an object instance.
42 >>> is_object(object())
45 >>> is_object(lambda x: 1)
48 return (isinstance(obj
, object) and
49 type(obj
) is not types
.TypeType
and
50 type(obj
) is not types
.FunctionType
)
52 def is_primitive(obj
):
53 """Helper method to see if the object is a basic data type. Strings,
54 integers, longs, floats, booleans, and None are considered primitive
55 and will return True when passed into *is_primitive()*
59 >>> is_primitive([4,4])
64 elif type(obj
) in PRIMITIVES
:
68 def is_dictionary(obj
):
69 """Helper method for testing if the object is a dictionary.
71 >>> is_dictionary({'key':'value'})
74 return type(obj
) is dict
76 def is_collection(obj
):
77 """Helper method to see if the object is a Python collection (list,
79 >>> is_collection([4])
82 return type(obj
) in COLLECTIONS
85 """Helper method to see if the object is a Python list.
90 return type(obj
) is list
93 """Helper method to see if the object is a Python set.
98 return type(obj
) is set
101 """Helper method to see if the object is a Python tuple.
106 return type(obj
) is tuple
108 def is_dictionary_subclass(obj
):
109 """Returns True if *obj* is a subclass of the dict type. *obj* must be
110 a subclass and not the actual builtin dict.
112 >>> class Temp(dict): pass
113 >>> is_dictionary_subclass(Temp())
116 return (hasattr(obj
, '__class__') and
117 issubclass(obj
.__class
__, dict) and not is_dictionary(obj
))
119 def is_collection_subclass(obj
):
120 """Returns True if *obj* is a subclass of a collection type, such as list
121 set, tuple, etc.. *obj* must be a subclass and not the actual builtin, such
122 as list, set, tuple, etc..
124 >>> class Temp(list): pass
125 >>> is_collection_subclass(Temp())
129 return issubclass(obj
.__class
__, COLLECTIONS
) and not is_collection(obj
)
131 def is_noncomplex(obj
):
132 """Returns True if *obj* is a special (weird) class, that is complex than
133 primitive data types, but is not a full object. Including:
135 * :class:`~time.struct_time`
137 if type(obj
) is time
.struct_time
:
142 """Returns True if the *obj* must be encoded and decoded using the
143 :func:`repr` function. Including:
145 * :class:`~datetime.datetime`
146 * :class:`~datetime.date`
147 * :class:`~datetime.time`
148 * :class:`~datetime.timedelta`
150 return isinstance(obj
, NEEDS_REPR
)
152 def is_function(obj
):
153 """Returns true if passed a function
155 >>> is_function(lambda x: 1)
158 >>> is_function(locals)
161 >>> def method(): pass
162 >>> is_function(method)
168 if type(obj
) is types
.FunctionType
:
170 if not is_object(obj
):
172 if not hasattr(obj
, '__class__'):
174 module
= obj
.__class
__.__module
__
175 name
= obj
.__class
__.__name
__
176 return (module
== '__builtin__' and
177 name
in ('function', 'builtin_function_or_method'))