pythonformat输入数据_关于python实现数据字典format格式化的应用和总结

# -*- coding: utf-8 -*-

#输入:引索,值,字节大小

track_template = \

'''{parameterIndex},{parameterName},{ParaAnnotate}'''

#{L_CRC},/**< CRC校验; 0~0xFFFFFFFF*/

file_template = \

'''#include "lwsf.h"#include "ato/conm.h"#include "ato/config/con_config.h"#include "common/dynamics.h"#include "common/utilities.h"#include "lwsf_platform/lwsf_platform_inf.h"​/*- ATO项目参数配置表 */​const MAINTAIN_FRM_PARAM_t appl_project_parameter_table[{DATA_LIST_LEN}]={DATA_LIST};​const uint16_t appl_project_parameter_table_len = sizeof(appl_project_parameter_table)/sizeof(appl_project_parameter_table[0]);​​'''

class DictGenerator(object):

def __init__(self, template, data_dict):

self.template = template

self.data_dict = data_dict

def get_data_str(self, data):

ret = ''

if isinstance(data, list):

ret = '{{ {}}}'.format(','.join([self.get_data_str(i) for i in data]))

elif isinstance(data, int):

ret = str(data)

elif isinstance(data, str):

ret = data

else:

ret = str(data)

# print data

# print('unsupported')

# assert(0)

return ret

def generate_c_str(self):

str_dict = {}

for k, v in self.data_dict.items():

str_dict.update({k: self.get_data_str(v)})

#print "================"

#print self.template

#print str_dict

#print "================"

ret = self.template.format(**str_dict)

return ' {{{}}}'.format(ret)

#产生用于将string传入的dict,key为XXX_LIST,内容为excel中某一个表对应的格式化string,key为XXX_LIST_LEN,内容为表的列数

class StructGenerator(object):

def __init__(self, file_template, key_word, template, data_dict_list):

self.key_word = key_word

self.file_template = file_template

self.template = template

self.data_dict_list = data_dict_list

self.dg_list = [DictGenerator(template, i) for i in data_dict_list]

#if key_word=='LINE_LIST':

# print self.dg_list

def get_cstr_dict(self):

dict_list_str = ',\n'.join(i.generate_c_str() for i in self.dg_list)

dict_list_str = '\n{{\n{}\n}}'.format(dict_list_str)

if len(self.data_dict_list) != 0:

return {self.key_word:dict_list_str,self.key_word+'_LEN':str(len(self.data_dict_list))}

else:

return {self.key_word:('{{{}}}'.format('0xff')),self.key_word+'_LEN':'1',self.crc_key:'0xffffffff'}

#产生C/BIN文件内容(string类型)

class FileGenerator(object):

def __init__(self, file_template, info_list):

self.file_template = file_template

self.fg_list = [StructGenerator(file_template, i[0], i[1], i[2]) for i in info_list]

def generate_c_str(self):

cstr_dict = {}

#for i in self.fg_list:

#print i.get_cstr_dict()

[cstr_dict.update(i.get_cstr_dict()) for i in self.fg_list]

ret = self.file_template.format(**cstr_dict)

return ret

if __name__ == '__main__':

#str = str_to_hex('3')

data_list = [{"parameterIndex":1,"parameterName":"Max_level","ParaAnnotate":"最大牵引级位"},

{"parameterIndex":2,"parameterName":"Min_level","ParaAnnotate":"最小制动级位" },

{"parameterIndex":3, "parameterName":"Hold_level","ParaAnnotate":"保持制动或者惰行级位"}]

fg = FileGenerator(file_template, [['DATA_LIST', track_template, data_list]])

total_str = fg.generate_c_str()

print total_str

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值