Duck-typed check for “emptiness” of an object. Works for standard sequences (lists, tuples, etc.), NumPy arrays, and sparse-like objects (e.g., SciPy sparse arrays). The function checks: 1. If the object supports len(), returns True if len(obj) == 0. 2. If the obje
(obj)
| 2322 | |
| 2323 | |
| 2324 | def is_empty(obj): |
| 2325 | """ |
| 2326 | Duck-typed check for “emptiness” of an object. |
| 2327 | |
| 2328 | Works for standard sequences (lists, tuples, etc.), NumPy arrays, |
| 2329 | and sparse-like objects (e.g., SciPy sparse arrays). |
| 2330 | |
| 2331 | The function checks: |
| 2332 | 1. If the object supports len(), returns True if len(obj) == 0. |
| 2333 | 2. If the object has a `.nnz` attribute (number of non-zero elements), |
| 2334 | returns True if `.nnz == 0`. |
| 2335 | 3. If the object has a `.shape` attribute, returns True if any |
| 2336 | dimension is zero. |
| 2337 | 4. Otherwise, returns False (assumes non-empty). |
| 2338 | |
| 2339 | Parameters |
| 2340 | ---------- |
| 2341 | obj : any |
| 2342 | The object to check for emptiness. |
| 2343 | |
| 2344 | Returns |
| 2345 | ------- |
| 2346 | bool |
| 2347 | True if the object is considered empty, False otherwise. |
| 2348 | """ |
| 2349 | # Check standard sequences |
| 2350 | with contextlib.suppress(Exception): |
| 2351 | return len(obj) == 0 |
| 2352 | |
| 2353 | # Sparse-like objects |
| 2354 | with contextlib.suppress(Exception): |
| 2355 | return obj.nnz == 0 |
| 2356 | |
| 2357 | with contextlib.suppress(Exception): |
| 2358 | return 0 in obj.shape |
| 2359 | |
| 2360 | # Fallback: assume non-empty |
| 2361 | return False |
no outgoing calls
searching dependent graphs…