1

Topic: Passing Python objects

I have a question about passing objects between scripts, if that is even possible. My application requires some initial code to execute, preferably at startup, and then respond to script inputs as normal. It is pretty inefficient to execute the startup code every time a script executes as with a typical setup. I'd like to place the startup code in a separate script and export the initialized object(which happens to be a socket) so that it may be used by other scripts. Can I use a threaded script for the startup code, do they automatically start when the project is started?

2

Re: Passing Python objects

Hi,

I have done that in several projects without any problem. It is quite useful to define classes in a script and let others just instantiate the classes. Also useful to delay (or sequence) scripts.
Yes you can use a threaded script.

I'll be happy to help if needed.

--
Raphaël Bollen
Ampco Belgium.

3

Re: Passing Python objects

Are you saying different scripts share the same scope/namespace? How would I access a class defined in a different script?

I'm not quite sure if that is appropriate for handling a socket object. I only want one object, the socket itself. I want it initialized once when the project starts, and simply call its methods in another script.

4

Re: Passing Python objects

Hi-

I posted a response to this previously but it appears to have been lost. I'll try to reconstruct it from memory.

First, unfortunately there is no way to pass a value between script blocks. You can share scripts between script blocks but the values of the variables are scoped to the block itself.

There is a way to pass values between invocations of a script block however. This is the purpose of the 'state' variable. This variable is made available to your script each time your script block is invoked and its value is preserved across invocations of your script block.

As Python variables are essentially 'untyped' you can set this variable to any value you care to preserve. By default it is an integer with the value of zero. It could however be set to a dictionary or a list if you need to preserve multiple values.

There are several ways to address the type of problem you describe in your original post. The most obvious is to save your socket object to the state variable and then to check to see if it's a valid socket object at the top of your script. Such a script would look something like this:

import socket

#create socket if the state variable has not already been initialized as a socket
if not isinstance(state, socket):
    state = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
    state.connect(address, port);

# it is safe to work with your socket now
state.send('Hello World');

Another option would be to create a custom class which has the socket as one of its member variables. An instance of this class could then be saved to the state variable in the same fashion as demonstrated above. This would be more convenient in the case where you had additional variables you wanted keep associated with your socket.

I hope this helps. Let us know if you have more questions.

Thanks-
Frank

5

Re: Passing Python objects

Thanks for the info, I have a couple more questions:

1. I didn't notice any documentation regarding the state variable in the help files, did I miss something?

2. Is the state variable global across the entire project, or local per script block?

3. If a script block is defined as threaded, does it automatically run upon startup of the project?

4. There are some references to an event object in Beta versions of Nware, things for waiting for an input to change state. Do these still exist? If they do, and question #3 is true, I could just have a threaded script do its initialization upon startup and then wait for input changes.

6

Re: Passing Python objects

jvalenzuela wrote:

Are you saying different scripts share the same scope/namespace? How would I access a class defined in a different script?

I'm not quite sure if that is appropriate for handling a socket object. I only want one object, the socket itself. I want it initialized once when the project starts, and simply call its methods in another script.

Here is a code sample:
In your init script:

global G
G = {}
G['is_ready'] = False
message.string_set("Initializing...")

import socket

class MySocket(socket.socket):
  """define your socket here"""
  pass


G['socket'] = MySocket
G['is_ready'] = True

In another script do:

global G
while True:
  message.string_set("Waiting ready state...")
  event.wait(1000)
  try:
    if G['is_ready']:
      break
    else:
      message.string_set("Waiting ready state")
  except:
    message.string_set("Exception: Waiting ready state...")
message.string_set("STARTED.")

# instantiate the socket
mysocket = G['socket']()

Note: you will need to subclass the socket if you want to use it in different scripts and implement some locking. That's the tricky part.
you can also use the state variable rather than a global G.
If you want to use the socket in only one script, why do you want to initialize it in another script? Can't you just use a threaded script like this?

import socket

def init_socket():
  # initialize socket...
  s = socket.socket(....
  ...
  return s

s = init_socket()
while True:
  try:
    # use socket
  except Exception:
    event.wait(100)
    s = init_socket()
--
Raphaël Bollen
Ampco Belgium.

7

Re: Passing Python objects

jvalenzuela wrote:

Thanks for the info, I have a couple more questions:

1. I didn't notice any documentation regarding the state variable in the help files, did I miss something?

2. Is the state variable global across the entire project, or local per script block?

3. If a script block is defined as threaded, does it automatically run upon startup of the project?

4. There are some references to an event object in Beta versions of Nware, things for waiting for an input to change state. Do these still exist? If they do, and question #3 is true, I could just have a threaded script do its initialization upon startup and then wait for input changes.

1.Yes, hit the help button on the edit script window
2.No sure, I never used the state variable but global var is global across one nion (not accross multiple nions in one project)
3 Yes
4 Yes

--
Raphaël Bollen
Ampco Belgium.