Source code for node_sort_columns
# This file is part of Sympathy for Data.
# Copyright (c) 2015, 2017 Combine Control Systems AB
#
# SYMPATHY FOR DATA COMMERCIAL LICENSE
# You should have received a link to the License with Sympathy for Data.
import re
from sympathy.api import node as synode
from sympathy.api.nodeconfig import Port, Ports, Tag, Tags
_sort_options = {'Ascending': 'Standard',
'Descending': 'Reverse'}
def combined_key(string):
"""
Alphanumeric key function.
It computes the sorting key from string using the string and integer parts
separately.
"""
def to_int(string):
try:
return int(string)
except ValueError:
return string
return [to_int(part) for part in re.split('([0-9]+)', string)]
[docs]
class SortColumnsInTable(synode.Node):
"""
Sort the columns in incoming table alphabetically. Output table will have
the same columns with the same data but ordered differently.
"""
name = 'Sort columns in Table'
author = 'Magnus Sandén'
icon = 'sort_table_cols.svg'
description = "Sort the columns in incoming table alphabeticaly."
nodeid = 'org.sysess.sympathy.data.table.sortcolumns'
tags = Tags(Tag.DataProcessing.TransformStructure)
inputs = Ports([
Port.Table('Table with columns in unsorted order', name='input')])
outputs = Ports([
Port.Table('Table with columns in sorted order', name='output')])
parameters = synode.parameters()
parameters.set_list(
'sort_order', label='Sort order',
list=['Ascending', 'Descending'],
value_names=["Ascending"],
description='Sort order',
editor=synode.editors.combo_editor(_sort_options))
def execute(self, node_context):
input_table = node_context.input['input']
output_table = node_context.output['output']
kwargs = {'reverse': node_context.parameters['sort_order'].selected ==
'Descending'}
columns = sorted(
input_table.column_names(), key=combined_key, **kwargs)
for column in columns:
output_table.set_column_from_array(
column, input_table.get_column_to_array(column))
output_table.set_attributes(input_table.get_attributes())
output_table.set_name(input_table.get_name())