Skip to content

Commit 3208bac

Browse files
committed
flake code
1 parent 16f771a commit 3208bac

6 files changed

Lines changed: 129 additions & 99 deletions

File tree

setup.cfg

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,8 @@ dev =
5454
numpy
5555
pandas
5656
setuptools-scm
57+
58+
[flake8]
59+
# See https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html#flake8
60+
max-line-length = 88
61+
extend-ignore = E203

src/scyjava/__init__.py

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,16 @@
1414
import sys
1515
from pathlib import Path
1616
from typing import Dict
17-
from jpype.types import *
17+
from jpype.types import (
18+
JArray,
19+
JBoolean,
20+
JChar,
21+
JDouble,
22+
JFloat,
23+
JInt,
24+
JLong,
25+
JShort,
26+
)
1827
from _jpype import _JObject
1928

2029
_logger = logging.getLogger(__name__)
@@ -72,7 +81,7 @@ def ___version__():
7281
pass
7382
# Third pass: use pkg_resources
7483
try:
75-
from pkg_resources import get_distribution, DistributionNotFound
84+
from pkg_resources import get_distribution
7685

7786
return get_distribution("scyjava").version
7887
except ImportError:
@@ -89,21 +98,27 @@ def ___version__():
8998

9099
def jvm_version():
91100
"""
92-
Gets the version of the JVM as a tuple, with each dot-separated digit as one element.
93-
Characters in the version string beyond only numbers and dots are ignored, in line
101+
Gets the version of the JVM as a tuple,
102+
with each dot-separated digit as one element.
103+
Characters in the version string beyond only
104+
numbers and dots are ignored, in line
94105
with the java.version system property.
95106
96107
Examples:
97108
* OpenJDK 17.0.1 -> [17, 0, 1]
98109
* OpenJDK 11.0.9.1-internal -> [11, 0, 9, 1]
99110
* OpenJDK 1.8.0_312 -> [1, 8, 0]
100111
101-
If the JVM is already started, this function should return the equivalent of:
102-
jimport('java.lang.System').getProperty('java.version').split('.')
112+
If the JVM is already started,
113+
this function should return the equivalent of:
114+
jimport('java.lang.System')
115+
.getProperty('java.version')
116+
.split('.')
103117
104-
In case the JVM is not started yet, a best effort is made to deduce the
105-
version from the environment without actually starting up the JVM in-process.
106-
If the version cannot be deduced, a RuntimeError with the cause is raised.
118+
In case the JVM is not started yet,a best effort is made to deduce
119+
the version from the environment without actually starting up the
120+
JVM in-process. If the version cannot be deduced, a RuntimeError
121+
with the cause is raised.
107122
"""
108123
jvm_version = jpype.getJVMVersion()
109124
if jvm_version and jvm_version[0]:
@@ -113,7 +128,8 @@ def jvm_version():
113128
return jvm_version
114129

115130
# JPype was clueless, which means the JVM has probably not started yet.
116-
# Let's look for a java executable, and ask it directly with 'java -version'.
131+
# Let's look for a java executable, and ask it directly with 'java
132+
# -version'.
117133

118134
default_jvm_path = jpype.getDefaultJVMPath()
119135
if not default_jvm_path:
@@ -215,7 +231,7 @@ def start_jvm(options=scyjava.config.get_options()):
215231
for libjvm_path, java_home_path in libjvm_paths.items():
216232
if (base / libjvm_path).exists():
217233
java_home = str((base / java_home_path).resolve())
218-
_logger.debug(f"Detected JAVA_HOME: %s", java_home)
234+
_logger.debug(f"Detected JAVA_HOME: {java_home}")
219235
os.environ["JAVA_HOME"] = java_home
220236
break
221237

@@ -465,7 +481,7 @@ def jstacktrace(exc):
465481
sw = StringWriter()
466482
exc.printStackTrace(PrintWriter(sw, True))
467483
return sw.toString()
468-
except:
484+
except BaseException:
469485
return ""
470486

471487

@@ -528,6 +544,10 @@ def to_java(obj: Any) -> Any:
528544
return _convert(obj, java_converters)
529545

530546

547+
def _is_a_long(obj) -> bool:
548+
return isinstance(obj, int) and obj <= Long.MAX_VALUE
549+
550+
531551
def _stock_java_converters() -> typing.List[Converter]:
532552
"""
533553
Returns all python-to-java converters supported out of the box!
@@ -572,7 +592,7 @@ def _stock_java_converters() -> typing.List[Converter]:
572592
),
573593
# Long converter
574594
Converter(
575-
predicate=lambda obj: isinstance(obj, int) and obj <= Long.MAX_VALUE,
595+
predicate=_is_a_long,
576596
converter=Long,
577597
priority=Priority.NORMAL - 1,
578598
),
@@ -628,9 +648,12 @@ def _stock_java_converters() -> typing.List[Converter]:
628648
]
629649

630650

631-
when_jvm_starts(
632-
lambda: [_add_converter(c, java_converters) for c in _stock_java_converters()]
633-
)
651+
def _initialize_converters():
652+
for converter in _stock_java_converters():
653+
_add_converter(converter, java_converters)
654+
655+
656+
when_jvm_starts(_initialize_converters)
634657

635658

636659
# -- Java to Python --
@@ -649,7 +672,7 @@ def __init__(self, jobj, intended_class=None):
649672
intended_class = Object
650673
if not isinstance(jobj, intended_class):
651674
raise TypeError(
652-
"Not a " + intended_class.getName() + ": " + jclass(jobj).getName()
675+
f"Not a {intended_class.getName()}: {jclass(jobj).getName()}"
653676
)
654677
self.jobj = jobj
655678

@@ -713,15 +736,17 @@ def __getitem__(self, key):
713736
return to_python(self.jobj.get(key), gentle=True)
714737

715738
def __setitem__(self, key, value):
716-
# NB: List.set(int, Object) returns inserted element, so be gentle here.
739+
# NB: List.set(int, Object) returns inserted element, so be gentle
740+
# here.
717741
return to_python(self.jobj.set(key, to_java(value)), gentle=True)
718742

719743
def __delitem__(self, key):
720744
# NB: List.remove(Object) returns boolean, so no need for gentleness.
721745
return to_python(self.jobj.remove(to_java(key)))
722746

723747
def insert(self, index, object):
724-
# NB: List.set(int, Object) returns inserted element, so be gentle here.
748+
# NB: List.set(int, Object) returns inserted element, so be gentle
749+
# here.
725750
return to_python(self.jobj.set(index, to_java(object)), gentle=True)
726751

727752

@@ -735,8 +760,10 @@ def __getitem__(self, key):
735760
return to_python(self.jobj.get(to_java(key)), gentle=True)
736761

737762
def __setitem__(self, key, value):
738-
# NB: Map.put(Object, Object) returns inserted value, so be gentle here.
739-
return to_python(self.jobj.put(to_java(key), to_java(value)), gentle=True)
763+
# NB: Map.put(Object, Object) returns inserted value, so be gentle
764+
# here.
765+
put_return: bool = self.jobj.put(to_java(key), to_java(value))
766+
return to_python(put_return, gentle=True)
740767

741768
def __delitem__(self, key):
742769
# NB: Map.remove(Object) returns the removed key, so be gentle here.
@@ -756,16 +783,17 @@ def __eq__(self, other):
756783
if len(self) != len(other):
757784
return False
758785
for k in self:
759-
if not k in other or self[k] != other[k]:
786+
if k not in other or self[k] != other[k]:
760787
return False
761788
return True
762789
except TypeError:
763790
return False
764791

765792
def __str__(self):
766-
return (
767-
"{" + ", ".join(_jstr(k) + ": " + _jstr(v) for k, v in self.items()) + "}"
768-
)
793+
def item_str(k, v):
794+
return _jstr(k) + ": " + _jstr(v)
795+
796+
return "{" + ", ".join(item_str(k, v) for k, v in self.items()) + "}"
769797

770798

771799
class JavaSet(JavaCollection, collections.abc.MutableSet):
@@ -788,7 +816,7 @@ def __eq__(self, other):
788816
if len(self) != len(other):
789817
return False
790818
for k in self:
791-
if not k in other:
819+
if k not in other:
792820
return False
793821
return True
794822
except TypeError:
@@ -998,7 +1026,7 @@ def _is_table(obj: Any) -> bool:
9981026
"""Checks if obj is a table"""
9991027
try:
10001028
return isinstance(obj, jimport("org.scijava.table.Table"))
1001-
except:
1029+
except BaseException:
10021030
# No worries if scijava-table is not available.
10031031
pass
10041032

@@ -1007,7 +1035,7 @@ def _convert_table(obj: Any):
10071035
"""Converts obj to a table."""
10081036
try:
10091037
return _table_to_pandas(obj)
1010-
except:
1038+
except BaseException:
10111039
# No worries if scijava-table is not available.
10121040
pass
10131041

@@ -1037,8 +1065,6 @@ def _table_to_pandas(table):
10371065

10381066

10391067
def _pandas_to_table(df):
1040-
pd = _import_pandas()
1041-
10421068
if len(df.dtypes.unique()) > 1:
10431069
TableClass = jimport("org.scijava.table.DefaultGenericTable")
10441070
else:

src/scyjava/config/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ def add_endpoints(*new_endpoints):
2222
Please modify the endpoints field directly instead.
2323
"""
2424
_logger.warning(
25-
"Deprecated method call: scyjava.config.add_endpoints(). Please modify scyjava.config.endpoints directly instead."
25+
"Deprecated method call: scyjava.config.add_endpoints(). "
26+
"Please modify scyjava.config.endpoints directly instead."
2627
)
2728
global endpoints
2829
_logger.debug("Adding endpoints %s to %s", new_endpoints, endpoints)
@@ -35,7 +36,8 @@ def get_endpoints():
3536
Please access the endpoints field directly instead.
3637
"""
3738
_logger.warning(
38-
"Deprecated method call: scyjava.config.get_endpoints(). Please access scyjava.config.endpoints directly instead."
39+
"Deprecated method call: scyjava.config.get_endpoints(). "
40+
"Please access scyjava.config.endpoints directly instead."
3941
)
4042
global endpoints
4143
return endpoints

0 commit comments

Comments
 (0)