Astropy Core Package Utilities (astropy.utils)

Introduction

The astropy.utils package contains general-purpose utility functions and classes. Examples include data structures, tools for downloading and caching from URLs, and version intercompatibility functions.

This functionality is not astronomy-specific, but is intended primarily for use by Astropy developers. It is all safe for users to use, but the functions and classes are typically more complicated or specific to a particular need of Astropy.

Because of the mostly standalone and grab-bag nature of these utilities, they are generally best understood through their docstrings, and hence this documentation generally does not have detailed sections like the other packages. The exception is below:

Note

The astropy.utils.compat subpackage is not included in this documentation. It contains utility modules for compatibility with older/newer versions of python and numpy, as well as including some bugfixes for the stdlib that are important for Astropy. It is recommended that developers at least glance over the source code for this subpackage, but most of it cannot be reliably included here because of the large amount of version-specific code it contains. For numpy, however, there are instructions on how to deal with issues of compatibility between different versions.

Reference/API

astropy.utils.codegen Module

Utilities for generating new Python code at runtime.

Functions

make_function_with_signature(func[, args, …])

Make a new function from an existing function but with the desired signature.

astropy.utils.collections Module

A module containing specialized collection classes.

Classes

HomogeneousList(types[, values])

A subclass of list that contains only elements of a given type or types.

astropy.utils.console Module

Utilities for console input and output.

Functions

isatty(file)

Returns True if file is a tty.

color_print(\*args[, end])

Prints colors and styles to the terminal uses ANSI escape sequences.

human_time(seconds)

Returns a human-friendly time string that is always exactly 6 characters long.

human_file_size(size)

Returns a human-friendly string representing a file size that is 2-4 characters long.

print_code_line(line[, col, file, tabwidth, …])

Prints a line of source code, highlighting a particular character position in the line.

terminal_size([file])

Returns a tuple (height, width) containing the height and width of the terminal.

Classes

ProgressBar(total_or_items[, …])

A class to display a progress bar in the terminal.

Spinner(msg[, color, file, step, chars])

A class to display a spinner in the terminal.

ProgressBarOrSpinner(total, msg[, color, file])

A class that displays either a ProgressBar or Spinner depending on whether the total size of the operation is known or not.

astropy.utils.data_info Module

This module contains functions and methods that relate to the DataInfo class which provides a container for informational attributes as well as summary info methods.

A DataInfo object is attached to the Quantity, SkyCoord, and Time classes in astropy. Here it allows those classes to be used in Tables and uniformly carry table column attributes such as name, format, dtype, meta, and description.

Functions

data_info_factory(names, funcs)

Factory to create a function that can be used as an option for outputting data object summary information.

dtype_info_name(dtype)

Return a human-oriented string name of the dtype arg.

Classes

BaseColumnInfo([bound])

Base info class for anything that can be a column in an astropy Table.

DataInfo([bound])

Descriptor that data classes use to add an info attribute for storing data attributes in a uniform and portable way.

MixinInfo([bound])

ParentDtypeInfo([bound])

Mixin that gets info.dtype from parent

astropy.utils.decorators Module

Sundry function and class decorators.

Functions

deprecated(since[, message, name, …])

Used to mark a function or class as deprecated.

deprecated_attribute(name, since[, message, …])

Used to mark a public attribute as deprecated.

deprecated_renamed_argument(old_name, …[, …])

Deprecate a _renamed_ or _removed_ function argument.

format_doc(docstring, \*args, \*\*kwargs)

Replaces the docstring of the decorated object and then formats it.

Classes

classproperty(fget[, doc, lazy])

Similar to property, but allows class-level properties.

lazyproperty(fget[, fset, fdel, doc])

Works similarly to property(), but computes the value only once.

sharedmethod

This is a method decorator that allows both an instancemethod and a classmethod to share the same name.

astropy.utils.diff Module

Functions

diff_values(a, b[, rtol, atol])

Diff two scalar values.

report_diff_values(a, b[, fileobj, indent_width])

Write a diff report between two values to the specified file-like object.

where_not_allclose(a, b[, rtol, atol])

A version of numpy.allclose() that returns the indices where the two arrays differ, instead of just a boolean value.

astropy.utils.exceptions Module

This module contains errors/exceptions and warnings of general use for astropy. Exceptions that are specific to a given subpackage should not be here, but rather in the particular subpackage. Exception is the _erfa module as we rather have the users import those exceptions from here.

Classes

AstropyBackwardsIncompatibleChangeWarning

A warning class indicating a change in astropy that is incompatible with previous versions.

AstropyDeprecationWarning

A warning class to indicate a deprecated feature.

AstropyPendingDeprecationWarning

A warning class to indicate a soon-to-be deprecated feature.

AstropyUserWarning

The primary warning class for Astropy.

AstropyWarning

The base warning class from which all Astropy warnings should inherit.

ErfaError

A class for errors triggered by ERFA functions (status codes < 0)

ErfaWarning

A class for warnings triggered by ERFA functions (status codes > 0)

astropy.utils.iers Package

Classes

Conf

Configuration parameters for astropy.utils.iers.

IERS([data, masked, names, dtype, meta, …])

Generic IERS table class, defining interpolation functions.

IERSRangeError

Any error for when dates are outside of the valid range for IERS

IERSStaleWarning

IERS_A([data, masked, names, dtype, meta, …])

IERS Table class targeted to IERS A, provided by USNO.

IERS_Auto([data, masked, names, dtype, …])

Provide most-recent IERS data and automatically handle downloading of updated values as necessary.

IERS_B([data, masked, names, dtype, meta, …])

IERS Table class targeted to IERS B, provided by IERS itself.

astropy.utils.introspection Module

Functions related to Python runtime introspection.

Functions

resolve_name(name, \*additional_parts)

Resolve a name like module.object to an object and return it.

minversion(module, version[, inclusive, …])

Returns True if the specified Python module satisfies a minimum version requirement, and False if not.

find_current_module([depth, finddiff])

Determines the module/package from which this function is called.

isinstancemethod(cls, obj)

Returns True if the given object is an instance method of the class it is defined on (as opposed to a staticmethod or a classmethod).

astropy.utils.metadata Module

This module contains helper functions and classes for handling metadata.

Functions

common_dtype(arrs)

Use numpy to find the common dtype for a list of ndarrays.

enable_merge_strategies(\*merge_strategies)

Context manager to temporarily enable one or more custom metadata merge strategies.

merge(left, right[, merge_func, …])

Merge the left and right metadata objects.

Classes

MergeConflictError

MergeConflictWarning

MergePlus

Merge left and right objects using the plus operator.

MergeNpConcatenate

Merge left and right objects using np.concatenate.

MergeStrategy

Base class for defining a strategy for merging metadata from two sources, left and right, into a single output.

MergeStrategyMeta

Metaclass that registers MergeStrategy subclasses into the MERGE_STRATEGIES registry.

MetaData([doc, copy])

A descriptor for classes that have a meta property.

astropy.utils.misc Module

A “grab bag” of relatively small general-purpose utilities that don’t have a clear module/package to live in.

Functions

isiterable(obj)

Returns True if the given object is iterable.

silence()

A context manager that silences sys.stdout and sys.stderr.

format_exception(msg, \*args, \*\*kwargs)

Given an exception message string, uses new-style formatting arguments {filename}, {lineno}, {func} and/or {text} to fill in information about the exception that occurred.

find_api_page(obj[, version, openinbrowser, …])

Determines the URL of the API page for the specified object, and optionally open that page in a web browser.

is_path_hidden(filepath)

Determines if a given file or directory is hidden.

walk_skip_hidden(top[, onerror, followlinks])

A wrapper for os.walk that skips hidden files and directories.

indent(s[, shift, width])

Indent a block of text.

set_locale(name)

Context manager to temporarily set the locale to name.

check_broadcast(\*shapes)

Determines whether two or more Numpy arrays can be broadcast with each other based on their shape tuple alone.

dtype_bytes_or_chars(dtype)

Parse the number out of a dtype.str value like ‘<U5’ or ‘<f8’.

Classes

NumpyRNGContext(seed)

A context manager (for use with the with statement) that will seed the numpy random number generator (RNG) to a specific value, and then restore the RNG state back to whatever it was before.

JsonCustomEncoder(*[, skipkeys, …])

Support for data types that JSON default encoder does not do.

InheritDocstrings(name, bases, dct)

This metaclass makes methods of a class automatically have their docstrings filled in from the methods they override in the base class.

OrderedDescriptor(*args, **kwargs)

Base class for descriptors whose order in the class body should be preserved.

OrderedDescriptorContainer(cls_name, bases, …)

Classes should use this metaclass if they wish to use OrderedDescriptor attributes, which are class attributes that “remember” the order in which they were defined in the class body.

ShapedLikeNDArray

Mixin class to provide shape-changing methods.

IncompatibleShapeError(shape_a, shape_a_idx, …)

astropy.utils.state Module

A simple class to manage a piece of global science state. See Adding New Configuration Items for more details.

Classes

ScienceState()

Science state subclasses are used to manage global items that can affect science results.

astropy.utils.timer Module

General purpose timer related functions.

Functions

timefunc([num_tries, verbose])

Decorator to time a function or method.

Classes

RunTimePredictor(func, *args, **kwargs)

Class to predict run time.

File Downloads

astropy.utils.data Module

This module contains helper functions for accessing, downloading, and caching data files.

Functions

get_readable_fileobj(name_or_obj[, …])

Given a filename, pathlib.Path object or a readable file-like object, return a context manager that yields a readable file-like object.

get_file_contents(\*args, \*\*kwargs)

Retrieves the contents of a filename or file-like object.

get_pkg_data_fileobj(data_name[, package, …])

Retrieves a data file from the standard locations for the package and provides the file as a file-like object that reads bytes.

get_pkg_data_filename(data_name[, package, …])

Retrieves a data file from the standard locations for the package and provides a local filename for the data.

get_pkg_data_contents(data_name[, package, …])

Retrieves a data file from the standard locations and returns its contents as a bytes object.

get_pkg_data_fileobjs(datadir[, package, …])

Returns readable file objects for all of the data files in a given directory that match a given glob pattern.

get_pkg_data_filenames(datadir[, package, …])

Returns the path of all of the data files in a given directory that match a given glob pattern.

compute_hash(localfn)

Computes the MD5 hash for a file.

clear_download_cache([hashorurl])

Clears the data file cache by deleting the local file(s).

get_free_space_in_dir(path)

Given a path to a directory, returns the amount of free space (in bytes) on that filesystem.

check_free_space_in_dir(path, size)

Determines if a given directory has enough space to hold a file of a given size.

download_file(remote_url[, cache, …])

Accepts a URL, downloads and optionally caches the result returning the filename, with a name determined by the file’s MD5 hash.

download_files_in_parallel(urls[, cache, …])

Downloads multiple files in parallel from the given URLs.

is_url_in_cache(url_key)

Check if a download from url_key is in the cache.

get_cached_urls()

Get the list of URLs in the cache.

Classes

Conf

Configuration parameters for astropy.utils.data.

CacheMissingWarning

This warning indicates the standard cache directory is not accessible, with the first argument providing the warning message.

XML

The astropy.utils.xml.* modules provide various XML processing tools.

astropy.utils.xml.check Module

A collection of functions for checking various XML-related strings for standards compliance.

Functions

check_anyuri(uri)

Returns True if uri is a valid URI as defined in RFC 2396.

check_id(ID)

Returns True if ID is a valid XML ID.

check_mime_content_type(content_type)

Returns True if content_type is a valid MIME content type (syntactically at least), as defined by RFC 2045.

check_token(token)

Returns True if token is a valid XML token, as defined by XML Schema Part 2.

fix_id(ID)

Given an arbitrary string, create one that can be used as an xml id.

astropy.utils.xml.iterparser Module

This module includes a fast iterator-based XML parser.

Functions

get_xml_iterator(source[, …])

Returns an iterator over the elements of an XML file.

get_xml_encoding(source)

Determine the encoding of an XML file by reading its header.

xml_readlines(source)

Get the lines from a given XML file.

astropy.utils.xml.unescaper Module

URL unescaper functions.

Functions

unescape_all(url)

Recursively unescape a given URL.

astropy.utils.xml.validate Module

Functions to do XML schema and DTD validation. At the moment, this makes a subprocess call to xmllint. This could use a Python-based library at some point in the future, if something appropriate could be found.

Functions

validate_schema(filename, schema_file)

Validates an XML file against a schema or DTD.

astropy.utils.xml.writer Module

Contains a class that makes it simple to stream out well-formed and nicely-indented XML.

Classes

XMLWriter(file)

A class to write well-formed and nicely indented XML.