在现有函数中附加增量编号

时间:2017-10-04 00:07:59

标签: python python-2.7

我使用名为“REG_LOADS”的单个列表启动了下面的python 2.7脚本,但现在我想将名称更改为REG_LOADS_40并添加其他列表,例如“REG_LOADS_41”等等。如何修改已存在的函数以将“_40”或“_041”或“_xx”附加到函数中?我的最终目标是有几个名为40,41,42,43等的列表。并让程序增加列表。

谢谢,

def bytes(word):
global data_high
global data_low
data_high, data_low = divmod(word, 0x100)


def read_reg_22(): #must read register 22 (0x16) before chaging the page
   print(hex(REG_LOADS[x][0]), hex(0x16), hex(2))

def set_page_4(): #set phy page to 4 for writing to non-volatile memory
    print (hex(REG_LOADS[x][0]), hex(0x16), hex(0x0004), hex(2))

def write_to_nvm(): #write to 88E1112 non-volatile memory register
    print (hex(REG_LOADS[x][0]), hex(0x12), hex(0xA000+(REG_LOADS[x][data_ctr])), hex(0x02))

def write_to_nvm_high(): #write to 88E1112 non-volatile memory register
    print (hex(REG_LOADS[x][0]), hex(0x12), hex(0xA000+(data_high)), hex(0x02))

def write_to_nvm_low(): #write to 88E1112 non-volatile memory register
    print (hex(REG_LOADS[x][0]), hex(0x12), hex(0xA000+(data_low)), hex(0x02))

def send_nvm_to_eeprom(): #write to 88E1112 to send data in nvm to EEPROM
    print (hex(REG_LOADS[x][0]), hex(0x10), hex(0xA000+(eeprom_ctr)), hex(0x02))

# PHY_ADDR, PHY_PAGE, PHY_REG, REG_DATA
REG_LOADS = [[0x40, 0x00, 0x01, 0x1234],[0x41, 0xFF, 0xFF, 0xFFFF],[0x42, 0xAA, 0xAA, 0xAAAA], [0x40, 0xAA, 0xAA, 0xAAAA]]


print ("Checking that all PHYs are available")
if (1):
    PHY_Success = 1
    print ("Success, PHYs [64, 65, 67, 71, 72, 75, 76, 79, 80] were found")
else:
    PHY_Success = 0
    print ("Failure, all PHYs were not found")

if PHY_Success == 1:
    data_ctr = 1
    eeprom_ctr = 0x00
    x = 0
    for a in range(len(REG_LOADS)):
        read_reg_22()
        set_page_4()
        for i in range(len(REG_LOADS[0])+1):
            if data_ctr <= 2:
                write_to_nvm()
                send_nvm_to_eeprom()
            if data_ctr == 3:
                bytes (REG_LOADS[x][data_ctr])
                write_to_nvm_high()
                send_nvm_to_eeprom()
            if data_ctr == 4:
                write_to_nvm_low()
                send_nvm_to_eeprom()
            data_ctr +=1
            eeprom_ctr +=1
        data_ctr = 1
        eeprom_ctr =0x00
        x += 1

else:
    print ("Fail")

1 个答案:

答案 0 :(得分:0)

我没有使用多个单独命名的列表和与每个列表对应的功能,而是推荐列表和一个能够获取该列表键的函数:

# Assuming the numbers refer to PHYs:
phys = {
    40: [],
    41: [],
    ...
}

def increment_phy(phy_key):
    phys[phy_key] = whatever you want to do with it