|
1 | 1 |
|
2 | 2 | from ipywidgets import register, widget_serialization
|
3 |
| -from traitlets import validate, TraitError |
4 |
| -from ipydatawidgets import NDArrayWidget |
| 3 | +from traitlets import validate, TraitError, Undefined |
| 4 | +from ipydatawidgets import NDArrayWidget, get_union_array |
5 | 5 |
|
6 | 6 | from .BufferGeometry_autogen import BufferGeometry as BufferGeometryBase
|
7 | 7 |
|
@@ -30,3 +30,43 @@ def validate(self, proposal):
|
30 | 30 | raise TraitError('Index attribute must be a flat array. Consider using array.ravel().')
|
31 | 31 |
|
32 | 32 | return value
|
| 33 | + |
| 34 | + |
| 35 | + def _gen_repr_from_keys(self, keys): |
| 36 | + # Hide data in repr to avoid overly large datasets |
| 37 | + # Replace with uuids of buffer attributes |
| 38 | + class_name = self.__class__.__name__ |
| 39 | + signature_parts = [ |
| 40 | + '%s=%r' % (key, getattr(self, key)) |
| 41 | + for key in keys if key not in ('attributes', 'morphAttributes') |
| 42 | + ] |
| 43 | + for name in ('attributes', 'morphAttributes'): |
| 44 | + if not _dict_is_default(self, name): |
| 45 | + signature_parts.append('%s=%s' % (name, _attr_dict_repr(getattr(self, name)))) |
| 46 | + return '%s(%s)' % (class_name, ', '.join(signature_parts)) |
| 47 | + |
| 48 | + |
| 49 | +def _dict_is_default(ht, name): |
| 50 | + value = getattr(ht, name) |
| 51 | + return ( |
| 52 | + getattr(ht.__class__, name).default_value == Undefined and |
| 53 | + (value is None or len(value) == 0) |
| 54 | + ) |
| 55 | + |
| 56 | +def _attr_value_repr(v): |
| 57 | + array = get_union_array(v.array) |
| 58 | + # Return full repr if array size is small: |
| 59 | + if array.size < 50: |
| 60 | + return repr(v) |
| 61 | + # Otherwise, return a summary: |
| 62 | + return '<%s shape=%r, dtype=%s>' % (v.__class__.__name__, array.shape, array.dtype) |
| 63 | + |
| 64 | +def _attr_dict_repr(d): |
| 65 | + parts = [] |
| 66 | + for key, value in d.items(): |
| 67 | + if isinstance(value, tuple): |
| 68 | + value_parts = [_attr_value_repr(v) for v in value] |
| 69 | + else: |
| 70 | + value_parts = [_attr_value_repr(value)] |
| 71 | + parts.append('%r: %s' % (key, ', '.join(value_parts))) |
| 72 | + return '{%s}' % (', '.join(parts),) |
0 commit comments