set_context | Teradata Python Package - set_context - Teradata Package for Python

Teradata® Package for Python User Guide

Deployment
VantageCloud
VantageCore
Edition
Enterprise
IntelliFlex
VMware
Product
Teradata Package for Python
Release Number
20.00
Published
December 2024
ft:locale
en-US
ft:lastEdition
2025-01-23
dita:mapPath
nvi1706202040305.ditamap
dita:ditavalPath
plt1683835213376.ditaval
dita:id
rkb1531260709148
lifecycle
latest
Product Category
Teradata Vantage

The set_context function specifies a Vantage sqlalchemy engine as current context.

  • When overwriting an existing context associated with a Vantage connection, most of the operations on any teradataml DataFrames created before will not work.
  • Running the set_context() triggers garbage collection in teradataml. For more details of Garbage Collection,see Garbage Collection in teradataml.

Example

The following example creates two contexts using different host, then sets the context to the first one.

>>> from teradataml.context.context import *
>>> from teradataml.dataframe.dataframe import DataFrame
Create a connection:
>>> td_connection1 = create_context(host = "myhostname", username="myusername", password = "mypassword")
Overwrite the existing connection with new connection using 'create_context()':
>>> td_connection2 = create_context(host = "myhostname2", username="myusername2", password = "mypassword2")
302: UserWarning: [Teradata][teradataml](TDML_2002) Overwriting an existing context associated with Teradata connection. Most of the operations on any teradataml DataFrames created before this will not work.
  warnings.warn(Messages.get_message(MessageCodes.OVERWRITE_CONTEXT))
Get the engine currently associated with the context:
>>> get_context()
Engine(teradatasql://myusername2:***@myhostname2)
Overwrite the existing context, by passing a sqlalchemy engine object as input to 'set_context()', thereby creating a new context:
>>> set_context(tdsqlengine = td_connection1)
302: UserWarning: [Teradata][teradataml](TDML_2002) Overwriting an existing context associated with Teradata connection. Most of the operations on any teradataml DataFrames created before this will not work.
  warnings.warn(Messages.get_message(MessageCodes.OVERWRITE_CONTEXT))
>>> get_context()
Engine(teradatasql://myusername:***@myhostname)