Skip to content

Instantly share code, notes, and snippets.

@benwilber
Last active August 29, 2015 13:58
Show Gist options
  • Save benwilber/9986083 to your computer and use it in GitHub Desktop.
Save benwilber/9986083 to your computer and use it in GitHub Desktop.
import os
import struct
from io import BytesIO
from pycoin.block import Block
def read_uint32(fd):
return struct.unpack(b'<I', fd.read(4))[0]
def parse_file(block_file):
basename = os.path.basename(block_file)
with open(block_file, 'rb') as fd:
fd.seek(-1, os.SEEK_END)
file_end = fd.tell()
fd.seek(0)
while fd.tell() < file_end:
# Skip the magic bytes
fd.seek(4, os.SEEK_CUR)
position = fd.tell()
block_length = read_uint32(fd)
bio = BytesIO(fd.read(block_length))
yield basename, position, block_length, Block.parse(bio)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment