0

I'm trying to make function VisitAllObjects accept different files and return a dictionary. But what I have currently points to the same name? So once gdata is ran, fdata changes to gdata. How can I make it so fdata and gdata equal their respected data from their gh5 files?

f = h5py.File('testfile.gh5', 'r')
g = h5py.File('testfile2.gh5', 'r')

def VisitAllObjects(Group,Path, FileInfo=None):
    if FileInfo is None:
        FileInfo = {}
    for i in Group.items():
        if isinstance(i[1], h5py.Group):
            VisitAllObjects(i[1], Path + '/' + i[0])
        else:
            DatasetName = Path + '/' + i[0]
            FileInfo[DatasetName] = (Group[DatasetName].shape, Group[DatasetName].dtype)
    return FileInfo

fdata = VisitAllObjects(f,'')
gdata = VisitAllObjects(g,'')

With your help and adding file_info when calling visit_all_objects within itself I was able to get this to work. If you see any problems with this please let me know, I still need to check it more but using fdata and gdata come back with different data and fdata is gdata is False. Thanks.

I got it to work with the following:

def visit_all_objects(Group,Path, file_info=None):
    if file_info is None:
        file_info = {}
    for i in Group.items():
        if isinstance(i[1], h5py.Group):
            visit_all_objects(i[1], Path + '/' + i[0], file_info)
        else:
            DatasetName = Path + '/' + i[0]
            file_info[DatasetName] = (Group[DatasetName].shape, Group[DatasetName].dtype)
    return file_info
tzelleke
  • 15,023
  • 5
  • 33
  • 49
tlab
  • 103
  • 1
  • 1
  • 7
  • It's highly recommended that you follow [PEP-8](http://www.python.org/dev/peps/pep-0008/#prescriptive-naming-conventions), which recommends reserving `CapWords` for classes, and using `lowercase_with_underscores` for functions and local variables - it makes your code far easier to take in at a glance. (You can see it affects the syntax highlighting here on SO too). – Gareth Latty Dec 31 '12 at 16:26
  • (a) PEP8. Find it and read it (b) What does "But what I have currently points to the same name?" mean. Please provide example input and output, and explain why it is erroneous. – Marcin Dec 31 '12 at 16:26
  • 1
    The recursive calls you are doing are useless because you are not saving the results, only the topmost values are saved. `fdata` shouldn't "become"`gdata` in your example(even though I do not know how `h5py` works, but anyway...). You're probably seeing the same results because only the topmost elements are taken into account. Did you see if they are the same object using the `is` operator? – Bakuriu Dec 31 '12 at 16:27
  • Thanks for PEP8 suggestion (I'm still new, as you can tell I'm sure). When I do a gdata is fdata I get true. VisitAllObjects was taken from www.hdfeos.org/workshops/ws13/.../day1/H5py-Python.ppt. The function works for a single file if I make FileInfo a global, but I'd rather return it from the function for future use. – tlab Dec 31 '12 at 17:20

1 Answers1

0

Instead of mutating the dictionary, try making a new one, and mutating that. You could do that by putting FileInfo = FileInfo.copy() near the top of the function, right after you handle the case where FileInfo is None.

Phil Frost
  • 3,668
  • 21
  • 29