2222
2323
2424def is_number (obj ):
25+ """
26+ Check if the object is a number.
27+
28+ Parameters
29+ ----------
30+ obj : The object to check.
31+
32+ Returns
33+ -------
34+ is_number : bool
35+ Whether `obj` is a number or not.
36+
37+ Examples
38+ --------
39+ >>> is_number(1)
40+ True
41+ >>> is_number("foo")
42+ False
43+ """
44+
2545 return isinstance (obj , (Number , np .number ))
2646
2747
2848def is_string_like (obj ):
49+ """
50+ Check if the object is a string.
51+
52+ Parameters
53+ ----------
54+ obj : The object to check.
55+
56+ Examples
57+ --------
58+ >>> is_string_like("foo")
59+ True
60+ >>> is_string_like(1)
61+ False
62+
63+ Returns
64+ -------
65+ is_str_like : bool
66+ Whether `obj` is a string or not.
67+ """
68+
2969 return isinstance (obj , (text_type , string_types ))
3070
3171
32- def _iterable_not_string (x ):
33- return (isinstance (x , collections .Iterable ) and
34- not isinstance (x , string_types ))
72+ def _iterable_not_string (obj ):
73+ """
74+ Check if the object is an iterable but not a string.
75+
76+ Parameters
77+ ----------
78+ obj : The object to check.
79+
80+ Returns
81+ -------
82+ is_iter_not_string : bool
83+ Whether `obj` is a non-string iterable.
84+
85+ Examples
86+ --------
87+ >>> _iterable_not_string([1, 2, 3])
88+ True
89+ >>> _iterable_not_string("foo")
90+ False
91+ >>> _iterable_not_string(1)
92+ False
93+ """
94+
95+ return (isinstance (obj , collections .Iterable ) and
96+ not isinstance (obj , string_types ))
3597
3698
3799def is_iterator (obj ):
100+ """
101+ Check if the object is an iterable.
102+
103+ Parameters
104+ ----------
105+ obj : The object to check.
106+
107+ Returns
108+ -------
109+ is_iter : bool
110+ Whether `obj` is an iterable.
111+
112+ Examples
113+ --------
114+ >>> is_iterator([1, 2, 3])
115+ True
116+ >>> is_iterator(1)
117+ False
118+ """
119+
38120 if not hasattr (obj , '__iter__' ):
39121 return False
40122
@@ -47,6 +129,45 @@ def is_iterator(obj):
47129
48130
49131def is_file_like (obj ):
132+ """
133+ Check if the object is a file-like object.
134+
135+ For objects to be considered file-like, they must
136+ have the following four methods:
137+
138+ 1) read
139+ 2) write
140+ 3) seek
141+ 4) tell
142+
143+ In addition, the object must be iterable, as mock
144+ objects can easily have the four aforementioned
145+ properties. However, being truly iterable is much
146+ more difficult to do.
147+
148+ Note: file-like objects must be iterable, but
149+ iterable objects need not be file-like.
150+
151+ .. versionadded:: 0.20.0
152+
153+ Parameters
154+ ----------
155+ obj : The object to check.
156+
157+ Returns
158+ -------
159+ is_file_like : bool
160+ Whether `obj` has file-like properties.
161+
162+ Examples
163+ --------
164+ >>> buffer(StringIO("data"))
165+ >>> is_file_like(buffer)
166+ True
167+ >>> is_file_like([1, 2, 3])
168+ False
169+ """
170+
50171 file_attrs = ('read' , 'write' , 'seek' , 'tell' )
51172
52173 for attr in file_attrs :
@@ -60,10 +181,50 @@ def is_file_like(obj):
60181
61182
62183def is_re (obj ):
184+ """
185+ Check if the object is a regex pattern instance.
186+
187+ Parameters
188+ ----------
189+ obj : The object to check.
190+
191+ Returns
192+ -------
193+ is_regex : bool
194+ Whether `obj` is a regex pattern.
195+
196+ Examples
197+ --------
198+ >>> is_re(re.compile(".*"))
199+ True
200+ >>> is_re("foo")
201+ False
202+ """
203+
63204 return isinstance (obj , re ._pattern_type )
64205
65206
66207def is_re_compilable (obj ):
208+ """
209+ Check if the object can be compiled into a regex pattern instance.
210+
211+ Parameters
212+ ----------
213+ obj : The object to check.
214+
215+ Returns
216+ -------
217+ is_regex_compilable : bool
218+ Whether `obj` can be compiled as a regex pattern.
219+
220+ Examples
221+ --------
222+ >>> is_re_compilable(".*")
223+ True
224+ >>> is_re_compilable(1)
225+ False
226+ """
227+
67228 try :
68229 re .compile (obj )
69230 except TypeError :
@@ -72,21 +233,86 @@ def is_re_compilable(obj):
72233 return True
73234
74235
75- def is_list_like (arg ):
76- return (hasattr (arg , '__iter__' ) and
77- not isinstance (arg , string_and_binary_types ))
236+ def is_list_like (obj ):
237+ """
238+ Check if the object is list-like.
239+
240+ Parameters
241+ ----------
242+ obj : The object to check.
243+
244+ Returns
245+ -------
246+ is_list_like : bool
247+ Whether `obj` has list-like properties.
248+
249+ Examples
250+ --------
251+ >>> is_list_like([1, 2, 3])
252+ True
253+ >>> is_list_like({1, 2, 3})
254+ True
255+ >>> is_list_like(1)
256+ False
257+ """
258+
259+ return (hasattr (obj , '__iter__' ) and
260+ not isinstance (obj , string_and_binary_types ))
261+
78262
263+ def is_dict_like (obj ):
264+ """
265+ Check if the object is dict-like.
266+
267+ Parameters
268+ ----------
269+ obj : The object to check.
79270
80- def is_dict_like (arg ):
81- return hasattr (arg , '__getitem__' ) and hasattr (arg , 'keys' )
271+ Returns
272+ -------
273+ is_dict_like : bool
274+ Whether `obj` has dict-like properties.
82275
276+ Examples
277+ --------
278+ >>> is_dict_like({1: 2})
279+ True
280+ >>> is_dict_like([1, 2, 3])
281+ False
282+ """
83283
84- def is_named_tuple (arg ):
85- return isinstance (arg , tuple ) and hasattr (arg , '_fields' )
284+ return hasattr (obj , '__getitem__' ) and hasattr (obj , 'keys' )
86285
87286
88- def is_hashable (arg ):
89- """Return True if hash(arg) will succeed, False otherwise.
287+ def is_named_tuple (obj ):
288+ """
289+ Check if the object is a named tuple.
290+
291+ Parameters
292+ ----------
293+ obj : The object to check.
294+
295+ Returns
296+ -------
297+ is_named_tuple : bool
298+ Whether `obj` is a named tuple.
299+
300+ Examples
301+ --------
302+ >>> Point = namedtuple("Point", ["x", "y"])
303+ >>> p = Point(1, 2)
304+ >>>
305+ >>> is_named_tuple(p)
306+ True
307+ >>> is_named_tuple((1, 2))
308+ False
309+ """
310+
311+ return isinstance (obj , tuple ) and hasattr (obj , '_fields' )
312+
313+
314+ def is_hashable (obj ):
315+ """Return True if hash(obj) will succeed, False otherwise.
90316
91317 Some types will pass a test against collections.Hashable but fail when they
92318 are actually hashed with hash().
@@ -102,25 +328,48 @@ def is_hashable(arg):
102328 >>> is_hashable(a)
103329 False
104330 """
105- # unfortunately , we can't use isinstance(arg , collections.Hashable), which
106- # can be faster than calling hash, because numpy scalars on Python 3 fail
107- # this test
331+ # Unfortunately , we can't use isinstance(obj , collections.Hashable), which
332+ # can be faster than calling hash. That is because numpy scalars on Python
333+ # 3 fail this test.
108334
109- # reconsider this decision once this numpy bug is fixed:
335+ # Reconsider this decision once this numpy bug is fixed:
110336 # https://github.com/numpy/numpy/issues/5562
111337
112338 try :
113- hash (arg )
339+ hash (obj )
114340 except TypeError :
115341 return False
116342 else :
117343 return True
118344
119345
120- def is_sequence (x ):
346+ def is_sequence (obj ):
347+ """
348+ Check if the object is a sequence of objects.
349+ String types are not included as sequences here.
350+
351+ Parameters
352+ ----------
353+ obj : The object to check.
354+
355+ Returns
356+ -------
357+ is_sequence : bool
358+ Whether `obj` is a sequence of objects.
359+
360+ Examples
361+ --------
362+ >>> l = [1, 2, 3]
363+ >>>
364+ >>> is_sequence(l)
365+ True
366+ >>> is_sequence(iter(l))
367+ False
368+ """
369+
121370 try :
122- iter (x )
123- len (x ) # it has a length
124- return not isinstance (x , string_and_binary_types )
371+ iter (obj ) # Can iterate over it.
372+ len (obj ) # Has a length associated with it.
373+ return not isinstance (obj , string_and_binary_types )
125374 except (TypeError , AttributeError ):
126375 return False
0 commit comments