1

I'm already struggeling some hours with a problem within my python project.

The situation is as follows:

I have a script A.py and a Script B.py

**Script A.py:**
#in this script the function def main() is running 

def main():

       #some coding in here

       x=str(body)#then i assign the string of the variable body to a new variable x

#some other coding in here

if __name__=='__main__':

        main()

REMIND: this a pseudo code to explain my struggle (the script as a standalone module is working properly) !

Now I have Script B.py (in the same folder)

**Script B.py** #in this script i try to run Script A.py and assign the value of variable x to a new variable in order to do furhter processing with it.

import A

A.main() # When importing the module and excuting its main() function by running B.py I see the values of variable x appearing on my screen

QUESTION: How can I assign the value of variable x now to a new variable so that i can do further processing with it in B.py ? Is this even possible ? Cause after calling the main function of A.py no other operations are processed.

Please consider that I'm a relatively newby regaring programming over several modules.

I would be very glad for any help.

Thank you very much in advance

Kind regards

Slin


Ok i tried your approaches but still not getting the desired result.

A.py is a AMQP subscribing script (https://www.rabbitmq.com/tutorials/tutorial-one-python.html) (see below):

import pika



   credentials = pika.PlainCredentials('admin', 'admin')
   connection = pika.BlockingConnection(pika.ConnectionParameters('Ipaddress',
                                          5672,
                                          '/',
                                          credentials))
   #connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
   channel = connection.channel()

   channel.exchange_declare(exchange='logs',
                               exchange_type='fanout')

   result = channel.queue_declare(exclusive=True)
   queue_name = result.method.queue

   channel.queue_bind(exchange='logs',
                         queue=queue_name)

   print(' [*] Waiting for logs. To exit press CTRL+C')

   def callback(ch, method, properties, body):

          x = str(body)
          print str(x)
   channel.basic_consume(callback,
                            queue=queue_name,
                            no_ack=True)



   channel.start_consuming()


if __name__ == '__main__':
       main()

B.py:

import pika
import A
A.main()

With the approaches so far i get the same as shown with the coding above. I would like to assign x (which values can chane when A is running) to a new variable within B.py to do some processing to publish it afterwards with the counterpart script of A.py.

When executing B.py i receive:

[*] Waiting for logs. To exit press CTRL+C
['20'] #this is the string of variable x from script A

Now i want to assign this ['20'] to a new variable wihtin B.py.. but the script B.py keeps running A.main() (which is logical cause it is a loop).

Thanks so far for your support.

Kind regards

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260
Slin
  • 21
  • 1
  • Rather than pseudocode, can you make this into a self-contained example? It's not clear to me how you'd see the value of `x` from `A`. But, `main` in `A` doesn't `return` anything so it's not clear what you're trying to do – roganjosh Apr 26 '18 at 19:48
  • I posted the code in the new answer of mine. does that help for the understanding ? I basically want just to write x to a new variable in B.py to do further processing with it. So far the values of x ist just appearing in the shell since a new message gets subscribed by a.py. – Slin Apr 26 '18 at 20:56
  • How to exit main() in B.py after it received a value and how to assign it to a new variable... – Slin Apr 26 '18 at 20:57

3 Answers3

0

You could return the value of x to script B using the return statement:

return str(body)#then i assign the string of the variable body to a new variable x

Then get this value in script B by storing it in a variable x = A.main().

Xantium
  • 11,201
  • 10
  • 62
  • 89
0

Just treat it like you do with any other module. In script A, create a function as you have called main. In it you have to have a variable that you can insert from the other script, call it body.

Script A:

def main(body):
    ...
    return x

Next you import the other script like you would any other model, using import _.py as _. The you use the function as you would in the other script, B. It is as if you are importing an object A and calling its method b.

Script B:

import a.py as A

object = A.main(thing2string) # or object = A.main() if the script b has something to return

Now you have the object that is created in A from that you can process in be. For example,

processed = [object[i] for i in object]
# ... processing steps

I don't know if you can have it notify you on change. But you use a timer to check if the value is updated and use an if statement to update if it has.

Summary:

You have to add the return line in script A. And you have to set a variable equal to it in script b. This new variable then becomes whatever b returns.

10donovanr
  • 93
  • 12
  • @roganjosh Your correct. I botched the arrangement. I'm trying to show that you can plug in the values [1, 2, .., 9] and return an array of the strings of those integers. – 10donovanr Apr 26 '18 at 20:05
  • @slin I think you'll find the answer here no matter what you are asking if you read it carefully. This can be a useful question, but you need to be more careful when you ask it. There is too much going on in your question, you need to simplify it into the one thing you want to output and what type of object you are trying to process. – 10donovanr Apr 27 '18 at 18:17
0

Make x as a global variable inside A.py

**Script A.py:**
#in this script the function def main() is running 

x = ''
def main():
   global x
   #some coding in here

   x=str(body)
   #then i assign the string of the variable body to a new variable x

   if __name__=='__main__':
      main()

Now you can access that x from B.py as A.x

K2A
  • 172
  • 2
  • 7
  • Hey ! Thanks for your answer..Unfortunately this is not working in my case caus the variable x ist not static. This means if i use import A.py in B.py by writing import A and afterwards: A.main() it only runs the A code but i cannot further process or even access the variable x within B.py... Is there a way to execute script A.py as a long as the variable is empy (by starting it from B.py) and if the value is not None keep running A.main() ? Thanks a lot – Slin Apr 27 '18 at 11:42