Skip to content

Commit

Permalink
hand-wrapped the datetimeutils module, fixes #1640 (#1812)
Browse files Browse the repository at this point in the history
 hand-wrapped The DateTimeUtils module, fixes #1640
  • Loading branch information
jmao-denver authored Jan 31, 2022
1 parent c38eafa commit b9bc0c3
Show file tree
Hide file tree
Showing 11 changed files with 1,158 additions and 43 deletions.
6 changes: 3 additions & 3 deletions pyintegration/deephaven2/column.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#
from dataclasses import dataclass, field
from enum import Enum
from typing import Sequence
from typing import Sequence, Any

import jpy
from deephaven2 import DHError
from deephaven2.dtypes import DType

import deephaven2.dtypes as dtypes
from deephaven2.dtypes import DType
from deephaven2 import DHError

_JColumnHeader = jpy.get_type("io.deephaven.qst.column.header.ColumnHeader")
_JColumn = jpy.get_type("io.deephaven.qst.column.Column")
Expand Down
5 changes: 3 additions & 2 deletions pyintegration/deephaven2/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from enum import Enum, auto
import jpy

_JQueryConstants = jpy.get_type("io.deephaven.util.QueryConstants")


class SortDirection(Enum):
"""An enum defining the sorting orders."""
Expand All @@ -15,8 +17,6 @@ class SortDirection(Enum):


# Deephaven Special Null values for primitive types
_JQueryConstants = jpy.get_type("io.deephaven.util.QueryConstants")

NULL_BOOLEAN = _JQueryConstants.NULL_BOOLEAN
""" Null boolean value. """
NULL_CHAR = _JQueryConstants.NULL_CHAR
Expand Down Expand Up @@ -92,3 +92,4 @@ class SortDirection(Enum):
MIN_POS_DOUBLE = _JQueryConstants.MIN_POS_DOUBLE
""" Minimum positive value of type double. """


24 changes: 11 additions & 13 deletions pyintegration/deephaven2/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,11 @@
"""
from __future__ import annotations

from typing import Iterable, Any, Tuple, Sequence, Callable
from typing import Any, Sequence, Callable, Iterable

import numpy as np
import pandas as pd
import jpy

from deephaven2 import DHError
from deephaven2.constants import *

_JQstType = jpy.get_type("io.deephaven.qst.type.Type")
_JTableTools = jpy.get_type("io.deephaven.engine.util.TableTools")
Expand All @@ -26,15 +23,16 @@ def _qst_custom_type(cls_name: str):

class DType:
""" A class representing a data type in Deephaven."""
_j_name_map = {}

_j_name_type_map = {}

@classmethod
def from_jtype(cls, j_class: Any) -> DType:
if not j_class:
return None

j_name = j_class.getName()
dtype = DType._j_name_map.get(j_name)
dtype = DType._j_name_type_map.get(j_name)
if not dtype:
return cls(j_name=j_name, j_type=j_class)
else:
Expand All @@ -46,13 +44,16 @@ def __init__(self, j_name: str, j_type: Any = None, qst_type: Any = None, is_pri
self.qst_type = qst_type if qst_type else _qst_custom_type(j_name)
self.is_primitive = is_primitive

DType._j_name_map[j_name] = self
DType._j_name_type_map[j_name] = self

def __repr__(self):
return self.j_name

def __call__(self, *args, **kwargs):
return self.j_type(*args, **kwargs)
try:
return self.j_type(*args, **kwargs)
except Exception as e:
raise DHError(e, f"failed to create an instance of {self.j_name}") from e

def array(self, size: int):
""" Creates a Java array of the same data type of the specified size.
Expand Down Expand Up @@ -111,7 +112,6 @@ def array_from(self, seq: Sequence, remap: Callable[[Any], Any] = None):
return super().array_from(seq, remap)


# region predefined types and aliases
bool_ = DType(j_name="java.lang.Boolean", qst_type=_JQstType.booleanType())
byte = DType(j_name="byte", qst_type=_JQstType.byteType(), is_primitive=True)
int8 = byte
Expand All @@ -130,14 +130,12 @@ def array_from(self, seq: Sequence, remap: Callable[[Any], Any] = None):
string = DType(j_name="java.lang.String", qst_type=_JQstType.stringType())
BigDecimal = DType(j_name="java.math.BigDecimal")
StringSet = DType(j_name="io.deephaven.stringset.StringSet")
DateTime = DType(j_name="io.deephaven.time.DateTime")
Period = DType(j_name="io.deephaven.time.Period")
PyObject = DType(j_name="org.jpy.PyObject")
JObject = DType(j_name="java.lang.Object")
DateTime = DType(j_name="io.deephaven.time.DateTime")
Period = DType(j_name="io.deephaven.time.Period")


# endregion

def j_array_list(values: Iterable):
j_list = jpy.get_type("java.util.ArrayList")(len(values))
try:
Expand Down
26 changes: 26 additions & 0 deletions pyintegration/deephaven2/html.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#
# Copyright (c) 2016-2021 Deephaven Data Labs and Patent Pending
#
""" This module supports exporting Deephaven data in the HTML format. """

import jpy

from deephaven2 import DHError
from deephaven2.table import Table

_JTableTools = jpy.get_type("io.deephaven.engine.util.TableTools")


def to_html(table: Table) -> str:
""" Returns a table formatted as an HTML string. Limit use to small tables to avoid running out of memory.
Returns:
a HTML string
Raises:
DHError
"""
try:
return _JTableTools.html(table.j_table)
except Exception as e:
raise DHError(e, "table to_html failed") from e
10 changes: 0 additions & 10 deletions pyintegration/deephaven2/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,16 +98,6 @@ def to_string(self, num_rows: int = 10, cols: List[str] = []) -> str:
except Exception as e:
raise DHError(e, "table to_string failed") from e

def to_html(self):
"""
Returns a printout of a table formatted as HTML. Limit use to small tables to avoid running out of memory.
:param source: (io.deephaven.db.tables.Table) - a Deephaven table object
:return: (java.lang.String) a String of the table printout formatted as HTML
"""

return _JTableTools.html(self.j_table)

def coalesce(self) -> Table:
""" Returns a coalesced child table. """
return Table(j_table=self.j_table.coalesce())
Expand Down
21 changes: 15 additions & 6 deletions pyintegration/deephaven2/table_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

import jpy

from deephaven2.column import Column
from deephaven2 import DHError
from deephaven2.column import InputColumn
from deephaven2.table import Table

_JTableFactory = jpy.get_type("io.deephaven.engine.table.TableFactory")
Expand Down Expand Up @@ -55,8 +55,17 @@ def time_table(period: str, start_time: str = None) -> Table:
raise DHError(e, "failed to create a time table.") from e


def new_table(cols: List[Column]) -> Table:
""" Creates an in-memory table from a list of columns. Each column must have an equal number of elements.
def new_table(cols: List[InputColumn]) -> Table:
""" Creates an in-memory table from a list of input columns. Each column must have an equal number of elements.
Args:
cols (List[InputColumn]): a list of InputColumn
Returns:
a Table
Raises:
DHError
"""
try:
return Table(j_table=_JTableFactory.newTable(*[col.j_column for col in cols]))
Expand All @@ -72,7 +81,7 @@ def merge(tables: List[Table]):
tables (List[Table]): the source tables
Returns:
a new table
a Table
Raises:
DHError
Expand All @@ -83,7 +92,7 @@ def merge(tables: List[Table]):
raise DHError(e, "merge tables operation failed.") from e


def merge_sorted(tables: List[Table], order_by: str):
def merge_sorted(tables: List[Table], order_by: str) -> Table:
""" Combines two or more tables into one sorted, aggregate table. This essentially stacks the tables one on top
of the other and sorts the result. Null tables are ignored. mergeSorted is more efficient than using merge
followed by sort.
Expand All @@ -93,7 +102,7 @@ def merge_sorted(tables: List[Table], order_by: str):
order_by (str): the name of the key column
Returns:
a new table
a Table
Raises:
DHError
Expand Down
Loading

0 comments on commit b9bc0c3

Please sign in to comment.