Skip to content

Commit

Permalink
Run linter
Browse files Browse the repository at this point in the history
  • Loading branch information
cachitas committed Apr 3, 2024
1 parent 6743fc5 commit bea2dc0
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 44 deletions.
38 changes: 20 additions & 18 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,18 @@ dependencies = ["httpx>=0.27.0"]
requires-python = ">=3.9"
readme = "README.md"
keywords = [
"string", "api", "client", "httpx",
"sib", "cpr", "embl", "biodata", "elixir",
"protein", "gene", "interaction",
"string",
"api",
"client",
"httpx",
"sib",
"cpr",
"embl",
"biodata",
"elixir",
"protein",
"gene",
"interaction",
]
classifiers = [
"Development Status :: 4 - Beta",
Expand Down Expand Up @@ -40,29 +49,22 @@ Repository = "https://github.com/cachitas/stringx"
Issues = "https://github.com/cachitas/stringx/issues"

[tool.pdm.dev-dependencies]
dev = [
"pytest>=8.1.1",
"pytest-httpx>=0.30.0",
"ruff>=0.3.5",
]
examples = [
"pandas>=2.2.1",
"ipykernel>=6.29.4",
]
dev = ["pytest>=8.1.1", "pytest-httpx>=0.30.0", "ruff>=0.3.5"]
examples = ["pandas>=2.2.1", "ipykernel>=6.29.4"]

[tool.pdm.version]
source = "file"
path = "src/stringx/__init__.py"

[tool.ruff]
lint.select = [
"B", # flake8-bugbear
"B", # flake8-bugbear
"C4", # flake8-comprehensions
"E", # pycodestyle - Error
"F", # Pyflakes
"I", # isort
"W", # pycodestyle - Warning
"E", # pycodestyle - Error
"F", # Pyflakes
"I", # isort
"W", # pycodestyle - Warning
"UP", # pyupgrade
]

include = ["**/*.py", "**/*.pyi", "**/pyproject.toml"]
extend-include = ["*.ipynb"]
12 changes: 8 additions & 4 deletions src/stringx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

__version__ = "0.3.0"

from typing import List

from .client import Client

Expand All @@ -13,16 +12,21 @@
DEFAULT_CALLER_IDENTITY = f"{__name__} {__version__}"


def map(identifiers: List[str], species: int):
def map(identifiers: list[str], species: int):
with Client() as client:
return client.map(identifiers=identifiers, species=species)


def network(identifiers: List[str], species: int):
def network(identifiers: list[str], species: int):
with Client() as client:
return client.network(identifiers=identifiers, species=species)


def interaction_partners(identifiers: List[str], species: int):
def interaction_partners(identifiers: list[str], species: int):
with Client() as client:
return client.interaction_partners(identifiers=identifiers, species=species)


def version():
with Client() as client:
return client.version()
52 changes: 31 additions & 21 deletions src/stringx/client.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,43 @@
import logging
from typing import List, Optional

import httpx


logger = logging.getLogger(__name__)


class Client(httpx.Client):
def __init__(
self, base_url: str = "https://string-db.org", *, identity: Optional[str] = None
self, base_url: str = "https://string-db.org", *, identity: str | None = None
) -> None:
from stringx import DEFAULT_CALLER_IDENTITY

super().__init__(
params=dict(caller_identity=identity or DEFAULT_CALLER_IDENTITY),
params={"caller_identity": identity or DEFAULT_CALLER_IDENTITY},
base_url=base_url,
)

def request(
self,
endpoint: str,
params: dict = {},
params: dict | None = None,
*,
method: str = "POST",
format: str = "json",
):
if params is None:
params = {}
url = "/".join(["api", format, endpoint])
logger.info("POST", url, params)
print("params BEFORE request", self.params)
response = super().request(method, url, params=params)
print("params AFTER request", self.params)
logger.info(response.status_code)
response.raise_for_status()
return response.json()

def map(
self,
identifiers: List[str],
identifiers: list[str],
species: int,
limit: int = 1,
echo_query: bool = True,
Expand All @@ -50,19 +52,19 @@ def map(

def network(
self,
identifiers: List[str],
identifiers: list[str],
species: int,
required_score: Optional[float] = None,
required_score: float | None = None,
network_type: str = "functional",
add_nodes: Optional[int] = None,
add_nodes: int | None = None,
show_query_node_labels: bool = False,
):
params = dict(
identifiers="\r".join(identifiers),
species=species,
network_type=network_type,
show_query_node_labels=int(show_query_node_labels),
)
params = {
"identifiers": "\r".join(identifiers),
"species": species,
"network_type": network_type,
"show_query_node_labels": int(show_query_node_labels),
}

if required_score:
params |= {"required_score": required_score}
Expand All @@ -74,16 +76,24 @@ def network(

def interaction_partners(
self,
identifiers: List[str],
identifiers: list[str],
species: int,
limit: Optional[int] = None,
limit: int | None = None,
):
params = dict(
identifiers="\r".join(identifiers),
species=species,
)
params = {
"identifiers": "\r".join(identifiers),
"species": species,
}

if limit:
params.update(limit=limit)

return self.request("interaction_partners", params=params)

def version(self):
print(self.params)
self.params = {}
request = self.build_request("GET", "https://api.example.com")
del request.headers["X-Api-Key"]

return self.request("version", params={})
14 changes: 13 additions & 1 deletion tests/test_client.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import httpx

import stringx


Expand Down Expand Up @@ -116,3 +115,16 @@ def test_interaction_partners(httpx_mock):

with stringx.Client() as client:
client.interaction_partners(["id1", "id2"], 7227)


def test_version(httpx_mock):
httpx_mock.add_response(
url=httpx.URL("https://string-db.org/api/json/version"), method="POST", json={}
)
with stringx.Client() as client:
version = client.version()
version = client.map(["edin"], species=7227)
version = client.version()

print(version)
assert version == ""

0 comments on commit bea2dc0

Please sign in to comment.