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

kwarg for specifying date format as ISO date string instead of python datetime #3879

Closed
2 tasks
dogversioning opened this issue Sep 28, 2023 · 5 comments
Closed
2 tasks
Assignees
Labels
feature-request This issue requests a feature.

Comments

@dogversioning
Copy link

Describe the feature

Adding an additional keyword argument (pehaps DateFormat with string values, or AsISODate as a boolean) for calls retrieving dictionary configs from AWS resources. Would also be nice for create/update methods.

Use Case

When working with configurations, and especially when writing them to disk in a human readable format, it is convenient to pipe these to a JSON object - this also creates a helpful way to test responses outside of boto (with the AWS CLI tool, or with raw HTTP posts, as examples). Since these currently come back as python datetimes, I have found myself recently doing a lot of schema inspection to identify date related keys, and then manually casting these to ISO date strings, before serializing out to JSON. As an example, with Glue tables:

      tables = { 'TableList' : [] }    
      while next_token is not None:
            response = glue_client.get_tables(
                DatabaseName=db_name, NextToken=next_token
            )
            for table in response["TableList"]:
                for key in ["CreateTime", "UpdateTime", "LastAccessTime"]:
                    if key in table:
                        table[key] = table[key].isoformat()
                tables["TableList"].append(table)
                table_loc.append(
                    (table["Name"], table["StorageDescriptor"]["Location"])
                )
            next_token = response.get("NextToken")
      with open("tables.json", "w") as f:
                f.write(json.dumps(tables, indent=4))

It would be helpful to be able to specify which form these dates were returned in.

Proposed Solution

From an end user's perspective, I think this would make the most sense as a session/client-level argument? I think you'd generally want one format or the other, depending on your use case.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

SDK version used

1.28.56

Environment details (OS name and version, etc.)

MacOS

@dogversioning dogversioning added feature-request This issue requests a feature. needs-triage This issue or PR still needs to be triaged. labels Sep 28, 2023
@dogversioning dogversioning changed the title kwarg for specifying date format as ISO date instead of python dict kwarg for specifying date format as ISO date string instead of python datetime Sep 28, 2023
@Taragolis
Copy link

I have found myself recently doing a lot of schema inspection to identify date related keys, and then manually casting these to ISO date strings, before serializing out to JSON.

Just wondering did you tried to use custom json encoder

import json
import datetime


class CustomJSONEncoder(json.JSONEncoder):
    # For more details see: https://docs.python.org/3/library/json.html#module-json
    def default(self, obj):
        if isinstance(obj, (datetime.datetime, datetime.date)):
            return obj.isoformat()
        return super().default(obj)


xxx = {"def": datetime.datetime(2021, 2, 1), "def2": datetime.date(2021, 2, 1), }

with open("custom.json", "w") as f:
    f.write(json.dumps(xxx, cls=CustomJSONEncoder, indent=4))

@tim-finnigan tim-finnigan self-assigned this Oct 3, 2023
@tim-finnigan
Copy link
Contributor

Hi @dogversioning thanks for reaching out. I brought this feature request up for discussion with the team, and this isn't something that they plan to implement. There is overhead associated with adding and maintaining a new argument like this, and using workarounds like the one described above should be sufficient to address your use case. I'm going to close this as something that the team is not considering at this time but please feel free to add comments explaining other use cases to highlight why this is needed and maybe we can revisit the request at some point in the future.

@tim-finnigan tim-finnigan closed this as not planned Won't fix, can't repro, duplicate, stale Oct 3, 2023
@tim-finnigan tim-finnigan removed the needs-triage This issue or PR still needs to be triaged. label Oct 3, 2023
@github-actions
Copy link

github-actions bot commented Oct 3, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@dogversioning
Copy link
Author

@tim-finnigan that makes sense to me - I haven't swung back around on this yet, but I get the tradeoff, and while I haven't tried it yet @Taragolis 's approach makes sense.

Would you consider adding something like the above as a note in documentation? Someone else on my team expressed this same desire (which is why I opened this in the first place), and if there's a known workaround, it might help to gently point users in this direction.

@tim-finnigan
Copy link
Contributor

Thanks for following up — I think examples involving custom classes generally aren't documented. But if this is a common use case that you and others would benefit from seeing, maybe that could go here: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/examples.html. You or someone else are welcome to create a PR for the maintainers to consider.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request This issue requests a feature.
Projects
None yet
Development

No branches or pull requests

3 participants