Namespaces#

This page contains the protocols describing the layout of a conforming cosmology library.

class CosmologyNamespace#

Bases: Protocol

Runtime-checkable Protocol for the Cosmology API namespace.

Notes

This is a Protocol, so it is not meant to be instantiated. It is meant to be used for static and runtime type checking. See https://docs.python.org/3/library/typing.html#typing.Protocol for more details.

When used in a runtime check, isinstance will only look for the existence of objects, not details like their type. For example, constants should be a ~cosmology.api.CosmologyConstantsNamespace, but isinstance will not check this.

Examples

A library can be checked for conformance with the Cosmology API namespace.

>>> from cosmology.api import CosmologyNamespace
>>> from astropy import cosmology as astropy_cosmology
>>> isinstance(astropy_cosmology, CosmologyNamespace)
False

We can check what is missing from the library.

>>> print(
...     set(n for n in dir(CosmologyNamespace) if not n.startswith('_'))
...     - set(dir(astropy_cosmology)))
{'constants'}

Most libraries are not yet Cosmology API compatible. There are compatibility wrappers to help with this, with documentation upcoming.

Attributes

constants

The cosmology constants API.

property constants: CosmologyConstantsNamespace#

The cosmology constants API.

class CosmologyConstantsNamespace#

Bases: Protocol

Runtime-checkable Protocol for the Cosmology API constants module.

Notes

This is a Protocol, so it is not meant to be instantiated. It is meant to be used for static and runtime type checking. See https://docs.python.org/3/library/typing.html#typing.Protocol for more details.

When used in a runtime check, isinstance will only look for the existence of objects, not details like their type. For example, c should be in units of kilometers per second, but isinstance will not check this.

Examples

A module can be checked for conformance with the Cosmology API’s constants namespace.

>>> from types import SimpleNamespace
>>> constants = SimpleNamespace(G=1, c=2)
>>> isinstance(constants, CosmologyConstantsNamespace)
True
>>> constants.G
1

Most cosmology libraries are not yet Cosmology API compatible. There are compatibility wrappers to help with this, with documentation upcoming.

Attributes

G

Gravitational constant G in pc km2 s-2 Msol-1.

c

Speed of light in km s-1.

property G: Any#

Gravitational constant G in pc km2 s-2 Msol-1.

property c: Any#

Speed of light in km s-1.