Example #1
0
    def register_and_unregister_json(self):
        """Setup & teardown for JSON read/write tests."""
        # Register
        readwrite_registry.register_reader("json", Cosmology, read_json, force=True)
        readwrite_registry.register_writer("json", Cosmology, write_json, force=True)
        readwrite_registry.register_identifier("json", Cosmology, json_identify, force=True)

        yield  # Run all tests in class

        # Unregister
        readwrite_registry.unregister_reader("json", Cosmology)
        readwrite_registry.unregister_writer("json", Cosmology)
        readwrite_registry.unregister_identifier("json", Cosmology)
Example #2
0
    def setup_readwrite(self):
        """Setup & teardown for read/write tests."""
        # register
        readwrite_registry.register_reader("json", Cosmology, read_json, force=True)
        readwrite_registry.register_writer("json", Cosmology, write_json, force=True)
        readwrite_registry.register_identifier("json", Cosmology, json_identify, force=True)

        yield  # run all tests in class

        # unregister
        readwrite_registry.unregister_reader("json", Cosmology)
        readwrite_registry.unregister_writer("json", Cosmology)
        readwrite_registry.unregister_identifier("json", Cosmology)
Example #3
0
    **kwargs
        Passed to ``cls.write``

    Raises
    ------
    TypeError
        If kwarg (optional) 'cls' is not a subclass of `astropy.table.Table`
    """
    table = to_table(cosmology, cls=cls, cosmology_in_meta=cosmology_in_meta)

    kwargs["format"] = "ascii.ecsv"
    table.write(file, overwrite=overwrite, **kwargs)


def ecsv_identify(origin, filepath, fileobj, *args, **kwargs):
    """Identify if object uses the Table format.

    Returns
    -------
    bool
    """
    return filepath is not None and filepath.endswith(".ecsv")


# ===================================================================
# Register

readwrite_registry.register_reader("ascii.ecsv", Cosmology, read_ecsv)
readwrite_registry.register_writer("ascii.ecsv", Cosmology, write_ecsv)
readwrite_registry.register_identifier("ascii.ecsv", Cosmology, ecsv_identify)
Example #4
0
    cosmology : `~astropy.cosmology.Cosmology` instance
    file : str, bytes, or `~os.PathLike`
    overwrite : bool (optional, keyword-only)
        Whether to overwrite an existing file. Default is False.
    """
    cosmo = cosmology.to_format(
        "mypackage")  # ← convert Cosmology ↓ write file
    file_writer(file, cosmo, overwrite=overwrite)


def myformat_identify(origin, filepath, fileobj, *args, **kwargs):
    """Identify if object uses ``myformat`` (JSON)."""
    return filepath is not None and filepath.endswith(".myformat")


# -------------------------------------------------------------------
# Register read/write/identify methods with Astropy Unified I/O

readwrite_registry.register_reader("myformat",
                                   Cosmology,
                                   read_myformat,
                                   force=True)
readwrite_registry.register_writer("myformat",
                                   Cosmology,
                                   write_myformat,
                                   force=True)
readwrite_registry.register_identifier("myformat",
                                       Cosmology,
                                       myformat_identify,
                                       force=True)
Example #5
0
    return Cosmology.from_format(mycosmo, format="mypackage", **kwargs)


def write_myformat(cosmology, file, *, overwrite=False):
    """Write files in format 'myformat'.

    Parameters
    ----------
    cosmology : `~astropy.cosmology.Cosmology` instance
    file : str, bytes, or `~os.PathLike`
    overwrite : bool (optional, keyword-only)
        Whether to overwrite an existing file. Default is False.
    """
    cosmo = cosmology.to_format(
        "mypackage")  # ← convert Cosmology ↓ write file
    file_writer(file, cosmo, overwrite=overwrite)


def myformat_identify(origin, filepath, fileobj, *args, **kwargs):
    """Identify if object uses ``myformat`` (JSON)."""
    return filepath is not None and filepath.endswith(".myformat")


# -------------------------------------------------------------------
# Register read/write/identify methods with Astropy Unified I/O

readwrite_registry.register_reader("myformat", Cosmology, read_myformat)
readwrite_registry.register_writer("myformat", Cosmology, write_myformat)
readwrite_registry.register_identifier("myformat", Cosmology,
                                       myformat_identify)