Source code for sympathy.typeutils.text
# This file is part of Sympathy for Data.
# Copyright (c) 2013, Combine Control Systems AB
#
# SYMPATHY FOR DATA COMMERCIAL LICENSE
# You should have received a link to the License with Sympathy for Data.
"""
API for working with the Text type.
Import this module like this::
from sympathy.api import text
Class :class:`text.Text`
------------------------
.. autoclass:: Text
:members:
:special-members:
"""
import numpy as np
from .. utils import filebase
from .. utils.context import inherit_doc
[docs]
@filebase.typeutil('sytypealias text = sytext')
@inherit_doc
class Text(filebase.TypeAlias):
"""
A Text type containing arbitrary text, be it Hamlet or some json encoded
data structure.
Any node port with the *Text* type will produce an object of this kind.
"""
[docs]
def set(self, text_data):
"""Set text data."""
self._data.set(text_data)
[docs]
def get(self):
"""Return text data."""
return self._data.get()
[docs]
def update(self, other):
"""Copy the contents from ``other`` :class:`text.Text`.
Equivalent to :meth:`source`.
"""
self._data.update(other._data)
[docs]
def source(self, other, shallow=False):
"""Copy the contents from ``other`` :class:`text.Text`.
Equivalent to :meth:`update`.
"""
self.set(other.get())
[docs]
def names(self, kind=None, fields=None, **kwargs):
"""
Return a formatted list with a name and type of the data.
columns.
"""
names = filebase.names(kind, fields)
kind, fields = names.updated_args()
fields_list = names.fields()
if kind == 'cols':
item = names.create_item()
for f in fields_list:
if f == 'type':
item[f] = np.dtype('U')
elif f == 'expr':
item[f] = ".get()"
return names.created_items_to_result_list()
[docs]
@classmethod
def viewer(cls):
from .. platform import text_viewer
return text_viewer.TextViewer
[docs]
@classmethod
def icon(cls):
return 'ports/text.svg'
@classmethod
def from_str(cls, data: str):
obj = cls()
obj.set(data)
return obj
File = Text
@inherit_doc
class FileList(filebase.FileListBase):
"""
FileList has been changed and is now just a function which creates
generators to sybase types.
Old documentation follows:
The :class:`FileList` class is used when working with lists of Texts.
The main interfaces in :class:`FileList` are indexing or iterating for
reading (see the :meth:`__getitem__()` method) and the :meth:`append()`
method for writing.
"""
sytype = '[text]'
scheme = 'hdf5'