Skip to main content

TypeDB Driver for Python

Project description

TypeDB Python Driver

Driver Architecture

To learn about the mechanism that TypeDB drivers use to set up communication with databases running on the TypeDB Server, refer to the Drivers Overview.

API Reference

To learn about the methods available for executing queries and retrieving their answers using Python, refer to the API Reference.

Install TypeDB Python Driver through Pip

  1. Install typedb-driver using pip:
pip install typedb-driver
  1. If multiple Python versions are available, you may wish to use:
pip3 install typedb-driver
  1. Make sure a TypeDB Server is running.
  2. In your python program, import from typedb.driver (see Example usage or tests/integration for examples):
from typedb.driver import *

driver = TypeDB.driver(addresses=TypeDB.DEFAULT_ADDRESS, ...)

Example usage

from typedb.driver import *


class TypeDBExample:

    def typedb_example(self):
        # Open a driver connection. Specify your parameters if needed
        # The connection will be automatically closed on the "with" block exit
        with TypeDB.driver(TypeDB.DEFAULT_ADDRESS, Credentials("admin", "password"),
                           DriverOptions(DriverTlsConfig.disabled())) as driver:
            # Create a database
            driver.databases.create("typedb")
            database = driver.databases.get("typedb")

            # Use "try" blocks to catch driver exceptions
            try:
                # Open transactions of 3 types
                tx = driver.transaction(database.name, TransactionType.READ)

                # Execute any TypeDB query using TypeQL. Wrong queries are rejected with an explicit exception
                result_promise = tx.query("define entity i-cannot-be-defined-in-read-transactions;")

                print("The result is still promised, so it needs resolving even in case of errors!")
                result_promise.resolve()
            except TypeDBDriverException as expected_exception:
                print(f"Once the query's promise is resolved, the exception is revealed: {expected_exception}")
            finally:
                # Don't forget to close the transaction!
                tx.close()

            # Open a schema transaction to make schema changes
            # Transactions can be opened with configurable options. This option limits its lifetime
            options = TransactionOptions(transaction_timeout_millis=10_000)

            # Use "with" blocks to forget about "close" operations (similarly to connections)
            with driver.transaction(database.name, TransactionType.SCHEMA, options) as tx:
                define_query = """
                define 
                  entity person, owns name, owns age; 
                  attribute name, value string;
                  attribute age, value integer;
                """
                answer = tx.query(define_query).resolve()
                if answer.is_ok():
                    print(f"OK results do not give any extra interesting information, but they mean that the query "
                          f"is successfully executed!")

                # Commit automatically closes the transaction. It can still be safely called inside "with" blocks
                tx.commit()

            # Open a read transaction to safely read anything without database modifications
            with driver.transaction(database.name, TransactionType.READ) as tx:
                answer = tx.query("match entity $x;").resolve()

                # Collect concept rows that represent the answer as a table
                rows = list(answer.as_concept_rows())
                row = rows[0]

                # Collect column names to get concepts by index if the variable names are lost
                header = list(row.column_names())

                column_name = header[0]

                # Get concept by the variable name (column name)
                concept_by_name = row.get(column_name)

                # Get concept by the header's index
                concept_by_index = row.get_index(0)

                print(f"Getting concepts by variable names ({concept_by_name.get_label()}) and "
                      f"indexes ({concept_by_index.get_label()}) is equally correct. ")

                # Check if it's an entity type before the conversion
                if concept_by_name.is_entity_type():
                    print(f"Both represent the defined entity type: '{concept_by_name.as_entity_type().get_label()}' "
                          f"(in case of a doubt: '{concept_by_index.as_entity_type().get_label()}')")

                # Continue querying in the same transaction if needed
                answer = tx.query("match attribute $a;").resolve()

                # Concept rows can be used as any other iterator
                rows = [row for row in answer.as_concept_rows()]

                for row in rows:
                    # Same for column names
                    column_names_iter = row.column_names()
                    column_name = next(column_names_iter)

                    concept_by_name = row.get(column_name)

                    # Check if it's an attribute type before the conversion
                    if concept_by_name.is_attribute_type():
                        attribute_type = concept_by_name.as_attribute_type()
                        print(f"Defined attribute type's label: '{attribute_type.get_label()}', "
                              f"value type: '{attribute_type.try_get_value_type()}'")


                    print(f"It is also possible to just print the concept itself: '{concept_by_name}'")

            # Open a write transaction to insert data
            with driver.transaction(database.name, TransactionType.WRITE) as tx:
                insert_query = "insert $z isa person, has age 10; $x isa person, has age 20, has name \"John\";"
                answer = tx.query(insert_query).resolve()

                # Insert queries also return concept rows
                rows = list(answer.as_concept_rows())
                row = rows[0]

                for column_name in row.column_names():
                    inserted_concept = row.get(column_name)
                    print(f"Successfully inserted ${column_name}: {inserted_concept}")
                    if inserted_concept.is_entity():
                        print("This time, it's an entity, not a type!")

                # It is possible to ask for the column names again
                header = [name for name in row.column_names()]

                x = row.get_index(header.index("x"))
                print("As we expect an entity instance, we can try to get its IID (unique identification): "
                      "{x.try_get_iid()}. ")
                if x.is_entity():
                    print(f"It can also be retrieved directly and safely after a cast: {x.as_entity().get_iid()}")

                # Do not forget to commit if the changes should be persisted
                print('CAUTION: Committing or closing (including leaving the "with" block) a transaction will '
                      'invalidate all its uncollected answer iterators')
                tx.commit()

            # Open another write transaction to try inserting even more data
            with driver.transaction(database.name, TransactionType.WRITE) as tx:
                # When loading a large dataset, it's often better not to resolve every query's promise immediately.
                # Instead, collect promises and handle them later. Alternatively, if a commit is expected in the end,
                # just call `commit`, which will wait for all ongoing operations to finish before executing.
                queries = ["insert $a isa person, has name \"Alice\";", "insert $b isa person, has name \"Bob\";"]
                for query in queries:
                    tx.query(query)
                tx.commit()

            with driver.transaction(database.name, TransactionType.WRITE) as tx:
                # Commit will still fail if at least one of the queries produce an error.
                queries = ["insert $c isa not-person, has name \"Chris\";", "insert $d isa person, has name \"David\";"]
                promises = []
                for query in queries:
                    promises.append(tx.query(query))

                try:
                    tx.commit()
                    assert False, "TypeDBDriverException is expected"
                except TypeDBDriverException as expected_exception:
                    print(f"Commit result will contain the unresolved query's error: {expected_exception}")

            # Open a read transaction to verify that the previously inserted data is saved
            with driver.transaction(database.name, TransactionType.READ) as tx:
                # Queries can also be executed with configurable options. This option forces the database
                # to include types of instance concepts in ConceptRows answers
                options = QueryOptions(include_instance_types=True)

                # A match query can be used for concept row outputs
                var = "x"
                answer = tx.query(f"match ${var} isa person;", options).resolve()

                # Simple match queries always return concept rows
                count = 0
                for row in answer.as_concept_rows():
                    x = row.get(var)
                    x_type = x.as_entity().get_type().as_entity_type()
                    count += 1
                    print(f"Found a person {x} of type {x_type}")
                print(f"Total persons found: {count}")

                # A fetch query can be used for concept document outputs with flexible structure
                fetch_query = """
                match
                  $x isa! person, has $a;
                  $a isa! $t;
                fetch {
                  "single attribute type": $t,
                  "single attribute": $a,
                  "all attributes": { $x.* },
                };
                """
                answer = tx.query(fetch_query).resolve()

                # Fetch queries always return concept documents
                count = 0
                for document in answer.as_concept_documents():
                    count += 1
                    print(f"Fetched a document: {document}.")
                    print(f"This document contains an attribute of type: {document['single attribute type']['label']}")
                print(f"Total documents fetched: {count}")

        print("More examples can be found in the API reference and the documentation.\nWelcome to TypeDB!")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

typedb_driver-3.11.3-py313-none-manylinux_2_17_x86_64.whl (7.1 MB view details)

Uploaded Python 3.13manylinux: glibc 2.17+ x86-64

typedb_driver-3.11.3-py313-none-manylinux_2_17_aarch64.whl (6.9 MB view details)

Uploaded Python 3.13manylinux: glibc 2.17+ ARM64

typedb_driver-3.11.3-py313-none-macosx_11_0_x86_64.whl (5.7 MB view details)

Uploaded Python 3.13macOS 11.0+ x86-64

typedb_driver-3.11.3-py313-none-macosx_11_0_arm64.whl (5.6 MB view details)

Uploaded Python 3.13macOS 11.0+ ARM64

typedb_driver-3.11.3-py312-none-manylinux_2_17_x86_64.whl (7.1 MB view details)

Uploaded Python 3.12manylinux: glibc 2.17+ x86-64

typedb_driver-3.11.3-py312-none-manylinux_2_17_aarch64.whl (6.9 MB view details)

Uploaded Python 3.12manylinux: glibc 2.17+ ARM64

typedb_driver-3.11.3-py312-none-macosx_11_0_x86_64.whl (5.7 MB view details)

Uploaded Python 3.12macOS 11.0+ x86-64

typedb_driver-3.11.3-py312-none-macosx_11_0_arm64.whl (5.6 MB view details)

Uploaded Python 3.12macOS 11.0+ ARM64

typedb_driver-3.11.3-py311-none-manylinux_2_17_x86_64.whl (7.1 MB view details)

Uploaded Python 3.11manylinux: glibc 2.17+ x86-64

typedb_driver-3.11.3-py311-none-manylinux_2_17_aarch64.whl (6.9 MB view details)

Uploaded Python 3.11manylinux: glibc 2.17+ ARM64

typedb_driver-3.11.3-py311-none-macosx_11_0_x86_64.whl (5.7 MB view details)

Uploaded Python 3.11macOS 11.0+ x86-64

typedb_driver-3.11.3-py311-none-macosx_11_0_arm64.whl (5.6 MB view details)

Uploaded Python 3.11macOS 11.0+ ARM64

typedb_driver-3.11.3-py310-none-manylinux_2_17_x86_64.whl (7.1 MB view details)

Uploaded Python 3.10manylinux: glibc 2.17+ x86-64

typedb_driver-3.11.3-py310-none-manylinux_2_17_aarch64.whl (6.9 MB view details)

Uploaded Python 3.10manylinux: glibc 2.17+ ARM64

typedb_driver-3.11.3-py310-none-macosx_11_0_x86_64.whl (5.7 MB view details)

Uploaded Python 3.10macOS 11.0+ x86-64

typedb_driver-3.11.3-py310-none-macosx_11_0_arm64.whl (5.6 MB view details)

Uploaded Python 3.10macOS 11.0+ ARM64

typedb_driver-3.11.3-py39-none-manylinux_2_17_x86_64.whl (7.1 MB view details)

Uploaded Python 3.9manylinux: glibc 2.17+ x86-64

typedb_driver-3.11.3-py39-none-manylinux_2_17_aarch64.whl (6.9 MB view details)

Uploaded Python 3.9manylinux: glibc 2.17+ ARM64

typedb_driver-3.11.3-py39-none-macosx_11_0_x86_64.whl (5.7 MB view details)

Uploaded Python 3.9macOS 11.0+ x86-64

typedb_driver-3.11.3-py39-none-macosx_11_0_arm64.whl (5.6 MB view details)

Uploaded Python 3.9macOS 11.0+ ARM64

File details

Details for the file typedb_driver-3.11.3-py313-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py313-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 004d73ce6de21fd1fe22bd29993cacd06445d756b1880ab1c62ace2d62a721da
MD5 160cf99f8b6d50c3b9d1c3d3c264fccd
BLAKE2b-256 5cd39bfcc78a733456a53ead835dc6ffc57a2389299754adbc27f6459d5539a7

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py313-none-manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py313-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 63479cf629c9ba8e994c35969467f10eddcd265fa1383bc0e2f67befaee94878
MD5 9bfb2fb6c5a52d3a9b9935df63e404d0
BLAKE2b-256 400144b08037299b6c9ea326f6e10ad869b6d68812f7436e64742cff2aebac5c

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py313-none-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py313-none-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 48e4b660e569a44c9fc31f39a5d2684b946b80b2c2e6f3f513e81ec03c8c8794
MD5 3f4059f79e965a781f7e992d1ddb3e69
BLAKE2b-256 6c114f9176a2d9d2509747b0ffed485ebe53b3da1567a10417fcfa3c7b0a32d1

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py313-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py313-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f04c620733b807594430952c8bb2cc6702d6cd2fa4083950b7402b09bb5ddeb4
MD5 5d4367019637a99ac224ed20452b7098
BLAKE2b-256 d8f6d6366116edb16d2602c2453d1d5bea02e001ef56debb0fcdc6e91a1ecc17

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py312-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py312-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 1b442ab6271befd553f7a4f699d6d79a615ee5703ab54316600bd7c1e38d3992
MD5 32bbf9ed614db9ceab3dacd5242d79be
BLAKE2b-256 5eb63c5b2419a162967612b3219816100cba85272244ff94bbecce59fdc0474f

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py312-none-manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py312-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 0b3d1e328ebb5305337ff0c809ec06fb2acfc6970e514f4acb1e418bf10a1c05
MD5 89d1171f19be420474b2dc68285d7d19
BLAKE2b-256 7334db461f8033f7af53a92863cac2540f4ecca13eb8e0fc1de8a100df36bceb

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py312-none-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py312-none-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 cd8d3e5618aba106d5497fa65b20f75dddb2d5fbc350c977e1eb575e154b38de
MD5 b7b357e4df2be6a59773cdfd451b9bcc
BLAKE2b-256 6edc5119a3d67d05a6ac1842b092a41c0b72a9255f827eeabe5f62180c9b8c96

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py312-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py312-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3c2e49678045e1dc9b463e9d4d5f2068073e32a6617e4d7efc675440f2c8dadc
MD5 893f5f6fdd48f5bb23889fb2244cae37
BLAKE2b-256 f2c45f0e4d321555701d6d95d7d245c810a19d08ae457639c8a8083f1148f027

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py311-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py311-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 9ca6f8dd839efc6920697128d7ce214c3b90a64d2ef04bf5606998d5488cd81c
MD5 7353f125b13063b72e9864ffcc7f321e
BLAKE2b-256 7149bf672f4a4e7449193de88eb564a3d367da1aa3bbc38fc5b3a93e205b312d

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py311-none-manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py311-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 49f1d757d86ec99cbf487ee72f428548b89771d2e1d8eb960a0d463cad56b0c6
MD5 04587014634c677d5222db017a849f9d
BLAKE2b-256 9e10b01d9d4908fb164e820ecdafef54b31cf4de3062155802846abff2750e8e

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py311-none-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py311-none-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 be4b8b727128b40e875a623eb430b102187076901e98e29e7dbeefb5f38da856
MD5 968d48a87a624b3f196e5dc04837ff99
BLAKE2b-256 afff4b482328bf0d246ffc0d08090aedb3f0325b3d08db6f501ba23d07d15612

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py311-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py311-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 f64384de02ef7d746a34c7604e1940bf003b634ec7dbae561b3a2133837d83be
MD5 37999201d22523b27117e2fe9ef7a704
BLAKE2b-256 60028e0a558c358b60529601f81caf6a34eb4e523767ac6fde28de5d29deebfd

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py310-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py310-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 468acd4ee3f19eb360a77e2ca7bada2e0a5a5849335f2779eec0539bafa1244a
MD5 e32a899c6aabb8a2ccb0344156d2c515
BLAKE2b-256 c0f330a872f59527462f51b8829412f11e9e98a15004a0307f5079bd9d14b39b

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py310-none-manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py310-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 39d25a3b37cce4f120220fb87cbd723e9e38322d95bc6d56e7833105c0ec7f5e
MD5 1f92d9d978d4eaf18ecb0213c01f2374
BLAKE2b-256 db3e74d805ee19e2be9eb288c3e274d26ea4dfcb77c829990c31917115e3382a

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py310-none-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py310-none-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 18769b017d9bcbcb8d395ac9c71a37c0915ec5b48ea7a2b936ac21d426a23334
MD5 0556349844627c32def1502a72b38142
BLAKE2b-256 3f21fffb818cf5dc5814dd7dee6f7d8c11baeae483ec58c246ff958f93d8f12a

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py310-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py310-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c396c64148f44422402c508b2dde138b21d1406689b74e0719ff53825d0dde51
MD5 f5b12df07a5976e0217b16d326ff13bd
BLAKE2b-256 3eda79c68951b56a0da8c13aa51c13bfbbb115181460c3df680c37da2ab49c11

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py39-none-manylinux_2_17_x86_64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py39-none-manylinux_2_17_x86_64.whl
Algorithm Hash digest
SHA256 259d8769b79e5960b33a395b54807bfcc6ef5b3d3e02a553c897b5f1ec02a7f9
MD5 0d6027365a991e03e785e26881014f46
BLAKE2b-256 2ff33e59d44548031cd142bc10db32a8690010dc15f9e321989da26b7ee7c15e

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py39-none-manylinux_2_17_aarch64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py39-none-manylinux_2_17_aarch64.whl
Algorithm Hash digest
SHA256 ede6ffc891fa826aa5d17fa6d54534e1cbc54583506ce97d5b141e97aebf99af
MD5 d31b076b13833b29dfcfc8f8d06df745
BLAKE2b-256 d279faf6a8aecdca92418a503850f45af2903e1983b16320f0111d8aec9679a9

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py39-none-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py39-none-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 295c21e0bd2238db199fcc8b7fa1591b6edbdf69cd087830b6b197c7946500b8
MD5 be2eb94c3755919c7a851f8da0e3ba21
BLAKE2b-256 1df6e64c68040c0613cdc72f78f23034911804b89006360947a31e68128b885f

See more details on using hashes here.

File details

Details for the file typedb_driver-3.11.3-py39-none-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for typedb_driver-3.11.3-py39-none-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0605d08df7d911eb784dc4a644e4cfca983725fb4b439e543680caec3bb9ea26
MD5 50455c057364495e0adab9a8f425dd2e
BLAKE2b-256 e8b5de564064c71fde7b642c53056b6c39cb77285b660c6045973a0b9a127b8f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page