Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Empty VLAarray evaluates to False #1071

Open
spchamp opened this issue Oct 13, 2023 · 2 comments
Open

Empty VLAarray evaluates to False #1071

spchamp opened this issue Oct 13, 2023 · 2 comments

Comments

@spchamp
Copy link

spchamp commented Oct 13, 2023

I'm a new py-tables user. I was using the following code, trying to understand how to use VLStringAtom individually or with an IsDescription class. While trying to ensure that the test script might be reentrant somehow, I'd encountered a concern with how an (empty) VLArray seems to be evaluated under a true/false expression.

I'd expected that the varlist_00 value in the following may evaluate to a non-falsey value whenvarlist_00 denotes a VLArray

import os
import tables
from typing import Optional

TEST_FILE =  os.path.join(os.path.dirname(__file__), "test_00.h5")
ROOT_FILTERS = tables.Filters(complib="blosc2", shuffle=True, bitshuffle=True, complevel=2)

h5file = tables.open_file(TEST_FILE, "r+", filters=ROOT_FILTERS)

root_node: tables.group.RootGroup = h5file.get_node("/")

def node_if_found(name, base: Optional[tables.Group] = None) -> Optional[tables.Node]:
    global root_node
    grp = base or root_node
    for node in grp._f_iter_nodes():
        if node._v_name == name:
            return node

varlist_00 = node_if_found("varlist_00", root_node)


if varlist_00:
    # ...
    pass
else:
    varlist_00 = h5file.create_vlarray(root_node, "varlist_00", atom=tables.VLStringAtom)

After the initial call to create_vlarray I see the following:

In [77]: varlist_00
Out[77]: 
/varlist_00 (VLArray(0,)) ''
  atom = VLStringAtom()
  byteorder = 'irrelevant'
  nrows = 0
  flavor = 'numpy'

The object exists, yet there's the following behavior where varlist_00 is not evaluated quite as expected:

In [78]: if varlist_00:
    ...:     print("exists")
    ...: else:
    ...:     print("not found")
    ...: 
not found

If one may borrow a term from Ruby, is it an accurate assumption that varlist_00 is being evaluated as a falsey value?

There's a workaround for this usage case, simply to test for whether varlist_00 is exactly None.

varlist_00 = node_if_found("varlist_00", root_node)

if varlist_00 is not None:
    # ...
    pass
else:
    varlist_00 = h5file.create_vlarray(root_node, "varlist_00", atom=tables.VLStringAtom)

Maybe it's an isolated instance?

In [81]: if root_node:
    ...:     print("root ok")
    ...: else:
    ...:     print("?")
    ...: 
root ok

using tables 3.8.0, compiled individually on a FreeBSD system

@spchamp spchamp changed the title Node evaluates to False? using an empty VLAarray Node evaluates to False, using an empty VLAarray Oct 13, 2023
@spchamp
Copy link
Author

spchamp commented Oct 13, 2023

Once items are added to the varlist, then it no longer evaluates to false.

Albeit, I'm also seeing an error when trying to append to the varlist initially, with the following showing up under ipython:

In [5]: varlist_01 = h5file.create_vlarray(root_node, "varlist_01", atom=tables.VLStringAtom)
In [6]: varlist_01.append("var01")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[6], line 1
----> 1 varlist_01.append("var01")

File ~/.../env/lib/python3.11/site-packages/tables/vlarray.py:510, in VLArray.append(self, sequence)
    508 atom = self.atom
    509 if not hasattr(atom, 'size'):  # it is a pseudo-atom
--> 510     sequence = atom.toarray(sequence)
    511     statom = atom.base
    512 else:

TypeError: _BufferedAtom.toarray() missing 1 required positional argument: 'object_'

This is similar to another error I'm seeing, perhaps it may be somehow a side effect of the build environment.

After closing the HDF5 file then re-opening and reinitializing the scoped variables, then I can append to the varlist.

Once an element is added to the varlist, then the varlist does not evaluate to False:

In [13]: varlist_00 = node_if_found("varlist_00", root_node)

In [14]: varlist_00.append("thunk1024")

In [15]: if varlist_00:
    ...:     print("OK")
    ...: else:
    ...:     print("?")
    ...: 
OK

I'll try testing this with a newer release

@spchamp spchamp changed the title Node evaluates to False, using an empty VLAarray Empty VLAarray evaluates to False Oct 13, 2023
@spchamp
Copy link
Author

spchamp commented Oct 13, 2023

The initial behavior is showing up also with a py-tables 3.9.1 build (blosc2-2.2.10.dev0).

A reference to a new, empty varlist (VLArray) is being evaluated as not a truthy value

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@spchamp and others