Skip to main content

scyjava

Project description

scyjava

Supercharged Java access from Python.

Built on pyjnius and jgo.

Use Java classes from Python

>>> import scyjava, jnius
>>> System = jnius.autoclass('java.lang.System')
>>> System.getProperty('java.version')
'1.8.0_152-release'

To pass parameters to the JVM, such as an increased max heap size:

>>> import scyjava_config
>>> scyjava_config.add_options('-Xmx6g')
>>> import scyjava, jnius
>>> Runtime = jnius.autoclass('java.lang.Runtime')
>>> Runtime.getRuntime().maxMemory() / 2**30
5.33349609375

See the Pyjnius documentation for more about calling Java from Python.

Use Maven artifacts from remote repositories

From Maven Central

>>> import sys; sys.version_info
sys.version_info(major=3, minor=6, micro=5, releaselevel='final', serial=0)
>>> import scyjava_config
>>> scyjava_config.add_endpoints('org.python:jython-standalone:2.7.1')
>>> import scyjava, jnius
>>> jython = jnius.autoclass('org.python.util.jython')
>>> jython.main([])
Jython 2.7.1 (default:0df7adb1b397, Jun 30 2017, 19:02:43)
[OpenJDK 64-Bit Server VM (JetBrains s.r.o)] on java1.8.0_152-release
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys; sys.version_info
sys.version_info(major=2, minor=7, micro=1, releaselevel='final', serial=0)

From other Maven repositories

>>> import scyjava_config
>>> scyjava_config.add_repositories({'scijava.public': 'https://maven.scijava.org/content/groups/public'})
>>> scyjava_config.add_endpoints('net.imagej:imagej:2.0.0-rc-65')
>>> import scyjava, jnius
>>> System = jnius.autoclass('java.lang.System')
>>> System.setProperty('java.awt.headless', 'true')
>>> ImageJ = jnius.autoclass('net.imagej.ImageJ')
>>> ij = ImageJ()
>>> formula = "10 * (Math.cos(0.3*p[0]) + Math.sin(0.3*p[1]))"
>>> blank = ij.op().create().img([64, 16])
>>> sinusoid = ij.op().image().equation(blank, formula)
>>> print(ij.op().image().ascii(sinusoid))
,,,--+oo******oo+--,,,,,--+oo******o++--,,,,,--+oo******o++--,,,
...,--+ooo**oo++--,....,,--+ooo**oo++-,,....,,--+ooo**oo++-,,...
 ...,--++oooo++--,... ...,--++oooo++--,... ...,--++oooo++-,,...
   ..,--++++++--,..     ..,--++o+++--,..     .,,--++o+++--,..
   ..,,-++++++-,,.      ..,,-++++++-,,.      ..,--++++++-,,.
    .,,--++++--,,.       .,,--++++--,,.       .,,--++++--,..
    .,,--++++--,,.       .,,-+++++--,,.       .,,-+++++--,,.
   ..,--++++++--,..     ..,--++++++--,..     ..,--++++++-,,..
  ..,,-++oooo++-,,..   ..,,-++oooo++-,,..   ..,,-++ooo+++-,,..
...,,-++oooooo++-,,.....,,-++oooooo++-,,.....,,-++oooooo+--,,...
.,,,-++oo****oo++-,,,.,,,-++oo****oo+--,,,.,,,-++oo****oo+--,,,.
,,--++o***OO**oo++-,,,,--++o***OO**oo+--,,,,--++o***OO**oo+--,,,
---++o**OOOOOO**o++-----++o**OOOOOO*oo++-----++o**OOOOOO*oo++---
--++oo*OO####OO*oo++---++oo*OO####OO*oo++---++o**OO####OO*oo++--
+++oo*OO######O**oo+++++oo*OO######O**oo+++++oo*OO######O**oo+++
+++oo*OO######OO*oo+++++oo*OO######OO*oo+++++oo*OO######OO*oo+++

See the jgo documentation for more about Maven endpoints.

Convert between Python and Java data structures

Convert Java collections to Python

>>> import scyjava, jnius
>>> System = jnius.autoclass('java.lang.System')
>>> props = System.getProperties()
>>> props
<java.util.Properties at 0x10dc2daf0 jclass=java/util/Properties jself=<LocalRef obj=0x7fcfefd34b20 at 0x10dc371f0>>
>>> [k for k in props]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'java.util.Properties' object is not iterable
>>> [k for k in scyjava.to_python(props) if k.startswith('java.vm.')]
['java.vm.version', 'java.vm.vendor', 'java.vm.name', 'java.vm.specification.name', 'java.vm.specification.vendor', 'java.vm.specification.version', 'java.vm.info']

Convert Python collections to Java

>>> squares = [n**2 for n in range(1, 10)]
>>> squares
[1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> squares.stream()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'stream'
>>> scyjava.to_java(squares).stream()
<java.util.stream.Stream at 0x119d8ba40 jclass=java/util/stream/Stream jself=<LocalRef obj=0x7fcfefd34810 at 0x10dc37810>>

Introspect Java classes

>>> import scyjava
>>> NumberClass = scyjava.jclass('java.lang.Number')
>>> NumberClass
<Class at 0x10dca89e8 jclass=java/lang/Class jself=<LocalRef obj=0x7fcfefd33420 at 0x10dc37a30>>
>>> NumberClass.getName()
'java.lang.Number'
>>> NumberClass.isInstance(scyjava.to_java(5))
True
>>> NumberClass.isInstance(scyjava.to_java('Hello'))
False

Available functions

>>> import scyjava
>>> help(scyjava.convert)
...
FUNCTIONS
    isjava(data)
        Return whether the given data object is a Java object.

    jclass(data)
        Obtain a Java class object.

        :param data: The object from which to glean the class.
        Supported types include:
        A. Name of a class to look up, analogous to
        Class.forName("java.lang.String");
        B. A jnius.MetaJavaClass object e.g. from jnius.autoclass, analogous to
        String.class;
        C. A jnius.JavaClass object e.g. instantiated from a jnius.MetaJavaClass,
        analogous to "Hello".getClass().
        :returns: A java.lang.Class object, suitable for use with reflection.
        :raises TypeError: if the argument is not one of the aforementioned types.

    to_java(data)
        Recursively convert a Python object to a Java object.
        :param data: The Python object to convert.
        Supported types include:
        * str -> String
        * bool -> Boolean
        * int -> Integer, Long or BigInteger as appropriate
        * float -> Float, Double or BigDecimal as appropriate
        * dict -> LinkedHashMap
        * set -> LinkedHashSet
        * list -> ArrayList
        :returns: A corresponding Java object with the same contents.
        :raises TypeError: if the argument is not one of the aforementioned types.

    to_python(data)
        Recursively convert a Java object to a Python object.
        :param data: The Java object to convert.
        Supported types include:
        * String, Character -> str
        * Boolean -> bool
        * Byte, Short, Integer, Long, BigInteger -> int
        * Float, Double, BigDecimal -> float
        * Map -> collections.abc.MutableMapping (dict-like)
        * Set -> collections.abc.MutableSet (set-like)
        * List -> collections.abc.MutableSequence (list-like)
        * Collection -> collections.abc.Collection
        * Iterable -> collections.abc.Iterable
        * Iterator -> collections.abc.Iterator
        :returns: A corresponding Python object with the same contents.
        :raises TypeError: if the argument is not one of the aforementioned types.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

scyjava-0.4.0.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

scyjava-0.4.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

Details for the file scyjava-0.4.0.tar.gz.

File metadata

  • Download URL: scyjava-0.4.0.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.16

File hashes

Hashes for scyjava-0.4.0.tar.gz
Algorithm Hash digest
SHA256 bccb1ba7f4ef21ada6b7f491ab98fc1600c044daa1c5ba64d1f5c14dac928937
MD5 d9c4bd979233cc06e2ec0e3c1d47e262
BLAKE2b-256 1816602f1f7093604d75e64c69cef79d1d1754965fcc47916037298e27f7d104

See more details on using hashes here.

File details

Details for the file scyjava-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: scyjava-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/2.7.16

File hashes

Hashes for scyjava-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fbb5654337fb058ea543cf526f2f22a0d74a2d5b1441252818e820f4aba89001
MD5 2036d95bfa9fa3a16df98db1647eb628
BLAKE2b-256 ae8ee60170180aef0276bffd1afece6c4ddd57e4cbde33da18b940f278a708e1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page