Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_create(self):
# create main test file
filename = self.getFileName("create_group")
print("filename:", filename)
f = h5py.File(filename, 'w')
is_hsds = False
if isinstance(f.id.id, str) and f.id.id.startswith("g-"):
is_hsds = True # HSDS has different permission defaults
self.assertTrue('/' in f)
r = f['/']
self.assertEqual(len(r), 0)
self.assertTrue(isinstance(r, h5py.Group))
self.assertTrue(r.name, '/')
self.assertEqual(len(r.attrs.keys()), 0)
self.assertFalse('g1' in r)
r.create_group("g1")
self.assertEqual(len(r), 1)
self.assertTrue('g1' in r)
g1 = r['g1']
self.assertTrue(g1.id.id != r.id.id)
self.assertEqual(g1.name, "/g1")
r.create_group("g1/g1.1")
g1_1 = r["g1/g1.1"]
self.assertEqual(g1_1.name, "/g1/g1.1")
self.assertEqual(len(r), 1)
# Check group's last modified time
if h5py.__name__ == "h5pyd":
self.assertTrue(isinstance(g1.modified, datetime))
#self.assertEqual(g1.modified.tzname(), six.u('UTC'))
f.close()
# re-open file in read-only mode
f = h5py.File(filename, 'r')
self.assertEqual(len(f), 6)
for name in ("g1", "g2", "g4", "g1.1", "a space", "mysoftlink"):
self.assertTrue(name in f)
self.assertTrue("/g1/g1.1" in f)
g1_1 = f["/g1/g1.1"]
linkee_class = r.get('mysoftlink', getclass=True)
self.assertEqual(linkee_class, h5py.Group)
link_class = r.get('mysoftlink', getclass=True, getlink=True)
self.assertEqual(link_class, h5py.SoftLink)
softlink = r.get('mysoftlink', getlink=True)
self.assertEqual(softlink.path, '/g1/g1.1')
linked_obj = f["mysoftlink"]
self.assertEqual(linked_obj.id, g1_1.id)
f.close()
#f.create_group("subgrp")
root_grp.create_group("subgrp")
self.assertEqual(len(f.keys()), 1)
f.close()
self.assertEqual(f.id.id, 0)
# re-open as read-only
f = h5py.File(filename, 'r')
self.assertEqual(f.filename, filename)
self.assertEqual(f.name, "/")
self.assertTrue(f.id.id is not None)
self.assertEqual(len(f.keys()), 1)
self.assertEqual(f.mode, 'r')
self.assertTrue('/' in f)
r = f['/']
self.assertTrue(isinstance(r, h5py.Group))
self.assertEqual(len(f.attrs.keys()), 0)
# verify that trying to modify the file fails
try:
f.create_group("another_subgrp")
self.assertTrue(False) # expect exception
except (IOError, OSError, ValueError):
# h5py throws ValueError
# h5pyd is throwing IOError
pass
try:
f.attrs["foo"] = "bar"
self.assertTrue(False) # expect exception
except (IOError, OSError):
pass
self.assertTrue(f.modified + 30.0 > now)
self.assertTrue(f.modified >= f.created)
if is_hsds:
# owner prop is just for HSDS
self.assertTrue(len(f.owner) > 0)
version = f.serverver
# server version should be of form "n.n.n"
n = version.find(".")
self.assertTrue(n>=1)
limits = f.limits
for k in ('min_chunk_size', 'max_chunk_size', 'max_request_size', 'max_chunks_per_request'):
self.assertTrue(k in limits)
r = f['/']
self.assertTrue(isinstance(r, h5py.Group))
self.assertEqual(len(f.attrs.keys()), 0)
# flush any pending changes - this would be called by f.close() internally,
# but try here to confirm it can be called explicitly
f.flush()
f.close()
self.assertEqual(f.id.id, 0)
# re-open as read-write
f = h5py.File(filename, 'w')
self.assertTrue(f.id.id is not None)
self.assertEqual(f.mode, 'r+')
self.assertEqual(len(f.keys()), 0)
root_grp = f['/']
#f.create_group("subgrp")
from distutils.version import LooseVersion
from .compat import ChainMap, OrderedDict, unicode
from .attrs import Attributes
from .dimensions import Dimensions
from .utils import Frozen
try:
import h5pyd
except ImportError:
no_h5pyd = True
h5_group_types = (h5py.Group,)
else:
no_h5pyd = False
h5_group_types = (h5py.Group, h5pyd.Group)
__version__ = '0.8.1'
_NC_PROPERTIES = (u'version=2,h5netcdf=%s,hdf5=%s,h5py=%s'
% (__version__, h5py.version.hdf5_version, h5py.__version__))
NOT_A_VARIABLE = b'This is a netCDF dimension but not a netCDF variable.'
def _reverse_dict(dict_):
return dict(zip(dict_.values(), dict_.keys()))
def _join_h5paths(parent_path, child_path):
return '/'.join([parent_path.rstrip('/'), child_path.lstrip('/')])