Python 通过引用访问对象/需要标记
我需要从标准输入中获取数据并创建一个对象。
传入数据的长度在 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 和哈希并执行以下操作:
myarray[hash] = myarray[ip]
以下是错误的:
myarray[hash] == myarray[ip]
希望这是清楚的。 我不愿意承认,在 VB 时代,我记得能够通过 byref 而不是 byval 来处理对象。 Python中有类似的东西吗?或者我只是错误地对待这个问题?
I need to suck data from stdin and create a object.
The incoming data is between 5 and 10 lines long.
Each line has a process number and either an IP address or a hash.
For example:
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
I need to put this data into a class like:
class MyData():
pid = None
hash = None
ip = None
lines = []
I need to be able to look up the object by IP, HASH, or PID.
The tough part is that there are multiple streams of data intermixed coming from stdin. (There could be hundreds or thousands of processes writing data at the same time.)
I have regular expressions pulling out the PID, IP, and HASH that I need, but how can I access the object by any of those values?
My thought was to do something like this:
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>
This gives my an array with two keys (a PID and an IP) and they both point to the same object.
But on the next iteration of the loop, if I find (for example) an IP and HASH and do:
myarray[hash] = myarray[ip]
The following is False:
myarray[hash] == myarray[ip]
Hopefully that was clear.
I hate to admit that waaay back in the VB days, I remember being able handle objects byref instead of byval. Is there something similar in Python? Or am I just approaching this wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
制作两个单独的字典(不要称它们为数组!),
byip
和byhash
- 为什么你需要将所有内容混合在一起并冒冲突的风险?!顺便说一句,您不可能连续使用以下两行:
要使
assert
保持不变,您必须在中间做其他事情(扰乱错误命名的myarray
)。顺便说一句,Python 中的赋值总是引用一个对象——如果你想要一个副本,你必须明确要求一个。
Make two separate dicts (and don't call them arrays!),
byip
andbyhash
-- why do you need to smoosh everything together and risk conflicts?!BTW, you't possibly can have the following two lines back to back:
To make the
assert
hold you must be doing something else in between (perturbing the misnamedmyarray
).BTW squared, assignments in Python are always by reference to an object -- if you want a copy you must explicitly ask for one.
仅 Python 有参考资料。
创建一次对象,然后将其立即添加到所有相关的键中。
Python only has references.
Create the object once, and add it to all relevant keys at once.