PDA

View Full Version : python problem



mohit.joshi
July 20th, 2010, 12:06 PM
typedef struct
{
unsigned int priority;
unsigned int prioritisedBitRate;
unsigned int bucketSizeDur;
unsigned int logchannelGrp;
}logicalChannelExplicitCnf_st;
these type of files... i have to change it into the files mentioned below using python in unix

class logicalChannelExplicitCnf_st(strucure) #structure comes from struct,if union i #is their then in union it should be union
_fields_=[("priority",c_int32),("prioritisedBitRate",c_int32), ("bucketSizeDur",c_int32 ),("logchannelGrp",)c_int32]


#c_int32 is used instead of unsigned int, c_char for char

mohit.joshi
July 20th, 2010, 12:09 PM
please help me or i am fired.. this is my second day and my boss has given this.. i have nevr worked on python

surfer
July 20th, 2010, 01:56 PM
this should get you started: you could extract the types and stuff with the regex module. all you have to do now is print them in the appropriate way... and check for the cases that do not match the above example.



#!/usr/bin/python
# -*- coding: utf-8 -*-

import re

c_str = '''
typedef struct
{
unsigned int priority;
unsigned int prioritisedBitRate;
unsigned int bucketSizeDur;
unsigned int logchannelGrp;
}logicalChannelExplicitCnf_st;
'''


re_typedef = re.compile( '''
(?P<typedef_struct>typedef\ struct) # header
(?P<whitespace0>[^{]) # whitespace
(?P<opening_bracket>{)
(?P<statement>[^}]*)
(?P<closing_bracket>})
(?P<class>[^;]*)
;
''',
re.MULTILINE | re.VERBOSE)

res_typedef = re_typedef.search(c_str)

print res_typedef.group('class')

stmt_strg = res_typedef.group('statement')
print stmt_strg


re_statement = re.compile( '''
(?P<type>\w+)
(?P<whitespace0>\ )
(?P<type2>\w+)
(?P<whitespace1>\ )
(?P<var_name>\w+)
;
''',
re.MULTILINE | re.VERBOSE)

res_statement = re_statement.finditer(stmt_strg)

# print res_statement.group('var_name')
for item in res_statement:
print item.group('var_name')

mohit.joshi
July 21st, 2010, 06:59 AM
thanks for the help..
but this doesnt work..

surfer
July 21st, 2010, 07:33 AM
what exactly does not work?

it does not solve your whole problem of course, but it should get you started to get there. all it does for the moment is parse the above structure. what you have to do is rearrange the output as you need it.

mohit.joshi
July 21st, 2010, 07:54 AM
actually m just a intern.. not a great programmer.. i dint get the code.. so editing it is a tedious job

mohit.joshi
August 2nd, 2010, 11:13 AM
typedef struct logicalChannelExplicitCnf_st
{
unsigned int priority;
unsigned int prioritisedBitRate;
unsigned int bucketSizeDur;
unsigned int logchannelGrp;
};
these type of files... i have to change it into the files mentioned below using python in unix

class logicalChannelExplicitCnf_st(strucure) #structure comes from struct,if union i #is their then in union it should be union
_fields_=[("priority",c_int32),("prioritisedBitRate",c_int32 ), ("bucketSizeDur",c_int32 ),("logchannelGrp",)c_int32]


#c_int32 is used instead of unsigned int, c_char for char
http://ubuntuforums.org/images/statusicon/user_online.gif http://ubuntuforums.org/images/buttons/report.gif (http://ubuntuforums.org/report.php?p=9612059) http://ubuntuforums.org/images/misc/progress.gif

mohit.joshi
August 2nd, 2010, 12:13 PM
well the replacing of c_char can be done by replace command.. help me through the string comparison

mohit.joshi
August 4th, 2010, 06:56 AM
i am new to the programming world .. and brand new to python...
typedef struct logicalChannelExplicitCnf_st
{
unsigned int priority;
unsigned int prioritisedBitRate;
unsigned int bucketSizeDur;
unsigned int logchannelGrp;
};
i have to change it into the files mentioned below using python in unix

class logicalChannelExplicitCnf_st(strucure) #structure comes from struct,if union i #is their then in union it should be union
_fields_=[("priority",c_int32),("prioritisedBitRate",c_in t32 ), ("bucketSizeDur",c_int32 ),("logchannelGrp",)c_int32]


c_int 32 is for unsigned int..
how will i read the file and then store these variables in array and then access them to write in other file...
or is there any other method of doin it????

cariboo
August 4th, 2010, 07:34 AM
Please don't create multiple threads on the same subject, I have merged your three threads.