我有一个关于如何编写程序的快速问题,因此它可以决定需要使用多少变量。
这是我到目前为止的代码:
#!/usr/bin/python
from subprocess import PIPE, Popen
from os import system
class TransMsg(object):
def __init__(self):
self.msgType = ""
self.canType = ""
self.ID = 0
self.DLC = 0
self.tData0 = 0
self.tData1 = 0
self.tData2 = 0
self.tData3 = 0
self.tData4 = 0
self.tData5 = 0
self.tData6 = 0
self.tData7 = 0
# extended can only
self.tData8 = 0
self.tData9 = 0
self.tData10 = 0
self.tData11 = 0
self.tData12 = 0
self.tData13 = 0
self.tData14 = 0
self.tData15 = 0
self.count = 0
def startTrans(msg):
print( msg.ID, msg.tData0, msg.tData1, msg.tData2, msg.tData3, msg.tData4, msg.tData5, msg.tData6, msg.tData7)
def createMsg():
msg = TransMsg()
msg.msgType = raw_input("Please Enter Msg Type\n m = Message String Follows\n r = RTR-Message String Follows\n i = Initialsation String Follows\n")
msg.canType = raw_input("Please Enter Can Type\n s = Standard Can\n e = Extended Can\n")
msg.ID = raw_input("Please enter message ID: ")
if(msg.canType == 'e'):
msg.DLC = 16
extendedString = raw_input("Please enter can message:\n")
cutter = extendedString.split()
msg.tData0 = cutter[0]
msg.tData1 = cutter[1]
msg.tData2 = cutter[2]
msg.tData3 = cutter[3]
msg.tData4 = cutter[4]
msg.tData5 = cutter[5]
msg.tData6 = cutter[6]
msg.tData7 = cutter[7]
msg.tData8 = cutter[8]
msg.tData9 = cutter[9]
msg.tData10 = cutter[10]
msg.tData11 = cutter[11]
msg.tData12 = cutter[12]
msg.tData13 = cutter[13]
msg.tData14 = cutter[14]
msg.tData15 = cutter[15]
elif(msg.canType == 's'):
msg.DLC = 8
standardString = raw_input("Please enter can message:\n")
cutter = standardString.split()
msg.tData0 = cutter[0]
msg.tData1 = cutter[1]
msg.tData2 = cutter[2]
msg.tData3 = cutter[3]
msg.tData4 = cutter[4]
msg.tData5 = cutter[5]
msg.tData6 = cutter[6]
msg.tData7 = cutter[7]
return msg
if __name__ == '__main__':
msg4Trans = createMsg()
startTrans(msg4Trans)
我需要的是如果使用定义DLC变量,例如3,程序只需要使用tData1,2和3.这是否可以在我的程序的上下文中?
由于