Skip to content

Commit

Permalink
Fixed analog __bytes__ issue.
Browse files Browse the repository at this point in the history
  • Loading branch information
mcferrill committed Mar 24, 2021
1 parent a079f9a commit 60a3ffb
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 35 deletions.
58 changes: 28 additions & 30 deletions chapter10/analog.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from .util import BitFormat
from . import packet

from bitstring import Bits


class AnalogF1(packet.Packet):
"""
Expand Down Expand Up @@ -35,26 +37,21 @@ class AnalogF1(packet.Packet):
"""

# u4 factor
# u1 same
# p3

# u6 length
# u2 mode
csdw_format = BitFormat('''
u6 length
u2 mode
# u8 subchannel_count
u8 subchannel
u8 subchannel_count
# u8 subchannel
csdw_format = BitFormat('''
u8 one
u8 two
u8 three
u8 four
p3
u1 same
u4 factor
''')

class Message(packet.Message):
def __repr__(self):
return '<Analog Sample %s bytes>' % len(self.data)
return '<Analog Sample %s bits>' % len(self.length)

@classmethod
def from_packet(cls, packet):
Expand All @@ -66,21 +63,21 @@ def from_packet(cls, packet):

# Find the sample size in bytes (16 bit aligned).
length = csdw['length'] or 64 # Length 0 = 64 bits
length = length // 8 + (length % 16 and 1 or 0)

data = packet.buffer.read(length)
if len(data) < length:
if packet.bit_offset > len(packet.data):
raise EOFError

# Account for filler byte when length is odd.
if length % 2:
packet.buffer.seek(1, 1)
data = packet.data[packet.bit_offset:packet.bit_offset + length]
packet.bit_offset += length

packet._subchannel += 1
if packet._subchannel >= len(packet.subchannels):
packet._subchannel = 0

return packet.Message(data, parent=packet, **csdw)
return packet.Message(data.bytes, parent=packet, **csdw)

def __bytes__(self):
return self.data

def __init__(self, *args, **kwargs):
packet.Packet.__init__(self, *args, **kwargs)
Expand All @@ -101,18 +98,19 @@ def __init__(self, *args, **kwargs):

# Read CSDWs for additional subchannels if applicable.
if not self.same:
for i in range(self.subchannel_count):
raw = self.buffer.read(4)
print('parse: ', raw)
# assert 0
for i in range(self.subchannel_count - 1):
self.subchannels.append(
self.csdw_format.unpack(raw))
self.csdw_format.unpack(self.buffer.read(4)))

# Get the raw bytes of data since samples are specified in bit length
raw = self.buffer.read(
self.data_length - (4 * len(self.subchannels)))
self.data = Bits(raw)
self.bit_offset = 0

def _raw_body(self):
raw = bytes()
for channel in self.subchannels:
print(channel)
csdw = self.csdw_format.pack(channel)
raw += csdw
raw += b''.join([bytes(msg) for msg in list(self)])
raw += self.csdw_format.pack(channel)
raw += b''.join(bytes(msg) for msg in self._messages)
return raw
4 changes: 1 addition & 3 deletions chapter10/packet.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,7 @@ def __init__(self, file=None, parent=None, **kwargs):
self.validate()

# Read channel specific data word (CSDW)
csdw = self.csdw_format.unpack(self.buffer.read(4))
print(csdw)
self.__dict__.update(csdw)
self.__dict__.update(self.csdw_format.unpack(self.buffer.read(4)))

def get_time(self):
"""Return a timestamp for this packet. Depends on parent C10 object."""
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def run(self):
description='A parser library for the IRIG 106 Chapter 10 data format.',
long_description=open('README.rst').read(),
url='https://github.com/atac/pychapter10',
install_requires=['bitstruct>=8.11.0'],
install_requires=['bitstruct>=8.11.0', 'bitstring>=3.1.7'],
packages=['chapter10'],
cmdclass=cmdclass,
python_requires='>=py3.6',
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_analog.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_count(packet):

def test_next(packet):
for i, sample in enumerate(packet):
assert sample.data == b'\xfc\xfc'
assert sample.data == b'\xfc'
if i >= 2:
break

Expand Down

0 comments on commit 60a3ffb

Please sign in to comment.