Skip to content

Commit 195d381

Browse files
committed
More cleaning up
1 parent 1989253 commit 195d381

1 file changed

Lines changed: 27 additions & 31 deletions

File tree

scyjava/__init__.py

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ class Priority:
192192
class Converter(NamedTuple):
193193
predicate: Callable[[Any], bool]
194194
converter: Callable[[Any], Any]
195-
# Corresponds with Priority.NORMAL
196-
priority: float = 0
195+
priority: float = Priority.NORMAL
197196

198197

199198
def _convert(obj: Any, converters: typing.List[Converter]) -> Any:
@@ -281,7 +280,7 @@ def _raise_type_exception(obj: Any):
281280
raise TypeError('Unsupported type: ' + str(type(obj)))
282281

283282

284-
def convertMap(obj: collections.abc.Mapping):
283+
def _convertMap(obj: collections.abc.Mapping):
285284
jmap = LinkedHashMap()
286285
for k, v in obj.items():
287286
jk = to_java(k)
@@ -290,15 +289,15 @@ def convertMap(obj: collections.abc.Mapping):
290289
return jmap
291290

292291

293-
def convertSet(obj: collections.abc.Set):
292+
def _convertSet(obj: collections.abc.Set):
294293
jset = LinkedHashSet()
295294
for item in obj:
296295
jitem = to_java(item)
297296
jset.add(jitem)
298297
return jset
299298

300299

301-
def convertIterable(obj: collections.abc.Iterable):
300+
def _convertIterable(obj: collections.abc.Iterable):
302301
jlist = ArrayList()
303302
for item in obj:
304303
jitem = to_java(item)
@@ -341,6 +340,11 @@ def to_java(obj: Any) -> Any:
341340

342341

343342
def _stock_java_converters() -> typing.List[Converter]:
343+
"""
344+
Returns all python-to-java converters supported out of the box!
345+
This should only be called after the JVM has been started!
346+
:returns: A list of Converters
347+
"""
344348
return [
345349
# Other (Exceptional) converter
346350
Converter(
@@ -364,19 +368,16 @@ def _stock_java_converters() -> typing.List[Converter]:
364368
Converter(
365369
predicate=lambda obj: isinstance(obj, str),
366370
converter=lambda obj: String(obj.encode('utf-8'), 'utf-8'),
367-
priority=Priority.NORMAL
368371
),
369372
# Boolean converter
370373
Converter(
371374
predicate=lambda obj: isinstance(obj, bool),
372375
converter=Boolean,
373-
priority=Priority.NORMAL
374376
),
375377
# Integer converter
376378
Converter(
377379
predicate=lambda obj: isinstance(obj, int) and obj <= Integer.MAX_VALUE and obj >= Integer.MIN_VALUE,
378380
converter= Integer,
379-
priority=Priority.NORMAL
380381
),
381382
# Long converter
382383
Converter(
@@ -394,7 +395,6 @@ def _stock_java_converters() -> typing.List[Converter]:
394395
Converter(
395396
predicate=lambda obj: isinstance(obj, float) and obj <= Float.MAX_VALUE and obj >= Float.MIN_VALUE,
396397
converter= Float,
397-
priority=Priority.NORMAL
398398
),
399399
# Double converter
400400
Converter(
@@ -417,19 +417,17 @@ def _stock_java_converters() -> typing.List[Converter]:
417417
# Mapping converter
418418
Converter(
419419
predicate=lambda obj: isinstance(obj, collections.abc.Mapping),
420-
converter=convertMap,
421-
priority=Priority.NORMAL
420+
converter=_convertMap,
422421
),
423422
# Set converter
424423
Converter(
425424
predicate=lambda obj: isinstance(obj, collections.abc.Set),
426-
converter=convertSet,
427-
priority=Priority.NORMAL
425+
converter=_convertSet,
428426
),
429427
# Iterable converter
430428
Converter(
431429
predicate=lambda obj: isinstance(obj, collections.abc.Iterable),
432-
converter=convertIterable,
430+
converter=_convertIterable,
433431
priority=Priority.NORMAL -1
434432
),
435433
]
@@ -605,6 +603,13 @@ def __str__(self):
605603

606604

607605
def add_py_converter(predicate: Callable[[Any], bool], converter: Callable[[Any], Any], priority: float):
606+
"""
607+
Adds a converter to the list used by to_python
608+
:param predicate: A Callable identifying suitable data types for this converter
609+
:param converter: A Callable able to convert a set of types
610+
:priority:
611+
612+
"""
608613
c = Converter(predicate, converter, priority)
609614
_add_converter(c, py_converters)
610615

@@ -639,6 +644,11 @@ def to_python(data: Any, gentle: bool =False) -> Any:
639644

640645

641646
def _stock_py_converters() -> typing.List:
647+
"""
648+
Returns all java-to-python converters supported out of the box!
649+
This should only be called after the JVM has been started!
650+
:returns: A list of Converters
651+
"""
642652
return [
643653
# Other (Exceptional) converter
644654
Converter(
@@ -680,97 +690,81 @@ def _stock_py_converters() -> typing.List:
680690
Converter(
681691
predicate=lambda obj: isinstance(obj, Boolean),
682692
converter=lambda obj: obj.booleanValue(),
683-
priority=Priority.NORMAL
684693
),
685694
# Byte converter
686695
Converter(
687696
predicate=lambda obj: isinstance(obj, Byte),
688697
converter=lambda obj: obj.byteValue(),
689-
priority=Priority.NORMAL
690698
),
691699
# Char converter
692700
Converter(
693701
predicate=lambda obj: isinstance(obj, Character),
694702
converter=lambda obj: obj.toString(),
695-
priority=Priority.NORMAL
696703
),
697704
# Double converter
698705
Converter(
699706
predicate=lambda obj: isinstance(obj, Double),
700707
converter=lambda obj: obj.doubleValue(),
701-
priority=Priority.NORMAL
702708
),
703709
# Float converter
704710
Converter(
705711
predicate=lambda obj: isinstance(obj, Float),
706712
converter=lambda obj: obj.floatValue(),
707-
priority=Priority.NORMAL
708713
),
709714
# Integer converter
710715
Converter(
711716
predicate=lambda obj: isinstance(obj, Integer),
712717
converter=lambda obj: obj.intValue(),
713-
priority=Priority.NORMAL
714718
),
715719
# Long converter
716720
Converter(
717721
predicate=lambda obj: isinstance(obj, Long),
718722
converter=lambda obj: obj.longValue(),
719-
priority=Priority.NORMAL
720723
),
721724
# Short converter
722725
Converter(
723726
predicate=lambda obj: isinstance(obj, Short),
724727
converter=lambda obj: obj.shortValue(),
725-
priority=Priority.NORMAL
726728
),
727729
# Void converter
728730
Converter(
729731
predicate=lambda obj: isinstance(obj, Void),
730732
converter=lambda obj: None,
731-
priority=Priority.NORMAL
732733
),
733734
# String converter
734735
Converter(
735736
predicate=lambda obj: isinstance(obj, String),
736737
converter=lambda obj: str(obj),
737-
priority=Priority.NORMAL
738738
),
739739
# BigInteger converter
740740
Converter(
741741
predicate=lambda obj: isinstance(obj, BigInteger),
742742
converter=lambda obj: int(str(obj.toString())),
743-
priority=Priority.NORMAL
744743
),
745744
# BigDecimal converter
746745
Converter(
747746
predicate=lambda obj: isinstance(obj, BigDecimal),
748747
converter=lambda obj: float(obj.toString),
749-
priority=Priority.NORMAL
750748
),
751749
# SciJava Table converter
752750
Converter(
753751
predicate=_is_table,
754752
converter=_convert_table,
755-
priority=Priority.NORMAL
756753
),
757754
# List converter
758755
Converter(
759756
predicate=lambda obj: isinstance(obj, List),
760757
converter=JavaList,
761-
priority=Priority.NORMAL
762758
),
763759
# Map converter
764760
Converter(
765761
predicate=lambda obj: isinstance(obj, Map),
766762
converter=JavaMap,
767-
priority=Priority.NORMAL
768763
),
769764
# Set converter
770765
Converter(
771766
predicate=lambda obj: isinstance(obj, Set),
772767
converter=JavaSet,
773-
priority=Priority.NORMAL
774768
),
775769
# Collection converter
776770
Converter(
@@ -798,7 +792,8 @@ def _stock_py_converters() -> typing.List:
798792
)
799793

800794

801-
def _is_table(obj: Any):
795+
def _is_table(obj: Any) -> bool:
796+
"""Checks if obj is a table"""
802797
try:
803798
return isinstance(obj, jimport('org.scijava.table.Table'))
804799
except:
@@ -807,6 +802,7 @@ def _is_table(obj: Any):
807802

808803

809804
def _convert_table(obj: Any):
805+
"""Converts obj to a table."""
810806
try:
811807
return _table_to_pandas(obj)
812808
except:

0 commit comments

Comments
 (0)