我需要从标准输入中吸取数据并创建一个对象。
传入数据的长度在 5 到 10 行之间。每行都有一个进程号和一个 IP 地址或一个哈希值。例如:
pid=123 ip=192.168.0.1 - some data
pid=123 hash=ABCDEF0123 - more data
hash=ABCDEF123 - More data
ip=192.168.0.1 - even more data
我需要将这些数据放入一个类中,例如:
class MyData():
pid = None
hash = None
ip = None
lines = []
我需要能够通过 IP、HASH 或 PID 来查找对象。
困难的部分是来自标准输入的多个混合数据流。(可能有成百上千个进程同时写入数据。)
我有正则表达式提取我需要的 PID、IP 和 HASH,但是我如何通过这些值访问对象?
我的想法是做这样的事情:
myarray = {}
for each line in sys.stdin.readlines():
if pid and ip: #If we can get a PID out of the line
myarray[pid] = MyData().pid = pid #Create a new MyData object, assign the PID, and stick it in myarray accessible by PID.
myarray[pid].ip = ip #Add the IP address to the new object
myarray[pid].lines.append(data) #Append the data
myarray[ip] = myarray[pid] #Take the object by PID and create a key from the IP.
<snip>do something similar for pid and hash, hash and ip, etc...</snip>
这给了我一个包含两个键(一个 PID 和一个 IP)的数组,它们都指向同一个对象。但是在循环的下一次迭代中,如果我找到(例如)一个 IP 和 HASH 并执行以下操作:
myarray[hash] = myarray[ip]
以下是假的:
myarray[hash] == myarray[ip]
希望这很清楚。我不想承认,在 VB 时代,我记得能够处理对象 byref 而不是 byval。Python中有类似的东西吗?还是我只是在接近这个错误?