📄 mfbox_xmlparse.py
字号:
} def __init__(self): d = {} for key, (sm, em) in self.unmarshal_meth.items(): if sm is not None: sm = getattr(self, sm) if em is not None: em = getattr(self, em) d[key] = sm,em self.unmarshal_meth = d self._clear() def _clear(self): self.data_stack = [] self.dict = {} self.accumulating_chars = 0 def load(self, file): m = self.__class__() return m._load(file) def loads(self, string): m = self.__class__() import StringIO file = StringIO.StringIO(string) return m._load(file) def _load(self, file): p = sax.make_parser() p.setContentHandler(self) p.parse(file) assert len(self.data_stack) == 1 result = self.data_stack[0] self._clear() if result.__class__ is nameddata: return result.data() return result def startElement(self, name, attrs): sm, em = self.unmarshal_meth[name] if sm is not None: return sm(name,attrs) def characters(self, ch): if self.accumulating_chars: self.data_stack[-1].append(ch) def endElement(self, name): sm, em = self.unmarshal_meth[name] if em is not None: em(name) def setDocumentLocator(self, locator): pass def startDocument(self): pass def endDocument(self): pass def startPrefixMapping(self, prefix, uri): pass def endPrefixMapping(self, prefix): pass def startElementNS(self, name, qname, attrs): if not name[0]: return self.startElement(name[1],attrs) def endElementNS(self, name, qname): if not name[0]: return self.endElement(name[1]) def ignorableWhitespace(self, whitespace): pass def processingInstruction(self, target, data): pass def skippedEntity(self, name): pass def um_start_root(self, name, attrs): if self.dict or self.data_stack: raise ValueError, \ "root element %s found elsewhere than root" \ % repr(name) def um_start_struct(self, name, attrs): self.data_stack.append(attrs) self.data_stack.append(STRUCT) size = [0,0] if attrs.has_key('size'): size = map(int,attrs['size'].split()) L = struct(*size) self.data_stack.append(L) def um_start_cell(self, name, attrs): self.data_stack.append(attrs) self.data_stack.append(CELL) size = [0,0] if attrs.has_key('size'): size = map(int,attrs['size'].split()) L = cell(*size) self.data_stack.append(L) def um_start_matrix(self, name, attrs): self.data_stack.append(attrs) self.data_stack.append([]) self.accumulating_chars = 1 um_start_float = um_start_int = um_start_bool = um_start_matrix def um_start_string(self, name, attrs): self.data_stack.append(attrs) self.data_stack.append([]) self.accumulating_chars = 1 def um_end_string(self, name): ds = self.data_stack data = str(string.join(ds[-1], "")) attrs = ds[-2] name = 'ans' if attrs.has_key('name'): name = attrs['name'] ds[-2:] = [nameddata(name,decode(data))] self.accumulating_chars = 0 def um_end_matrix(self, name): ds = self.data_stack data = str(string.join(ds[-1], "")).split() attrs = ds[-2] cls = str(name if not name=='logical' else 'bool') name = 'ans' size = [len(data),1 if len(data)>0 else 0] if attrs.has_key('name'): name = attrs['name'] if attrs.has_key('size'): s = map(int,attrs['size'].split()) l = reduce(lambda x,y:x*y,s) if l==len(data): size = s l = reduce(lambda x,y:x*y,size) cplex = False for i in range(0,len(data)): data[i] = parsefloat(data[i]) if type(data[i]) is complex: cplex = True if cplex: cls = 'complex' d = numpy.zeros(l,numpy.dtype(cls)) for i in range(0,len(data)): d[i] = data[i] if len(d)==1: d = d[0] t = str(type(d)) if t.find('numpy.int')>0: d = long(d) elif t.find('numpy.float')>0: d = float(d) elif t.find('numpy.complex')>0: d = complex(d) elif t.find('numpy.bool')>0: d = bool(d) elif len(d)==0: d = None else: d = d.reshape(size) ds[-2:] = [nameddata(name,d)] self.accumulating_chars = 0 um_end_float = um_end_int = um_end_bool = um_end_matrix def um_end_cell(self, name): ds = self.data_stack for index in range(len(ds)-1, -1, -1): if ds[index] is CELL: break assert index != -1 attrs = ds[index-1] name = 'ans' if attrs.has_key('name'): name = attrs['name'] L = ds[index+1] d = ds[(index+2):len(ds)] for i in range(0,len(d)): if d[i].__class__ is nameddata: L[i] = d[i].data() else: L[i] = d[i] if len(L.size)==2 and L.size[0]==1: L = [x for x in L] ds[(index-1):] = [nameddata(name,L)] def um_end_struct(self, name): ds = self.data_stack for index in range(len(ds)-1, -1, -1): if ds[index] is STRUCT: break assert index != -1 attrs = ds[index-1] name = 'ans' if attrs.has_key('name'): name = attrs['name'] L = ds[index+1] d = ds[(index+2):len(ds)] fn = set() for i in range(0,len(d)): if d[i].__class__ is nameddata: fn.add(d[i].name()) else: fn.add('ans') d[i] = nameddata('ans',d[i]) size = [0,0] if len(fn)==0 else [1,1] if attrs.has_key('size'): size = map(int,attrs['size'].split()) l = reduce(lambda x,y:x*y,size) if len(d)==(l*len(fn)): if l>1: k = 0 for i in range(0,l): c = {} for j in range(0,len(fn)): c[str(d[k].name())] = d[k].data() k += 1 L[i] = c else: c = {} for j in range(0,len(fn)): c[str(d[j].name())] = d[j].data() L = c ds[(index-1):] = [nameddata(name,L)]class MfboxXmlMarshal: tag_dictionary = 'struct' tag_struct = 'struct' tag_list = 'cell' tag_cell = 'cell' tag_float = 'double' tag_matrix = 'double' tag_none = 'double' tag_int = 'int32' tag_string = 'char' tag_boolean = 'logical' PROLOGUE = '<?xml version="1.0" encoding="UTF-8"?>' def dump(self, value, file): dict = {'id': 1} L = [self.PROLOGUE] + self.m_root(value, dict) file.write(string.join(L, "")) def dumps(self, value): dict = {'id': 1} L = [self.PROLOGUE] + self.m_root(value, dict) return string.join(L, "") def _marshal(self, value, dic, name="ans"): t = type(value) meth = 'm_none' if value.__class__ is struct: meth = 'm_struct' elif value.__class__ is cell: meth = 'm_cell' elif type(value) is dict: meth = 'm_dictionary' elif type(value) is list or type(value) is tuple: meth = 'm_list' elif type(value) is float or type(value) is complex: meth = 'm_float' elif type(value) is int or type(value) is long: meth = 'm_int' elif type(value) is str: meth = 'm_string' elif type(value) is bool: meth = 'm_boolean' elif type(value) is numpy.ndarray: meth = 'm_matrix' return getattr(self, meth)(value, dic, name) def m_root(self, value, dict, name="ans"): L = ['<mfbox xmlns="http://mfbox.sf.net/mfbox">'] L.extend(self._marshal(value,dict)) L.append('</mfbox>') return L def m_dictionary(self, value, dict, name="ans"): tag = self.tag_dictionary L = [] dict['id'] = dict['id'] + 1 i = str(dict['id']) dict[str(id(value))] = i dict[i] = value L.append("<%s name=\"%s\" id=\"i%s\" size=\"1 1\">" % (tag,name,i)) items = value.items() for key, v in items: zl = self._marshal(v, dict, key) L = L + zl L.append("</%s>"%(tag,)) return L def m_struct(self, value, dict, name="ans"): tag = self.tag_dictionary L = [] dict['id'] = dict['id'] + 1 i = str(dict['id']) dict[str(id(value))] = i dict[i] = value s = string.join(map(str,value.__size__())," ") L.append("<%s name=\"%s\" id=\"i%s\" size=\"%s\">" % (tag,name,i,s)) for t in value: for key in sorted(t.keys()): zl = self._marshal(t[key], dict, key) L = L + zl L.append("</%s>"%(tag,)) return L def m_none(self, value, dict, name="ans"): tag = self.tag_none dict['id'] = dict['id'] + 1 i = str(dict['id']) dict[str(id(value))] = i dict[i] = value return ["<%s name=\"%s\" id=\"i%s\" size=\"0 0\"></%s>"%(tag,name,i,tag)] def m_cell(self, value, dict, name="ans"): tag = self.tag_cell L = [] dict['id'] = dict['id'] + 1 i = str(dict['id']) dict[str(id(value))] = i dict[i] = value s = string.join(map(str,value.size)," ") L.append("<%s name=\"%s\" id=\"i%s\" size=\"%s\">" % (tag,name,i,s)) for v in value: zl = self._marshal(v, dict) L = L + zl L.append("</%s>"%(tag,)) return L def m_list(self, value, dict, name="ans"): tag = self.tag_list L = [] dict['id'] = dict['id'] + 1 i = str(dict['id']) dict[str(id(value))] = i dict[i] = value l = len(value) L.append("<%s name=\"%s\" id=\"i%s\" size=\"1 %d\">" % (tag,name,i,l)) for v in value: zl = self._marshal(v, dict) L = L + zl L.append("</%s>"%(tag,)) return L def m_float(self, value, dict, name="ans"): tag = self.tag_float L = [] dict['id'] = dict['id'] + 1 i = str(dict['id']) dict[str(id(value))] = i dict[i] = value if type(value) is complex: v = "%e+%ei"%(value.real,value.imag) else: v = "%e"%(value,) L.append("<%s name=\"%s\" id=\"i%s\" size=\"1 1\">%s</%s>" % (tag,name,i,v,tag)) return L def m_int(self, value, dict, name="ans"): tag = self.tag_int L = [] dict['id'] = dict['id'] + 1 i = str(dict['id']) dict[str(id(value))] = i dict[i] = value v = "%d"%(value,) L.append("<%s name=\"%s\" id=\"i%s\" size=\"1 1\">%s</%s>" % (tag,name,i,v,tag)) return L def m_string(self, value, dict, name="ans"): tag = self.tag_string L = [] dict['id'] = dict['id'] + 1 i = str(dict['id']) dict[str(id(value))] = i dict[i] = value v = encode(value) L.append("<%s name=\"%s\" id=\"i%s\" size=\"1 %d\">%s</%s>" % (tag,name,i,len(value),v,tag)) return L def m_boolean(self, value, dict, name="ans"): tag = self.tag_boolean L = [] dict['id'] = dict['id'] + 1 i = str(dict['id']) dict[str(id(value))] = i dict[i] = value v = "%d"%(value,) L.append("<%s name=\"%s\" id=\"i%s\" size=\"1 1\">%s</%s>" % (tag,name,i,v,tag)) return L def m_matrix(self, value, dict, name="ans"): tag = self.tag_float if str(value.dtype)=='int32' or str(value.dtype)=='int16' or str(value.dtype)=='int8': tag = 'int32' elif str(value.dtype)=='bool': tag = 'logical' L = [] dict['id'] = dict['id'] + 1 i = str(dict['id']) dict[str(id(value))] = i dict[i] = value v = ("%e+%ei"%(x.real,x.imag) if type(x) is complex else "%e"%(x,) for x in value.transpose().flatten().tolist()) v = string.join(v," ") s = string.join(map(str,value.shape)," ") L.append("<%s name=\"%s\" id=\"i%s\" size=\"%s\">%s</%s>" % (tag,name,i,s,v,tag)) return L
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -