Formatters

Summary

detect_type(value)

Attempt to determine the model class corresponding to an API result

enable_logging([level])

Configure logging to standard output with prettier tracebacks and terminal colors (if supported).

ensure_model_list(values)

If the given values are raw JSON responses, attempt to detect their type and convert to model objects

format_request(request[, dry_run])

Format HTTP request info

format_table(values)

Format model objects as a table.

get_model_fields(obj)

Modification for rich's pretty-printer (specifically, rich.pretty._get_attr_fields).

pprint(values)

Pretty-print any model object or list into a condensed summary.

simplify_observations(observations[, align])

Flatten out some nested data structures within observation records:

Module Contents

Utilities for formatting API responses and model objects, for convenience/readability when exploring data. Not used directly by API functions.

These functions will accept any of the following:

  • A JSON response

  • A list of response objects

  • A single response object

pyinaturalist.formatters.detect_type(value)

Attempt to determine the model class corresponding to an API result

Return type

Type[BaseModel]

pyinaturalist.formatters.enable_logging(level='INFO')

Configure logging to standard output with prettier tracebacks and terminal colors (if supported). Logging can of course be configured however you want using the stdlib logging module; this is just here for convenience.

Parameters

level (str) – Logging level to use

pyinaturalist.formatters.ensure_model_list(values)

If the given values are raw JSON responses, attempt to detect their type and convert to model objects

Return type

List[BaseModel]

pyinaturalist.formatters.format_controlled_terms(obj, *, cls=<class 'pyinaturalist.models.controlled_term.ControlledTerm'>, **kwargs)

Generic function to format a response, object, or list of objects

pyinaturalist.formatters.format_identifications(obj, *, cls=<class 'pyinaturalist.models.identification.Identification'>, **kwargs)

Generic function to format a response, object, or list of objects

pyinaturalist.formatters.format_observations(obj, *, cls=<class 'pyinaturalist.models.observation.Observation'>, **kwargs)

Generic function to format a response, object, or list of objects

pyinaturalist.formatters.format_places(obj, *, cls=<class 'pyinaturalist.models.place.Place'>, **kwargs)

Generic function to format a response, object, or list of objects

pyinaturalist.formatters.format_projects(obj, *, cls=<class 'pyinaturalist.models.project.Project'>, **kwargs)

Generic function to format a response, object, or list of objects

pyinaturalist.formatters.format_request(request, dry_run=False)

Format HTTP request info

Return type

str

pyinaturalist.formatters.format_search_results(obj, *, cls=<class 'pyinaturalist.models.search.SearchResult'>, **kwargs)

Generic function to format a response, object, or list of objects

pyinaturalist.formatters.format_species_counts(obj, *, cls=<class 'pyinaturalist.models.taxon.TaxonCount'>, **kwargs)

Generic function to format a response, object, or list of objects

pyinaturalist.formatters.format_table(values)

Format model objects as a table. If rich isn’t installed or the model doesn’t have a table format defined, just return a basic list of stringified objects.

pyinaturalist.formatters.format_taxa(obj, *, cls=<class 'pyinaturalist.models.taxon.Taxon'>, **kwargs)

Generic function to format a response, object, or list of objects

pyinaturalist.formatters.format_users(obj, *, cls=<class 'pyinaturalist.models.user.User'>, **kwargs)

Generic function to format a response, object, or list of objects

pyinaturalist.formatters.get_model_fields(obj)

Modification for rich’s pretty-printer (specifically, rich.pretty._get_attr_fields).

Adds placeholder attributes for lazy-loaded model properties so they get included in the output. This is particularly useful for previewing in Jupyter or another REPL. These nested objects are shown in condensed format so the preview is more readable. Otherwise, some objects] (especially observations) can turn into a huge wall of text several pages long.

Does not change behavior for anything except BaseModel subclasses.

Return type

Iterable[Attribute]

pyinaturalist.formatters.pprint(values)

Pretty-print any model object or list into a condensed summary.

Experimental: May also be used on most raw JSON API responses

pyinaturalist.formatters.simplify_observations(observations, align=False)

Flatten out some nested data structures within observation records:

  • annotations

  • comments

  • identifications

  • non-owner IDs

Return type

List[Dict[str, Any]]