赞
踩
#! /usr/bin/env python #! -*- coding:utf-8 -*- #mybuffer.py class myBuffer: '''定义内存缓存操作类''' import struct as _stc import cStringIO as _cio #---------------------------------- class _asInt4: '''作为子类,将指定位置的数据作为整型值读出或写入''' def __init__(self): pass #------------------- def __getitem__(self,pos): myBuffer._buf.seek(pos,0) i= myBuffer._buf.read(4) if (len(i)==4): ii=myBuffer._stc.unpack('l',i) return ii[0] else: i=i+'/x00/x00/x00/x00' ii=myBuffer._stc.unpack('l',i[0:4]) return ii[0] #------------------- def __setitem__(self,pos,value): myBuffer._buf.seek(0,2) l= myBuffer._buf.tell() if (l > pos): myBuffer._buf.seek(pos,0) myBuffer._buf.write(myBuffer._stc.pack('l',value)) #-------------------- _buf=_cio.StringIO() asInt=_asInt4() #---------------------------------- def __init__(self,buf1): self._buf.seek(0,0) self._buf.write(buf1) #---------------------------------- def __getattr__(self,name): if (name == 'asInt2'): i=self._buf.read(4) if (len(i)==4): ii=myBuffer._stc.unpack('l',i) return ii[0] else: i=i+'/x00/x00/x00/x00' ii=myBuffer._stc.unpack('l',i[0:4]) return ii[0] else: raise (AttributeError,name) #---------------------------------- def __setattr__(self,name,value): if (name=='asInt2'): self._buf.write(self._stc.pack('l',value)) else: raise (AttributeError,name) #---------------------------------- def __getitem__(self,pos): self._buf.seek(0,2) l= self._buf.tell() if (l > pos): self._buf.seek(pos,0) self._buf.seek(pos,0) return self |
>>> import mybuffer >>> a=mybuffer.myBuffer('/x01') >>> a.asInt[0] 1 >>> a[0].asInt2 1 >>> a.asInt[0]=3 >>> a.asInt[0] 3 >>> a[0].asInt2 3 >>> a[0].asInt2=9 >>> a.asInt[0] 9 >>> |
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。