diff --git a/DB/src/main/java/io/deephaven/db/tables/remote/preview/ArrayPreview.java b/DB/src/main/java/io/deephaven/db/tables/remote/preview/ArrayPreview.java index 08d22712dad..78068124aca 100644 --- a/DB/src/main/java/io/deephaven/db/tables/remote/preview/ArrayPreview.java +++ b/DB/src/main/java/io/deephaven/db/tables/remote/preview/ArrayPreview.java @@ -2,6 +2,7 @@ import io.deephaven.db.tables.dbarrays.DbArrayBase; import io.deephaven.db.v2.sources.chunk.ChunkType; +import org.jpy.PyListWrapper; /** * A Preview Type for Arrays and DbArray. Converts long arrays to a String "[1, 2, 3, 4, 5...]" @@ -10,14 +11,14 @@ public class ArrayPreview implements PreviewType { private static final int ARRAY_SIZE_CUTOFF = 5; private final String displayString; - public static ArrayPreview fromDbArray(DbArrayBase dbArray) { + public static ArrayPreview fromDbArray(final DbArrayBase dbArray) { if (dbArray == null) { return null; } return new ArrayPreview(dbArray.toString(ARRAY_SIZE_CUTOFF)); } - public static ArrayPreview fromArray(Object array) { + public static ArrayPreview fromArray(final Object array) { if (array == null) { return null; } @@ -28,6 +29,13 @@ public static ArrayPreview fromArray(Object array) { .toString(ARRAY_SIZE_CUTOFF)); } + public static ArrayPreview fromPyListWrapper(final PyListWrapper list) { + if (list == null) { + return null; + } + return new ArrayPreview(list.toString(ARRAY_SIZE_CUTOFF)); + } + private ArrayPreview(String displayString) { this.displayString = displayString; diff --git a/DB/src/main/java/io/deephaven/db/tables/remote/preview/ColumnPreviewManager.java b/DB/src/main/java/io/deephaven/db/tables/remote/preview/ColumnPreviewManager.java index 93019ee23ed..1d9cde74dc0 100644 --- a/DB/src/main/java/io/deephaven/db/tables/remote/preview/ColumnPreviewManager.java +++ b/DB/src/main/java/io/deephaven/db/tables/remote/preview/ColumnPreviewManager.java @@ -10,6 +10,7 @@ import io.deephaven.db.v2.sources.ColumnSource; import io.deephaven.util.type.TypeUtils; import org.apache.commons.lang3.StringUtils; +import org.jpy.PyListWrapper; import java.util.*; import java.util.function.Function; @@ -27,6 +28,8 @@ public class ColumnPreviewManager { new PreviewColumnFactory<>(Object.class, ArrayPreview.class, ArrayPreview::fromArray); private static final PreviewColumnFactory dbArrayPreviewFactory = new PreviewColumnFactory<>(DbArrayBase.class, ArrayPreview.class, ArrayPreview::fromDbArray); + private static final PreviewColumnFactory pyListWrapperPreviewFactory = + new PreviewColumnFactory<>(PyListWrapper.class, ArrayPreview.class, ArrayPreview::fromPyListWrapper); // Factory for non-serializable types private static final PreviewColumnFactory nonDisplayableFactory = @@ -87,6 +90,10 @@ public static Table applyPreview(final Table table) { // Always wrap DbArrays selectColumns.add(dbArrayPreviewFactory.makeColumn(name)); originalTypes.put(name, type.getName()); + } else if (PyListWrapper.class.isAssignableFrom(type)) { + // Always wrap PyListWrapper + selectColumns.add(pyListWrapperPreviewFactory.makeColumn(name)); + originalTypes.put(name, type.getName()); } else if (type.isArray()) { // Always wrap arrays selectColumns.add(arrayPreviewFactory.makeColumn(name)); diff --git a/py/jpy/src/main/java/org/jpy/PyListWrapper.java b/py/jpy/src/main/java/org/jpy/PyListWrapper.java index 38831860a76..f4875036063 100644 --- a/py/jpy/src/main/java/org/jpy/PyListWrapper.java +++ b/py/jpy/src/main/java/org/jpy/PyListWrapper.java @@ -23,7 +23,7 @@ /** * A simple wrapper around a Python List object that implements a java List of PyObjects. */ -class PyListWrapper implements List { +public class PyListWrapper implements List { // todo: https://docs.python.org/3/c-api/list.html vs https://docs.python.org/3/c-api/sequence.html private PyObject pyObject; @@ -235,4 +235,28 @@ public ListIterator listIterator(int index) { public List subList(int fromIndex, int toIndex) { throw new UnsupportedOperationException(); } + + /** + * Return a summarized preview of this list. + * + * @param prefixLength The maximum prefix of the array to convert + * @return The String representation of array + */ + public String toString(final int prefixLength) { + if (isEmpty()) { + return "[]"; + } + final StringBuilder builder = new StringBuilder("["); + final int displaySize = Math.min(size(), prefixLength); + builder.append(get(0).str()); + for (int ei = 1; ei < displaySize; ++ei) { + builder.append(", ").append(get(ei).str()); + } + if (displaySize == size()) { + builder.append(']'); + } else { + builder.append(", ...]"); + } + return builder.toString(); + } }