RecordList

class emlib.containers.RecordList(data, fields='', itemname='')[source]

Bases: list

A list of namedtuples / dataclasses

Parameters:
  • data (list) – A seq of namedtuples or dataclass objects. A seq. of tuples or lists is also possible. In that case, fields must be given

  • fields (Union[str, Sequence[str]]) – a string as passed to namedtuple

  • itemname – The name of each row (optional), overrides the name given for the namedtuples

  • convert – True, data will be converted to namedtuples if they are not already

Example

# generate a RecordList of measures
>>> from dataclasses import dataclass
>>> @dataclass
... class Measure:
...     tempo: int
...     timesig: tuple[int, int]
>>> measures = [Measure(tempo, timesig) for tempo, timesig in [(60, (3, 4)), (60, (4, 4)), (72, (5, 8))]]
>>> measurelist = RecordList(measures)
>>> measurelist.get_column('tempo')
[60, 60, 72]

Attributes Summary

item_constructor

Methods Summary

add_column(name, data[, itemname, missing])

Return a new RecordList with the added data as a column

copy()

return a copy of self

from_csv(csvfile)

Create a new RecordList with the data in csvfile

from_dataframe(dataframe[, itemname])

create a RecordList from a pandas.DataFrame

get_column(column)

Return a column by name or index as a list of values.

get_columns(columns)

Returns a new RecordList with the selected columns

merge_with(other)

A new list is returned with a union of the fields of self and other

remove_column(colname)

Return a new RecordList with the column removed

reversed()

return a reversed copy of self

sort_by(column)

Sort this RecordList (in place) by the given column

to_csv(outfile)

Write the data in this RecordList as a csv file

to_dataframe()

create a pandas.DataFrame from this RecordList

to_html([showindex])

rtype:

str

Attributes Documentation

item_constructor

Methods Documentation

add_column(name, data, itemname='', missing=None)[source]

Return a new RecordList with the added data as a column

If len(data) < len(self), pad data with missing

Parameters:
  • name (str) – the name of the new column

  • data – the data of the column

  • itemname (str) – the name of each item

  • missing – value to use when padding is needed

Return type:

RecordList

Returns:

the resulting RecordList

copy()[source]

return a copy of self

Return type:

RecordList

classmethod from_csv(csvfile)[source]

Create a new RecordList with the data in csvfile

Return type:

RecordList

classmethod from_dataframe(dataframe, itemname='row')[source]

create a RecordList from a pandas.DataFrame

Return type:

RecordList

get_column(column)[source]

Return a column by name or index as a list of values.

Raises ValueError if column is not found

Parameters:

column (int | str) – the column to get, as index or column name

Return type:

list

Returns:

a list with the values

get_columns(columns)[source]

Returns a new RecordList with the selected columns

Parameters:

columns (list[str]) – a list of column names

Return type:

RecordList

Returns:

the resulting RecordList

merge_with(other)[source]

A new list is returned with a union of the fields of self and other

If there are fields in common, other prevails (similar to dict.update) If self and other have a different number of rows, the lowest is taken.

Parameters:

other (RecordList) – the RecordList to merge with

Return type:

RecordList

Returns:

the merged RecordList

remove_column(colname)[source]

Return a new RecordList with the column removed

Parameters:

colname (str) – the name of the column to remove

Return type:

RecordList

Returns:

the resulting RecordList

reversed()[source]

return a reversed copy of self

Return type:

RecordList

sort_by(column)[source]

Sort this RecordList (in place) by the given column

Parameters:

column (str) – the column name to use to sort this RecordList

Return type:

None

to_csv(outfile)[source]

Write the data in this RecordList as a csv file

Return type:

None

to_dataframe()[source]

create a pandas.DataFrame from this RecordList

to_html(showindex=True)[source]
Return type:

str