Skip to content

Commit

Permalink
Merge pull request #4090 from t20100/use-mappingproxytype
Browse files Browse the repository at this point in the history
silx.io.commonh5: Improved code to use built-in types
  • Loading branch information
payno authored Apr 18, 2024
2 parents ac05280 + b2aa970 commit e3b2acb
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 48 deletions.
47 changes: 3 additions & 44 deletions src/silx/io/commonh5.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# /*##########################################################################
# Copyright (C) 2016-2023 European Synchrotron Radiation Facility
# Copyright (C) 2016-2024 European Synchrotron Radiation Facility
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Expand All @@ -24,7 +24,7 @@
This module contains generic objects, emulating *h5py* groups, datasets and
files. They are used in :mod:`spech5` and :mod:`fabioh5`.
"""
from collections import abc
from types import MappingProxyType
import weakref

import h5py
Expand All @@ -37,47 +37,6 @@
__date__ = "02/07/2018"


class _MappingProxyType(abc.MutableMapping):
"""Read-only dictionary
This class is available since Python 3.3, but not on earlyer Python
versions.
"""

def __init__(self, data):
self._data = data

def __getitem__(self, key):
return self._data[key]

def __len__(self):
return len(self._data)

def __iter__(self):
return iter(self._data)

def get(self, key, default=None):
return self._data.get(key, default)

def __setitem__(self, key, value):
raise RuntimeError("Cannot modify read-only dictionary")

def __delitem__(self, key):
raise RuntimeError("Cannot modify read-only dictionary")

def pop(self, key):
raise RuntimeError("Cannot modify read-only dictionary")

def clear(self):
raise RuntimeError("Cannot modify read-only dictionary")

def update(self, key, value):
raise RuntimeError("Cannot modify read-only dictionary")

def setdefault(self, key):
raise RuntimeError("Cannot modify read-only dictionary")


class Node(object):
"""This is the base class for all :mod:`spech5` and :mod:`fabioh5`
classes. It represents a tree node, and knows its parent node
Expand Down Expand Up @@ -173,7 +132,7 @@ def attrs(self):
if self._is_editable():
return self.__attrs
else:
return _MappingProxyType(self.__attrs)
return MappingProxyType(self.__attrs)

@property
def name(self):
Expand Down
5 changes: 1 addition & 4 deletions src/silx/io/test/test_commonh5.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,8 @@ def test_node_readonly_attrs(self):
node.attrs["b"] = 8
f.add_node(node)
self.assertEqual(node.attrs["b"], 8)
try:
with self.assertRaises(TypeError):
node.attrs["b"] = 1
self.fail()
except RuntimeError:
pass

def test_create_dataset(self):
f = commonh5.File(name="Foo", mode="w")
Expand Down

0 comments on commit e3b2acb

Please sign in to comment.