Python 将增量数字追加到现有函数
我在下面的Python2.7脚本中使用了一个名为“REG\u LOADS”的列表,但现在我想将名称更改为REG\u LOADS\u 40,并添加其他列表,如“REG\u LOADS\u 41”,以及其他一些列表。如何修改已存在的函数,以便在函数中附加“\u 40”或“\u 041”或“\u xx”?我的最终目标是列出几个名为40、41、42、43等的列表。。并让程序增加列表 谢谢Python 将增量数字追加到现有函数,python,python-2.7,Python,Python 2.7,我在下面的Python2.7脚本中使用了一个名为“REG\u LOADS”的列表,但现在我想将名称更改为REG\u LOADS\u 40,并添加其他列表,如“REG\u LOADS\u 41”,以及其他一些列表。如何修改已存在的函数,以便在函数中附加“\u 40”或“\u 041”或“\u xx”?我的最终目标是列出几个名为40、41、42、43等的列表。。并让程序增加列表 谢谢 def bytes(word): global data_high global data_low data_hig
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")
def字节(字):
全球数据高
全球数据低
数据高,数据低=divmod(字,0x100)
def read_reg_22():#必须在更改页面之前读取寄存器22(0x16)
打印(十六进制(REG_加载[x][0])、十六进制(0x16)、十六进制(2))
def set_page_4():#将phy page设置为4以写入非易失性内存
打印(十六进制(REG_加载[x][0])、十六进制(0x16)、十六进制(0x0004)、十六进制(2))
def write_to_nvm():#写入88E1112非易失性存储器寄存器
打印(十六进制(注册表加载[x][0])、十六进制(0x12)、十六进制(0xA000+(注册表加载[x][数据加载])、十六进制(0x02))
def write_to_nvm_high():#写入88E1112非易失性存储器寄存器
打印(十六进制(寄存器加载[x][0])、十六进制(0x12)、十六进制(0xA000+(数据高))、十六进制(0x02))
def write_to_nvm_low():#写入88E1112非易失性存储器寄存器
打印(十六进制(寄存器加载[x][0])、十六进制(0x12)、十六进制(0xA000+(数据低))、十六进制(0x02))
def send_nvm_to_eeprom():#写入88E1112以将nvm中的数据发送至eeprom
打印(十六进制(REG_加载[x][0])、十六进制(0x10)、十六进制(0xA000+(eeprom_ctr))、十六进制(0x02))
#物理地址,物理页面,物理注册,注册数据
注册表加载=[[0x40,0x00,0x01,0x1234],[0x41,0xFF,0xFF,0xFFFF],[0x42,0xAA,0xAA,0xAAAA],[0x40,0xAA,0xAAAA]]
打印(“检查所有物理设备是否可用”)
如果(1):
PHY_Success=1
打印(“成功,物理[64,65,67,71,72,75,76,79,80]被发现”)
其他:
PHY_Success=0
打印(“失败,未找到所有物理”)
如果PHY_Success==1:
数据中心=1
eeprom\u ctr=0x00
x=0
对于范围内的(len(REG_加载)):
读_reg_22()
设置第4页()
对于范围内的i(len(REG_载荷[0])+1):
如果data_ctr没有许多单独命名的列表和对应于每个列表的函数,我推荐一个列表字典和一个使用该列表键的函数,如下所示:
# Assuming the numbers refer to PHYs:
phys = {
40: [],
41: [],
...
}
def increment_phy(phy_key):
phys[phy_key] = whatever you want to do with it
我建议使用一个列表字典和一个函数来获取列表的键,而不是使用许多单独命名的列表和对应于每个列表的函数,如下所示:
# Assuming the numbers refer to PHYs:
phys = {
40: [],
41: [],
...
}
def increment_phy(phy_key):
phys[phy_key] = whatever you want to do with it