Python 通过引用访问对象/需要标记

发布于 2024-09-06 02:52:22 字数 1379 浏览 8 评论 0原文

我需要从标准输入中获取数据并创建一个对象。

传入数据的长度在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别理我 2024-09-13 02:52:27

制作两个单独的字典(不要称它们为数组!),byipbyhash - 为什么你需要将所有内容混合在一起并冒冲突的风险?!

顺便说一句,您不可能连续使用以下两行:

myarray[hash] = myarray[ip]
assert not(myarray[hash] == myarray[ip])

要使 assert 保持不变,您必须在中间做其他事情(扰乱错误命名的 myarray)。

顺便说一句,Python 中的赋值总是引用一个对象——如果你想要一个副本,你必须明确要求一个。

Make two separate dicts (and don't call them arrays!), byip and byhash -- why do you need to smoosh everything together and risk conflicts?!

BTW, you't possibly can have the following two lines back to back:

myarray[hash] = myarray[ip]
assert not(myarray[hash] == myarray[ip])

To make the assert hold you must be doing something else in between (perturbing the misnamed myarray).

BTW squared, assignments in Python are always by reference to an object -- if you want a copy you must explicitly ask for one.

べ映画 2024-09-13 02:52:26

仅 Python 有参考资料。

创建一次对象,然后将其立即添加到所有相关的键中。

class MyData(object):
  def __init__(self, pid, ip, hash):
    self.pid = pid
     ...

for line in sys.stdin:
  pid, ip, hash = process(line)
  obj = MyData(pid=pid, ip=ip, hash=hash)
  if pid:
    mydict[pid] = obj
  if ip:
    mydict[ip] = obj
  if hash:
    mydict[hash] = obj

Python only has references.

Create the object once, and add it to all relevant keys at once.

class MyData(object):
  def __init__(self, pid, ip, hash):
    self.pid = pid
     ...

for line in sys.stdin:
  pid, ip, hash = process(line)
  obj = MyData(pid=pid, ip=ip, hash=hash)
  if pid:
    mydict[pid] = obj
  if ip:
    mydict[ip] = obj
  if hash:
    mydict[hash] = obj
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文