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 503653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:24:57+00:00 2026-05-13T06:24:57+00:00

I have a message definition file that looks like this struct1 { field=name type=string

  • 0

I have a message definition file that looks like this

struct1 
{
  field="name" type="string" ignore="false"; 
  field="id" type="int" enums=" 0="val1" 1="val2" ";
}

struct2
{
  field = "object" type="struct1";
  ...
}

How can I parse this into a dictionary with keys ‘struct1, struct2’ and values should be a list of dictionaries, each corresponding to the respective line number so that i can do the following

dict['struct1'][0]['type'] // Would return 'string'
dict['struct1'][1]['type'] // Would return 'int'
dict['struct1'][1]['enums']['0'] // Would return 'val1'
dict['struct2'][0]['type'] // Would return 'struct1'

and so on..

Also, I can change the format of the definition file and if any of you have suggestions on modifying the definition file format to make it easier to parse, please let me know.

Thanks

  • 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-13T06:24:58+00:00Added an answer on May 13, 2026 at 6:24 am

    I would simply use Python for the message definition file format.

    Let your message definition file be a plain Python file:

    # file messages.py
    messages = dict(
        struct1=[
            dict(field="name", type="string", ignore=False),
            dict(field="id", type="int", enums={0: "val1", 1: "val2"}),
            ],
        struct2=[
            dict(field="object", type="struct1"),
            ]
        )
    

    Your program can then import and use that data structure directly:

    # in your program
    from messages import messages
    print messages['struct1'][0]["type"]
    print messages['struct1'][1]['type']
    print messages['struct1'][1]['enums'][0]
    print messages['struct2'][0]['type']
    

    Using this approach, you let Python do the parsing for you.

    And you also gain a lot of possibilities. For instance, imagine you (for some strange reason) have a message structure with 1000 fields named “field_N”. Using a conventional file format you would have to add 1000 lines of field definitions (unless you build some looping into your config file parser – you are then on your way to creating a programming language anyway). Using Python for this purpose, you could do something like:

    messages = dict(
        ...
        strange_msg=[dict(field="field_%d" % i) for i in range(1000)],
        ...
        )
    

    BTW, on Python 2.6, using named tuples instead of dict is an option. Or use on of the numerous “Bunch” classes available (see the Python cookbook for a namedtuple for 2.5).

    EDIT:

    Below is code that reads message definition files as specified on the command line. It uses execfile instead of import.

    # file mainprogram.py
    
    def read_messages_from_file(filename):
        module_dict = {}
        execfile(filename, module_dict)
        return module_dict['messages']
    
    if __name__ == "__main__":
        from pprint import pprint
        import sys
    
        for arg in sys.argv[1:]:
            messages = read_messages_from_file(arg)
            pprint(messages)
    

    Executing:

    $ python mainprogram.py messages1 messages2 messages3
    

    will read and print the messages defined in each file.

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

Sidebar

Related Questions

I have a simple message box in a WPF application that is launched as
In a Java web application I have a recurring message load of type A
I have an incoming soap message wich form is TStream (Delphi7), server that send
I have a Perl script that pops up a message box when its work
I have a status message box (div box) positioned at the bottom of a
I have one user who gets an error message when he closes his browser.
I have been trying to encrypt soap message and send to the server, so
I have a variable last_login and want to output a message if the last
I have a User class with reference to a Message class. The message class
I have a simple GtkStatusBar created with glade,and want to add a simple message

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.