turicas / rows

A common, beautiful interface to tabular data, no matter the format
GNU Lesser General Public License v3.0
865 stars 136 forks source link

What do you think about making `_asdict` method public? #336

Closed berinhard closed 4 years ago

berinhard commented 4 years ago

I'm using rows to parse a CSV, fetch some data from an API and create a new CSV. So, my code is:

output_rows = []
for book in rows.import_from_csv(csv_path):
    isbn = get_isbn_from_title(book.title)
    row = book._asdict().copy()
    row['isbn_13'] = isbn
    output_rows.append(row)

# some code to use output_rows with a csv.DictWriter

I feel I'm violating something by calling _asdict internal method, but it's really useful for me since I'm using rows.import_from_csv to facilitate me from manipulating and adding data. What do you think about this? Is there a risk on exposing an asdict behavior the the Row object?

turicas commented 4 years ago

I'm not sure if this is really necessary (but I'm open to discuss), since:

berinhard commented 4 years ago

Got it @turicas! I feel the suggestion in #30 covers this scenario I've described. So, after #30 being implemented, the following code snippet would work, right?

output_rows = []
for book in rows.import_from_csv(csv_path, row_class=dict):
    isbn = get_isbn_from_title(book['title'])
    row = book.copy()
    row['isbn_13'] = isbn
    output_rows.append(row)
turicas commented 4 years ago

@berinhard yes! I've added this code snippet to #30's description as a suggested API.