Example #1
0
"""
.
../snippets/intro_connector.py
"""
import snowmobile

sn = snowmobile.connect()
sn2 = snowmobile.connect(creds="creds1")

sn.cfg.connection.current == sn2.cfg.connection.current  #> True
sn.sql.current("schema") == sn2.sql.current("schema")  #> True
sn.sql.current("session") == sn2.sql.current("session")  #> False

type(sn)  #> snowmobile.core.connection.Snowmobile

type(sn.cfg)  #> snowmobile.core.configuration.Configuration
str(sn.cfg)  #> snowmobile.Configuration('snowmobile.toml')

type(sn.con)  #> snowflake.connector.connection.SnowflakeConnection
type(sn.cursor)  #> snowflake.connector.cursor.SnowflakeCursor

df1 = sn.query("select 1")  #  == pd.read_sql()
type(df1)  #> pandas.core.frame.DataFrame

cur1 = sn.ex("select 1")  #  == SnowflakeConnection.cursor().execute()
type(cur1)  #> snowflake.connector.cursor.SnowflakeCursor

dcur1 = sn.exd(
    "select 1")  #  == SnowflakeConnection.cursor(DictCursor).execute()
type(dcur1)  #> snowflake.connector.DictCursor
"""
Inspect the parsing of a sql file containing two bare statements.
../docs/snippets/script/a-few-statements.py
"""

# Setup -----------------------------------------------------------------------
from pathlib import Path
paths = {p.name: p for p in Path.cwd().glob('**/*.sql')}
path = paths['a-few-tagged-statements.sql']

import snowmobile

sn = snowmobile.connect(delay=True)

# Example ---------------------------------------------------------------------

# -- Block 1 --
script = snowmobile.Script(path=path, sn=sn)
script.dtl()
"""
>>>
a-few-tagged-statements.sql
===========================
1: Statement('select data~s1')
2: Statement('select data~sample_table')
"""

# -- Block 2 --
s1, s2 = script(1), script(2)
print(s1.nm)  #> select data~s1
print(s2.nm)  #> select data~sample_table
Example #3
0
"""
Establish an initial connection and explore :class:`Connector` attributes.
../docs/snippets/test_connection.py
"""
import snowmobile

sn = snowmobile.connect()  # optionally provide `creds='credentials_alias'`
assert sn.alive

type(sn)  # > snowmobile.core.connection.Snowmobile

type(sn.cfg)  # > snowmobile.core.configuration.Configuration
str(sn.cfg)  # > snowmobile.Configuration('snowmobile.toml')

type(sn.con)  # > snowflake.connector.connection.SnowflakeConnection
type(sn.cursor)  # > snowflake.connector.cursor.SnowflakeCursor

df1 = sn.query("select 1")  #  == pd.read_sql()
type(df1)  # > pandas.core.frame.DataFrame

cur1 = sn.query("select 1",
                as_df=False)  #  == SnowflakeConnection.cursor().execute()
type(cur1)  # > snowflake.connector.cursor.SnowflakeCursor

import pandas as pd

df2 = pd.read_sql(sql="select 1", con=sn.con)
cur2 = sn.con.cursor().execute("select 1")

print(df2.equals(df1))  # > True
print(cur1.fetchone() == cur2.fetchone())  # > True
Example #4
0
def sn_delayed():
    """Returns a delayed `Connector` object."""
    import snowmobile

    return snowmobile.connect(creds=CREDS, config_file_nm=CONFIG_FILE_NM, delay=True)
Example #5
0
"""
Demonstrate calling .connect() on existing Snowmobile instances.
..docs/snippets/snowmobile/connector_delayed2.py
"""
import snowmobile

# -- Delayed Connection --
sn_del = snowmobile.connect(delay=True)

print(type(sn_del.con))  #> None
sn_del.connect()
print(type(sn_del.con))  #> snowflake.connector.connection.SnowflakeConnection

# -- Live Connection --
sn_live = snowmobile.connect()

session1 = sn_live.sql.current('session')
sn_live.connect()
session2 = sn_live.sql.current('session')
print(session1 != session2)  #> True

# -- complete example; should run 'as is' --
# snowmobile-include
Example #6
0
def get_testing_script(nm: str, results_limit: int = 1):
    path = Path(__file__).absolute().parent.parent / nm
    sn = snowmobile.connect(delay=True,
                            config_file_nm="snowmobile_testing.toml")
    sn.cfg.script.markup.result_limit = results_limit
    return snowmobile.Script(path=path, sn=sn)
Example #7
0
"""Demonstrate primary methods for executing raw sql.
../docs/snippets/snowmobile/executing.py
"""
import snowmobile

sn = snowmobile.connect()

# -- sn.query() ---------------------------------------------------------------
df = sn.query("select 1")  #  == pd.read_sql()
type(df)  #> pandas.core.frame.DataFrame

# -- pd.read_sql() --
import pandas as pd

df2 = pd.read_sql(sql="select 1", con=sn.con)

print(df2.equals(df))  #> True

# -- sn.ex() ------------------------------------------------------------------
cur = sn.ex("select 1")  #  == SnowflakeConnection.cursor().execute()
type(cur)  #> snowflake.connector.cursor.SnowflakeCursor

# -- SnowflakeConnection.cursor().execute() --
cur2 = sn.con.cursor().execute("select 1")

print(cur.fetchone() == cur2.fetchone())  #> True

# -- sn.exd() -----------------------------------------------------------------
dcur = sn.exd(
    "select 1")  #  == SnowflakeConnection.cursor(DictCursor).execute()
type(dcur)  #> snowflake.connector.DictCursor