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 givenfields (
Union
[str
,Sequence
[str
]]) – a string as passed to namedtupleitemname – 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
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
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 columndata – the data of the column
itemname (
str
) – the name of each itemmissing – value to use when padding is needed
- Return type:
- Returns:
the resulting RecordList
- classmethod from_csv(csvfile)[source]¶
Create a new RecordList with the data in csvfile
- Return type:
- classmethod from_dataframe(dataframe, itemname='row')[source]¶
create a RecordList from a pandas.DataFrame
- Return type:
- 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:
- 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:
- 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:
- Returns:
the resulting RecordList