{ "info": { "author": "Daltix NV", "author_email": "snowconn@daltix.com", "bugtrack_url": null, "classifiers": [], "description": "# SnowConn\n\nThis repository is a wrapper around the [snowflake SQLAlchemy](https://docs.snowflake.net/manuals/user-guide/sqlalchemy.html)\nlibrary. It manages the creation of connections and provides a few convenience functions that should be good enough\nto cover most use cases yet be flexible enough to allow additional wrappers to be written around to serve more specific\nuse cases for different teams. \n\n## Installation\n\nTo install latest version released to pypi with pip:\n\n```bash\npip install snowconn\n```\n\nTo install the latest version directly from the repo:\n\n```bash\npip install 'git+ssh://git@github.com/Daltix/SnowConn.git@master#egg=snowconn'\n```\n\n## Connection\n\nEverything is implemented in a single `SnowConn` class. To import it is always the same:\n\n```py\nfrom snowconn import SnowConn\n```\n\n### (1) Connection using your own personal creds\n\nInstall [snowsql](https://docs.snowflake.net/manuals/user-guide/snowsql-install-config.html)\nand then configure the `~/.snowsql/config` as per the instructions. You can test that it is correctly installed\nby then executing `snowsql` from the command line. \n\n*WARNING* Be sure to configure your account name like the following:\n\n```\naccountname = eq94734.eu-west-1\n```\n\nIf you don't include the `eu-west-1` part, it will hang for about a minute and then give you a permission denied.\n\nNow that you are able to execute `snowsql` to successfully connect, you are ready to use the `SnowConn.connect` function:\n\n```py\nconn = SnowConn.connect()\n```\nThat's it you are connected! You can connect to a specific schema / database with the following:\n\n```py\nconn = SnowConn.connect('daltix_prod', 'public')\n```\n\n### (2) Connection using aws secrets manager\n\nYou need to have boto3 installed which you can do so with the following:\n\n```\npip install boto3\n```\n\nNow you must satisfy the folloing requirements:\n\n1. Have a secret stored in an accessable aws account\n1. The secret must have the following keys:\n - `USERNAME`\n - `PASSWORD`\n - `ACCOUNT`\n - `ROLE`\n\nFor this example, we will assume the `price_plotter` is the secret manager that we will be using. \n\nNow that you know the name of the secret, you MUST be sure that the context in which it is running has access to read\nthat secret. Once this is done, you can now execute the following code:\n\n```py\nconn = SnowConn.credsman_connect('price_plotter')\n```\n\nAnd you are connected! You can also pass the database and schema along\n\n```py\nconn = SnowConn.credsman_connect('price_plotter', 'daltix_prod', 'public')\n```\n\nAn example of a policy that gives access to the `price_plotter` looks like this:\n\n```\n{\n \"Version\": \"2012-10-17\",\n \"Statement\": [\n {\n \"Sid\": \"VisualEditor0\",\n \"Effect\": \"Allow\",\n \"Action\": [\n \"secretsmanager:GetResourcePolicy\",\n \"secretsmanager:GetSecretValue\",\n \"secretsmanager:DescribeSecret\",\n \"secretsmanager:ListSecretVersionIds\"\n ],\n \"Resource\": \"arn:aws:secretsmanager:eu-west-1::secret:price_plotter-AdcNpp\"\n }\n ]\n}\n```\n\nAnd an example of this in a serverless.yml looks like this:\n\n```\niamRoleStatements:\n - Effect: Allow\n Action:\n - secretsmanager:DescribeSecret\n - secretsmanager:List*\n Resource:\n - \"*\"\n - Effect: Allow\n Action:\n - secretsmanager:*\n Resource:\n - { Fn::Sub: \"arn:aws:secretsmanager:${AWS::Region}:${AWS::AccountId}:secret:price_plotter-??????\" }\n```\n\n## API\n\nNow that you're connected, there are a few low-level functions that you can use to programatically interact with\nthe snowflake tables that you have access to.\n\nThe rest of these examples assume that you have used one of the above methods to connect and have access to the\n`daltix_prod.public.price` table.\n\n### execute_simple\n\nThe exc_simple function is used for when you have a single statement to execute and the result set can fit into memory. It\ntakes a single argument which a string of the SQL statement that you with to execute. Take the following for example:\n\n```py\n>>> conn.execute_simple('select * from price limit 1;')\n[{'DALTIX_ID': '0d3c30353035a6ab5747237a1f2600bbf5ddd27401372c5effe0f2790a88ad56', 'SHOP': 'ahed', 'COUNTRY': 'de', 'PRODUCT_ID': '616846.0', 'LOCATION': 'base', 'PRICE': 37.99, 'PROMO_PRICE': None, 'PRICE_STD': None, 'PROMO_PRICE_STD': None, 'UNIT': None, 'UNIT_STD': None, 'IS_MAIN': True, 'VENDOR': None, 'VENDOR_STD': None, 'DOWNLOADED_ON': datetime.datetime(2018, 11, 18, 0, 0, 1), 'DOWNLOADED_ON_LOCAL': datetime.datetime(2018, 11, 18, 1, 0, 1), 'DOWNLOADED_ON_DATE': datetime.date(2018, 11, 18), 'IS_LATEST_PRICE': False}]\n```\n\n### execute_string\n\nIf you have multiple sql statements in a single string that you want to execute or the resultset is larger than\nwill fit into memory, this is the function that you want to use. It returns a list of cursors that are a result\nof each of the statements that are contained in the string. See [here](https://docs.snowflake.net/manuals/user-guide/python-connector-api.html#execute_string) for the full documentation.\n\n```py\n>>> conn.execute_string('create temporary table price_small as (select * from price limit 1); select * from price_small;')\n[, ]\n```\n\n### execute_file\n\nIf you have the contents of an sql file that you want to execute, you can use this function. For example:\n\n```bash\necho \"select * from price limit 1;\" > query.sql\n```\n\n```py\n>>> conn.execute_file('query.sql')\n>>> []\n```\nThis also returns a list of cursors the same as `execute_string` does. In fact, this function is nothing more than a very\nsimple wrapper around `execute_string`.\n\n### read_df\n\nUse this function to read the results of a query into a dataframe. Note that pandas is NOT a dependency of this repo so\nif you want to use it you must satisfy this dependency yourself.\n\nIt takes one sql string as an argument and returns a dataframe.\n\n```bash\n>>> conn.read_df('select daltix_id, downloaded_on, price from price limit 5;')\n daltix_id downloaded_on price\n0 0d3c30353035a6ab5747237a1f2600bbf5ddd27401372c 2018-11-18 00:00:01 37.99\n1 f5be8a5da3bde2da6a63fcad4e5c30823027324092234c 2018-11-18 00:00:02 9.99\n2 f5be8a5da3bde2da6a63fcad4e5c30823027324092234c 2018-11-18 00:00:02 0.40\n3 807e2a7706b8c515264fa55bed3891d5685ac5ee0148f0 2018-11-18 00:00:04 3.70\n4 1e56339f99dc866cd4b87679aa686556a5ad2398d00c95 2018-11-18 00:00:06 3.76\n>>> \n```\n\n### write_df\n\nUse this to write a dataframe to Snowflake. This is a very thin wrapper around the pandas [DataFrame.to_sql()](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_sql.html) function.\n\nUnfortunately, it doesn't play nice with dictionaries and arrays so the use cases are quite limited. Hopefully\nwe will improve upon this in the future.\n\n### get_current_role\n\nReturns the current role.\n\n### close\n\nUse this to cleanly close all connections that have ever been associated with this instance of SnowConn. If you don't\nuse this your process will hang for a while without saying anything before it actually exits.\n\n## Accessing the connection objects directly\n\nThese functions are mostly wrappers around 2 connection libraries:\n\n- [The snowflake python connector](https://docs.snowflake.net/manuals/user-guide/python-connector-api.html)\n- [The snowflake SQLAlchemy library](https://docs.snowflake.net/manuals/user-guide/sqlalchemy.html)\n\nShould you need to use either of these yourself, you can ask for the connections yourself with the following\nfunctions:\n\n### get_raw_connection\n\nThis will return the instance of a snowflake connector which is documented [here](https://docs.snowflake.net/manuals/user-guide/python-connector-api.html#connect). It is a good choice if you have very simple needs and for some reason none\nof the functions in the rest of this repo are serving your needs.\n\n### get_alchemy_engine\n\nThis is the result of [create_engine()](https://docs.snowflake.net/manuals/user-guide/sqlalchemy.html#connection-parameters)\nwhich was called during `connect()` or `credsman_connect()`. It does not represent an active connection to the database\nbut rather acts as a factory for connections.\n\nThis is useful for using the most commonly abstracted things in other libraries such as dashboards, pandas, etc. \nHowever, like SQLAlchemy in general, despite being very widely supported and feature-complete, it is not the simplest \nAPI so it should probably not be your first choice unless you know exactly that you need it.\n\n### get_connection\n\nThis returns the result of the creating the sqlalchemy engine and then calling `connect()` on it. Unlike the result\nof `get_alchemy_engine` this represents an active connection to Snowflake and this has a session associated with it.\n\nYou can see the object documentation [here](https://docs.snowflake.net/manuals/user-guide/sqlalchemy.html#parameters-and-behavior)", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/Daltix/snowconn", "keywords": "", "license": "", "maintainer": "", "maintainer_email": "", "name": "snowconn", "package_url": "https://pypi.org/project/snowconn/", "platform": "", "project_url": "https://pypi.org/project/snowconn/", "project_urls": { "Homepage": "https://github.com/Daltix/snowconn" }, "release_url": "https://pypi.org/project/snowconn/3.5.10/", "requires_dist": null, "requires_python": "", "summary": "Python utilities for connection to the Snowflake data warehouse", "version": "3.5.10" }, "last_serial": 5928233, "releases": { "3.5.10": [ { "comment_text": "", "digests": { "md5": "86421d0c9fe18af31c44c8a2e0197b37", "sha256": "c456344c4d6d22c7de6b0219d6856968b69076a067e2717881a4157d3c819325" }, "downloads": -1, "filename": "snowconn-3.5.10.tar.gz", "has_sig": false, "md5_digest": "86421d0c9fe18af31c44c8a2e0197b37", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8475, "upload_time": "2019-10-04T13:05:51", "url": "https://files.pythonhosted.org/packages/01/b5/5d1e350321e32f31d2a0153116a0eb347c560557812b683e1a5e0a948567/snowconn-3.5.10.tar.gz" } ], "3.5.8": [ { "comment_text": "", "digests": { "md5": "8d154b86b71deb3749d910dfc9c0fd9f", "sha256": "d4cf795bd65998f52b33122ac164924c13881acf7034c20b76e6728f7b6b656e" }, "downloads": -1, "filename": "snowconn-3.5.8.tar.gz", "has_sig": false, "md5_digest": "8d154b86b71deb3749d910dfc9c0fd9f", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8444, "upload_time": "2019-06-13T06:24:25", "url": "https://files.pythonhosted.org/packages/c0/c5/2252699d9c667acd43a09d83dae4a4de8e0fb792347611306a7cc486f797/snowconn-3.5.8.tar.gz" } ], "3.5.9": [ { "comment_text": "", "digests": { "md5": "ae6d791c1d9fa0387724fa043199d3c1", "sha256": "dfd40d63c8f70b95fb9dbb1f03ff40adbb5ec1206ac941394621ed254fec774d" }, "downloads": -1, "filename": "snowconn-3.5.9.tar.gz", "has_sig": false, "md5_digest": "ae6d791c1d9fa0387724fa043199d3c1", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8453, "upload_time": "2019-09-05T12:19:06", "url": "https://files.pythonhosted.org/packages/6d/73/c9c36199ab4aadd84d879ee8c3e4a02684414cec2733fa308b7c6fbc26f0/snowconn-3.5.9.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "86421d0c9fe18af31c44c8a2e0197b37", "sha256": "c456344c4d6d22c7de6b0219d6856968b69076a067e2717881a4157d3c819325" }, "downloads": -1, "filename": "snowconn-3.5.10.tar.gz", "has_sig": false, "md5_digest": "86421d0c9fe18af31c44c8a2e0197b37", "packagetype": "sdist", "python_version": "source", "requires_python": null, "size": 8475, "upload_time": "2019-10-04T13:05:51", "url": "https://files.pythonhosted.org/packages/01/b5/5d1e350321e32f31d2a0153116a0eb347c560557812b683e1a5e0a948567/snowconn-3.5.10.tar.gz" } ] }