Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
each value or list of values not found in the rows of the table.
:param default_row:
An array of values or a function to generate new rows. The length of
the input array should be equal to row length minus column_names
count. The length of array generated by the function should be the
row length.
:returns:
A new :class:`.Table`.
"""
rows = list(self._rows)
if not utils.issequence(key):
key = [key]
if len(key) == 1:
if any(not utils.issequence(compare_value) for compare_value in compare_values):
compare_values = [[compare_value] for compare_value in compare_values]
column_values = [self._columns.get(name) for name in key]
column_indexes = [self._column_names.index(name) for name in key]
column_values = zip(*column_values)
differences = list(set(map(tuple, compare_values)) - set(column_values))
for difference in differences:
if callable(default_row):
rows.append(Row(default_row(difference), self._column_names))
else:
if default_row is not None:
new_row = default_row
else:
new_row = [None] * (len(self._column_names) - len(key))
def exclude(self, key):
"""
Create a new table without the specified columns.
:param key:
Either the name of a single column to exclude or a sequence of such
names.
:returns:
A new :class:`.Table`.
"""
if not utils.issequence(key):
key = [key]
selected_column_names = [n for n in self._column_names if n not in key]
return self.select(selected_column_names)
def select(self, key):
"""
Create a new table with only the specified columns.
:param key:
Either the name of a single column to include or a sequence of such
names.
:returns:
A new :class:`.Table`.
"""
if not utils.issequence(key):
key = [key]
column_types = [self.columns[name].data_type for name in key]
new_rows = []
for row in self._rows:
new_rows.append(Row(tuple(row[n] for n in key), key))
return self._fork(new_rows, key, column_types)
def run(self, table):
"""
:returns:
:class:`string`
"""
new_column = []
for row in table.rows:
if issequence(self._column_name):
column_value = ''
for column_name in self._column_name:
column_value = column_value + ' ' + row[column_name]
new_column.append(column_value)
else:
new_column.append(row[self._column_name])
return slugify(new_column, ensure_unique=self._ensure_unique, **self._slug_args)
def distinct(self, key=None):
"""
Create a new table with only unique rows.
:param key:
Either the name of a single column to use to identify unique rows, a
sequence of such column names, a :class:`function` that takes a
row and returns a value to identify unique rows, or `None`, in
which case the entire row will be checked for uniqueness.
:returns:
A new :class:`.Table`.
"""
key_is_row_function = hasattr(key, '__call__')
key_is_sequence = utils.issequence(key)
uniques = []
rows = []
if self._row_names is not None:
row_names = []
else:
row_names = None
for i, row in enumerate(self._rows):
if key_is_row_function:
k = key(row)
elif key_is_sequence:
k = (row[j] for j in key)
elif key is None:
k = tuple(row)
Create a new table that is sorted.
:param key:
Either the name of a single column to sort by, a sequence of such
names, or a :class:`function` that takes a row and returns a value
to sort by.
:param reverse:
If `True` then sort in reverse (typically, descending) order.
:returns:
A new :class:`.Table`.
"""
if len(self._rows) == 0:
return self._fork(self._rows)
else:
key_is_row_function = hasattr(key, '__call__')
key_is_sequence = utils.issequence(key)
def sort_key(data):
row = data[1]
if key_is_row_function:
k = key(row)
elif key_is_sequence:
k = tuple(utils.NullOrder() if row[n] is None else row[n] for n in key)
else:
k = row[key]
if k is None:
return utils.NullOrder()
return k
def aggregate(self, aggregations):
"""
Apply one or more :class:`.Aggregation` instances to this table.
:param aggregations:
A single :class:`.Aggregation` instance or a sequence of tuples in the
format :code:`(name, aggregation)`, where each :code:`aggregation` is
an instance of :class:`.Aggregation`.
:returns:
If the input was a single :class:`Aggregation` then a single result
will be returned. If it was a sequence then an :class:`.OrderedDict` of
results will be returned.
"""
if utils.issequence(aggregations):
results = OrderedDict()
for name, agg in aggregations:
agg.validate(self)
for name, agg in aggregations:
results[name] = agg.run(self)
return results
else:
aggregations.validate(self)
return aggregations.run(self)
Create a new table that is sorted.
:param key:
Either the name of a single column to sort by, a sequence of such
names, or a :class:`function` that takes a row and returns a value
to sort by.
:param reverse:
If `True` then sort in reverse (typically, descending) order.
:returns:
A new :class:`.Table`.
"""
if len(self._rows) == 0:
return self._fork()
key_is_row_function = hasattr(key, '__call__')
key_is_sequence = utils.issequence(key)
def sort_key(data):
row = data[1]
if key_is_row_function:
k = key(row)
elif key_is_sequence:
k = tuple(row[n] for n in key)
else:
k = row[key]
if k is None:
return utils.NullOrder()
return k