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

Teradata® Package for Python User Guide

Product
Teradata Package for Python
Release Number
17.00
Published
November 2021
Language
English (United States)
Last Update
2022-01-14
dita:mapPath
bol1585763678431.ditamap
dita:ditavalPath
ayr1485454803741.ditaval
dita:id
B700-4006
lifecycle
previous
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)