Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/8 handle multiple databases and users #20

Merged
merged 20 commits into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d696245
change config module to handle multiple databases
pmayd Oct 27, 2023
4c06192
finalize work on config module to handle multiple databases; signific…
pmayd Oct 27, 2023
0f13d31
add a new db module that serves as a layer between the user and the c…
pmayd Oct 27, 2023
c574d5f
simplify config module
pmayd Oct 28, 2023
857af8e
refactor code to implement new config; correct tests
pmayd Oct 28, 2023
8fcddc0
fix all remaining tests
pmayd Oct 29, 2023
1f74065
fix all text issues
pmayd Oct 29, 2023
4675f6d
update notebooks according to latest changes in config
pmayd Oct 29, 2023
28e8f4c
drop support for Python 3.9 due to pipe operator for types and set su…
pmayd Oct 29, 2023
f94093c
fix problem with config dir creation during setup
pmayd Oct 29, 2023
e6b5112
fix isort
pmayd Oct 29, 2023
724eb54
Improve clear_cache output for full wipe, remove unused import
MarcoHuebner Nov 1, 2023
a580d57
Address all non global-related pylint issues #20
MarcoHuebner Nov 1, 2023
8cd0d24
because of complexity get rid of the current support of custom config…
pmayd Nov 3, 2023
c754159
fix all tests; get rid of settings.ini and functionality for user to …
pmayd Nov 4, 2023
b2679da
fix all tests; get rid of settings.ini and functionality for user to …
pmayd Nov 4, 2023
2859462
refactor config module to work with a ConfigParser global config obje…
pmayd Nov 4, 2023
4a03be3
address pylint issues
pmayd Nov 4, 2023
e88b8b6
fix mypy issues
pmayd Nov 4, 2023
14de704
fix pylint issues
pmayd Nov 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ name: Run tests
on:
push:
branches:
- main
- dev
- main
- dev
pull_request:
branches:
- main
- dev
- main
- dev
workflow_dispatch:

env:
Expand All @@ -24,31 +24,31 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.10"]
os: [ubuntu-22.04, macOS-latest, windows-latest]
python-version: ["3.10", "3.11"]
os: [ubuntu-latest, macOS-latest, windows-latest]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Run poetry image
uses: abatilo/actions-poetry@v2.0.0
with:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v3
with:
python-version: ${{ matrix.python-version }}
- name: Run poetry image
uses: abatilo/actions-poetry@v2.0.0
with:
poetry-version: $POETRY_VERSION
- name: Install dependencies
run: |
poetry install --with dev
- name: Run tests
run: |
poetry run pytest --cov=pystatis tests
- name: Install dependencies
run: |
poetry install --with dev
- name: Run tests
run: |
poetry run pytest --cov=pystatis tests
code-quality:
strategy:
fail-fast: false
matrix:
# only support specific python version, as guidelines differ beween (minor) versions
python-version: ["3.10.13"]
python-version: ["3.11.6"]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Global options:

[mypy]
python_version = 3.9
python_version = 3.10
warn_return_any = True
warn_unused_configs = True
show_error_codes=True
Expand Down
135 changes: 135 additions & 0 deletions nb/00_Setup.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Setup `pystatis`\n",
"\n",
"You don't need to do much to use `pystatis`. Basically, the first time you import the package, it will create a `config.ini` file under `~/.pystatis`. This file is used for storing settings, for example your credentials fpr the supported databases.\n",
"\n",
"To set up your credentials, we need to ask you for your username and password. This is done by the `setup_credentials()` function. It will ask you interactivly for the credentials, or you can set the following environmental variables:\n",
"- `PYSTATIS_GENESIS_API_USERNAME`\n",
"- `PYSTATIS_GENESIS_API_PASSWORD`\n",
"- `PYSTATIS_ZENSUS_API_USERNAME`\n",
"- `PYSTATIS_ZENSUS_API_PASSWORD`\n",
"- `PYSTATIS_REGIO_API_USERNAME`\n",
"- `PYSTATIS_REGIO_API_PASSWORD`"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`dotenv` is uses here to load a local `.env` file that contains the above mentioned environmental variables so we don't have to input them."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "888706f5-3a9e-4e0a-9ca6-fa430280bc03",
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"\n",
"import dotenv\n",
"\n",
"import pystatis\n",
"\n",
"print(\"pystatis version: \", pystatis.__version__)\n",
"dotenv.load_dotenv()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ee7969b6",
"metadata": {},
"outputs": [],
"source": [
"# only execute if you want to delete your config file for test purposes\n",
"# config.delete_config()"
]
},
{
"cell_type": "markdown",
"id": "3928f347",
"metadata": {},
"source": [
"`init_config` is called when loading pystatis, so a config with empty credentials will be created in your user home directory by default."
]
},
{
"cell_type": "markdown",
"id": "18c2633f",
"metadata": {},
"source": [
"The only thing you have to do is to set up your user credentials.\n",
"\n",
"You can do so either by:\n",
"1. specifying the 4 environment variables `PYSTATIS_GENESIS_API_USERNAME|PASSWORD`, and `PYSTATIS_ZENSUS_API_USERNAME|PASSWORD`\n",
"2. calling the function `setup_credentials()` which will guide you through the process\n",
"\n",
"Even if you do 1. please call `setup_credentials()` once as it will read out the environment variables and write the credentials to the `config.ini` in your config dir."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6c301da5",
"metadata": {},
"outputs": [],
"source": [
"pystatis.setup_credentials() # also part of config module"
]
},
{
"cell_type": "markdown",
"id": "d7d92f0d",
"metadata": {},
"source": [
"Once you have set up your credentials, they are stored in the `config.ini` and in the `config` object of the `config.py` module. You don't have to know this as regular user, this is more internal knowledge."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Warning**: The following code will print out the content of your `config.ini` file **with** the credentials set, so please do not share or push this notebook with outputs enabled."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7ce103d9",
"metadata": {},
"outputs": [],
"source": [
"with open(Path.home() / \".pystatis\" / \"config.ini\") as f:\n",
" print(f.read())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
123 changes: 123 additions & 0 deletions nb/01_Databases.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Using different databases\n",
"\n",
"Once you have at least once called `setup_credentials()` you are good to use any method of this package with any of the supported databases.\n",
"\n",
"However, before you can do so, you have to tell `pystatis` which database you want to work with. \n",
"\n",
"This can be done with the `set_db()` function that is used to lock in the database for all subsequent called functions.\n",
"\n",
"If you don't know the names of the supported databases, ask `pystatis.config.get_supported_db()`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from pystatis import set_db, logincheck, db\n",
"from pystatis import config"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"config.get_supported_db()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Whenever `pystatis` needs to fetch data from a database, it calls `get_db()` internally to get the database set is currently set as active database. If `set_db()` was not called before, `get_db()` will throw an error and inform you about this."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# expected to fail so you know you have to call set_db() first\n",
"db.get_db()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# expected to fail!\n",
"set_db(\"test\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"set_db(\"genesis\")\n",
"logincheck()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"set_db(\"zensus\")\n",
"logincheck()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"set_db(\"regio\")\n",
"logincheck()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "pystatis",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
27 changes: 21 additions & 6 deletions nb/helloworld.ipynb → nb/02_helloworld.ipynb
Original file line number Diff line number Diff line change
@@ -1,15 +1,30 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Testing your credentials with the helloworld endpoints\n",
"\n",
"Once you have completed the setup as described [here](./00_Setup.ipynb), you can test your credentials with the `logincheck()` function oh the `helloworld` module."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import logging\n",
"logging.basicConfig(level=logging.INFO)\n",
"\n",
"from pystatis import logincheck, whoami"
"from pystatis import logincheck, whoami, set_db"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"set_db(\"genesis\")"
]
},
{
Expand All @@ -35,7 +50,7 @@
"metadata": {},
"outputs": [],
"source": [
"# test your login credentials (set via config functions)\n",
"# test your login credentials (set via setup_credentials() from config module)\n",
"logincheck()"
]
}
Expand All @@ -56,7 +71,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.7"
"version": "3.11.6"
},
"orig_nbformat": 4,
"vscode": {
Expand Down
Loading
Loading