Skip to content

Commit

Permalink
Python Black Formatting (#492)
Browse files Browse the repository at this point in the history
* Apply black formatting

* Add black.yml workflow for checking Python formatting
  • Loading branch information
olgabot authored Oct 29, 2024
1 parent cdc69d2 commit 31d3056
Show file tree
Hide file tree
Showing 14 changed files with 5,452 additions and 3,150 deletions.
13 changes: 13 additions & 0 deletions .github/workflows/black.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: Lint

on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: psf/black@stable
with:
src: "./src/python"
options: "--check --verbose"
814 changes: 560 additions & 254 deletions src/python/sourmash_plugin_branchwater/__init__.py

Large diffs are not rendered by default.

27 changes: 16 additions & 11 deletions src/python/sourmash_plugin_branchwater/prettyprint.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import csv


def pretty_print_manysearch(manysearch_csv):
"Pretty-print the manysearch output."
with open(manysearch_csv, newline='') as fp:
with open(manysearch_csv, newline="") as fp:
r = csv.DictReader(fp)
rows = list(r)

rows.sort(key=lambda row: row['query_name']) # sort by metagenome, for now
rows.sort(key=lambda row: row["query_name"]) # sort by metagenome, for now

first = True
for row in rows:
has_abundance = 'average_abund' in row
has_abundance = "average_abund" in row

#
# display!
Expand All @@ -23,21 +24,25 @@ def pretty_print_manysearch(manysearch_csv):
print("-------- -------- --------- ------- ---------------")
first = False

f_genome_found = float(row['containment'])
f_genome_found = float(row["containment"])
pct_genome = f"{f_genome_found*100:.1f}"

if has_abundance:
n_weighted_found = int(row['n_weighted_found'])
total_weighted_hashes = int(row['total_weighted_hashes'])
f_metag_weighted = n_weighted_found / total_weighted_hashes # results_d['f_match_weighted']
n_weighted_found = int(row["n_weighted_found"])
total_weighted_hashes = int(row["total_weighted_hashes"])
f_metag_weighted = (
n_weighted_found / total_weighted_hashes
) # results_d['f_match_weighted']
pct_metag = f"{f_metag_weighted*100:.1f}%"

avg_abund = float(row['average_abund'])
avg_abund = float(row["average_abund"])
avg_abund = f"{avg_abund:.1f}"
else:
avg_abund = "N/A"
pct_metag = "N/A"

query_name = row['query_name'][:17]
metag_name = row['match_name'][:17]
print(f'{query_name:<17} {pct_genome:>6}% {avg_abund:>6} {pct_metag:>6} {metag_name}')
query_name = row["query_name"][:17]
metag_name = row["match_name"][:17]
print(
f"{query_name:<17} {pct_genome:>6}% {avg_abund:>6} {pct_metag:>6} {metag_name}"
)
1 change: 1 addition & 0 deletions src/python/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from sourmash_plugin_branchwater import sourmash_plugin_branchwater

sourmash_plugin_branchwater.set_global_thread_pool(4)
7 changes: 7 additions & 0 deletions src/python/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .sourmash_tst_utils import TempDirectory, RunnerContext


@pytest.fixture
def runtmp():
with TempDirectory() as location:
Expand All @@ -12,26 +13,32 @@ def runtmp():
def toggle_internal_storage(request):
return request.param


@pytest.fixture(params=[True, False])
def zip_query(request):
return request.param


@pytest.fixture(params=[True, False])
def zip_db(request):
return request.param


@pytest.fixture(params=[True, False])
def zip_against(request):
return request.param


@pytest.fixture(params=[True, False])
def indexed(request):
return request.param


@pytest.fixture(params=[True, False])
def indexed_query(request):
return request.param


@pytest.fixture(params=[True, False])
def indexed_against(request):
return request.param
90 changes: 55 additions & 35 deletions src/python/tests/sourmash_tst_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,49 @@

def get_test_data(filename):
thisdir = os.path.dirname(__file__)
return os.path.join(thisdir, 'test-data', filename)
return os.path.join(thisdir, "test-data", filename)


def make_file_list(filename, paths):
with open(filename, 'wt') as fp:
with open(filename, "wt") as fp:
fp.write("\n".join(paths))
fp.write("\n")


def zip_siglist(runtmp, siglist, db):
runtmp.sourmash('sig', 'cat', siglist,
'-o', db)
runtmp.sourmash("sig", "cat", siglist, "-o", db)
return db


def index_siglist(runtmp, siglist, db, *, ksize=31, scaled=1000, moltype='DNA',
toggle_internal_storage='--internal-storage'):
def index_siglist(
runtmp,
siglist,
db,
*,
ksize=31,
scaled=1000,
moltype="DNA",
toggle_internal_storage="--internal-storage",
):
# build index
runtmp.sourmash('scripts', 'index', siglist,
'-o', db, '-k', str(ksize), '--scaled', str(scaled),
'--moltype', moltype, toggle_internal_storage)
runtmp.sourmash(
"scripts",
"index",
siglist,
"-o",
db,
"-k",
str(ksize),
"--scaled",
str(scaled),
"--moltype",
moltype,
toggle_internal_storage,
)
return db


def scriptpath(scriptname='sourmash'):
def scriptpath(scriptname="sourmash"):
"""Return the path to the scripts, in both dev and install situations."""
# note - it doesn't matter what the scriptname is here, as long as
# it's some script present in this version of sourmash.
Expand All @@ -53,18 +71,18 @@ def scriptpath(scriptname='sourmash'):
if os.path.exists(os.path.join(path, scriptname)):
return path

for path in os.environ['PATH'].split(':'):
for path in os.environ["PATH"].split(":"):
if os.path.exists(os.path.join(path, scriptname)):
return path


def _runscript(scriptname):
"""Find & run a script with exec (i.e. not via os.system or subprocess)."""
namespace = {"__name__": "__main__"}
namespace['sys'] = globals()['sys']
namespace["sys"] = globals()["sys"]

try:
pkg_resources.load_entry_point("sourmash", 'console_scripts', scriptname)()
pkg_resources.load_entry_point("sourmash", "console_scripts", scriptname)()
return 0
except pkg_resources.ResolutionError:
pass
Expand All @@ -75,15 +93,15 @@ def _runscript(scriptname):
if os.path.isfile(scriptfile):
if os.path.isfile(scriptfile):
exec( # pylint: disable=exec-used
compile(open(scriptfile).read(), scriptfile, 'exec'),
namespace)
compile(open(scriptfile).read(), scriptfile, "exec"), namespace
)
return 0

return -1


ScriptResults = collections.namedtuple('ScriptResults',
['status', 'out', 'err'])
ScriptResults = collections.namedtuple("ScriptResults", ["status", "out", "err"])


def runscript(scriptname, args, **kwargs):
"""Run a Python script using exec().
Expand All @@ -99,17 +117,17 @@ def runscript(scriptname, args, **kwargs):
sysargs.extend(args)

cwd = os.getcwd()
in_directory = kwargs.get('in_directory', cwd)
fail_ok = kwargs.get('fail_ok', False)
in_directory = kwargs.get("in_directory", cwd)
fail_ok = kwargs.get("fail_ok", False)

try:
status = -1
oldargs = sys.argv
sys.argv = sysargs

oldin = None
if 'stdin_data' in kwargs:
oldin, sys.stdin = sys.stdin, StringIO(kwargs['stdin_data'])
if "stdin_data" in kwargs:
oldin, sys.stdin = sys.stdin, StringIO(kwargs["stdin_data"])

oldout, olderr = sys.stdout, sys.stderr
sys.stdout = StringIO()
Expand All @@ -119,8 +137,8 @@ def runscript(scriptname, args, **kwargs):
os.chdir(in_directory)

try:
print('running:', scriptname, 'in:', in_directory, file=oldout)
print('arguments', sysargs, file=oldout)
print("running:", scriptname, "in:", in_directory, file=oldout)
print("arguments", sysargs, file=oldout)

status = _runscript(scriptname)
except SystemExit as err:
Expand Down Expand Up @@ -150,7 +168,7 @@ def runscript(scriptname, args, **kwargs):

class TempDirectory(object):
def __init__(self):
self.tempdir = tempfile.mkdtemp(prefix='sourmashtest_')
self.tempdir = tempfile.mkdtemp(prefix="sourmashtest_")

def __enter__(self):
return self.tempdir
Expand All @@ -168,7 +186,7 @@ def __exit__(self, exc_type, exc_value, traceback):
class SourmashCommandFailed(Exception):
def __init__(self, msg):
Exception.__init__(self, msg)
self.message = msg
self.message = msg


class RunnerContext(object):
Expand All @@ -181,32 +199,34 @@ class RunnerContext(object):
You can use the 'output' method to build filenames in my temp directory.
"""

def __init__(self, location):
self.location = location
self.last_command = None
self.last_result = None

def run_sourmash(self, *args, **kwargs):
"Run the sourmash script with the given arguments."
kwargs['fail_ok'] = True
if 'in_directory' not in kwargs:
kwargs['in_directory'] = self.location
kwargs["fail_ok"] = True
if "in_directory" not in kwargs:
kwargs["in_directory"] = self.location

cmdlist = ['sourmash']
cmdlist.extend(( str(x) for x in args))
cmdlist = ["sourmash"]
cmdlist.extend((str(x) for x in args))
self.last_command = " ".join(cmdlist)
self.last_result = runscript('sourmash', args, **kwargs)
self.last_result = runscript("sourmash", args, **kwargs)

if self.last_result.status:
raise SourmashCommandFailed(self.last_result.err)

return self.last_result

sourmash = run_sourmash

def run(self, scriptname, *args, **kwargs):
"Run a script with the given arguments."
if 'in_directory' not in kwargs:
kwargs['in_directory'] = self.location
if "in_directory" not in kwargs:
kwargs["in_directory"] = self.location
self.last_command = " ".join(args)
self.last_result = runscript(scriptname, args, **kwargs)
return self.last_result
Expand All @@ -224,11 +244,11 @@ def __str__(self):
if self.last_result.out:
s += "- stdout:\n---\n{}---\n".format(self.last_result.out)
else:
s += '(no stdout)\n\n'
s += "(no stdout)\n\n"
if self.last_result.err:
s += "- stderr:\n---\n{}---\n".format(self.last_result.err)
else:
s += '(no stderr)\n'
s += "(no stderr)\n"

return s

Expand Down
Loading

0 comments on commit 31d3056

Please sign in to comment.