Skip to content

Commit

Permalink
style: apply style black, flake8, isort (biocommons#108)
Browse files Browse the repository at this point in the history
Also includes this as a check in CI.
  • Loading branch information
holtgrewe committed May 4, 2023
1 parent eb3bdc6 commit 7db5205
Show file tree
Hide file tree
Showing 38 changed files with 534 additions and 427 deletions.
47 changes: 45 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,45 @@ jobs:
env:
GITHUB_TOKEN: '${{ secrets.GITHUB_TOKEN }}'

linting:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1

- name: Install Python
uses: actions/setup-python@v2
with:
python-version: "3.8"

- name: Install dependencies
run: |
# Install / update package management tools
pip install -U pip setuptools
# Test dependencies
pip install -U pytest pytest-cov pytest-vcr black==23.3.0 flake8>=6.0,<7.0 isort>=5.0,<6.0
# Dump installed packages and versions
pip freeze
- name: Run linting tools
run: |
black -l 120 --check --diff .
isort --profile=black --check --diff .
flake8 src tests docs setup.py
- name: Comment PR
if: github.event_name == 'pull_request' && failure()
uses: marocchino/[email protected]
with:
message: |
- Please format your Python code with [black](https://black.readthedocs.io): `make black`
- Please organize your imports [isorts](https://isort.readthedocs.io): `make isort`
- Please ensure that your code passes [flake8](https://flake8.pycqa.org/en/latest/): `make flake8`
You can trigger all lints locally by running `black -l 120 --check . && isort --profile=black --check . && flake8 src tests docs setup.py`
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

testing:
needs: linting
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -36,12 +74,17 @@ jobs:
with:
python-version: ${{ matrix.python-version }}

- name: Install system dependencies
- name: Install dependencies
run: |
sudo apt install libhts-dev libhts3 libhtscodecs-dev libhtscodecs2 tabix
# Install / update package management tools.
pip install -U pip setuptools
pip install -U pytest pytest-cov pytest-vcr
# Test dependencies
pip install -U pytest pytest-cov pytest-vcr black==23.3.0 flake8>=6.0,<7.0 isort>=5.0,<6.0
# Install the local package itself in editable mode.
pip install -e .
# Dump installed packages and versions
pip freeze
- name: Run tests
run: |
Expand Down
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Editors
.*.sw?
*~

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
16 changes: 7 additions & 9 deletions docs/utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import math
from base64 import urlsafe_b64decode, urlsafe_b64encode
from binascii import hexlify, unhexlify
import datetime
import math
import sys


def _format_time(timespan, precision=3):
"""Formats the timespan in a human readable form
>>> _format_time(0.35)
'350 ms'
Expand All @@ -26,21 +24,21 @@ def _format_time(timespan, precision=3):
if timespan >= 60.0:
# we have more than a minute, format that in a human readable form
# Idea from http://snipplr.com/view/5713/
parts = [("d", 60*60*24),("h", 60*60),("min", 60), ("s", 1)]
parts = [("d", 60 * 60 * 24), ("h", 60 * 60), ("min", 60), ("s", 1)]
time = []
leftover = timespan
for suffix, length in parts:
value = int(leftover / length)
if value > 0:
leftover = leftover % length
time.append(u'%s%s' % (str(value), suffix))
time.append(u"%s%s" % (str(value), suffix))
if leftover < 1:
break
return " ".join(time)

units = [u"s", u"ms", u"us", u"ns"] # the save value
units = [u"s", u"ms", u"us", u"ns"] # the save value
scaling = [1, 1e3, 1e6, 1e9]

if timespan > 0.0:
order = min(-int(math.floor(math.log10(timespan)) // 3), 3)
else:
Expand All @@ -51,6 +49,6 @@ def _format_time(timespan, precision=3):
def hex_to_base64url(s):
return urlsafe_b64encode(unhexlify(s)).decode("ascii")


def base64url_to_hex(s):
return hexlify(urlsafe_b64decode(s)).decode("ascii")

3 changes: 1 addition & 2 deletions fixes/fixer
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ This script isn't for general users

import argparse
import logging
import sqlite3
import os
import sqlite3
import sys

import coloredlogs
import yaml


_logger = logging.getLogger()

fixes_dir = os.path.dirname(sys.argv[0])
Expand Down
1 change: 0 additions & 1 deletion misc/rsync-log-summary
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import sys
import coloredlogs
from dateutil.parser import parse


_logger = logging.getLogger(__name__)

# e.g., 2016/08/31 01:24:34 [32383] rsync on seqrepo/ from ec2-....compute.amazonaws.com (52.34.43.195)
Expand Down
10 changes: 5 additions & 5 deletions misc/threading-verification.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@


import os
from multiprocessing import Process, Queue
import sqlite3
import sys
from multiprocessing import Process, Queue

from biocommons.seqrepo import SeqRepo

Expand All @@ -37,7 +37,7 @@ def fetch_in_thread(sr, nsa):
def fetch_seq(q, nsa):
pid, ppid = os.getpid(), os.getppid()
q.put((pid, ppid, sr[nsa]))

q = Queue()
p = Process(target=fetch_seq, args=(q, nsa))
p.start()
Expand All @@ -46,9 +46,9 @@ def fetch_seq(q, nsa):

assert pid != ppid, "sequence was not fetched from thread"
return pid, ppid, seq


def make_seqrepo(writeable):

def make_seqrepo(writeable):
sr = SeqRepo("/tmp/sr", writeable=True)
sr.store("SMELLASSWEET", [{"namespace": "en", "alias": "rose"}, {"namespace": "fr", "alias": "rose"}])

Expand All @@ -70,6 +70,6 @@ def _test(sr):
print("sys.platform: " + sys.platform)
print("sys.version: " + sys.version.replace("\n", " "))
print("sqlite3.sqlite_version: " + sqlite3.sqlite_version)

_test(make_seqrepo(writeable=False))
_test(make_seqrepo(writeable=True))
2 changes: 1 addition & 1 deletion sbin/get-sequence-urls
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import logging
import re
import sys
from urllib.request import urljoin, urlopen
from requests_html import HTMLSession, HTML

import coloredlogs
from requests_html import HTML, HTMLSession

_logger = logging.getLogger(__name__)

Expand Down
3 changes: 1 addition & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,5 @@ all_files = 1
max-line-length = 120
exclude = tests/*
max-complexity = 10
ignore = E129,E221,E241,E251,E303,W291

ignore = E203, E266, E501, W503

1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from setuptools import setup

setup(use_scm_version=True)
1 change: 1 addition & 0 deletions src/biocommons/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# pragma: nocover
import pkg_resources

pkg_resources.declare_namespace(__name__)
10 changes: 5 additions & 5 deletions src/biocommons/seqrepo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
from __future__ import absolute_import, division, print_function, unicode_literals

import logging
import pkg_resources
import warnings

import pkg_resources

_logger = logging.getLogger(__name__)

from ._versionwarning import *
from ._versionwarning import * # noqa; F403

try:
__version__ = pkg_resources.get_distribution(__name__).version
except pkg_resources.DistributionNotFound as e: # pragma: no cover
except pkg_resources.DistributionNotFound: # pragma: no cover
warnings.warn("can't get __version__ because %s package isn't installed" % __package__, Warning)
__version__ = None

_logger.info(__name__ + " " + __version__)


from .seqrepo import SeqRepo

from .seqrepo import SeqRepo # noqa: F401, E402

# <LICENSE>
# Copyright 2016 biocommons.fastadir Contributors (https://github.com/biocommons/biocommons.fastadir/)
Expand Down
2 changes: 0 additions & 2 deletions src/biocommons/seqrepo/_internal/logging_support.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import logging

class DuplicateFilter:
"""
Filters away duplicate log messages.
Expand Down
Loading

0 comments on commit 7db5205

Please sign in to comment.