Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7542159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:05:29+00:00 2026-05-30T08:05:29+00:00

I am parsing a binary log file. The log file is formatted as follows:

  • 0

I am parsing a binary log file. The log file is formatted as follows: every 10 bytes is a record, the first byte of the record is the record type, the next 5 bytes are a timestamp and the last 4 bytes are record type specific data.

Currently I am doing the following:

# read the input binary stream
with open(filename, mode='rb') as trace_stream:
    # create an empty list of trace records
    trace = []
    # iterate over each record in the binary stream
    for record_type, record_data in yield_record(trace_stream,
                                                 size=RECORD_LENGTH):
        # create a new record instance
        if record_type == SEN_RECORD:
            new_record = sen_record(record_data)
        elif record_type == DSP_RECORD:
            new_record = dsp_record(record_data)
        elif record_type == USO_RECORD:
            new_record = uso_record(record_data)
        elif record_type == SDM_RECORD:
            new_record = sdm_record(record_data)
        elif record_type == DOC_RECORD:
            new_record = doc_record(record_data)
        elif record_type == DAT_RECORD:
            new_record = dat_record(record_data)
        elif record_type == LAT_RECORD:
            new_record = lat_record(record_data)
        elif record_type == SWI_RECORD:
            new_record = swi_record(record_data)
        elif record_type == FTL_RECORD:
            new_record = ftl_record(record_data)

        # append this new record to our trace
        trace.append(new_record)

Where sen_record, dsp_record, uso_record etc. are all sub-classes of a generic record class

What I would like to do is the following:

# read the input binary stream
with open(filename, mode='rb') as trace_stream:
    # create an empty list of trace records
    trace = []
    # iterate over each record in the binary stream
    for record_type, record_data in yield_record(trace_stream,
                                                 size=RECORD_LENGTH):
            new_record = record(record_data)

    trace.append(new_record)

And then have the record class constructor do the work of determining what type of record it is and creating the appropriate class instances. Ideally my “main” routine shouldn’t need to know about the record types?

Is there any way to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:05:31+00:00Added an answer on May 30, 2026 at 8:05 am

    It would be simpler just to store the mapping

    record_types = {SEN_RECORD: sen_record,
                    DSP_RECORD: dsp_record,
                    USO_RECORD: uso_record,
                    SDM_RECORD: sdm_record,
                    DOC_RECORD: doc_record,
                    DAT_RECORD: dat_record,
                    LAT_RECORD: lat_record,
                    SWI_RECORD: swi_record,
                    FTL_RECORD: ftl_record}
    

    somewhere, and use that to look up the correct record type. (Note that you can do this, because classes are just objects, so you can put them in a dictionary.)

    Specifically, you’d do

    new_record = record_types[record_type](record_data)
    

    There are more complicated ways of doing this (say if you wanted the subclasses to be created dynamically and automatically registered with their superclass upon creation), but there’s no need to employ them in your situation.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing a binary file format. It encodes an integer using four bytes in
I am reading a binary file into a parsing program. I will need to
Does anybody recommend a design pattern for taking a binary data file, parsing parts
I am parsing a binary file using a specification. The file comes in big-endian
MySQL saves all changes to the database in a binary file called binary log.
I am parsing binary file. File size can be large. I want to search
I am parsing a binary protocol which has UTF-8 strings interspersed among raw bytes.
I'm having some difficulties while parsing a binary STL file with Python (2.7.1 32-bit
I'm parsing a binary file in javascript that is storing two pieces of information
I'm was messing around with some parsing of a binary file when I came

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.