Skip to content

Commit 4ef3781

Browse files
authored
Merge pull request #62 from scijava/numeric-bounds
Add numeric_bounds function, and do some related refactoring
2 parents 5be0963 + 112c4bd commit 4ef3781

11 files changed

Lines changed: 478 additions & 345 deletions

File tree

README.md

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ FUNCTIONS
180180

181181
This function is a shortcut for Java's System.gc().
182182

183-
:raise RuntimeError: if the JVM has not yet been started.
183+
:raise RuntimeError: If the JVM has not started yet.
184184

185185
get_version(java_class_or_python_package) -> str
186186
Return the version of a Java class or Python package.
@@ -213,13 +213,13 @@ FUNCTIONS
213213
those actions via the jpype.setupGuiEnvironment wrapper function;
214214
see the Troubleshooting section of the scyjava README for details.
215215

216-
is_jarray(data) -> bool
216+
is_jarray(data: Any) -> bool
217217
Return whether the given data object is a Java array.
218218

219219
is_jvm_headless() -> bool
220220
Return true iff Java is running in headless mode.
221221

222-
:raises RuntimeError: If the JVM has not started yet.
222+
:raise RuntimeError: If the JVM has not started yet.
223223

224224
is_memoryarraylike(arr: Any) -> bool
225225
Return True iff the object is memoryarraylike:
@@ -265,7 +265,7 @@ FUNCTIONS
265265
:param lengths: List of lengths for the array. For example:
266266
`jarray('z', [3, 7])` is the equivalent of `new boolean[3][7]` in Java.
267267
You can pass a single integer to make a 1-dimensional array of that length.
268-
:returns: The newly allocated array
268+
:return: The newly allocated array
269269

270270
jclass(data)
271271
Obtain a Java class object.
@@ -285,22 +285,23 @@ FUNCTIONS
285285
i.e. the Java class for the Class class. :-)
286286

287287
:param data: The object from which to glean the class.
288-
:returns: A java.lang.Class object, suitable for use with reflection.
289-
:raises TypeError: if the argument is not one of the aforementioned types.
288+
:return: A java.lang.Class object, suitable for use with reflection.
289+
:raise TypeError: if the argument is not one of the aforementioned types.
290290

291291
jimport(class_name: str)
292292
Import a class from Java to Python.
293293

294294
:param class_name: Name of the class to import.
295-
:returns: A pointer to the class, which can be used to
296-
e.g. instantiate objects of that class.
295+
:return:
296+
A pointer to the class, which can be used to
297+
e.g. instantiate objects of that class.
297298

298299
jinstance(obj, jtype) -> bool
299300
Test if the given object is an instance of a particular Java type.
300301

301302
:param obj: The object to check.
302303
:param jtype: The Java type, as either a jimported class or as a string.
303-
:returns: True iff the object is an instance of that Java type.
304+
:return: True iff the object is an instance of that Java type.
304305

305306
jstacktrace(exc) -> str
306307
Extract the Java-side stack trace from a Java exception.
@@ -315,7 +316,7 @@ FUNCTIONS
315316
print(jstacktrace(exc))
316317

317318
:param exc: The Java Throwable from which to extract the stack trace.
318-
:returns: A multi-line string containing the stack trace, or empty string
319+
:return: A multi-line string containing the stack trace, or empty string
319320
if no stack trace could be extracted.
320321

321322
jvm_started() -> bool
@@ -379,8 +380,18 @@ FUNCTIONS
379380
:return: The used memory in bytes.
380381
:raise RuntimeError: if the JVM has not yet been started.
381382

383+
numeric_bounds(the_type: type) -> Union[Tuple[int, int], Tuple[float, float], Tuple[NoneType, NoneType]]
384+
Get the minimum and maximum values for the given numeric type.
385+
For example, a Java long returns (int(Long.MIN_VALUE), int(Long.MAX_VALUE)),
386+
whereas a Java double returns (float(-Double.MAX_VALUE), float(Double.MAX_VALUE)).
387+
388+
:param the_type: The type whose minimum and maximum values are needed.
389+
:return:
390+
The minimum and maximum values as a two-element tuple of int or float,
391+
or a two-element tuple of None if no known bounds.
392+
382393
shutdown_jvm() -> None
383-
Shutdown the JVM.
394+
Shut down the JVM.
384395

385396
This function makes a best effort to clean up Java resources first.
386397
In particular, shutdown hooks registered with scyjava.when_jvm_stops
@@ -398,7 +409,7 @@ FUNCTIONS
398409
Note that if the JVM is not already running, then this function does
399410
nothing! In particular, shutdown hooks are skipped in this situation.
400411

401-
:raises RuntimeError: if this method is called while in Jep mode.
412+
:raise RuntimeError: if this method is called while in Jep mode.
402413

403414
start_jvm(options=None) -> None
404415
Explicitly connect to the Java virtual machine (JVM). Only one JVM can
@@ -407,8 +418,9 @@ FUNCTIONS
407418
time a scyjava function needing a JVM is invoked, one is started on the
408419
fly with the configuration specified via the scijava.config mechanism.
409420

410-
:param options: List of options to pass to the JVM. For example:
411-
['-Dfoo=bar', '-XX:+UnlockExperimentalVMOptions']
421+
:param options:
422+
List of options to pass to the JVM.
423+
For example: ['-Dfoo=bar', '-XX:+UnlockExperimentalVMOptions']
412424

413425
to_java(obj: Any, **hints: Dict) -> Any
414426
Recursively convert a Python object to a Java object.
@@ -451,11 +463,13 @@ FUNCTIONS
451463
* float values in Double range but outside float range convert to Double
452464
* float values outside double range convert to BigDecimal
453465

454-
:param obj: The Python object to convert.
455-
:param hints: An optional dictionary of hints, to help scyjava
456-
make decisions about how to do the conversion.
457-
:returns: A corresponding Java object with the same contents.
458-
:raises TypeError: if the argument is not one of the aforementioned types.
466+
:param obj:
467+
The Python object to convert.
468+
:param hints:
469+
An optional dictionary of hints, to help scyjava
470+
make decisions about how to do the conversion.
471+
:return: A corresponding Java object with the same contents.
472+
:raise TypeError: if the argument is not one of the aforementioned types.
459473

460474
to_python(data: Any, gentle: bool = False) -> Any
461475
Recursively convert a Java object to a Python object.
@@ -472,12 +486,15 @@ FUNCTIONS
472486
* Iterable -> collections.abc.Iterable
473487
* Iterator -> collections.abc.Iterator
474488

475-
:param data: The Java object to convert.
476-
:param gentle: If set, and the type cannot be converted, leaves
477-
the data alone rather than raising a TypeError.
478-
:returns: A corresponding Python object with the same contents.
479-
:raises TypeError: if the argument is not one of the aforementioned types,
480-
and the gentle flag is not set.
489+
:param data:
490+
The Java object to convert.
491+
:param gentle:
492+
If set, and the type cannot be converted, leaves
493+
the data alone rather than raising a TypeError.
494+
:return: A corresponding Python object with the same contents.
495+
:raise TypeError:
496+
if the argument is not one of the aforementioned types,
497+
and the gentle flag is not set.
481498

482499
when_jvm_starts(f) -> None
483500
Registers a function to be called when the JVM starts (or immediately).

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ extend-ignore = ["E203"]
7777
# See https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html#flake8
7878
max-line-length = 88
7979
min_python_version = "3.8"
80+
per-file-ignores = "__init__.py:F401"
8081

8182
[tool.isort]
8283
profile = "black"

src/scyjava/__init__.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,8 @@
7070
from functools import lru_cache
7171
from typing import Any, Callable, Dict
7272

73-
from scyjava._arrays import ( # noqa: F401
74-
is_arraylike,
75-
is_memoryarraylike,
76-
is_xarraylike,
77-
)
78-
from scyjava._convert import ( # noqa: F401
73+
from ._arrays import is_arraylike, is_memoryarraylike, is_xarraylike
74+
from ._convert import (
7975
Converter,
8076
JavaCollection,
8177
JavaIterable,
@@ -94,19 +90,12 @@
9490
to_java,
9591
to_python,
9692
)
97-
from scyjava._java import ( # noqa: F401
98-
JavaClasses,
93+
from ._jvm import ( # noqa: F401
9994
available_processors,
10095
gc,
10196
is_awt_initialized,
102-
is_jarray,
10397
is_jvm_headless,
104-
isjava,
105-
jarray,
106-
jclass,
10798
jimport,
108-
jinstance,
109-
jstacktrace,
11099
jvm_started,
111100
jvm_version,
112101
memory_max,
@@ -117,12 +106,18 @@
117106
when_jvm_starts,
118107
when_jvm_stops,
119108
)
120-
from scyjava._script import enable_python_scripting # noqa: F401
121-
from scyjava._versions import ( # noqa: F401
122-
compare_version,
123-
get_version,
124-
is_version_at_least,
109+
from ._script import enable_python_scripting
110+
from ._types import (
111+
JavaClasses,
112+
is_jarray,
113+
isjava,
114+
jarray,
115+
jclass,
116+
jinstance,
117+
jstacktrace,
118+
numeric_bounds,
125119
)
120+
from ._versions import compare_version, get_version, is_version_at_least
126121

127122
__version__ = get_version("scyjava")
128123
__all__ = [

src/scyjava/_convert.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,9 @@
1212

1313
from jpype import JBoolean, JByte, JChar, JDouble, JFloat, JInt, JLong, JShort
1414

15-
from ._java import (
16-
JavaClasses,
17-
Mode,
18-
is_jarray,
19-
isjava,
20-
jarray,
21-
jclass,
22-
jimport,
23-
jinstance,
24-
mode,
25-
start_jvm,
26-
)
15+
from scyjava._jvm import jimport, start_jvm
16+
from scyjava._types import JavaClasses, is_jarray, isjava, jarray, jclass, jinstance
17+
from scyjava.config import Mode, mode
2718

2819
_logger = logging.getLogger(__name__)
2920

@@ -193,11 +184,13 @@ def to_java(obj: Any, **hints: Dict) -> Any:
193184
* float values in Double range but outside float range convert to Double
194185
* float values outside double range convert to BigDecimal
195186
196-
:param obj: The Python object to convert.
197-
:param hints: An optional dictionary of hints, to help scyjava
198-
make decisions about how to do the conversion.
199-
:returns: A corresponding Java object with the same contents.
200-
:raises TypeError: if the argument is not one of the aforementioned types.
187+
:param obj:
188+
The Python object to convert.
189+
:param hints:
190+
An optional dictionary of hints, to help scyjava
191+
make decisions about how to do the conversion.
192+
:return: A corresponding Java object with the same contents.
193+
:raise TypeError: if the argument is not one of the aforementioned types.
201194
"""
202195
start_jvm()
203196
return _convert(obj, java_converters, **hints)
@@ -206,7 +199,7 @@ def to_java(obj: Any, **hints: Dict) -> Any:
206199
def _stock_java_converters() -> List[Converter]:
207200
"""
208201
Construct the Python-to-Java converters supported out of the box.
209-
:returns: A list of Converters
202+
:return: A list of Converters
210203
"""
211204
start_jvm()
212205
return [
@@ -541,12 +534,15 @@ def to_python(data: Any, gentle: bool = False) -> Any:
541534
* Iterable -> collections.abc.Iterable
542535
* Iterator -> collections.abc.Iterator
543536
544-
:param data: The Java object to convert.
545-
:param gentle: If set, and the type cannot be converted, leaves
546-
the data alone rather than raising a TypeError.
547-
:returns: A corresponding Python object with the same contents.
548-
:raises TypeError: if the argument is not one of the aforementioned types,
549-
and the gentle flag is not set.
537+
:param data:
538+
The Java object to convert.
539+
:param gentle:
540+
If set, and the type cannot be converted, leaves
541+
the data alone rather than raising a TypeError.
542+
:return: A corresponding Python object with the same contents.
543+
:raise TypeError:
544+
if the argument is not one of the aforementioned types,
545+
and the gentle flag is not set.
550546
"""
551547
start_jvm()
552548
try:
@@ -560,7 +556,7 @@ def to_python(data: Any, gentle: bool = False) -> Any:
560556
def _stock_py_converters() -> List:
561557
"""
562558
Construct the Java-to-Python converters supported out of the box.
563-
:returns: A list of Converters
559+
:return: A list of Converters
564560
"""
565561
start_jvm()
566562

0 commit comments

Comments
 (0)