Skip to main content
Previous sectionNext section

Connecting and Using Proxy Objects

This chapter demonstrates how to establish a gateway connection to an External Python Server instance, and how to create and use ObjectScript proxy objects.

Creating a Gateway Connection

To create the connection:

The following example assumes that an External Python Server process named PyGate is running (see the chapter on “Managing External Python Servers”).

Read the named External Python Gateway definition and create the gateway connection
# get settings for "PyGate" external server
  set status = ##class(%Net.Remote.Service).OpenGateway("PyGate",.PyServer)
  set host = PyServer.Server
  set port = PyServer.Port

# create a gateway connection to the PyGate External Python Server
  set status = gateway.%Connect(host, port)
Copy code to clipboard

Specifying Class Paths

You must specify a path for each module that contains a class you want to import. In this example, the fully qualified path to is inserted into pathList. Paths to other modules can be added in the same way.

Create a class path list and add to the Gateway object
  set pathList = ##class(%ListOfDataTypes).%New()
  do pathList.Insert("C:\Project\demo\")
  write !,"Added module: "_pathList.GetAt(1)

  do gateway.%AddToCurrentClassPath(pathList)
Copy code to clipboard

See the previous section for the code to create and connect the Gateway object.

Specifying a Path within a Package Structure

When a module is part of a package, the module path must be specified with dot notation starting at the top level package directory. For example, the path to module (listed in “The demo Package”) could be specified in either of two ways:

  • Standard path notation if treated as a module: C:\Dev\demo\

  • Dot notation if treated as part of package demo: C:\Dev\

Creating and Using Proxy Objects

All proxy objects are instances of %Net.Remote.Object. They are created by calling %New(), specifying the gateway connection to use and the Python class to be imported. Both the proxy object and the Python object are instantiated when this call is made:

  set proxy = ##class(%Net.Remote.Object).%New(gateway,className)
Copy code to clipboard

The arguments for this call are:

  • gateway — a %Net.Remote.Gateway object connected to an External Python Server (as demonstrated previously in “Creating a Gateway Connection”).

  • className — the fully qualified class name of the Python class. For example, the full name for the Person class (listed in “The demo Package”) would be:

    • PersonDemo.Person if is an unpackaged module.

    • demo.PersonDemo.Person if is part of package demo.

In the following example, the Gateway object is connected to the PyGate external server. Once the person proxy object is created, method displayPerson() and property accessors name and age are available just as they would be in the corresponding Python object:

ObjectScript: creating and using a proxy object
  set person = ##class(%Net.Remote.Object).%New(gateway,"PersonDemo.Person")

  write !,"Default values: ",person.displayPerson()

  set person.age=99
  write !,"Changed values: ",person.displayPerson()
  write ", ",!
Copy code to clipboard


  Default values: name: Tom, age: 5
  Changed values: name: George, age: 99, employer: Intersystems
Copy code to clipboard

The demo Package

The module
from Company import Company

class Person:

  def __init__(self):
    self._name = "Tom"
    self._age = 5 = Company()

  def name(self):
    return self._name

  def name(self, newName):
    self._name = newName

  def age(self):
    return str(self._age)

  def age(self, newAge):
    self._age = newAge

  def displayPerson(self):
    return "name: " + + ", age: " + self.age
Copy code to clipboard
The module
class Company:

  def __init__(self):
    self._employer = "InterSystems"

  def displayEmployer(self):
    return "employer: " + self._employer
Copy code to clipboard