简体   繁体   中英

Importing only specific variable from different file to current file

I have 2 files on the same folder alice.py and bob.py . 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 . The lists are bell_state=[] and encrypted_bits=[] . How do I perform this without rerunning the entire alice.py program? I did not get the expected result because every time bob.py just rerun the entire alice.py .

In principal, this is how the code should run:

  1. alice.py runs and output these results (values are example):
    Alice bits: [1 0 1 1 0 0 1 0] -> saved as list in 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

    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

  2. bob.py runs. Taking prepared lists of bell_state and encrypted_bits to be used in the program. As the result it should give:
    epr_index = [7, 20, 30, 40, 43, 47, 50, 51] (index number where result is EPR)

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

  3. Expected result should always be alice_bits == bob_bits is True .

Problem is every time I run the program in this order, the result becomes 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. If only I can import prepared bell_state=[] and encrypted_bits=[] , this would not be an issue.

nb this program is to simulate Quantum key distribution protocol

here, do this in alice.py replace

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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