Skip to content
Jeff Daily edited this page Feb 18, 2016 · 1 revision

The Python bindings come in two flavors, using ctypes (recommended) or using cython to compile a native C/Python extension. The cython version was written first though it is deprecated because it wasn't straightforward to compile on Windows. Therefore, please use the ctypes interface.

The ctypes interface is found in the python subfolder of the FNCS source distribution, for example fncs-2.1.0/python/fncs.py. It is tested with Python 2.7.x but should work with any Python version from 2.5.x through 2.7.x since cytpes was added as a default module for Python as of 2.5.

By default, it will look for the 'libfncs.so' shared library. This works for most Linux platforms. Note that in many cases this is a symbolic link to the properly libtool-versioned library such as 'libfncs.so.1.0.0'. On OSX, this library defaults to 'libfncs.dylib'. On Windows, this defaults to 'libfncs' and ctypes will automatically append the *.dll suffix to the name.

If you're unable to "import fncs", make sure your $LD_LIBRARY_PATH environment variable (on Linux) or $DYLD_LIBRARY_PATH (on OSX) is set appropriately to point to the FNCS library and its dependent libraries (sodium, zmq, and czmq). On Windows, it uses your $PATH environment variable to locate DLLs.

Here's a sample program using the Python interface.

import fncs

# embed the ZPL file into the initialize call, 
# or call fncs.initialize() without arguments to rely on default
# fncs.zpl parsing behavior
fncs.initialize("""name = pydemo
time_delta = 1s
values
    key
        topic = sim1/someval
        default = 10
""")

# run for 100 simulated seconds
for time in range(100):
    granted = fncs.time_request(time)
    assert granted == time
    # do some useful work
    someval = fncs.get_value("key")
    # fncs-2.1.0 required a string value for both key and value fields
    fncs.publish("time", str(time)) # just to publish something

fncs.finalize()

Home

Clone this wiki locally