Skip to content

Commit

Permalink
Added Redshift connector.
Browse files Browse the repository at this point in the history
  • Loading branch information
adocquin committed May 23, 2023
1 parent 340742e commit a263a8a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
1 change: 1 addition & 0 deletions coolpandas/connect/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""Connection to external services module."""
49 changes: 49 additions & 0 deletions coolpandas/connect/redshift.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import pandas as pd
import redshift_connector
from redshift_connector import Connection, Cursor


def get_redshift_cursor(host: str, database: str, user: str, password: str) -> Cursor:
"""Get a cursor to the Redshift database.
Args:
host (str): The host of the Redshift database
database (str): The name of the database
user (str): The user to connect to the database
password (str): The password to connect to the database
Returns:
Cursor: A cursor to the Redshift database
"""
conn: Connection = redshift_connector.connect(
host=host, database=database, user=user, password=password
)
return conn.cursor()


def get_table_columns_information(cursor: Cursor, redshift_table: str) -> tuple:
"""Get the columns information of the table.
Args:
cursor (Cursor): A cursor to the Redshift database
redshift_table (str): The name of the table
Returns:
tuple: A tuple containing the columns information of the table
"""
cursor.execute(f"SELECT pg_get_cols('{redshift_table}');")
return cursor.fetchall()


def query_to_dataframe(cursor: Cursor, query: str) -> pd.DataFrame:
"""Execute a query and return the result as a pandas DataFrame.
Args:
cursor (Cursor): A cursor to the Redshift database
query (str): A query to execute
Returns:
pd.DataFrame: The result of the query as a pandas DataFrame
"""
cursor.execute(query)
return cursor.fetch_dataframe()
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "coolpandas"
version = "0.3.1"
version = "0.3.2"
authors = [
{ name="Avel Docquin", email="adocquin@outlook.com" },
]
Expand Down Expand Up @@ -44,6 +44,7 @@ classifiers = [
[tool.setuptools]
packages = [
"coolpandas",
"coolpandas.connect",
"coolpandas.eda",
"coolpandas.evaluate",
"coolpandas.plot",
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plotly>=5.11.0
nbformat>=5.7.0
ipython>=8.6.0
openrouteservice>=2.3.3
redshift-connector>=2.0.910
statsmodels>=0.14.0
jinja2>=3.1.2
pytest-cov>=4.0.0
Expand Down

0 comments on commit a263a8a

Please sign in to comment.