[Wisp-cvs] wisp/users/dig bits.py,1.1,1.2
Status: Alpha
Brought to you by:
digg
From: <di...@us...> - 2003-04-26 20:56:59
|
Update of /cvsroot/wisp/wisp/users/dig In directory sc8-pr-cvs1:/tmp/cvs-serv3871 Modified Files: bits.py Log Message: introduced Bits.__getitem__ Index: bits.py =================================================================== RCS file: /cvsroot/wisp/wisp/users/dig/bits.py,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- bits.py 26 Apr 2003 17:36:17 -0000 1.1 +++ bits.py 26 Apr 2003 20:56:56 -0000 1.2 @@ -8,23 +8,23 @@ from array import array from struct import pack, unpack +from types import SliceType class Bits (object): - __slots__ = ['_contents', '_skipped'] + __slots__ = ['_contents', '_skipped', '_byte_order', + 'emit_wyde', 'emit_tetra'] def __init__ (this, byte_order = None): this._contents = array('c') if byte_order == None: pass elif byte_order == '<': this.emit_wyde = this.emit_lewyde this.emit_tetra = this.emit_letetra - this.add_wyde = this.add_lewyde - this.add_tetra = this.add_letetra elif byte_order == '>': this.emit_wyde = this.emit_bewyde this.emit_tetra = this.emit_betetra - this.add_wyde = this.add_bewyde - this.add_tetra = this.add_betetra else: raise "Unknown byte order", byte_order + this._skipped = 0 + this._byte_order = byte_order def emit_byte (this, b): if this._skipped <> 0: raise "Events out of order", this this._contents.append(chr(b & 0xff)) @@ -51,9 +51,19 @@ datum, = unpack(tpl, this._contents[ofs : ofs + size]) datum += value datum %= 1L << (size << 3) - this._contents[ofs : ofs + size] = array('c', pack(tpl, datum)) - def add_lewyde (this, ofs, value): this._add(ofs, value, 2, '<H') - def add_bewyde (this, ofs, value): this._add(ofs, value, 2, '>H') - def add_letetra (this, ofs, value): this._add(ofs, value, 4, '<L') - def add_betetra (this, ofs, value): this._add(ofs, value, 4, '>L') + this._contents[ofs : ofs + size] = \ + array('c', pack(this._byte_order + tpl, datum)) + def add_wyde (this, ofs, value): this._add(ofs, value, 2, 'H') + def add_tetra (this, ofs, value): this._add(ofs, value, 4, 'L') + def __getitem__ (this, index): + if isinstance(index, SliceType): + if index.start == None or index.stop != None or \ + not index.step in (1, 2, 4): + raise 'Bad slice for Bits.__getitem__', index + if index.step == 1: return ord(this._contents[index.start]) + else: + data = this._contents[index.start : index.start + index.step] + tpl = this._byte_order + {2: 'H', 4: 'L'}[index.step] + return unpack(tpl, data) + else: return ord(this._contents[index]) |