def others(self, other, name='other'): '''Check this and an other instance for type compatiblility. @param other: The other instance (any). @keyword name: Optional, name for other (string). @return: None. @raise TypeError: Mismatch of this and type(other). ''' if not (isinstance(other, self.__class__) or (hasattr(other, 'lat') and hasattr(other, 'lon'))): raise TypeError('type(%s) mismatch: %s vs %s' % (name, classname(other), classname(self)))
def writeMember(self, obj, memberName): if isString(obj): logging.warning(u"String as object provided! " + self._warningPrefix(obj, memberName)) if isInteger(memberName) and isList(obj): member = obj[memberName] memberName = str(memberName) else: member = getattr(obj, memberName, None) if member is None: self.log(u"skipped " + self._warningPrefix(obj, memberName) + u"It is empty or does not exist (=None).") return if isCallable(member) and not hasattr(member, "hdfWrite"): member = member() if hasattr(member, "hdfWrite"): # support instances and types # store the member in a group of its own oldLocation = self.location self._location = "/".join((oldLocation.rstrip('/'), memberName)) member.hdfWrite(self) # recursion entry, mind the loops! self._location = oldLocation elif isList(member): self.writeDataset(memberName, member) elif isString(member) or isNumber(member): self.writeAttribute(memberName, member) else: self.log(u"skipped " + self._warningPrefix(obj, memberName) + "(={}) It is not a compatible value type!".format( classname(member)))
def toStr2(self, **kwds): '''This L{LatLon_} as a string "class(<degrees>, ...)". @keyword kwds: Optional, keyword arguments. @return: Class instance (string). ''' return '%s(%s)' % (classname(self), self.toStr(**kwds))
def radius(self): '''Get the earth radius (meter). ''' r = self._radius if r < EPS: t = '%s.%s' % (classname(self), 'radius') raise ValueError('%s invalid: %r' % (t, r)) return r
def hdfWrite(self, hdf): xlst = ("onValueUpdate", ) selfAttr = self.attributes(exclude=xlst) selfAttr['cls'] = classname(selfAttr['cls']) for name in self.hdfStoreAsMember(): if name in selfAttr: selfAttr.pop(name) hdf.writeMember(self, name) hdf.writeAttributes(**selfAttr)
def writeDataset(self, name, data): if not isList(data): self.log("dataset {} is not of a list type! (={})".format( name, classname(data))) return shape = getattr(data, 'shape', "(len: {})".format(len(data))) self.log("dataset '{loc}/{name}' {shape}".format( loc=self.location.rstrip('/'), name=name, shape=shape)) writeLocation = self._writeLocation() if name in writeLocation: del writeLocation[name] try: writeLocation.create_dataset(name, data=data, compression="gzip") except TypeError: # a TypeError is raised for non-chunkable data (such as string) writeLocation.create_dataset(name, data=data)
def hdfWrite(self, hdf): hdf.writeAttributes(modelName = classname(self)) for p in self.params(): logging.debug("Writing model parameter: {} to HDF5".format(p.name())) hdf.writeMember(self, p.name())
def hdfWrite(self, hdf): hdf.writeAttributes(cls=classname(self))
def hdfWrite(self, hdf): hdf.writeAttributes(type=classname(self), displayMagnitudeName=self.displayMagnitudeName, magnitudeConversion=self.magnitudeConversion)
def hdfWrite(self, hdf): hdf.writeAttributes(name=self.name(), cls=classname(self)) hdf.writeMembers(self, *[p.name() for p in self.params()])
def _warningPrefix(self, obj, memberName): return (u"{cls}.{mem} {cal} ".format(cal=getCallerInfo(type(self)), cls=classname(obj), mem=memberName))
def log(self, msg): logging.debug(u"[{}] {}".format(classname(self), msg))