# -*- coding:utf-8 -*-
def addr_to_lenth(jsonlist):
t = len(jsonlist)
i = 0
typelist = list()
while i < t :
for j in range(i+1,t):
if jsonlist[i]['起始地址'] + jsonlist[i]['长度'] == jsonlist[j]['起始地址'] and j-i-1 < 9:
jsonlist[i]['长度'] += jsonlist[j]['长度']
jsonlist[i]['采集数据类型'] = jsonlist[i]['采集数据类型'] + ',' + jsonlist[j]['采集数据类型']
jsonlist[i]['id'] = jsonlist[i]['id'] + ','+ jsonlist[j]['id']
else:
break
typelist.append(jsonlist[i])
if i + 1 < j:
i = j
else:
i += 1
return typelist
def sort(jsonlist):#冒泡排序,按地址大小重新排序
for i in range(len(jsonlist)-1):
for j in range(len(jsonlist)-1-i):
if jsonlist[j]['起始地址'] > jsonlist[j+1]['起始地址']:
addrlist[j],addrlist[j+1] = addrlist[j+1],addrlist[j]
return jsonlist
def add_lenth(jsonlist):
for i in range(len(jsonlist)):
if jsonlist[i]['采集数据类型'] not in ('int16', 'uint16'):
jsonlist[i]['长度'] = 2
else:
jsonlist[i]['长度'] = 1
return jsonlist
def tear_down(jsonlist):
for item in jsonlist:
for i in range(len(item['采集数据类型'])):
pass
def run(jsonlist):
addrlist_sort = sort(jsonlist)
addlenth = add_lenth(addrlist_sort)
t = addr_to_lenth(addlenth)
return t
if __name__=="__main__":
addrlist = [
{
"采集数据类型": "float", "起始地址": 400012, "串口网口": "192.168.0.106:502", "id": "MID10001"},
{
"采集数据类型": "float", "起始地址": 400114, "串口网口": "192.168.0.106:502", "id": "MID20002"},
{
"采集数据类型": "float", "起始地址": 400014, "串口网口": "192.168.0.106:502", "id": "MID10002"},
{
"采集数据类型": "int16", "起始地址": 400016, "串口网口": "192.168.0.106:502", "id": "MID10003"},
{
"采集数据类型": "float", "起始地址": 400017, "串口网口": "192.168.0.106:502", "id": "MID10004"},
{
"采集数据类型": "int16", "起始地址": 400019, "串口网口": "192.168.0.106:502", "id": "MID10005"},
{
"采集数据类型": "float", "起始地址": 400020, "串口网口": "192.168.0.106:502", "id": "MID10006"},
{
"采集数据类型": "int16", "起始地址": 400022, "串口网口": "192.168.0.106:502", "id": "MID10006"},
{
"采集数据类型": "int16", "起始地址": 400023, "串口网口": "192.168.0.106:502", "id": "MID10006"},
{
"采集数据类型": "int16", "起始地址": 400024, "串口网口": "192.168.0.106:502", "id": "MID10006"},
{
"采集数据类型": "float", "起始地址": 400025, "串口网口": "192.168.0.106:502", "id": "MID10007"},
{
"采集数据类型": "float", "起始地址": 400027, "串口网口": "192.168.0.106:502", "id": "MID10008"},
{
"采集数据类型": "float", "起始地址": 400029, "串口网口": "192.168.0.106:502", "id": "MID10009"},
{
"采集数据类型": "float", "起始地址": 400031, "串口网口": "192.168.0.106:502", "id": "MID10009"},
{
"采集数据类型": "float", "起始地址": 400033, "串口网口": "192.168.0.106:502", "id": "MID10009"},
{
"采集数据类型": "float", "起始地址": 400035, "串口网口": "192.168.0.106:502", "id": "MID10009"},
{
"采集数据类型": "float", "起始地址": 400037, "串口网口": "192.168.0.106:502", "id": "MID10009"},
{
"采集数据类型": "int16", "起始地址": 400039, "串口网口": "192.168.0.106:502", "id": "MID10009"},
{
"采集数据类型": "float", "起始地址": 400040, "串口网口": "192.168.0.106:502", "id": "MID10010"}
]
t = run(addrlist)
print t
程序手动运行该程序是不会报错的,但当其他程序调用时就会出现错误:line15:UnboundLocalError: local variable 'j' referenced before assignment
修改:
def addr_to_lenth(jsonlist):
t = len(jsonlist)
i = 0
typelist = list()
while i < t :
s = 0
for j in range(i+1,t):
if jsonlist[i]['起始地址'] + jsonlist[i]['长度'] == jsonlist[j]['起始地址'] and j-i-1 < 9:
jsonlist[i]['长度'] += jsonlist[j]['长度']
jsonlist[i]['采集数据类型'] = jsonlist[i]['采集数据类型'] + ',' + jsonlist[j]['采集数据类型']
jsonlist[i]['id'] = jsonlist[i]['id'] + ','+ jsonlist[j]['id']
s = j
else:
s = j
break
typelist.append(jsonlist[i])
if i + 1 < s:
i = s
else:
i += 1
return typelist