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

Refactor event exporter and add JSON output format #189

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

NiklasRosenstein
Copy link
Collaborator

@NiklasRosenstein NiklasRosenstein commented Feb 14, 2025

Summary

This pull request refactors the transaction exporter code to improve maintainability and introduces support for exporting transactions in JSON format. This support for JSON sets itself apart from #50 in that it is not the raw Trade Republic timeline transactions that are exported, but instead our own summarized representation of events.

Key changes

  • Use csv.DictWriter for CSV formatting instead of custom code.
  • Added --[no-]decimal-localization option which defaults to False. This changes the current default behavior to format decimal numbers in the selected locale.
  • Changed --date-with-isoformat to --[no-]date-with-time as it is more correct: The previous date-only format was also ISO8601 compatible. This option now defaults to True, which is also different from the previous default behavior.

Additional notes

Changing the default behavior for CSV export could have some impact on users relying on the precise output format of the CSV, though I think that impact depends largely on how picky the applications are that the CSV gets fed into (and I don't have any context about that).

I'm suggesting this change regardless because I feel it is generally speaking the more useful output format for further processing (with time in the date and no decimal localization). We should probably remove, or change, the note about export_transactions targeting specifically "Portfolio Performance".

@NiklasRosenstein NiklasRosenstein marked this pull request as ready for review February 14, 2025 09:47
@RealCLanger
Copy link
Collaborator

Overall, looks like a good refactoring/enhancement. Please give me some time to play with it.

Copy link
Collaborator

@RealCLanger RealCLanger left a comment

Choose a reason for hiding this comment

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

I looked at the change a bit more in detail. Nice refactoring, especially that it does not read in a file again. I made two minor comments. Have not yet tried this in practice but planning to do so, soon.

Copy link
Collaborator

@RealCLanger RealCLanger left a comment

Choose a reason for hiding this comment

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

OK, now I played around with it a bit in practice, too. Looks really good, just two remarks with regards to default values which I leave open to you to decide. 👍

Copy link
Collaborator

@RealCLanger RealCLanger left a comment

Choose a reason for hiding this comment

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

Made some last comments. :)

transactions = (txn for event in events for txn in self.from_event(event))

if format == "csv":
writer = csv.DictWriter(fp, fieldnames=self.fields(), delimiter=self.csv_delimiter)
Copy link
Collaborator

@RealCLanger RealCLanger Feb 22, 2025

Choose a reason for hiding this comment

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

Suggested change
writer = csv.DictWriter(fp, fieldnames=self.fields(), delimiter=self.csv_delimiter)
lineterminator = "\n" if platform.system() == "Windows" else "\r\n"
writer = csv.DictWriter(fp, fieldnames=self.fields(), delimiter=";", lineterminator=lineterminator)

The delimiter needs to be different on Windows. (Or you open the file with newline="").

Copy link
Collaborator Author

@NiklasRosenstein NiklasRosenstein Mar 2, 2025

Choose a reason for hiding this comment

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

I've found this SO question about the same issue: https://stackoverflow.com/questions/3191528/csv-in-python-adding-an-extra-carriage-return-on-windows

The answer given here seems a more appropriate solution, considering it is also what is recommended in the documentation of the csv module: Opening the file with newline='' to disable universal newline.

Copy link
Collaborator

Choose a reason for hiding this comment

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

yes, as written above, you could open the file with newline=''. However the output file for exporting transactions is opened through type=argparse.FileType("w", encoding="utf8"), in main.py and this does not support the newline option. But I guess you could refactor that.

Copy link
Collaborator Author

@NiklasRosenstein NiklasRosenstein Mar 10, 2025

Choose a reason for hiding this comment

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

🤦 Sorry for overlooking that you already noted that. You're right of course, argparse.FileType does not accept the argument. People in Issue24739 encourages you to create your own FileType-like custom class that accepts the newline argument 🤔

Co-authored-by: Christoph Langer <[email protected]>
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