仅将不同文件中的特定变量导入当前文件

问题描述

我在同一个文件alice.pybob.py 中有 2 个文件。这些是我写的程序:

#alice.py

import random
import numpy as np
from numpy.random import randint

equal_to = {"upl":"u_plus","EPR":"u_minus","vpl":"v_plus","vmin":"v_minus"}

np.random.seed()
n = 8

alice_bits = randint(2,size=n)

bell_state = []
for i in range(n):
    while True:
        attempt = str(random.choice(list(equal_to)))
        bell_state.append(attempt)

        if attempt == 'EPR':
            break

def eva(alice,bell):
    if alice == 1:
        if bell == 'upl' or bell == 'EPR':
            return 1
        elif bell == 'vpl' or bell == 'vmin':
            return 0
    elif alice == 0:
        if bell == 'vpl' or bell == 'vmin':
            return 1
        elif bell == 'upl' or bell == 'EPR':
            return 0

encrypted_bits = []
_tmp = bell_state[:]
for i in alice_bits:
    while _tmp:
        if _tmp[:1] != ['EPR']:
            encrypted_bits.append(eva(i,_tmp[0]))
            _tmp = _tmp[1:]
        else:
            encrypted_bits.append(eva(i,*_tmp[:1]))
            _tmp = _tmp[1:]
            break


print(alice_bits)
print(dict((i,e) for i,e in enumerate(bell_state)),len(bell_state))
print(str(encrypted_bits).replace(',',''),len(encrypted_bits))
#bob.py

from alice_number import *
from operator import itemgetter

epr_index = [i for i,e in enumerate(bell_state) if e == 'EPR']

bob_bits = list(itemgetter(*epr_index)(encrypted_bits))

print(epr_index)
print(str(bob_bits).replace(',''))

我正在尝试在 alice.py 上导入准备好的列表并在 bob.py 上使用它。列表是 bell_state=[]encrypted_bits=[]。如何在不重新运行整个 alice.py 程序的情况下执行此操作?我没有得到预期的结果,因为每次 bob.py 只是重新运行整个 alice.py

原则上,代码应该是这样运行的:

  1. alice.py 运行并输出这些结果(值是示例):
    Alice bits: [1 0 1 1 0 0 1 0] -> 在 alice_bits 中保存为列表

    bell states: {0: 'vmin',1: 'vpl',2: 'upl',3: 'vpl',4: 'vmin',5: 'upl',6: 'vmin',7: 'EPR',8: 'vpl',9: 'vmin',10: 'upl',11: 'vpl',12: 'vmin',13: 'vpl',14: 'upl',15: 'upl',16: 'vmin',17: 'vpl',18: 'upl',19: 'upl',20: 'EPR',21: 'vpl',22: 'vmin',23: 'vmin',24: 'upl',25: 'upl',26: 'vmin',27: 'vmin',28: 'vmin',29: 'vpl',30: 'EPR',31: 'vpl',32: 'vmin',33: 'upl',34: 'vpl',35: 'upl',36: 'vmin',37: 'vpl',38: 'upl',39: 'vmin',40: 'EPR',41: 'upl',42: 'vmin',43: 'EPR',44: 'vpl',45: 'vpl',46: 'upl',47: 'EPR',48: 'vmin',49: 'vmin',50: 'EPR',51: 'EPR'} -> 在 bell_state 中保存为列表

    Encrypted bits: [0 0 1 0 0 1 0 1 1 1 0 1 1 1 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 0 0 0 1 0] -> 在 encrypted_bits 中保存为列表

  2. bob.py 运行。将准备好的 bell_stateencrypted_bits 列表用于程序中。结果它应该给出:
    epr_index = [7,20,30,40,43,47,50,51](索引号,其中结果是 EPR)

    bob_bits = [1 0 1 1 0 0 1 0]

  3. 预期结果应始终为 alice_bits == bob_bits is True

问题是我每次按这个顺序运行程序,结果就变成alice_bits != bob_bits。我想这是因为 alice.pybob.py 中再次执行,从而生成不同的随机位序列。如果我可以导入准备好的 bell_state=[]encrypted_bits=[],这将不是问题。

n.b.这个程序是模拟Quantum密钥分发协议

解决方法

在这里,在 alice.py 中执行此操作 替换

print(alice_bits)
print(dict((i,e) for i,e in enumerate(bell_state)),len(bell_state))
print(str(encrypted_bits).replace(',',''),len(encrypted_bits))

if __name__ == "__main__":
    print(alice_bits)
    print(dict((i,len(bell_state))
    print(str(encrypted_bits).replace(',len(encrypted_bits))

这样,在 if __name__ == "__main__": 下编写的任何代码在您导入 alice.py 时都不会运行