Skip to content

Commit

Permalink
Segment as named tuple.
Browse files Browse the repository at this point in the history
  • Loading branch information
eerimoq committed Jan 28, 2018
1 parent 098c4bc commit cf8fe7b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ array and hexdump formats:
bytearray(b'!F\x016\x01!G\x016\x00~\xfe\t\xd2\x19\x01!F\x01~\x17\xc2\x00\x01
\xff_\x16\x00!H\x01\x19\x19Ny#F#\x96Wx#\x9e\xda?\x01\xb2\xca?\x01Vp+^q+r+s!
F\x014!')
>>> list(f.segments)
[Segment(address=256, data=bytearray(b'!F\x016\x01!G\x016\x00~\xfe\t\xd2\x19\x01
!F\x01~\x17\xc2\x00\x01\xff_\x16\x00!H\x01\x19\x19Ny#F#\x96Wx#\x9e\xda?\x01
\xb2\xca?\x01Vp+^q+r+s!F\x014!'))]
>>> f.minimum_address
256
>>> f.maximum_address
Expand Down
16 changes: 10 additions & 6 deletions bincopy.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import string
import sys
import argparse
from collections import namedtuple

try:
from StringIO import StringIO
Expand Down Expand Up @@ -284,6 +285,8 @@ class _Segments(object):
"""

_Segment = namedtuple('Segment', ['address', 'data'])

def __init__(self):
self.current_segment = None
self.current_segment_index = None
Expand All @@ -298,7 +301,8 @@ def __iter__(self):
"""

for segment in self._list:
yield segment.minimum_address, segment.data
yield self._Segment(address=segment.minimum_address,
data=segment.data)

@property
def minimum_address(self):
Expand Down Expand Up @@ -584,13 +588,13 @@ def segments(self):
the binary.
Below is an example iterating over all segments, two in this
case, and printing their address and data:
case, and printing them.
>>> for address, data in binfile.segments:
... print('Address: {}, Data: {}'.format(address, data))
>>> for segment in binfile.segments:
... print(segment)
...
Address: 0, Data: bytearray(b'\x00')
Address: 2, Data: bytearray(b'\x01')
Segment(address=0, data=bytearray(b'\\x00'))
Segment(address=2, data=bytearray(b'\\x01'))
"""

Expand Down

0 comments on commit cf8fe7b

Please sign in to comment.