Source code for node_text2table

# This file is part of Sympathy for Data.
# Copyright (c) 2013, Combine Control Systems AB
#
# Sympathy for Data is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# Sympathy for Data is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Sympathy for Data.  If not, see <http://www.gnu.org/licenses/>.
import re

import numpy as np
from sympathy.api import node as synode
from sympathy.api import node_helper
from sympathy.api.nodeconfig import Port, Ports, Tag, Tags, adjust

NAME = 'Text'

universal_newline = r'\r\n?|\n'
text2table_delimiter_options = {
    ',': 'Comma',
    '': 'Empty',
    ';': 'Semicolon',
    ' ': 'Space',
    r'\t': 'Tab',
    universal_newline: 'Universal newline',
}


[docs]class Text2Table(synode.Node): """ Convert the `Text` to a `Table`, with one column, by separating it into rows using the selected delimiter. """ parameters = synode.parameters() parameters.set_string( 'name', label='Output name', value=NAME, description=( 'Specify name for output column. Must be a legal name. See ' ':ref:`Name restrictions<table_name_restrictions>` for more ' 'info.')) parameters.set_string( 'delimiter', label='Delimiter', description=( 'Choose delimiter, a character sequence to separate the Text into ' 'rows.'), value=universal_newline, editor=synode.editors.combo_editor( options=text2table_delimiter_options, edit=False)) name = 'Text to Table' description = 'Convert rows of Text into rows in a Table.' inputs = Ports([Port.Text('Input Text', name='text')]) outputs = Ports([Port.Table('Table with input Text', name='table')]) author = 'Erik der Hagopian' nodeid = 'org.sysess.sympathy.data.text.text2table' version = '0.1' icon = 'text2table.svg' tags = Tags(Tag.DataProcessing.Convert) def execute(self, node_context): name = node_context.parameters['name'].value delimiter = node_context.parameters['delimiter'].value table = node_context.output[0] text = node_context.input[0].get() split = re.split(delimiter, text) if split and split[-1] == '': split = split[:-1] table.set_column_from_array(name, np.array(split))
[docs]@node_helper.list_node_decorator( {'text': {'name': 'texts'}}, {'table': {'name': 'tables'}}) class Texts2Tables(Text2Table): name = 'Texts to Tables' nodeid = 'org.sysess.sympathy.data.text.texts2tables'
table2text_delimiter_options = { ',': 'Comma', '': 'Empty', ';': 'Semicolon', ' ': 'Space', '\t': 'Tab', '\n': 'Newline', }
[docs]class Table2Text(synode.Node): """ Convert the selected `Table` column to `Text`, by concatenating all rows. The column must be of text type. Before concatenating, the delimiter is inserted in-between rows. """ name = 'Table to Text' description = ( 'Convert Table to Text concatenating all rows of the selected column.') author = 'Magnus Sandén' nodeid = 'org.sysess.sympathy.data.text.table2text' version = '0.1' icon = 'table2text.svg' tags = Tags(Tag.DataProcessing.Convert) parameters = synode.parameters() parameters.set_string( 'name', label='Column name', description='Specify name for input column.', editor=synode.editors.combo_editor(options=[], edit=True)) parameters.set_string( 'delimiter', label='Delimiter', description=( 'Choose delimiter, a character sequence to separate the rows'), value='', editor=synode.editors.combo_editor( options=table2text_delimiter_options, edit=False)) inputs = Ports([Port.Table('Table with input Text', name='table')]) outputs = Ports([Port.Text('Output Text', name='text')]) def adjust_parameters(self, node_context): adjust(node_context.parameters['name'], node_context.input[0]) def execute(self, node_context): table = node_context.input[0] text = node_context.output[0] delimiter = node_context.parameters['delimiter'].value column = table._require_column(node_context.parameters['name'], 'U') text.set(delimiter.join(column))
[docs]@node_helper.list_node_decorator( {'table': {'name': 'tables'}}, {'text': {'name': 'texts'}}) class Tables2Texts(Table2Text): name = 'Tables to Texts' nodeid = 'org.sysess.sympathy.data.text.tables2texts'