简体   繁体   English

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

[英]Importing only specific variable from different file to current file

I have 2 files on the same folder alice.py and bob.py .我在同一个文件夹alice.pybob.py上有 2 个文件。 These are the program I wrote:这些是我写的程序:

#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(',', ''))

I am trying to import prepared list on alice.py and use it on bob.py .我正在尝试在bob.py上导入准备好的列表并在alice.py上使用它。 The lists are bell_state=[] and encrypted_bits=[] .列表是bell_state=[]encrypted_bits=[] How do I perform this without rerunning the entire alice.py program?如何在不重新运行整个alice.py程序的情况下执行此操作? I did not get the expected result because every time bob.py just rerun the entire alice.py .我没有得到预期的结果,因为每次bob.py只是重新运行整个alice.py

In principal, this is how the code should run:原则上,代码应该这样运行:

  1. alice.py runs and output these results (values are example): alice.py运行和 output 这些结果(值是示例):
    Alice bits: [1 0 1 1 0 0 1 0] -> saved as list in alice_bits 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'} -> saved as list in bell_state 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] -> saved as list in encrypted_bits 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 runs. bob.py运行。 Taking prepared lists of bell_state and encrypted_bits to be used in the program.准备好程序中使用的bell_stateencrypted_bits列表。 As the result it should give:结果它应该给出:
    epr_index = [7, 20, 30, 40, 43, 47, 50, 51] (index number where result is EPR) epr_index = [7, 20, 30, 40, 43, 47, 50, 51] (结果为 EPR 的索引号)

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

  3. Expected result should always be alice_bits == bob_bits is True .预期结果应该始终是alice_bits == bob_bits is True

Problem is every time I run the program in this order, the result becomes alice_bits != bob_bits .问题是每次我按此顺序运行程序时,结果都会变为alice_bits != bob_bits I suppose this happens because alice.py is being executed once again in bob.py , thus generating different random bit sequence.我想这是因为alice.pybob.py中再次执行,从而生成不同的随机位序列。 If only I can import prepared bell_state=[] and encrypted_bits=[] , this would not be an issue.如果我可以导入准备好的bell_state=[]encrypted_bits=[] ,这将不是问题。

nb this program is to simulate Quantum key distribution protocol nb 这个程序是模拟量子密钥分发协议

here, do this in alice.py replace在这里,在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))

with

if __name__ == "__main__":
    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))

that way, any code written under if __name__ == "__main__": will not run whenever you import alice.py这样,在if __name__ == "__main__":下编写的任何代码都不会在您导入alice.py时运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM