Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add command to export log files to CSV #557

Merged
merged 3 commits into from
Jun 12, 2021

Conversation

rafaelsteil
Copy link
Contributor

This PR depends on #532

Adds a command to export all plot logs as CSV. It can be to STDOUT (default) or to a specific file via the flag -o <filename>

@rafaelsteil rafaelsteil force-pushed the export-csv-pr branch 4 times, most recently from 61d2914 to 757712a Compare May 20, 2021 02:09
Copy link
Collaborator

@altendky altendky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are missing plotman.log_parser and plotman.plotinfo?

src/plotman/plotman.py Outdated Show resolved Hide resolved
src/plotman/plotman.py Outdated Show resolved Hide resolved
src/plotman/csv_exporter.py Outdated Show resolved Hide resolved
def send_to_stdout(logfilenames):
generate(logfilenames, sys.stdout)

def header(writer):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty long list to just carefully keep aligned between the two places that the columns are and values are identified. https://github.com/ericaltendorf/plotman/pull/299/files#diff-62b25901762e697ab64f8088b67b720707e694198e9d26ce3fafd3d08b14006fR262-R325 or such maybe but let's take a look again when the other files are here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understood. Mind to clarify?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 34 items here and also 34 items specified around line 80. It would be easy to get these out of order, or add a new item one place and not the other. There would be no exception, I don't think, just misaligned column headings and data. The linked code certainly isn't the only solution. It was just one that I had coded recently (at that point). Looking here again now I see that many of the fields are calculated from others. This makes the linked code less obviously good.

Another thought, have a function that can create a dict from a PlotInfo object, maybe a method on PlotInfo, and use the keys from that as the header. There is also a .writeheader() method. Then when you pass a dict to be written it will make sure the data goes in the matching column.

@rafaelsteil
Copy link
Contributor Author

I think we are missing plotman.log_parser and plotman.plotinfo?

This PR depends on #532

writer.writerow([
info.plot_id,
info.started_at,
parse_date(info.started_at).strftime('%Y-%m-%d'),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iso without time

Copy link
Collaborator

@altendky altendky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise here on the offer to take up the leg work from here. Thanks for your contributions here and elsewhere. It's very helpful to have someone else involved in the code.

def send_to_stdout(logfilenames):
generate(logfilenames, sys.stdout)

def header(writer):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are 34 items here and also 34 items specified around line 80. It would be easy to get these out of order, or add a new item one place and not the other. There would be no exception, I don't think, just misaligned column headings and data. The linked code certainly isn't the only solution. It was just one that I had coded recently (at that point). Looking here again now I see that many of the fields are calculated from others. This makes the linked code less obviously good.

Another thought, have a function that can create a dict from a PlotInfo object, maybe a method on PlotInfo, and use the keys from that as the header. There is also a .writeheader() method. Then when you pass a dict to be written it will make sure the data goes in the matching column.

save_to_file(logfilenames, save_to)

def save_to_file(logfilenames, filename: str):
with open(filename, 'w') as file:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://docs.python.org/3/library/csv.html#examples

If I remember correctly, this relates to quoted fields with newlines in them. Unlikely, but best to just always open files as the CSV modules suggests. (detail, py2 is different...)

Suggested change
with open(filename, 'w') as file:
with open(filename, 'w', newline='') as file:

from plotman.log_parser import PlotLogParser
from plotman.plotinfo import PlotInfo

def export(logfilenames, save_to = None):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably tend to leave the file opening to the caller instead of having these small 'helpers'. It just generally seems better to leave the file handling explicit at the higher levels.

@rafaelsteil
Copy link
Contributor Author

(Discussed the comments on Keybase)

@altendky altendky merged commit 5a9b631 into ericaltendorf:development Jun 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants