Skip to content

Python API

The Python API for package interactions provides you with the ability to list and load the contents of a package. There are two interfaces available under the kxi.packages namespace, packages and udfs, which allow you to interact with these entities independently.

Installation

This package can be installed from the KX Nexus using the following command to retrieve the latest version of the functionality:

pip install --extra-index-url https://$KXI_PYPI_USER:$KXI_PYPI_PASS@nexus.dl.kx.com/repository/kxi-pypi-public/simple/ kxi.packages

To retrieve a specific version of the kxi.packages functionality install using the following:

pakx_version=x.y.z
pip install --extra-index-url https://$KXI_PYPI_USER:$KXI_PYPI_PASS@nexus.dl.kx.com/repository/kxi-pypi-public/simple/ kxi.packages==$pakx_version

Quickstart Example

The below example assumes the following pre-requisites:

  1. You have installed the kxi.packages Python library following the instructions here.
  2. You have already followed the quickstart guide presented for the CLI here and have a local package qpackage-1.0.0.kxi installed.

Running the example:

  1. Start your Python process

    $ python
    
  2. Import and initialise the kxi.packages library

    >>> import pykx as kx
    >>> import kxi.packages as pakx
    
    >>> pakx.init()
    
  3. Set the KX_PACKAGE_PATH environment to the folder containing your packages in this instance

    >>> pakx.setup_env.set_package_path("test/test-packages")
    
  4. List all packages available on the current local installation

    >>> pakx.packages.list()
           name versions
    0  qpackage    1.0.0
    
  5. List all UDFs associated with the installed packages

    >>> pakx.udfs.search(package_name="qpackage")
         name      function language  ...   package  version
    0  sp_map  .test.sp.map        q  ...  qpackage    1.0.0
    
  6. Load the package contents

    >>> pakx.packages.load('qpackage', '1.0.0')
    >>> kx.q.test
    <pykx.ctx.QContext of .test with [variable, sp]>
    
  7. Retrieve the UDF as a named function

    >>> test_udf = pakx.udfs.load('sp_map', 'qpackage', '1.0.0')
    >>> test_udf
    pykx.Lambda(pykx.q('
     {[table;params]
      select from table where x<10
      }
    '))