Difference between revisions of "Python"

From truxwiki.com
Jump to navigation Jump to search
Line 4: Line 4:
 
You must use the "import" directive to load the Truxton Python provider in <code>truxton.pyd</code>.
 
You must use the "import" directive to load the Truxton Python provider in <code>truxton.pyd</code>.
 
This directive will search a variety of folders for the <code>[https://docs.python.org/3/faq/windows.html#is-a-pyd-file-the-same-as-a-dll pyd]</code> file for Truxton (<code>truxton.pyd</code>).
 
This directive will search a variety of folders for the <code>[https://docs.python.org/3/faq/windows.html#is-a-pyd-file-the-same-as-a-dll pyd]</code> file for Truxton (<code>truxton.pyd</code>).
Normally, the <code>truxton.pyd</code> file is installed in the SDK folder (<code>C:\Program Files\Truxton\SDK</code>).
+
Normally, this file is installed in the SDK folder (<code>C:\Program Files\Truxton\SDK</code>) along with all of the dependent files.
You can modify the <code>[https://docs.python.org/3/using/cmdline.html?highlight=pythonpath#envvar-PYTHONPATH PYTHONPATH]</code> environment variable.
+
 
Doing so will allow you to simply put:
+
You can modify the <code>[https://docs.python.org/3/using/cmdline.html?highlight=pythonpath#envvar-PYTHONPATH PYTHONPATH]</code> environment variable to include the SDK folder and Python will automatically search this folder.
 +
Doing so will allow you to simply specify the Truxton library at the top of your scripts:
  
 
<source lang="Python">
 
<source lang="Python">
Line 12: Line 13:
 
</source>
 
</source>
  
At the top of your Python scripts.
+
If you don't have permission to modify environment variables, or you want to test your code with different versions of Truxton, you can manually modify Python's module search path.
 +
Instead of using <code>PYTHONPATH</code> environment variable, you can tell Python which folder contains <code>truxton.pyd</code> by adding the following to the top of your scripts:
 +
 
 +
<source lang="Python">
 +
import sys
 +
sys.path.append('C:/Program Files/Truxton/SDK')
 +
import truxton
 +
</source>
 +
 
 +
You can, within code, change which version of Truxton will be used with your script.
 +
This ensures your script will always work should a breaking change be introduced in a subsequent release of Truxton.
  
 
When you import <code>truxton</code> you will get all of the predefined constants for [[File Types Supported|file type,]] [[DATA TYPE|data type,]] [[Origin|origins,]] [[Location Types|locations,]]
 
When you import <code>truxton</code> you will get all of the predefined constants for [[File Types Supported|file type,]] [[DATA TYPE|data type,]] [[Origin|origins,]] [[Location Types|locations,]]
Line 31: Line 42:
 
Once you have this working, you can start playing with Truxton.
 
Once you have this working, you can start playing with Truxton.
 
<source lang="Python">
 
<source lang="Python">
 +
import sys
 +
sys.path.append('C:/Program Files/Truxton/SDK')
 
import truxton
 
import truxton
  
Line 40: Line 53:
 
   main()
 
   main()
 
</source>
 
</source>
 
You can create a folder for your project, copy the SDK contents into it and work from there.
 
  
 
=Style=
 
=Style=

Revision as of 10:45, 9 February 2021

Truxton supports Python 3.8 via the Truxton.pyd library.

Overview

You must use the "import" directive to load the Truxton Python provider in truxton.pyd. This directive will search a variety of folders for the pyd file for Truxton (truxton.pyd). Normally, this file is installed in the SDK folder (C:\Program Files\Truxton\SDK) along with all of the dependent files.

You can modify the PYTHONPATH environment variable to include the SDK folder and Python will automatically search this folder. Doing so will allow you to simply specify the Truxton library at the top of your scripts:

import truxton

If you don't have permission to modify environment variables, or you want to test your code with different versions of Truxton, you can manually modify Python's module search path. Instead of using PYTHONPATH environment variable, you can tell Python which folder contains truxton.pyd by adding the following to the top of your scripts:

import sys
sys.path.append('C:/Program Files/Truxton/SDK')
import truxton

You can, within code, change which version of Truxton will be used with your script. This ensures your script will always work should a breaking change be introduced in a subsequent release of Truxton.

When you import truxton you will get all of the predefined constants for file type, data type, origins, locations, messages, entities, relations, events, media, objects, url methods, and urls. You can see them all by issuing a dir command.

>>> dir(truxton)

This will allow your Python code to use the same constant names as C, C++ and C# code.

>>> print(truxton.Type_JPEG)
203

Once you have this working, you can start playing with Truxton.

import sys
sys.path.append('C:/Program Files/Truxton/SDK')
import truxton

def main():
  t = truxton.create()
  print(t.version)

if __name__ == "__main__":
  main()

Style

In general, when using a Truxton Python object, you will follow this pattern:

  1. Create the object
  2. Set the data members of the object
  3. Call save() to store the object in the Truxton database.

Until you call save(), nothing will appear in Truxton.

Classes

Truxton exposes several classes to Python.

Remarks

The Python API is designed primarily for programs to contribute to Truxton.

Truxton.pyd was developed using the C API with a thin adapter layer of code to translate from Python provider semantics to Truxton calls.

Samples