Basic glom specifications consist of dict, list, tuple,
str, and callable objects. However, as data calls for more
complicated interactions, glom provides specialized specifier
types that can be used with the basic set of Python builtins.

Literal objects specify literal values in rare cases when part of
the spec should not be interpreted as a glommable
subspec. Wherever a Literal object is encountered in a spec, it is
replaced with its wrapped value in the output.

The specification techniques detailed above allow you to do pretty
much everything glom is designed to do. After all, you can always
define and insert a function or lambda into almost anywhere in the
spec?

Still, for even more specification readability and improved error
reporting, glom has a few more tricks up its sleeve.

Subspecs are passed as positional arguments, and keyword arguments
control defaults. Each subspec is evaluated in turn, and if none
match, a CoalesceError is raised, or a default is returned,
depending on the options used.

Note

This operation may seem very familar if you have experience with
SQL or even C# and others.

In practice, this fallback behavior’s simplicity is only surpassed
by its utility:

>>> target={'c':'d'}>>> glom(target,Coalesce('a','b','c'))'d'

glom tries to get 'a' from target, but gets a
KeyError. Rather than raise a PathAccessError as usual,
glom coalesces into the next subspec, 'b'. The process
repeats until it gets to 'c', which returns our value,
'd'. If our value weren’t present, we’d see:

On a further note, while lambda works great in glom specs, and
can be very handy at times, T and Call
eliminate the need for the vast majority of lambda usage with
glom.

Unlike lambda and other functions, T roundtrips
beautifully and transparently:

>>> T['a'].b['c']('success')T['a'].b['c']('success')

Note

While T is clearly useful, powerful, and here to stay, its
semantics are still being refined. Currently, operations beyond
method calls and attribute/item access are considered
experimental and should not be relied upon.

Error types and messages are also being rationalized to match
those of glom.Path.

glom introduces a few new exception types designed to maximize
readability and debuggability. Note that all these errors derive from
GlomError, and are only raised from glom() calls, not
from spec construction or glom type registration. Those declarative
and setup operations raise ValueError, TypeError, and
other standard Python exceptions as appropriate.

This GlomError subtype represents a failure to access an
attribute as dictated by the spec. The most commonly-seen error
when using glom, it maintains a copy of the original exception and
produces a readable error message for easy debugging.

Register target_type so glom() will
know how to handle instances of that type as targets.

Parameters:

target_type (type) – A type expected to appear in a glom()
call target

get (callable) – A function which takes a target object and
a name, acting as a default accessor. Defaults to
getattr().

iterate (callable) – A function which takes a target object
and returns an iterator. Defaults to iter() if
target_type appears to be iterable.

exact (bool) – Whether or not to match instances of subtypes
of target_type.

Note

The module-level register() function affects the
module-level glom() function’s behavior. If this
global effect is undesirable for your application, or
you’re implementing a library, consider instantiating a
Glommer instance, and using the
register() and Glommer.glom()
methods instead.

All the wholesome goodness that it takes to make glom work. This
type mostly serves to encapsulate the type registration context so
that advanced uses of glom don’t need to worry about stepping on
each other’s toes.

Glommer objects are lightweight and, once instantiated, provide
the glom() method we know and love:

Instances also provide register() method for
localized control over type handling.

Parameters:

register_default_types (bool) – Whether or not to enable the
handling behaviors of the default glom(). These
default actions include dict access, list and iterable
iteration, and generic object attribute access. Defaults to
True.