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

  • SEARCH
  • Home
  • 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 229219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:44:51+00:00 2026-05-11T19:44:51+00:00

Here is a contrived example of how a lot of our classes return binary

  • 0

Here is a contrived example of how a lot of our classes return binary representations (to be read by C++) of themselves.

def to_binary(self):
    'Return the binary representation as a string.'
    data = []

    # Binary version number.
    data.append(struct.pack('<I', [2]))

    # Image size.
    data.append(struct.pack('<II', *self.image.size))

    # Attribute count.
    data.append(struct.pack('<I', len(self.attributes)))

    # Attributes.
    for attribute in self.attributes:

        # Id.
        data.append(struct.pack('<I', attribute.id))

        # Type.
        data.append(struct.pack('<H', attribute.type))

        # Extra Type.        
        if attribute.type == 0:
            data.append(struct.pack('<I', attribute.typeEx))

    return ''.join(data)

What I dislike:

  • Every line starts with data.append(struct.pack(, distracting from the unique part of the line.
  • The byte order ('<') is repeated over and over again.
  • You have to remember to return the boilerplate ''.join(data).

What I like:

  • The format specifiers appear near the attribute name. E.g., it’s easy to see that self.image.size is written out as two unsigned ints.
  • The lines are (mostly) independent. E.g., To remove the Id field from an ‘attribute’, you don’t have to touch more than one line of code.

Is there a more readable/pythonic way to do this?

  • 1 1 Answer
  • 1 View
  • 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-11T19:44:51+00:00Added an answer on May 11, 2026 at 7:44 pm
    from StringIO import StringIO
    import struct
    
    class BinaryIO(StringIO):
        def writepack(self, fmt, *values):
            self.write(struct.pack('<' + fmt, *values))
    
    def to_binary_example():
        data = BinaryIO()
        data.writepack('I', 42)
        data.writepack('II', 1, 2)
        return data.getvalue()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In python, you can have a function return multiple values. Here's a contrived example:
Here's a contrived example of my code: <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN
To use a contrived example in Java, here's the code: enum Commands{ Save(S); File(F);
Here is a contrived example of an xml document. In my real world case,
Here is my contrived example that illustrates what I am attempting to accomplish. I
So here's a contrived example of what I want to have improved or confirmed.
This is a somewhat contrived example meant to show a point. Here are two
Here's an contrived example of what's going on: http://jsfiddle.net/adamjford/YNGcm/20/ HTML: <a href=#>Click me!</a> <div></div>
Here is a rather contrived example of what I am asking: public partial class
Here is a complete example. I want to forbid using A::set from objects casted

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.