Skip to content

Commit

Permalink
Add options tests
Browse files Browse the repository at this point in the history
Add cache tests

Add downloader tests

Add queries tests

Remove redundancy

Add requests tests

Update options

Add interactions tests

Add utils tests

Add CI

Fix final for Python3.7

Try typing extensions

Fix python3.7

[ci skip] Update README.rst
  • Loading branch information
michalk8 committed Nov 22, 2020
1 parent 1d04653 commit 7769da5
Show file tree
Hide file tree
Showing 34 changed files with 1,799 additions and 193 deletions.
22 changes: 22 additions & 0 deletions .coveragerc
@@ -0,0 +1,22 @@
[paths]
source =
omnipath
*/site-packages/omnipath

[run]
branch = true
parallel = true
source = omnipath
omit = */__init__.py

[report]
exclude_lines =
\#.*pragma:\s*no.?cover

if __name__ == .__main__.

^\s*raise AssertionError\b
^\s*raise NotImplementedError\b
^\s*return NotImplemented\b
show_missing = true
precision = 2
66 changes: 66 additions & 0 deletions .github/workflows/ci.yml
@@ -0,0 +1,66 @@
name: CI

on:
push:
branches:
- master
pull_request:
branches:
- master

jobs:
build:
runs-on: ${{ matrix.os }}
timeout-minutes: 10
strategy:
max-parallel: 4
matrix:
python: [3.7, 3.8] # , 3.9]
os: [ubuntu-latest, macos-latest]
env:
OS: ${{ matrix.os }}
PYTHON: ${{ matrix.python }}

steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python }}
submodules: false

- name: Get pip cache dir
id: pip-cache
run: |
echo "::set-output name=dir::$(pip cache dir)"
- name: Cache pip
uses: actions/cache@v2
with:
path: ${{ steps.pip-cache.outputs.dir }}
key: ${{ runner.os }}-pip-${{ hashFiles('**/requirements.txt') }}
restore-keys: |
${{ runner.os }}-pip-
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install tox tox-gh-actions codecov
- name: Linting
run: |
tox -e lint
- name: Testing
run: |
tox
env:
PLATFORM: ${{ matrix.platform }}

- name: Upload coverage to Codecov
if: success()
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
CODECOV_NAME: ${{ matrix.python }}-${{ matrix.os }}
run: |
codecov --no-color --required --flags unittests
15 changes: 7 additions & 8 deletions README.rst
@@ -1,13 +1,8 @@
|PyPI| |Travis| |Docs|
|PyPI| |CI| |Docs| |Coverage|

Python client for the OmniPath web service
==========================================

**This package is in planning stage, without any useful functionality yet.**
Contributions are welcome, please contact us at omnipathdb@gmail.com, open
issues or send pull requests. Otherwise please check out the resources below
and return to us later.

The OmniPath database
---------------------

Expand Down Expand Up @@ -65,10 +60,14 @@ certain (not all) annotations of the proteins.
:target: https://pypi.org/project/omnipath
:alt: PyPI

.. |Travis| image:: https://travis-ci.org/theislab/omnipath.svg?branch=master
:target: https://travis-ci.com/github/saezlab/omnipath
.. |CI| image:: https://img.shields.io/github/workflow/status/michalk8/omnipath/CI/master
:target: https://github.com/michalk8/omnipath/actions?query=workflow:CI
:alt: CI

.. |Coverage| image:: https://codecov.io/gh/michalk8/omnipath/branch/master/graph/badge.svg?token=5A086KQA51
:target: https://codecov.io/gh/michalk8/omnipath
:alt: Coverage

.. |Docs| image:: https://img.shields.io/readthedocs/omnipath
:target: https://omnipath.readthedocs.io/en/latest
:alt: Documentation
Expand Down
10 changes: 7 additions & 3 deletions docs/source/index.rst
@@ -1,4 +1,4 @@
|PyPI| |Travis| |Docs|
|PyPI| |CI| |Docs| |Coverage|

OmniPath
========
Expand All @@ -20,13 +20,17 @@ This package is a Python equivalent of an R package `OmnipathR`_ for accessing w
:target: https://pypi.org/project/omnipath
:alt: PyPI

.. |Travis| image:: https://travis-ci.org/theislab/omnipath.svg?branch=master
:target: https://travis-ci.com/github/saezlab/omnipath
.. |CI| image:: https://img.shields.io/github/workflow/status/michalk8/omnipath/CI/master
:target: https://github.com/michalk8/omnipath/actions?query=workflow:CI
:alt: CI

.. |Docs| image:: https://img.shields.io/readthedocs/omnipath
:target: https://omnipath.readthedocs.io/en/latest
:alt: Documentation

.. |Coverage| image:: https://codecov.io/gh/michalk8/omnipath/branch/master/graph/badge.svg?token=5A086KQA51
:target: https://codecov.io/gh/michalk8/omnipath
:alt: Coverage

.. _Saezlab : https://saezlab.org/
.. _OmniPathR : https://github.com/saezlab/omnipathR
2 changes: 1 addition & 1 deletion omnipath/__init__.py
Expand Up @@ -21,6 +21,6 @@
__full_version__ = (
f"{__version__}+{__full_version__.local}" if __full_version__.local else __version__
)
__server_version__ = _get_server_version()
__server_version__ = _get_server_version(options)

del parse, version, _get_server_version
4 changes: 4 additions & 0 deletions omnipath/_core/cache/_cache.py
Expand Up @@ -125,6 +125,10 @@ def __repr__(self) -> str:
def __copy__(self) -> "MemoryCache":
return self

def copy(self) -> "MemoryCache":
"""Return self."""
return self


def clear_cache() -> None:
"""Remove all cached data from :attr:`omnipath.options.cache`."""
Expand Down
19 changes: 10 additions & 9 deletions omnipath/_core/downloader/_downloader.py
Expand Up @@ -2,7 +2,6 @@
from copy import copy
from typing import Any, Mapping, Callable, Optional
from hashlib import md5
from functools import lru_cache
from urllib.parse import urljoin
import json
import logging
Expand Down Expand Up @@ -37,15 +36,20 @@ def __init__(self, opts: Optional[Options] = None):
if opts is None:
from omnipath import options as opts

if not isinstance(opts, Options):
raise TypeError(
f"Expected `opts` to be of type `Options`, found {type(opts).__name__}."
)

self._session = Session()
self._options = copy(opts)
self._options = copy(opts) # this does not copy MemoryCache

if self._options.num_retries > 0:
adapter = HTTPAdapter(
max_retries=Retry(
total=self._options.num_retries,
redirect=5,
method_whitelist=["HEAD", "GET", "OPTIONS"],
allowed_methods=["HEAD", "GET", "OPTIONS"],
status_forcelist=[413, 429, 500, 502, 503, 504],
backoff_factor=1,
)
Expand Down Expand Up @@ -176,13 +180,10 @@ def __repr__(self) -> str:
return str(self)


@lru_cache()
def _get_server_version() -> str:
def _get_server_version(options: Options) -> str:
"""Try and get the server version."""
import re

from omnipath import options

def callback(fp: BytesIO) -> str:
"""Parse the version."""
return re.findall(
Expand All @@ -191,7 +192,7 @@ def callback(fp: BytesIO) -> str:

try:
if not options.autoload:
raise ValueError("Autoload is disallowed.")
raise ValueError("Autoload is disabled.")

with Options.from_options(
options,
Expand All @@ -209,6 +210,6 @@ def callback(fp: BytesIO) -> str:
is_final=False,
)
except Exception as e:
logging.debug(f"Unable to get server version. Reason `{e}`")
logging.debug(f"Unable to get server version. Reason: `{e}`")

return UNKNOWN_SERVER_VERSION
29 changes: 18 additions & 11 deletions omnipath/_core/query/_query.py
Expand Up @@ -6,7 +6,6 @@

from omnipath.constants._constants import FormatterMeta, ErrorFormatter
from omnipath._core.query._query_validator import (
DummyValidator,
EnzsubValidator,
ComplexesValidator,
IntercellValidator,
Expand Down Expand Up @@ -58,8 +57,7 @@ def __new__(cls, clsname, superclasses, attributedict): # noqa: D102
for i, synonym in enumerate(_get_synonyms(k.lower())):
attributedict[f"{k}_{i}"] = synonym

res = super().__new__(cls, clsname, superclasses, attributedict)
return res
return super().__new__(cls, clsname, superclasses, attributedict)


class QueryMeta(SynonymizerMeta, FormatterMeta): # noqa: D101
Expand All @@ -69,13 +67,13 @@ class QueryMeta(SynonymizerMeta, FormatterMeta): # noqa: D101
class Query(ErrorFormatter, Enum, metaclass=QueryMeta): # noqa: D101
@property
def _query_name(self) -> str:
"""Convert synonym to actual query parameter name."""
"""Convert the synonym to an actual query parameter name."""
return "_".join(self.name.split("_")[:-1])

@property
def _delagate(self):
def _delegate(self):
"""Delegate the validation."""
return getattr(self.__validator__, self._query_name, DummyValidator)
return getattr(self.__validator__, self._query_name)

@property
def param(self) -> str:
Expand All @@ -85,23 +83,23 @@ def param(self) -> str:
@property
def valid(self) -> Optional[FrozenSet[str]]:
"""Return the set of valid values for :paramref:`param`."""
return self._delagate.valid
return self._delegate.valid

@property
def annotation(self) -> type:
"""Return type annotations for :paramref:`param`."""
return self._delagate.annotation
return self._delegate.annotation

@property
def doc(self) -> Optional[str]:
"""Return the docstring for :paramref:`param`."""
return self._delagate.doc
return self._delegate.doc

def __call__(
self, value: Optional[Union[str, Sequence[str]]]
) -> Optional[Set[str]]:
"""%(validate)s""" # noqa: D401
return self._delagate(value)
return self._delegate(value)


class EnzsubQuery(Query): # noqa: D101
Expand Down Expand Up @@ -139,5 +137,14 @@ def __call__(

@property
def endpoint(self) -> str:
"""Get the API endpoint for this type of query.."""
"""Get the API endpoint for this type of query."""
return self.name.lower()


__all__ = [
EnzsubQuery,
InteractionsQuery,
ComplexesQuery,
AnnotationsQuery,
IntercellQuery,
]

0 comments on commit 7769da5

Please sign in to comment.