Skip to content

Commit

Permalink
Friendly stringing of py list wrapper (#1520)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbauernfeind authored Nov 2, 2021
1 parent 5d5a8bd commit 5153657
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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...]"
Expand All @@ -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;
}
Expand All @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,6 +28,8 @@ public class ColumnPreviewManager {
new PreviewColumnFactory<>(Object.class, ArrayPreview.class, ArrayPreview::fromArray);
private static final PreviewColumnFactory<DbArrayBase, ArrayPreview> dbArrayPreviewFactory =
new PreviewColumnFactory<>(DbArrayBase.class, ArrayPreview.class, ArrayPreview::fromDbArray);
private static final PreviewColumnFactory<PyListWrapper, ArrayPreview> pyListWrapperPreviewFactory =
new PreviewColumnFactory<>(PyListWrapper.class, ArrayPreview.class, ArrayPreview::fromPyListWrapper);

// Factory for non-serializable types
private static final PreviewColumnFactory<Object, DisplayWrapper> nonDisplayableFactory =
Expand Down Expand Up @@ -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));
Expand Down
26 changes: 25 additions & 1 deletion py/jpy/src/main/java/org/jpy/PyListWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
/**
* A simple wrapper around a Python List object that implements a java List of PyObjects.
*/
class PyListWrapper implements List<PyObject> {
public class PyListWrapper implements List<PyObject> {
// todo: https://docs.python.org/3/c-api/list.html vs https://docs.python.org/3/c-api/sequence.html
private PyObject pyObject;

Expand Down Expand Up @@ -235,4 +235,28 @@ public ListIterator<PyObject> listIterator(int index) {
public List<PyObject> 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();
}
}

0 comments on commit 5153657

Please sign in to comment.