def test_general_rt_perf(self): Integer = jpy.get_type('java.lang.Integer') String = jpy.get_type('java.lang.String') File = jpy.get_type('java.io.File') HashMap = jpy.get_type('java.util.HashMap') # 1 million N = 1000000 indexes = list(range(N)) random.shuffle(indexes) t0 = time.time() pairs = [(Integer(index), File('path')) for index in indexes] t1 = time.time() print('Integer + File object instantiation took', t1-t0, 's for', N, 'calls, this is', 1000*(t1-t0)/N, 'ms per call') map = HashMap() t0 = time.time() for pair in pairs: i, f = pair map.put(i, f) t1 = time.time() print('HashMap.put() took', t1-t0, 's for', N, 'calls, this is', 1000*(t1-t0)/N, 'ms per call') t0 = time.time() for pair in pairs: i, f = pair f = map.get(i) t1 = time.time() print('HashMap.get() took', t1-t0, 's for', N, 'calls, this is', 1000*(t1-t0)/N, 'ms per call')
def test_ThatJavaTypesHaveAValidClassAttribute(self): Long = jpy.get_type('java.lang.Long') self.assertIsNotNone(Long.jclass) Class = jpy.get_type('java.lang.Class') self.assertIsNotNone(Class.jclass) Object = jpy.get_type('java.lang.Object') self.assertIsNotNone(Object.jclass)
def test_returnIntArray(self): fixture = self.Fixture() # See https://docs.python.org/2/c-api/buffer.html # "An array can only expose its contents via the old-style buffer interface. # This limitation does not apply to Python 3, where memoryview objects can be # constructed from arrays, too." # >>> import array # >>> a = array.array('i', [1,2,3]) # >>> m = memoryview(a) # TypeError: cannot make memory view because object does not have the buffer interface # if sys.version_info >= (3, 0, 0): a1 = array.array('i', [0, 0, 0]) a2 = fixture.returnIntArray(a1) # AssertionError: array('i', [0, 0, 0]) is not [I(objectRef=0x0778C364) self.assertIs(a1, a2) if np: a1 = np.array([0, 0, 0], dtype='int32') a2 = fixture.returnIntArray(a1) self.assertIs(a1, a2) a1 = jpy.array('int', 3) a2 = fixture.returnIntArray(a1) self.assertIs(a1, a2) a1 = None a2 = fixture.returnIntArray(a1) self.assertEqual(type(a2), jpy.get_type('[I')) a1 = [0, 0, 0] a2 = fixture.returnIntArray(a1) self.assertEqual(type(a2), jpy.get_type('[I'))
def test_get_class_of_unknown_type(self): with self.assertRaises(ValueError) as e: String = jpy.get_type('java.lang.Spring') self.assertEqual(str(e.exception), "Java class 'java.lang.Spring' not found") with self.assertRaises(ValueError) as e: IntArray = jpy.get_type('int[]') self.assertEqual(str(e.exception), "Java class 'int[]' not found")
def setUp(self): self.Fixture = jpy.get_type('org.jpy.fixtures.FieldTestFixture') self.assertIsNotNone(self.Fixture) self.Thing = jpy.get_type('org.jpy.fixtures.Thing') self.assertIsNotNone(self.Thing) self.String = jpy.get_type('java.lang.String') self.assertIsNotNone(self.String)
def test_toReproduceAndFixIssue54(self): String = jpy.get_type('java.lang.String') Arrays = jpy.get_type('java.util.Arrays') a = jpy.array(String, ['A', 'B', 'C']) # jpy.diag.flags = jpy.diag.F_METH s = Arrays.toString(a) # jpy.diag.flags = 0 # without the fix, we get str(s) = "java.lang.String@xxxxxx" self.assertEqual(str(s), '[A, B, C]')
def test_array_object(self): File = jpy.get_type('java.io.File') String = jpy.get_type('java.lang.String') Integer = jpy.get_type('java.lang.Integer') self.do_test_array_protocol('java.lang.Integer', [None, None, None], [1, None, 3]) self.do_test_array_protocol('java.lang.String', [None, None, None], ['A', 'B', 'C']) self.do_test_array_protocol('java.io.File', [None, None, None], [File('A'), File('B'), File('C')]) self.do_test_array_protocol('java.lang.Object', [None, None, None], [None, None, None]) self.do_test_array_protocol('java.lang.Object', [None, None, None], [File('A'), 'B', 3])
def test_toReproduceAndFixIssue57(self): HashMap = jpy.get_type('java.util.HashMap') Map = jpy.get_type('java.util.Map') m = HashMap() c = m.getClass() self.assertEqual(c.getName(), 'java.util.HashMap') m = jpy.cast(m, Map) # without the fix, we get "AttributeError: 'java.util.Map' object has no attribute 'getClass'" c = m.getClass() self.assertEqual(c.getName(), 'java.util.HashMap')
def test_issue_74(self): """ Try to create enough references to trigger collection by Python. """ java_types = ['boolean', 'char', 'byte', 'short', 'int', 'long', 'float', 'double', 'void', 'java.lang.String'] for java_type in java_types: for i in range(200): jpy.get_type(java_type)
def test_get_class_of_object_array(self): StringArray1D = jpy.get_type('[Ljava.lang.String;') self.assertEqual(str(StringArray1D), TYPE_STR_PREFIX + "'[Ljava.lang.String;'>") StringArray2D = jpy.get_type('[[Ljava.lang.String;') self.assertEqual(str(StringArray2D), TYPE_STR_PREFIX + "'[[Ljava.lang.String;'>") StringArray3D = jpy.get_type('[[[Ljava.lang.String;') self.assertEqual(str(StringArray3D), TYPE_STR_PREFIX + "'[[[Ljava.lang.String;'>") with self.assertRaises(RuntimeError) as e: StringArray1D() self.assertEqual(str(e.exception), "no constructor found (missing JType attribute '__jinit__')")
def test_get_class_of_primitive_array(self): IntArray1D = jpy.get_type('[I') self.assertEqual(str(IntArray1D), TYPE_STR_PREFIX + "'[I'>") IntArray2D = jpy.get_type('[[I') self.assertEqual(str(IntArray2D), TYPE_STR_PREFIX + "'[[I'>") IntArray3D = jpy.get_type('[[[I') self.assertEqual(str(IntArray3D), TYPE_STR_PREFIX + "'[[[I'>") with self.assertRaises(RuntimeError) as e: IntArray1D() self.assertEqual(str(e.exception), "no constructor found (missing JType attribute '__jinit__')")
def test_modifyAndReturnIntArray(self): fixture = self.Fixture() # See https://docs.python.org/2/c-api/buffer.html # "An array can only expose its contents via the old-style buffer interface. # This limitation does not apply to Python 3, where memoryview objects can be # constructed from arrays, too." # >>> import array # >>> a = array.array('i', [1,2,3]) # >>> m = memoryview(a) # TypeError: cannot make memory view because object does not have the buffer interface # if sys.version_info >= (3, 0, 0): a1 = array.array('i', [0, 0, 0]) # Python 2.7: TypeError: must be impossible<bad format char>, not bool a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18) self.assertIs(a1, a2) self.assertEqual(a2[0], 16) self.assertEqual(a2[1], 17) self.assertEqual(a2[2], 18) if np: a1 = np.array([0, 0, 0], dtype='int32') a2 = fixture.modifyAndReturnIntArray(a1, 10, 11, 12) self.assertIs(a1, a2) self.assertEqual(a2[0], 10) self.assertEqual(a2[1], 11) self.assertEqual(a2[2], 12) a1 = jpy.array('int', 3) a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18) self.assertIs(a1, a2) self.assertEqual(a2[0], 16) self.assertEqual(a2[1], 17) self.assertEqual(a2[2], 18) a1 = None a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18) self.assertEqual(type(a2), jpy.get_type('[I')) self.assertEqual(a2[0], 16) self.assertEqual(a2[1], 17) self.assertEqual(a2[2], 18) a1 = [0, 0, 0] a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18) self.assertEqual(type(a2), jpy.get_type('[I')) self.assertEqual(a2[0], 16) self.assertEqual(a2[1], 17) self.assertEqual(a2[2], 18)
def test_toReproduceAndFixIssue55(self): Paths = jpy.get_type('java.nio.file.Paths') # The following outcommented statement is will end in a Python error # RuntimeError: no matching Java method overloads found #p = Paths.get('testfile') # This is the embarrassing workaround p = Paths.get('testfile', [])
def test_toPath(self): f = self.File('/usr/local/bibo') self.assertTrue('toPath' in self.File.__dict__) path = f.toPath() if sys.version_info >= (3, 0, 0): self.assertEqual(str(type(path)), '<class \'java.nio.file.Path\'>') else: self.assertEqual(str(type(path)), '<type \'java.nio.file.Path\'>') jpy.get_type('java.nio.file.Path') n1 = path.getName(0) n2 = path.getName(1) n3 = path.getName(2) self.assertEqual(str(n1), 'usr') self.assertEqual(str(n2), 'local') self.assertEqual(str(n3), 'bibo')
def test_array_item_del(self): Integer = jpy.get_type('java.lang.Integer') a = jpy.array(Integer, 3) try: del a[1] except RuntimeError as err: self.assertEqual(err.args[0], 'cannot delete items of Java arrays')
def test_toReproduceAndFixIssue56(self): Paths = jpy.get_type('java.nio.file.Paths') p = Paths.get('testfile', []) s = str(p) self.assertEqual(s, 'testfile') # The following call crashed the Python interpreter with JDK/JRE 1.8.0 < update 60. s = p.toString() self.assertEqual(s, 'testfile')
def get_timeseries(product, variable_name, shape): timeseries = {} times = get_coordinate_variable(product, 'Time') if times is None: raise ValueError('No time variable found') time_units = get_axis_units(product, 'Time') if time_units: start = (num2date(times[0], time_units, calendar='standard')).isoformat() else: start = ''.join(times[0]) timeseries['global'] = {'time': start} units = get_axis_units(product, str(variable_name)) timeseries['units'] = units timeseries['data'] = {} factory = jpy.get_type('org.esa.beam.framework.datamodel.StxFactory')().withRoiShape(shape) ProgressMonitor = jpy.get_type('com.bc.ceres.core.ProgressMonitor') for time_index, band in enumerate(product.getBands()): print (str(time_index) + ': ' + band.getName()) if time_units: date = num2date(times[time_index], time_units, calendar='standard').isoformat() else: date = ''.join(times[time_index]) stx = factory.create(band, ProgressMonitor.NULL) maximum = stx.getMaximum() minimum = stx.getMinimum() std = stx.getStandardDeviation() mean = stx.getMean() median = stx.getMedian() if np.isnan(maximum) or np.isnan(minimum) or np.isnan(std) or np.isnan(mean) or np.isnan(median): pass else: timeseries['data'][date] = {'mean': mean, 'median': median,'std': std, 'min': minimum, 'max': maximum} return timeseries
def test_nArgOverloadsAreFoundInBaseClass(self): Fixture = jpy.get_type('org.jpy.fixtures.MethodOverloadTestFixture$MethodOverloadTestFixture2') fixture = Fixture() self.assertEqual(fixture.join('x'), 'String(x)') self.assertEqual(fixture.join('x', 'y'), 'String(x),String(y)') self.assertEqual(fixture.join('x', 'y', 'z'), 'String(x),String(y),String(z)') self.assertEqual(fixture.join('x', 'y', 'z', 'u'), 'String(x),String(y),String(z),String(u)') with self.assertRaises(RuntimeError, msg='RuntimeError expected') as e: fixture.join('x', 'y', 'z', 'u', 'v') self.assertEqual(str(e.exception), 'no matching Java method overloads found')
# # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # """ This module supports the conversion between Deephaven tables and numpy arrays. """ import re from typing import List import jpy import numpy as np from deephaven import DHError, dtypes, empty_table, new_table from deephaven.column import Column, InputColumn from deephaven.table import Table _JPrimitiveArrayConversionUtility = jpy.get_type("io.deephaven.integrations.common.PrimitiveArrayConversionUtility") def freeze_table(table: Table) -> Table: """ Returns a static snapshot of the source ticking table. Args: table (Table): the source table Returns: a new table """ return empty_table(0).snapshot(table, True) def _to_column_name(name: str) -> str: """ Transforms the given name string into a valid table column name. """
def __init__(self, value): threading.Thread.__init__(self) Integer = jpy.get_type('java.lang.Integer') self.intObj = Integer(value)
) ) return True jpy.type_callbacks["org.esa.beam.framework.datamodel.RasterDataNode"] = annotate_RasterDataNode_methods jpy.type_callbacks["org.esa.beam.framework.datamodel.AbstractBand"] = annotate_RasterDataNode_methods jpy.type_callbacks["org.esa.beam.framework.datamodel.Band"] = annotate_RasterDataNode_methods jpy.type_callbacks["org.esa.beam.framework.datamodel.VirtualBand"] = annotate_RasterDataNode_methods try: # Note we may later want to read pre-defined types from a configuration file (beampy.ini) String = jpy.get_type("java.lang.String") File = jpy.get_type("java.io.File") Rectangle = jpy.get_type("java.awt.Rectangle") SystemUtils = jpy.get_type("org.esa.beam.util.SystemUtils") ProductIO = jpy.get_type("org.esa.beam.framework.dataio.ProductIO") Product = jpy.get_type("org.esa.beam.framework.datamodel.Product") ProductData = jpy.get_type("org.esa.beam.framework.datamodel.ProductData") RasterDataNode = jpy.get_type("org.esa.beam.framework.datamodel.RasterDataNode") AbstractBand = jpy.get_type("org.esa.beam.framework.datamodel.AbstractBand") Band = jpy.get_type("org.esa.beam.framework.datamodel.Band") VirtualBand = jpy.get_type("org.esa.beam.framework.datamodel.VirtualBand") GeoCoding = jpy.get_type("org.esa.beam.framework.datamodel.GeoCoding") GeoPos = jpy.get_type("org.esa.beam.framework.datamodel.GeoPos") PixelPos = jpy.get_type("org.esa.beam.framework.datamodel.PixelPos")
def test_py_object_overload_test_1(self): PassPyObjectToJava = jpy.get_type('io/deephaven/jpy/integration/PassPyObjectToJava') self.assertEquals("String", PassPyObjectToJava.overload_test_1('a string')) self.assertEquals("PyObject", PassPyObjectToJava.overload_test_1(42))
# # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # """ This module implements the Color class and provides a list of predefined colors that can be used to paint a plot. """ from __future__ import annotations import jpy from deephaven import DHError from deephaven._wrapper import JObjectWrapper _JColor = jpy.get_type("io.deephaven.gui.color.Color") class Color(JObjectWrapper): """ A color. """ j_object_type = _JColor @property def j_object(self) -> jpy.JType: return self.j_color def __init__(self, j_color: jpy.JType): self.j_color = j_color @staticmethod def of_name(name: str) -> Color: """ Creates a Color instance represented by the name string.
def test_reenter_python(self): ReenterPython = jpy.get_type('io.deephaven.jpy.integration.ReenterPython') self.assertEquals(42, ReenterPython.calc1Plus41InPython())
def test_org_jpy_pylib(self): jpy.get_type('org.jpy.PyLib')
return True jpy.type_callbacks['org.esa.snap.framework.datamodel.RasterDataNode'] = annotate_RasterDataNode_methods jpy.type_callbacks['org.esa.snap.framework.datamodel.AbstractBand'] = annotate_RasterDataNode_methods jpy.type_callbacks['org.esa.snap.framework.datamodel.Band'] = annotate_RasterDataNode_methods jpy.type_callbacks['org.esa.snap.framework.datamodel.VirtualBand'] = annotate_RasterDataNode_methods # # Preload and assign frequently used Java classes from the Java SE and SNAP Java API. # try: # Note we may later want to read pre-defined types from a configuration file (snappy.ini) String = jpy.get_type('java.lang.String') File = jpy.get_type('java.io.File') Rectangle = jpy.get_type('java.awt.Rectangle') SystemUtils = jpy.get_type('org.esa.snap.util.SystemUtils') ProductIO = jpy.get_type('org.esa.snap.framework.dataio.ProductIO') Product = jpy.get_type('org.esa.snap.framework.datamodel.Product') ProductData = jpy.get_type('org.esa.snap.framework.datamodel.ProductData') RasterDataNode = jpy.get_type('org.esa.snap.framework.datamodel.RasterDataNode') AbstractBand = jpy.get_type('org.esa.snap.framework.datamodel.AbstractBand') Band = jpy.get_type('org.esa.snap.framework.datamodel.Band') VirtualBand = jpy.get_type('org.esa.snap.framework.datamodel.VirtualBand') GeoCoding = jpy.get_type('org.esa.snap.framework.datamodel.GeoCoding') GeoPos = jpy.get_type('org.esa.snap.framework.datamodel.GeoPos') PixelPos = jpy.get_type('org.esa.snap.framework.datamodel.PixelPos')
def test_has_jpy_classes(self): jpy.get_type('org.jpy.PyLib')
def listen(t, listener, description=None, retain=True, ltype="auto", start_listening=True, replay_initial=False, lock_type="shared"): """ Listen to table changes. Table change events are processed by listener, which can be either (1) a callable (e.g. function) or (2) an object which provides an "onUpdate" method. In either case, the method must have one of the following signatures. * (added, removed, modified): legacy * (isReplay, added, removed, modified): legacy + replay * (update): shift-aware * (isReplay, update): shift-aware + replay For legacy listeners, added, removed, and modified are the indices of the rows which changed. For shift-aware listeners, update is an object which describes the table update. Listeners which support replaying the initial table snapshot have an additional parameter, inReplay, which is true when replaying the initial snapshot and false during normal updates. See the Deephaven listener documentation for details on processing update events. This documentation covers the details of the added, removed, and modified indices; index shift information; as well as the details of how to apply the update object. It also has examples on how to access current and previous-tick table values. :param t: dynamic table to listen to. :param listener: listener to process changes. :param description: description for the UpdatePerformanceTracker to append to the listener's entry description. :param retain: whether a hard reference to this listener should be maintained to prevent it from being collected. :param ltype: listener type. Valid values are "auto", "legacy", and "shift_aware". "auto" (default) uses inspection to automatically determine the type of input listener. "legacy" is for a legacy listener, which takes three (added, removed, modified) or four (isReplay, added, removed, modified) arguments. "shift_aware" is for a shift-aware listener, which takes one (update) or two (isReplay, update) arguments. :param start_listening: True to create the listener and register the listener with the table. The listener will see updates. False to create the listener, but do not register the listener with the table. The listener will not see updates. :param replay_initial: True to replay the initial table contents to the listener. False to only listen to new table changes. To replay the initial image, the listener must support replay. :param lock_type: LTM lock type. Used when replay_initial=True. See :func:`doLocked` for valid values. :return: table listener handle. """ if replay_initial and not start_listening: raise ValueError( "Unable to create listener. Inconsistent arguments. If the initial snapshot is replayed (replay_initial=True), then the listener must be registered to start listening (start_listening=True)." ) _java_type_DynamicTable = jpy.get_type("io.deephaven.db.v2.DynamicTable") dt = jpy.cast(t, _java_type_DynamicTable) nargs = _nargsListener(listener) if ltype == None or ltype == "auto": if nargs == 1 or nargs == 2: ltype = "shift_aware" elif nargs == 3 or nargs == 4: ltype = "legacy" else: raise ValueError( "Unable to autodetect listener type. Listener does not take an expected number of arguments. args={}" .format(nargs)) if ltype == "legacy": if nargs == 3: if replay_initial: raise ValueError( "Listener does not support replay: ltype={} nargs={}". format(ltype, nargs)) ListenerAdapter = jpy.get_type( "io.deephaven.integrations.python.PythonListenerAdapter") listener_adapter = ListenerAdapter(description, dt, retain, listener) elif nargs == 4: ListenerAdapter = jpy.get_type( "io.deephaven.integrations.python.PythonReplayListenerAdapter") listener_adapter = ListenerAdapter(description, dt, retain, listener) else: raise ValueError( "Legacy listener must take 3 (added, removed, modified) or 4 (isReplay, added, removed, modified) arguments." ) elif ltype == "shift_aware": if nargs == 1: if replay_initial: raise ValueError( "Listener does not support replay: ltype={} nargs={}". format(ltype, nargs)) ListenerAdapter = jpy.get_type( "io.deephaven.integrations.python.PythonShiftAwareListenerAdapter" ) listener_adapter = ListenerAdapter(description, dt, retain, listener) elif nargs == 2: ListenerAdapter = jpy.get_type( "io.deephaven.integrations.python.PythonShiftAwareReplayListenerAdapter" ) listener_adapter = ListenerAdapter(description, dt, retain, listener) else: raise ValueError( "Shift-aware listener must take 1 (update) or 2 (isReplay, update) arguments." ) else: raise ValueError("Unsupported listener type: ltype={}".format(ltype)) handle = TableListenerHandle(dt, listener_adapter) def start(): if replay_initial: listener_adapter.replay() if start_listening: handle.register() if replay_initial: doLocked(start, lock_type=lock_type) else: start() return handle
# # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # """ This module implements the AxisFormat class that can be applied to format axis tick labels on a plot. """ import jpy from deephaven.time import TimeZone from deephaven._wrapper import JObjectWrapper _JAxisFormat = jpy.get_type("io.deephaven.plot.axisformatters.AxisFormat") _JDecimalAxisFormat = jpy.get_type("io.deephaven.plot.axisformatters.DecimalAxisFormat") _JNanosAxisFormat = jpy.get_type("io.deephaven.plot.axisformatters.NanosAxisFormat") class AxisFormat(JObjectWrapper): """ The AxisFormat class defines the format for axis tick labels. For time values, this would be how the dates are formatted. For numerical values, this would be the number of significant digits, etc. """ j_object_type = _JAxisFormat @property def j_object(self) -> jpy.JType: return self.j_axis_format def __init__(self, j_axis_format): self.j_axis_format = j_axis_format def set_pattern(self, pattern: str) -> None: """ Set the pattern used for formatting values.
def Config(): return jpy.get_type( "io.deephaven.configuration.Configuration").getInstance()
def setUp(self): self.File = jpy.get_type('java.io.File') self.assertIsNotNone(self.File)
def setUp(self): self.HashMap = jpy.get_type('java.util.HashMap') self.File = jpy.get_type('java.io.File')
def setUp(self): self.String = jpy.get_type('java.lang.String') self.assertIsNotNone(self.String)
def setUp(self): self.ArrayList = jpy.get_type('java.util.ArrayList') self.File = jpy.get_type('java.io.File')
def setUp(self): self.Fixture = jpy.get_type('org.jpy.fixtures.MethodReturnValueTestFixture') self.assertIsNotNone(self.Fixture) self.Thing = jpy.get_type('org.jpy.fixtures.Thing') self.assertIsNotNone(self.Thing)
# # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # """ This module provides a collection of business calendars and convenient calendar functions. """ from enum import Enum from typing import List import jpy from deephaven._wrapper import JObjectWrapper from deephaven.time import TimeZone, DateTime from deephaven import DHError _JCalendars = jpy.get_type("io.deephaven.time.calendar.Calendars") _JCalendar = jpy.get_type("io.deephaven.time.calendar.Calendar") _JDayOfWeek = jpy.get_type("java.time.DayOfWeek") _JBusinessPeriod = jpy.get_type("io.deephaven.time.calendar.BusinessPeriod") _JBusinessSchedule = jpy.get_type( "io.deephaven.time.calendar.BusinessSchedule") _JBusinessCalendar = jpy.get_type( "io.deephaven.time.calendar.BusinessCalendar") def calendar_names() -> List[str]: """ Returns the names of all available calendars. Returns: a list of names of all available calendars
def test_has_gil(self): PyLib = jpy.get_type('org.jpy.PyLib') self.assertTrue(PyLib.hasGil())
def setUp(self): self.Fixture = jpy.get_type('org.jpy.fixtures.TypeTranslationTestFixture') self.assertIsNotNone(self.Fixture)
def test_ping_pong_stack(self): PingPongStack = jpy.get_type('io.deephaven.jpy.integration.PingPongStack') self.assertEquals('test_jpy(java,5)(python,4)(java,3)(python,2)(java,1)', PingPongStack.pingPongPython('test_jpy', 5)) self.assertEquals('test_jpy(java,4)(python,3)(java,2)(python,1)', PingPongStack.pingPongPython('test_jpy', 4))
def do_test_buffer_protocol(self, type_name, itemsize, values): self.do_test_buffer_protocol2(type_name, itemsize, values) self.do_test_buffer_protocol2(jpy.get_type(type_name), itemsize, values)
def test_pass_function_to_java_var(self): PassPyObjectToJava = jpy.get_type('io/deephaven/jpy/integration/PassPyObjectToJava') #jpy.diag.flags = jpy.diag.F_ALL PassPyObjectToJava.from_python_with_love_var(some_function, some_function2)
# # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # """ This package is a place for Deephaven experimental features. """ import jpy from deephaven import DHError from deephaven.table import Table _JWindowCheck = jpy.get_type("io.deephaven.engine.util.WindowCheck") def time_window(table: Table, ts_col: str, window: int, bool_col: str) -> Table: """Creates a new table by applying a time window to the source table and adding a new Boolean column. The value of the new Boolean column is set to false when the timestamp column value is older than the window from now or true otherwise. If the timestamp column value is null, the Boolean column value will be null as well. The result table ticks whenever the source table ticks, or modifies a row when it passes out of the window. Args: table (Table): the source table ts_col (str): the timestamp column name window (int): the size of the window in nanoseconds bool_col (str): the name of the new Boolean column. Returns: a new Table Raises: DHError
def setUp(self): self.Fixture = jpy.get_type( 'org.jpy.fixtures.MethodReturnValueTestFixture') self.assertIsNotNone(self.Fixture) self.Thing = jpy.get_type('org.jpy.fixtures.Thing') self.assertIsNotNone(self.Thing)
def setUp(self): self.Fixture = jpy.get_type( 'org.jpy.fixtures.TypeResolutionTestFixture') self.assertIsNotNone(self.Fixture)
# # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # """This module gives the users a finer degree of control over when to clean up unreferenced nodes in the query update graph instead of solely relying on garbage collection.""" import contextlib import jpy from deephaven._wrapper import JObjectWrapper _JLivenessScopeStack = jpy.get_type( "io.deephaven.engine.liveness.LivenessScopeStack") _JLivenessScope = jpy.get_type("io.deephaven.engine.liveness.LivenessScope") class LivenessScope(JObjectWrapper): """A LivenessScope automatically manages reference counting of tables and other query resources that are created in it. It should not be instantiated directly but rather through the 'liveness_scope' context manager. """ j_object_type = _JLivenessScope def __init__(self, j_scope: jpy.JType): self.j_scope = j_scope @property def j_object(self) -> jpy.JType: return self.j_scope def preserve(self, wrapper: JObjectWrapper) -> None: """Preserves a query graph node (usually a Table) to keep it live for the outer scope.""" _JLivenessScopeStack.pop(self.j_scope)
def _defineSymbols(): if not jpy.has_jvm(): raise SystemError( "No java functionality can be used until the JVM has been initialized through the jpy module" ) global _table_tools_, _table_factory_, _col_def_, _python_tools_, _qst_col_header_, \ _qst_column_, _qst_newtable_, _qst_type_, _table_, \ DataType, bool_, byte, short, int16, char, int_, int32, long_, int64, \ float_, single, float32, double, float64, \ string, bigdecimal, biginteger, stringset, datetime, timeperiod, \ byte_array, short_array, int16_array, int_array, int32_array, long_array, int64_array, \ float_array, single_array, float32_array, double_array, float64_array, string_array, \ _type2jtype if _table_tools_ is None: # This will raise an exception if the desired object is not the classpath _table_tools_ = jpy.get_type("io.deephaven.engine.util.TableTools") _table_factory_ = jpy.get_type( "io.deephaven.engine.table.TableFactory") _col_def_ = jpy.get_type("io.deephaven.engine.table.ColumnDefinition") _python_tools_ = jpy.get_type( "io.deephaven.integrations.python.PythonTools") _qst_col_header_ = jpy.get_type( "io.deephaven.qst.column.header.ColumnHeader") _qst_column_ = jpy.get_type("io.deephaven.qst.column.Column") _qst_newtable_ = jpy.get_type("io.deephaven.qst.table.NewTable") _qst_type_ = jpy.get_type("io.deephaven.qst.type.Type") _table_ = jpy.get_type("io.deephaven.engine.table.Table") if DataType is None: DataType = NewType('DataType', _qst_type_) bool_ = DataType(_qst_type_.booleanType()) byte = DataType(_qst_type_.byteType()) short = DataType(_qst_type_.shortType()) int16 = short # make life simple for people who are used to pyarrow char = DataType(_qst_type_.charType()) int_ = DataType(_qst_type_.intType()) int32 = int_ # make life simple for people who are used to pyarrow long_ = DataType(_qst_type_.longType()) int64 = long_ # make life simple for people who are used to pyarrow float_ = DataType(_qst_type_.floatType()) single = float_ # make life simple for people who are used to NumPy float32 = float_ # make life simple for people who are used to pyarrow double = DataType(_qst_type_.doubleType()) float64 = double # make life simple for people who are used to pyarrow string = DataType(_qst_type_.stringType()) bigdecimal = _typeFromJavaClassName('java.math.BigDecimal') biginteger = _typeFromJavaClassName('java.math.BigInteger') stringset = _typeFromJavaClassName('io.deephaven.stringset.StringSet') datetime = _typeFromJavaClassName('io.deephaven.time.DateTime') timeperiod = _typeFromJavaClassName('io.deephaven.time.Period') # Array types. byte_array = DataType(byte.arrayType()) short_array = DataType(short.arrayType()) int16_array = short_array int_array = DataType(int_.arrayType()) int32_array = int_array long_array = DataType(long_.arrayType()) int64_array = long_array float_array = DataType(float_.arrayType()) single_array = float_array float32_array = float_array double_array = DataType(double.arrayType()) float64_array = double_array string_array = DataType(string.arrayType()) _type2jtype = { bool_: jpy.get_type('java.lang.Boolean'), byte: jpy.get_type('byte'), short: jpy.get_type('short'), int_: jpy.get_type('int'), long_: jpy.get_type('long'), float_: jpy.get_type('float'), double: jpy.get_type('double'), string: jpy.get_type('java.lang.String'), bigdecimal: jpy.get_type('java.math.BigDecimal'), biginteger: jpy.get_type('java.math.BigInteger'), stringset: jpy.get_type('io.deephaven.stringset.StringSet'), datetime: jpy.get_type('io.deephaven.time.DateTime'), timeperiod: jpy.get_type('io.deephaven.time.Period'), byte_array: jpy.get_type('[B'), short_array: jpy.get_type('[S'), int_array: jpy.get_type('[I'), long_array: jpy.get_type('[J'), float_array: jpy.get_type('[S'), double_array: jpy.get_type('[D'), string_array: jpy.get_type('[Ljava.lang.String;') }
#!/usr/bin/env python from IPython.terminal.ipapp import launch_new_instance import warnings import sys import jpy dp = jpy.get_type("main.hex.deepwater.DeepWater") warnings.filterwarnings("ignore", module="zmq.*") sys.argv.append("notebook") sys.argv.append("--IPKernelApp.pylab='inline'") #sys.argv.append("--NotebookApp.ip=" + gethostname()) sys.argv.append("--NotebookApp.open_browser=False") #sys.argv.append("--NotebookApp.password=" + passwd()) launch_new_instance()
def test_ThatInterfaceTypesIncludeMethodsOfExtendedTypes(self): ObjectInput = jpy.get_type('java.io.ObjectInput', resolve=True) # assert that a method declared of java.io.ObjectInput is in __dict__ self.assertTrue('readObject' in ObjectInput.__dict__) # assert that a method declared of java.io.DataInput is in __dict__ self.assertTrue('readLine' in ObjectInput.__dict__)
def do_test_array_protocol_float(self, type_name, initial, expected, places): self.do_test_array_protocol_float2(type_name, initial, expected, places) self.do_test_array_protocol_float2(jpy.get_type(type_name), initial, expected, places)
def _makeJavaArray(ndarray, javaType, containsArrays=False, depth=None): """ Internal helper method for transforming a one-dimensional numpy.ndarray to a corresponding java array. It is assumed that error checking will be performed before this stage :param ndarray: numpy.ndarray instance :param javaType: desired java type - in keeping with jpy usage :param containsArrays: boolean indicating whether ndarray is a 1-d array of dtype `object` whose elemnts are themselves arrays :param depth: if `containsArrays`, then this must be an integer argument for the depth (dimension) of the output array :return: the java array """ # No error checking, this is just to avoid tremendous code duplication if ndarray is None: return None elif not isinstance(ndarray, numpy.ndarray): raise ValueError("Cannot convert input of type {}".format( type(ndarray))) elif ndarray.size < 1: return jpy.array(javaType, 0) # return an empty array of the proper type elif len(ndarray.shape) > 1 or containsArrays: # recursively build the array if containsArrays: dimension = depth - 1 else: dimension = len(ndarray.shape) - 1 if javaType in _basicArrayTypes: arrayType = '[' * dimension + _basicArrayTypes[javaType] else: arrayType = '[' * dimension + 'L' + javaType + ';' javaArray = jpy.array( arrayType, [_makeJavaArray(subarray, javaType) for subarray in ndarray]) return javaArray elif javaType == 'char': # expecting only dtype = 'S1', 'U1' junk = None if ndarray.dtype == numpy.dtype('U1') and ndarray.dtype.name in [ 'unicode32', 'str32', 'string32', 'bytes32' ]: junk = numpy.copy(ndarray) junk.dtype = numpy.uint32 junk = junk.astype(numpy.uint16) elif ndarray.dtype == numpy.dtype('S1') and ndarray.dtype.name in [ 'str8', 'string8', 'bytes8' ]: junk = numpy.copy(ndarray) junk.dtype = numpy.uint8 junk = junk.astype(numpy.uint16) if junk is None: raise ValueError( "Cannot convert ndarray of dtype {} and dtype.name {} " "to type `char`".format(ndarray.dtype, ndarray.dtype.name)) junk[junk == 0] = NULL_CHAR return jpy.array('char', junk) elif javaType == 'float': newarray = numpy.copy(ndarray) newarray[numpy.isnan(newarray)] = NULL_FLOAT # setting NaN to NULL return jpy.array('float', newarray) elif javaType == 'double': newarray = numpy.copy(ndarray) newarray[numpy.isnan(newarray)] = NULL_DOUBLE # setting NaN to NULL return jpy.array('double', newarray) elif ndarray.dtype.name.startswith('datetime64'): if ndarray.dtype.name != 'datetime64[ns]': # convert to proper units (ns), then convert that to long longs = jpy.array('long', ndarray.astype('datetime64[ns]').astype('int64')) else: longs = jpy.array('long', ndarray.astype('int64')) if javaType == 'long': return longs else: return jpy.get_type(__ArrayConversionUtility__ ).translateArrayLongToDBDateTime(longs) elif javaType == 'java.lang.Boolean': # make corresponding byte version: None -> -1, False -> 0, True -> 1 byts = numpy.full(ndarray.shape, -1, dtype=numpy.int8) numpy.copyto(byts, ndarray, casting='unsafe', where=(ndarray != None)) # make the java version of the byte array jbytes = jpy.array('byte', byts) # convert to java boolean array return jpy.get_type( __ArrayConversionUtility__).translateArrayByteToBoolean(jbytes) elif javaType == 'java.lang.String': return jpy.array('java.lang.String', [_makeString(el) for el in ndarray]) # make every element a string elif javaType in _basicArrayTypes: return jpy.array(javaType, ndarray) elif javaType in jpy.types: # can we pass it straight-through? return jpy.array(javaType, ndarray) raise Exception("Conversion of ndarray to type {} failed".format(javaType))
# # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # """ This module defines the SelectableDateSet which is used to provides a view of a selectable subset of a table. For example, in some selectable data sets, a GUI click can be used to select a portion of a table. """ from typing import List import jpy from deephaven import DHError from deephaven._wrapper import JObjectWrapper from deephaven.table import Table _JSelectableDataSet = jpy.get_type( "io.deephaven.plot.filters.SelectableDataSet") _JTableDefinition = jpy.get_type("io.deephaven.engine.table.TableDefinition") _JSelectables = jpy.get_type("io.deephaven.plot.filters.Selectables") class SelectableDataSet(JObjectWrapper): """ A SelectableDataSet provides a view of a selectable subset of a table. For example, in some selectable data sets, a GUI click can be used to select a portion of a table. """ j_object_type = _JSelectableDataSet def __init__(self, j_sds: jpy.JType): self.j_sds = j_sds @property def j_object(self) -> jpy.JType:
# # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # """ This module implement various filters that can be used in deephaven table's filter operations.""" from __future__ import annotations from typing import List, Union import jpy from deephaven import DHError from deephaven._wrapper import JObjectWrapper from deephaven.jcompat import to_sequence _JFilter = jpy.get_type("io.deephaven.api.filter.Filter") _JRegexFilter = jpy.get_type( "io.deephaven.engine.table.impl.select.RegexFilter") _JFilterOr = jpy.get_type("io.deephaven.api.filter.FilterOr") _JFilterAnd = jpy.get_type("io.deephaven.api.filter.FilterAnd") _JFilterNot = jpy.get_type("io.deephaven.api.filter.FilterNot") class Filter(JObjectWrapper): """A Filter object represents a filter that can be used in Table's filtering(where) operations.""" j_object_type = _JFilter @property def j_object(self) -> jpy.JType: return self.j_filter def __init__(self, j_filter):
# # Copyright (c) 2016-2022 Deephaven Data Labs and Patent Pending # """ The module implements the LineStyle class that can be used to define the line style of a plot. """ from enum import Enum from numbers import Number from typing import List import jpy from deephaven import DHError from deephaven._wrapper import JObjectWrapper _JLineStyle = jpy.get_type("io.deephaven.plot.LineStyle") _JLineEndStyle = jpy.get_type("io.deephaven.plot.LineStyle$LineEndStyle") _JLineJoinStyle = jpy.get_type("io.deephaven.plot.LineStyle$LineJoinStyle") _JPlottingConvenience = jpy.get_type("io.deephaven.plot.PlottingConvenience") class LineEndStyle(Enum): """ An enum defining styles for shapes drawn at the end of a line. """ BUTT = _JLineEndStyle.BUTT """ Square line ending with edge against the end data points. """ ROUND = _JLineEndStyle.ROUND """ Round end shape. """ SQUARE = _JLineEndStyle.SQUARE
def test_has_local_classes(self): jpy.get_type('io.deephaven.jpy.integration.Empty')
def test_has_local_classes_dne(self): with self.assertRaises(ValueError): jpy.get_type('io.deephaven.jpy.integration.DoesNotExist')
def test_java_constructed_array_alloc(self): fixture = jpy.get_type('org.jpy.fixtures.JavaArrayTestFixture') # 100 * 1MB for _ in range(100): java_array = fixture.createByteArray(1000000) # 1MB
LIBS = os.path.join(os.path.dirname(EXAMPLES_DIR), 'lib') if not os.path.isdir(LIBS): LIBS = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(EXAMPLES_DIR))), 'target', 'jasperstarter-dev-bin', 'lib') CLASSPATH = os.path.join(LIBS, 'jasperstarter.jar') assert(os.path.exists(CLASSPATH)), 'Unable to find jasperstarter in {0}'.format(LIBS) # # Load the JVM. See the jpy docs for details. # import jpyutil jpyutil.init_jvm(jvm_maxmem='512M', jvm_classpath=[CLASSPATH]) # # Load the Java types needed. # import jpy Arrays = jpy.get_type('java.util.Arrays') File = jpy.get_type('java.io.File') Report = jpy.get_type('de.cenote.jasperstarter.Report') Config = jpy.get_type('de.cenote.jasperstarter.Config') DsType = jpy.get_type('de.cenote.jasperstarter.types.DsType') System = jpy.get_type('java.lang.System') PrintStream = jpy.get_type('java.io.PrintStream') ByteArrayInputStream = jpy.get_type('java.io.ByteArrayInputStream') ByteArrayOutputStream = jpy.get_type('java.io.ByteArrayOutputStream') def generate_pdf(report: str, query: str, data: str, parameters: Dict[str, str]) -> bytearray: """ Generate PDF from a report file using JSON. :param report: The name of the report .jrxml.
def setUp(self): self.Fixture = jpy.get_type('org.jpy.fixtures.ModifyAndReturnParametersTestFixture') self.assertIsNotNone(self.Fixture) self.Thing = jpy.get_type('org.jpy.fixtures.Thing') self.assertIsNotNone(self.Thing)
def do_test_array_protocol(self, type_name, initial, expected): self.do_test_array_protocol2(type_name, initial, expected) self.do_test_array_protocol2(jpy.get_type(type_name), initial, expected) self.do_test_array_with_initializer(type_name, expected)