Skip to content

Commit 2f259a3

Browse files
committed
Collect variables in .env, extend README.md, extend build process.
2 parents 500054a + dd81295 commit 2f259a3

14 files changed

+145
-46
lines changed

.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
PLANKQ_API_KEY="plqk_4Rk6rDnjDp6D38cRQXgGigFymWpCt3R7jw27kUZAcP"
2+
PLANKQ_TOKEN_URL="https://gateway.platform.planqk.de/token"
3+
PLANQK_DATA_POOL_URL="https://platform.planqk.de/qc-catalog/data-pools"
4+
PLANQK_OPEN_API_SPEC_URL="https://platform.planqk.de/qc-catalog/docs"

README.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,18 @@
22
````shell
33
.\install.sh
44
pip install .
5-
````
5+
````
6+
In *install.sh* the python client library is generated from the openapi-spec at https://platform.planqk.de/qc-catalog/docs.
7+
Addintionally there are some problems with generating the correct types which are also handled by replacing the lines with the script.
8+
9+
# Test
10+
````shell
11+
pytest -s -m MARK .
12+
````
13+
14+
Values for **MARK** are defined in the pyproject.toml
15+
- **interactive**: only run tests with user interaction
16+
- **slow_service**: only run tests wich are slow because of the service creation
17+
- **auto**: only run tests that run fully automatic
18+
19+
By setting **log_cli_level** to **DEBUG** you get a finer logging output.

install.sh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#!/bin/bash
22

3+
# Read .env
4+
source .env
5+
36
# Install openapi-generator-cli globally
47
npm install -g @openapitools/openapi-generator-cli
58

@@ -10,7 +13,7 @@ mkdir generator-output && cd generator-output || exit
1013
openapi-generator-cli version-manager set 5.4.0
1114

1215
# Generate Python client using the specified OpenAPI URL
13-
openapi-generator-cli generate -g python -i https://platform.planqk.de/qc-catalog/docs
16+
openapi-generator-cli generate -g python -i $PLANQK_OPEN_API_SPEC_URL
1417

1518
# Copy the generated client to the parent directory
1619
cp -R openapi_client ../src

pyproject.toml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ dependencies = [
1919
"requests>=2.31.0",
2020
"python-dateutil>=2.8.2",
2121
"urllib3>=2.2.0",
22-
"names-generator>=0.1.0"
22+
"names-generator>=0.1.0",
23+
"python-dotenv>=1.0.1"
2324
]
2425
dynamic = ["version"]
2526

@@ -32,9 +33,9 @@ version_file = "src/pyplanqk/version.py"
3233

3334
[tool.pytest.ini_options]
3435
markers = [
35-
"previously_wrong: only run previously wrong tests",
3636
"interactive: only run tests with user interaction",
3737
"slow_service: only run tests wich are slow because of the service creation",
38+
"auto: only run tests that run fully automatic",
3839
]
3940
log_cli = true
40-
log_cli_level = "INFO"
41+
log_cli_level = "DEBUG"

src/pyplanqk/helpers.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
import os
23
import time
34
from typing import Dict
45

@@ -106,3 +107,12 @@ def wait_for_service_job_to_be_finished(
106107
return False
107108
else:
108109
logger.debug(f"{status_timer + 1}|{timeout} Wait for job...")
110+
111+
112+
def get_path_delimiter() -> str:
113+
path_delimiter: str = ""
114+
if os.name == "nt":
115+
path_delimiter: str = "\\"
116+
elif os.name == "posix":
117+
path_delimiter: str = "/"
118+
return path_delimiter

src/pyplanqk/high_level_actions.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1+
from dotenv import load_dotenv
2+
13
from pyplanqk.low_level_actions import *
24

35
logger = logging.getLogger(__name__)
46

7+
load_dotenv(".env")
8+
PLANKQ_TOKEN_URL = os.getenv("PLANKQ_TOKEN_URL")
9+
510

611
class PyPlanQK:
712
def __init__(self, api_key):
813
self.api_key = {"apiKey": api_key}
9-
self.token_url = "https://gateway.platform.planqk.de/token"
14+
self.token_url = PLANKQ_TOKEN_URL
1015

1116
def create_service(self, config: Dict[str, Any]) -> Dict[str, Any]:
1217
service_name = None
@@ -50,6 +55,7 @@ def execute_service(
5055

5156
try:
5257
if data_ref is not None:
58+
logger.debug(f"triggering serice job with data pool: {data_ref}.")
5359
job = trigger_service_job(
5460
service_name=service_name,
5561
api_key=self.api_key,
@@ -58,6 +64,7 @@ def execute_service(
5864
params=params,
5965
)
6066
else:
67+
logger.debug(f"triggering serice job with data upload: {data}.")
6168
job = trigger_service_job(
6269
service_name=service_name,
6370
api_key=self.api_key,
@@ -84,18 +91,21 @@ def create_data_pool(self, data_pool_name: Optional[str], file) -> Dict[str, Any
8491
if data_pool is not None:
8592
logger.info(f"Data pool: {data_pool_name} already created.")
8693
return data_pool
94+
logger.debug(f"data pool: {data_pool_name} not found. Creating...")
95+
logger.debug(f"data pool: {data_pool_name} not found. Creating...")
8796

8897
create_data_pool(data_pool_name, self.api_key["apiKey"])
89-
98+
logger.debug(f"data pool: {data_pool_name} created. Adding data...")
9099
add_data_to_data_pool(data_pool_name, file, self.api_key["apiKey"])
91-
100+
logger.debug(f"data added to data pool")
92101
file_infos = get_data_pool_file_information(
93102
data_pool_name, self.api_key["apiKey"]
94103
)
95-
file_name = file.name.split("/")[-1]
104+
file_name = file.name.split(get_path_delimiter())[-1]
96105
file_info = file_infos[file_name]
97106
return file_info
98107
except Exception as e:
99108
logger.error(f"Creation of data pool: {data_pool_name} failed.")
109+
logger.error(f"file: {file.name} could not be added to data pool.")
100110
logger.error(e)
101111
raise e

src/pyplanqk/low_level_actions.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import json
2-
import os
32
from typing import Any, List, Optional
43

54
from openapi_client.apis import ServicePlatformApplicationsApi
@@ -718,8 +717,10 @@ def get_data_pool(data_pool_name: str, api_key: str) -> Optional[Dict[str, str]]
718717
found_data_pool = None
719718
for data_pool in data_pools:
720719
if data_pool_name == data_pool["name"]:
720+
logger.debug(f"Get Pool: Found it!")
721721
found_data_pool = data_pool
722-
722+
return found_data_pool
723+
logger.debug(f"Get Pool: Didn't found it!")
723724
return found_data_pool
724725
except Exception as e:
725726
logger.error("Get data pool failed.")
@@ -781,9 +782,14 @@ def get_data_pool_file_information(data_pool_name: str, api_key: str) -> Dict[st
781782

782783
def add_data_to_data_pool(data_pool_name: str, file, api_key: str) -> bool:
783784
logger.debug("Add data to data pool.")
784-
785785
try:
786-
data_pool = get_data_pool(data_pool_name, api_key)
786+
for count in range(10):
787+
logger.debug(f"Get pool try: {count+1}")
788+
data_pool = get_data_pool(data_pool_name, api_key)
789+
if data_pool is not None:
790+
break
791+
time.sleep(1)
792+
787793
assert data_pool is not None
788794
data_pool_id = data_pool["id"]
789795

src/pyplanqk/version.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
__version_tuple__: VERSION_TUPLE
1313
version_tuple: VERSION_TUPLE
1414

15-
__version__ = version = '0.0.post42+g60c51ed.d20240215'
16-
__version_tuple__ = version_tuple = (0, 0, 'g60c51ed.d20240215')
15+
__version__ = version = '0.0.post44+g500054a.d20240216'
16+
__version_tuple__ = version_tuple = (0, 0, 'g500054a.d20240216')

tests/conftest.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
from typing import Dict, Tuple
1+
from typing import Tuple
22

33
import pytest
4+
from dotenv import load_dotenv
45
from names_generator import generate_name
56
from util import *
67

8+
load_dotenv(".env")
9+
PLANKQ_API_KEY = os.getenv("PLANKQ_API_KEY")
10+
PLANKQ_TOKEN_URL = os.getenv("PLANKQ_API_KEY")
11+
PLANQK_DATA_POOL_URL = os.getenv("PLANQK_DATA_POOL_URL")
12+
713

814
@pytest.fixture(scope="function")
915
def api_key() -> Dict[str, str]:
10-
api_key = "plqk_4Rk6rDnjDp6D38cRQXgGigFymWpCt3R7jw27kUZAcP"
16+
api_key = PLANKQ_API_KEY
1117
api_key = {"apiKey": api_key}
1218
return api_key
1319

@@ -24,7 +30,7 @@ def step() -> int:
2430

2531
@pytest.fixture(scope="function")
2632
def token_url() -> str:
27-
return "https://gateway.platform.planqk.de/token"
33+
return PLANKQ_TOKEN_URL
2834

2935

3036
@pytest.fixture(scope="function")
@@ -79,7 +85,7 @@ def config() -> Dict[str, Any]:
7985
@pytest.fixture(scope="function")
8086
def data_pool(api_key: Dict[str, str]) -> Dict[str, Any]:
8187
data_pool_name = f"data_pool_{generate_name()}"
82-
url = "https://platform.planqk.de/qc-catalog/data-pools"
88+
url = PLANQK_DATA_POOL_URL
8389

8490
headers = {"Content-Type": "application/json", "X-Auth-Token": api_key["apiKey"]}
8591

@@ -92,10 +98,10 @@ def data_pool(api_key: Dict[str, str]) -> Dict[str, Any]:
9298

9399
@pytest.fixture(scope="function")
94100
def data_pool_with_data(
95-
api_key: Dict[str, str], train_data: Dict[str, list], train_params: Dict[str, list]
101+
api_key: Dict[str, str], train_data: Dict[str, list], train_params: Dict[str, list]
96102
) -> Dict[str, Any]:
97103
data_pool_name = f"data_pool_{generate_name()}"
98-
url = "https://platform.planqk.de/qc-catalog/data-pools"
104+
url = PLANQK_DATA_POOL_URL
99105

100106
headers = {"Content-Type": "application/json", "X-Auth-Token": api_key["apiKey"]}
101107

@@ -106,7 +112,7 @@ def data_pool_with_data(
106112

107113
# save_data(train_data, train_params)
108114

109-
file = open("tests/data/data.json", "rb")
115+
file = open(f"{get_test_data_path()}data.json", "rb")
110116
result = add_data_to_data_pool(data_pool_name, file, api_key["apiKey"])
111117
assert result
112118

@@ -117,7 +123,7 @@ def data_pool_with_data(
117123
def access_token() -> str:
118124
consumer_key = "wC1Dkq6ZPW7CRBUSB1oL5cURA_ga"
119125
consumer_secret = "o5oiJrJrNLbIuJPGk46vJyHfDj4a"
120-
token_url = "https://gateway.platform.planqk.de/token"
126+
token_url = PLANKQ_TOKEN_URL
121127
access_token = get_access_token(consumer_key, consumer_secret, token_url)
122128
assert access_token is not None
123129
assert len(access_token) == 1037
@@ -126,7 +132,7 @@ def access_token() -> str:
126132

127133
@pytest.fixture(scope="function")
128134
def service_info(
129-
config: Dict[str, Any], api_key: Dict[str, str]
135+
config: Dict[str, Any], api_key: Dict[str, str]
130136
) -> Tuple[Dict[str, Any], Dict[str, Any]]:
131137
service = create_managed_service(config, api_key)
132138
service_id = service["id"]
@@ -139,7 +145,7 @@ def service_info(
139145

140146
@pytest.fixture(scope="function")
141147
def internally_published_service(
142-
service_info: Tuple[Dict[str, Any], Dict[str, Any]], api_key: Dict[str, str]
148+
service_info: Tuple[Dict[str, Any], Dict[str, Any]], api_key: Dict[str, str]
143149
) -> Dict[str, Any]:
144150
simple_service, config = service_info
145151

@@ -172,7 +178,7 @@ def application_with_auth(api_key: Dict[str, str]) -> Tuple[Dict[str, Any], str,
172178

173179
@pytest.fixture(scope="function")
174180
def full_application(
175-
config: Dict[str, Any], api_key: Dict[str, str]
181+
config: Dict[str, Any], api_key: Dict[str, str]
176182
) -> Tuple[Dict[str, Any], Dict[str, Any], str, str]:
177183
application_name = f"application_{generate_name()}"
178184
application = create_application(application_name, api_key)
@@ -199,7 +205,7 @@ def full_application(
199205

200206
@pytest.fixture(scope="function")
201207
def service_job(
202-
data: Dict[str, Any], params: Dict[str, Any], api_key: Dict[str, str]
208+
data: Dict[str, Any], params: Dict[str, Any], api_key: Dict[str, str]
203209
) -> JobDto:
204210
logger.debug("Trigger service job")
205211
configuration = Configuration(api_key=api_key)

tests/data/data.json

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tests/test_end_to_end.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22
import logging
33
from typing import Any, Dict
44

5-
from conftest import cleanup_services_and_applications, get_data, get_params
5+
import pytest
6+
from conftest import (
7+
cleanup_services_and_applications,
8+
get_data,
9+
get_params,
10+
get_test_data_path,
11+
)
612
from names_generator import generate_name
713

814
import pyplanqk
@@ -11,6 +17,7 @@
1117
logger = logging.getLogger(__name__)
1218

1319

20+
@pytest.mark.auto
1421
def test_service_job_from_data_upload(config: Dict[str, Any], api_key: Dict[str, str]):
1522
print()
1623
logger.debug("test_service_job_from_data_upload")
@@ -23,15 +30,20 @@ def test_service_job_from_data_upload(config: Dict[str, Any], api_key: Dict[str,
2330
train_params = get_params(maxiter=100, reps=3)
2431

2532
plnqk = pyplanqk.PyPlanQK(api_key["apiKey"])
33+
34+
logger.debug(f"start create_service now\n")
2635
service = plnqk.create_service(config)
36+
logger.debug(f"create_service done\n")
2737
assert service is not None
2838
services.append(service)
2939

3040
service_name = service["name"]
41+
logger.debug(f"start execute_service {service_name} now\n")
3142
result = plnqk.execute_service(
3243
service_name, data=train_data, params=train_params
3344
)
3445
assert result is not None
46+
logger.debug(f"execute_service {service_name} done\n")
3547

3648
model = result["model"]
3749

@@ -54,20 +66,22 @@ def test_service_job_from_data_upload(config: Dict[str, Any], api_key: Dict[str,
5466
assert False
5567

5668

69+
@pytest.mark.auto
5770
def test_service_job_from_data_pool(config: Dict[str, Any], api_key: Dict[str, str]):
5871
print()
5972
logger.debug("test_service_job_from_data_pool")
6073

74+
data_path = get_test_data_path() + "data.json"
6175
applications = []
6276
services = []
6377

6478
try:
6579
train_data = get_data(num_samples_train=800, num_samples_test=200)
66-
f_data = open("/tmp/data.json", "w")
80+
f_data = open(data_path, "w")
81+
6782
json.dump(train_data, f_data)
6883
f_data.close()
69-
70-
f_data = open("/tmp/data.json", "rb")
84+
f_data = open(data_path, "rb")
7185

7286
train_params = get_params(maxiter=100, reps=3)
7387

0 commit comments

Comments
 (0)