diff --git a/h5json/hdf5db.py b/h5json/hdf5db.py index 6a5cc16..31192fc 100644 --- a/h5json/hdf5db.py +++ b/h5json/hdf5db.py @@ -1198,7 +1198,7 @@ def getAttributeItemByObj(self, obj, name, includeData=True): # todo - don't include data for OPAQUE until JSON serialization # issues are addressed - if type(typeItem) == dict and typeItem["class"] in ("H5T_OPAQUE"): + if isinstance(typeItem, dict) and typeItem["class"] in ("H5T_OPAQUE"): includeData = False shape_json = self.getShapeItemByAttrObj(attrObj) @@ -1461,7 +1461,7 @@ def makeAttribute(self, obj, attr_name, shape, attr_type, value): strPad = None strLength = 0 if ( - type(attr_type) == dict + isinstance(attr_type, dict) and attr_type["class"] == "H5T_STRING" and "strPad" in attr_type ): @@ -1470,7 +1470,7 @@ def makeAttribute(self, obj, attr_name, shape, attr_type, value): if ( rank == 0 - and type(strLength) == int + and isinstance(strLength, int) and strPad == "H5T_STR_NULLTERM" ): self.makeNullTermStringAttribute(obj, attr_name, strLength, value) @@ -1693,7 +1693,7 @@ def getRefValue(self, typeItem: dict, value: list): self.log.info(msg) raise IOError(errno.ENINVAL, msg) - if type(out) == list: + if isinstance(out, list): out = tuple(out) # convert to tuple return out @@ -1926,11 +1926,11 @@ def listToRef(self, data): else: out = obj_ref - elif type(data) in (list, tuple): + elif isinstance(data, (list, tuple)): out = [] for item in data: out.append(self.listToRef(item)) # recursive call - elif type(data) == dict: + elif isinstance(data, dict): # assume region ref out = self.createRegionReference(data) else: @@ -2096,7 +2096,7 @@ def createRegionReference(self, item): self.log.info(msg) raise IOError(errno.EINVAL, msg) start = slab[0] - if type(start) == list: + if isinstance(start, list): start = tuple(start) if type(start) is not tuple or len(start) != rank: msg = "selection value not valid, start element should have number " @@ -2104,7 +2104,7 @@ def createRegionReference(self, item): self.log.info(msg) raise IOError(errno.EINVAL, msg) stop = slab[1] - if type(stop) == list: + if isinstance(stop, list): stop = tuple(stop) if type(stop) is not tuple or len(stop) != rank: msg = "selection value not valid, count element should have number " @@ -2191,12 +2191,12 @@ def getDatasetValuesByUuid(self, obj_uuid, slices=Ellipsis, format="json"): if val is None: self.log.warning("no value returned from scalar dataset") - if type(slices) != list and type(slices) != tuple and slices is not Ellipsis: + if not isinstance(slices, (list, tuple)) and slices is not Ellipsis: msg = "Unexpected error: getDatasetValuesByUuid: bad type for dim parameter" self.log.error(msg) raise IOError(errno.EIO, msg) - if (type(slices) == list or type(slices) == tuple) and len(slices) != rank: + if isinstance(slices, (list, tuple)) and len(slices) != rank: msg = "Unexpected error: getDatasetValuesByUuid: number of dims in selection not same as rank" self.log.error(msg) raise IOError(errno.EIO, msg) @@ -2507,7 +2507,7 @@ def setDatasetValuesByUuid(self, obj_uuid, data, slices=None, format="json"): slices.append(s) slices = tuple(slices) - if type(slices) != tuple: + if not isinstance(slices, tuple): msg = "setDatasetValuesByUuid: bad type for dim parameter" self.log.error(msg) raise IOError(errno.EIO, msg) diff --git a/h5json/hdf5dtype.py b/h5json/hdf5dtype.py index 5b5dab7..9f867f2 100755 --- a/h5json/hdf5dtype.py +++ b/h5json/hdf5dtype.py @@ -59,7 +59,7 @@ def getTypeResponse(typeItem): response = {} # otherwise, return full type for k in typeItem.keys(): if k == "base": - if type(typeItem[k]) == dict: + if isinstance(typeItem[k], dict): response[k] = getTypeResponse(typeItem[k]) # recursive call else: response[k] = typeItem[k] # predefined type @@ -89,7 +89,7 @@ def getItemSize(typeItem): raise TypeError("Invalid Type") # none of the expect primative types mathched raise TypeError("Invalid Type") - if type(typeItem) != dict: + if not isinstance(typeItem, dict): raise TypeError("invalid type") item_size = 0 @@ -143,7 +143,7 @@ def getItemSize(typeItem): raise KeyError("no 'field' elements provided") # add up the size of each sub-field for field in fields: - if type(field) != dict: + if not isinstance(field, dict): raise TypeError("Expected dictionary type for field") if "type" not in field: raise KeyError("'type' missing from field") @@ -210,7 +210,7 @@ def getTypeItem(dt): # # check for h5py variable length extension vlen_check = check_dtype(vlen=dt.base) - if vlen_check is not None and type(vlen_check) != np.dtype: + if vlen_check is not None and not isinstance(vlen_check, np.dtype): vlen_check = np.dtype(vlen_check) ref_check = check_dtype(ref=dt.base) if vlen_check == bytes: @@ -364,7 +364,7 @@ def createBaseDataType(typeItem): dtRet = np.dtype(dtName) return dtRet # return predefined type - if type(typeItem) != dict: + if not isinstance(typeItem, dict): raise TypeError("Type Error: invalid type") if "class" not in typeItem: @@ -402,7 +402,7 @@ def createBaseDataType(typeItem): raise TypeError("unexpected 'charSet' value") else: nStrSize = typeItem["length"] - if type(nStrSize) != int: + if not isinstance(nStrSize, int): raise TypeError("expecting integer value for 'length'") type_code = None if typeItem["charSet"] == "H5T_CSET_ASCII": @@ -449,7 +449,7 @@ def createBaseDataType(typeItem): dt_base = createDataType(arrayBaseType) - if type(typeItem["dims"]) == int: + if isinstance(typeItem["dims"], int): dims = typeItem["dims"] # make into a tuple elif type(typeItem["dims"]) not in (list, tuple): raise TypeError("expected list or integer for dims") @@ -518,7 +518,7 @@ def createDataType(typeItem): dtRet = np.dtype(dtName) return dtRet # return predefined type - if type(typeItem) != dict: + if not isinstance(typeItem, dict): raise TypeError("invalid type") if "class" not in typeItem: @@ -536,7 +536,7 @@ def createDataType(typeItem): subtypes = [] for field in fields: - if type(field) != dict: + if not isinstance(field, dict): raise TypeError("Expected dictionary type for field") if "name" not in field: raise KeyError("'name' missing from field") diff --git a/h5json/jsontoh5/jsontoh5.py b/h5json/jsontoh5/jsontoh5.py index b9b015f..c12d037 100755 --- a/h5json/jsontoh5/jsontoh5.py +++ b/h5json/jsontoh5/jsontoh5.py @@ -68,13 +68,13 @@ def createDataset(self, uuid, body): shape = body["shape"] if shape["class"] == "H5S_SIMPLE": dims = shape["dims"] - if type(dims) == int: + if isinstance(dims, int): # convert int to array dim1 = shape dims = [dim1] if "maxdims" in shape: max_shape = shape["maxdims"] - if type(max_shape) == int: + if isinstance(max_shape, int): # convert to array dim1 = max_shape max_shape = [dim1] @@ -114,7 +114,7 @@ def createAttribute(self, attr_json, col_name, uuid): shape = attr_json["shape"] if shape["class"] == "H5S_SIMPLE": dims = shape["dims"] - if type(dims) == int: + if isinstance(dims, int): # convert int to array dim1 = shape dims = [dim1]