Skip to content

Add dump load dumps and loads to top level asdf#1930

Merged
braingram merged 5 commits into
asdf-format:mainfrom
braingram:dump
Jun 10, 2025
Merged

Add dump load dumps and loads to top level asdf#1930
braingram merged 5 commits into
asdf-format:mainfrom
braingram:dump

Conversation

@braingram

@braingram braingram commented Jun 9, 2025

Copy link
Copy Markdown
Contributor

Description

Add a few functions to asdf to mimic the interface used for other serialization libraries (json, pickle, yaml...):

  • asdf.dump: Write the provided tree to the provided file path (or file-like object)
  • asdf.load: Load the ASDF tree from the provided file path (or file-like object)
  • asdf.dumps: Dump to a string
  • asdf.loads: Load from a string

I did some minor test reorganization to make testing these new functions a bit easier by moving the tree fixture out of test_generic_io and adding a recursive_tree fixture (this expanded a few tests in test_generic_io).

One plan for these new functions is to use them during an upcoming test reorganization.

These functions should feel familiar to anyone who uses the python json, pickle and/or yaml libraries and should also be useful for some downstream libraries to simplify some asdf uses. For example, roman_datamodels contains the following code:

        af = asdf.AsdfFile()
        af.tree = {"roman": node}
        af.write_to(filepath, all_array_compression="lz4")

This can be simplified to:

        asdf.dump({"roman": node}, filepath, all_array_compression="lz4")

Closes #1873

The docstrings are largely copies of AsdfFile.__init__ asdf.open and AsdfFile.write_to options with some LLM peppered suggestions (which used those docstrings as context).

Tasks

  • run pre-commit on your machine
  • run pytest on your machine
  • Does this PR add new features and / or change user-facing code / API? (if not, label with no-changelog-entry-needed)
    • write news fragment(s) in changes/: echo "changed something" > changes/<PR#>.<changetype>.rst (see below for change types)
    • update relevant docstrings and / or docs/ page
    • for any new features, add unit tests
news fragment change types...
  • changes/<PR#>.feature.rst: new feature
  • changes/<PR#>.bugfix.rst: bug fix
  • changes/<PR#>.doc.rst: documentation change
  • changes/<PR#>.removal.rst: deprecation or removal of public API
  • changes/<PR#>.general.rst: infrastructure or miscellaneous change

@braingram braingram changed the title Dump Add dump load dumps and loads to top level asdf Jun 9, 2025
@braingram braingram marked this pull request as ready for review June 9, 2025 20:09
@braingram braingram requested a review from a team as a code owner June 9, 2025 20:09

@zacharyburnett zacharyburnett left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

was going to suggest pretty-printing options, but then I realized that we aren't minifying ASDF so it's ALWAYS pretty-printed 😅

@braingram

Copy link
Copy Markdown
Contributor Author

was going to suggest pretty-printing options, but then I realized that we aren't minifying ASDF so it's ALWAYS pretty-printed 😅

"mASDF" :)

@braingram braingram merged commit 3560ce2 into asdf-format:main Jun 10, 2025
35 checks passed
@braingram braingram deleted the dump branch June 10, 2025 18:19
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