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

output: to_json: improve formatting dict-ionaries #451

Merged
merged 1 commit into from
Jan 25, 2023

Conversation

rmilecki
Copy link
Collaborator

@rmilecki rmilecki commented Jan 22, 2023

Previous code was:
1. Expecting every field with "date" in its name to be datetime.date
2. Ignoring datetime.date in other fields

That didn't match possible parsing results after all recent changes:
1. Any field can contain a date
2. Fields with "date" in their names can be lists

This change rewrites to_json to allow dealing with datetime.date in any
field at any depth level.

This also fixes possible issue like:

Traceback (most recent call last):
    File "/opt/homebrew/bin/invoice2data", line 8, in <module>
    sys.exit(main())
    File "/opt/homebrew/lib/python3.10/site-packages/invoice2data/main.py", line 231, in main
    output_module.write_to_file(output, args.output_name, args.output_date_format)
    File "/opt/homebrew/lib/python3.10/site-packages/invoice2data/output/to_json.py", line 46, in write_to_file
    line[k] = v.strftime(date_format)
AttributeError: 'list' object has no attribute 'strftime'

@rmilecki
Copy link
Collaborator Author

This fixes #441

Copy link
Collaborator

@bosd bosd left a comment

Choose a reason for hiding this comment

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

LGTM

Previous code was:
1. Expecting every field with "date" in its name to be datetime.date
2. Ignoring datetime.date in other fields

That didn't match possible parsing results after all recent changes:
1. Any field can contain a date
2. Fields with "date" in their names can be lists

This change rewrites to_json to allow dealing with datetime.date in any
field at any depth level.

This also fixes possible issue like:

Traceback (most recent call last):
  File "/opt/homebrew/bin/invoice2data", line 8, in <module>
    sys.exit(main())
  File "/opt/homebrew/lib/python3.10/site-packages/invoice2data/main.py", line 231, in main
    output_module.write_to_file(output, args.output_name, args.output_date_format)
  File "/opt/homebrew/lib/python3.10/site-packages/invoice2data/output/to_json.py", line 46, in write_to_file
    line[k] = v.strftime(date_format)
AttributeError: 'list' object has no attribute 'strftime'

Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
@rmilecki rmilecki merged commit be13db4 into invoice-x:master Jan 25, 2023
@bosd
Copy link
Collaborator

bosd commented Jan 27, 2023

I'm using this as a lib. Also to digitize receipts.
There are cases where I have multiple receipts from the same issuer on a day.
With the regex I am capturing the date including time. The return is a datetime instance.

Is it an idea to make date time instance the default? Or how to differentiate between datetime and date

@rmilecki rmilecki deleted the json-items-formatting branch January 28, 2023 11:53
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.

None yet

2 participants